Skip to content

Commit 1ba6c9c

Browse files
author
Rakesh Venkatesh
committed
changes
1 parent 46a92b5 commit 1ba6c9c

File tree

4 files changed

+351
-0
lines changed

4 files changed

+351
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package Arrays;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* You have “n” empty baskets, you are given the position of the empty baskets in an array called "position", where position[i] specifies the position of the ith basket. Additionally you have m balls to be distributed among the n baskets, You have to distribute the m balls into n baskets such that the minimum magnetic force between any two balls is maximum.
7+
* The magnetic force between two different balls at position x and y is defined as |x-y|
8+
*/
9+
public class MaxDistBetweenBalls {
10+
11+
/**
12+
* This is a Binary Search on Answer problem, where we aim to maximize the minimum magnetic force between any two balls while distributing them in baskets.
13+
*
14+
* Approach
15+
* 1. Sort the Basket Positions
16+
* Since the magnetic force is defined as the absolute difference between positions, sorting helps in placing the balls optimally.
17+
* 2. Use Binary Search on the Minimum Magnetic Force (d)
18+
* • The minimum force between two balls should be as large as possible.
19+
* • We perform binary search on d (the minimum force between two balls).
20+
* • The range of d is between 1 (smallest possible gap) and max(position) - min(position) (largest possible gap).
21+
* 3. Check Feasibility Using Greedy Algorithm
22+
* • We try to place m balls into the baskets such that the minimum distance is at least d.
23+
* • Start placing the first ball in the first basket.
24+
* • Place the next ball only if the gap from the last placed ball is at least d.
25+
* • If we can place all m balls successfully, d is feasible, and we try a larger d.
26+
* @param positions
27+
* @param m
28+
* @return
29+
*/
30+
public int maxDistance(int[] positions, int m) {
31+
int result = 0;
32+
Arrays.sort(positions);
33+
34+
int left = 1, right = positions[positions.length - 1];
35+
36+
while (left <= right) {
37+
int mid = left + (right - left) / 2;
38+
39+
if (canPlaceBall(positions, m, mid)) {
40+
left = mid + 1;
41+
result = mid;
42+
} else {
43+
right = mid - 1;
44+
}
45+
}
46+
47+
return result;
48+
}
49+
50+
private boolean canPlaceBall(int[] positions, int m, int minDistance) {
51+
int prevPosition = positions[0];
52+
int ballsPaced = 1;
53+
54+
for (var i = 1; i < positions.length; i++) {
55+
if (positions[i] - prevPosition >= minDistance) {
56+
ballsPaced++;
57+
prevPosition = positions[i];
58+
}
59+
if (ballsPaced == m) {
60+
return true;
61+
}
62+
}
63+
64+
return false;
65+
}
66+
67+
public static void main(String[] args) {
68+
int[] position = {5, 4, 3, 2, 1, 100}; // Basket positions
69+
int m = 2; // Number of balls
70+
71+
MaxDistBetweenBalls solution = new MaxDistBetweenBalls();
72+
73+
System.out.println(solution.maxDistance(position,m));
74+
}
75+
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package Collections;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
import java.util.HashMap;
6+
import java.util.Iterator;
7+
import java.util.Map;
8+
9+
public class DatabaseSimulator {
10+
11+
Map<String, String> database;
12+
Deque<Map<String, String>> transactionStack;
13+
14+
public DatabaseSimulator() {
15+
this.database = new HashMap<>();
16+
this.transactionStack = new ArrayDeque<>();
17+
}
18+
19+
public void begin() {
20+
transactionStack.push(new HashMap<>());
21+
}
22+
23+
public String get(String key) {
24+
if (transactionStack.isEmpty()) {
25+
return database.get(key);
26+
}
27+
28+
for (Iterator<Map<String, String>> it = transactionStack.descendingIterator(); it.hasNext(); ) {
29+
Map<String, String> map = it.next();
30+
if (map.containsKey(key)) {
31+
return map.get(key);
32+
}
33+
}
34+
return database.get(key);
35+
}
36+
37+
public void set(String key, String value) throws RuntimeException {
38+
if (transactionStack.isEmpty()) {
39+
throw new RuntimeException("No transaction in progress");
40+
}
41+
42+
transactionStack.getLast().put(key, value);
43+
}
44+
45+
public int count() {
46+
return database.size();
47+
}
48+
49+
public void clear() {
50+
database.clear();
51+
}
52+
53+
public void commit() throws RuntimeException {
54+
if (transactionStack.isEmpty()) {
55+
throw new RuntimeException("No transaction in progress");
56+
}
57+
58+
Map<String, String> map = new HashMap<>();
59+
60+
for (Map<String, String> current : transactionStack) {
61+
map.putAll(current);
62+
}
63+
64+
database.putAll(map);
65+
transactionStack.clear();
66+
}
67+
68+
public void rollback() throws RuntimeException {
69+
if (transactionStack.isEmpty()) {
70+
throw new RuntimeException("No transaction in progress");
71+
}
72+
73+
transactionStack.pop();
74+
}
75+
76+
public static void main(String[] args) {
77+
DatabaseSimulator db = new DatabaseSimulator();
78+
db.begin();
79+
System.out.println(db.get("a"));
80+
db.set("a", "Hello A");
81+
System.out.println(db.get("a")); // hello A
82+
db.set("b", "Hello B");
83+
System.out.println(db.count()); // 0
84+
db.commit();
85+
System.out.println(db.get("a")); // Hello A
86+
System.out.println(db.get("b")); // Hello B
87+
System.out.println(db.count()); // 2
88+
db.clear();
89+
System.out.println("===================");
90+
91+
System.out.println(db.get("b")); // null
92+
System.out.println(db.count()); // 0
93+
try {
94+
db.set("b", "Hello B"); // No transaction in progress
95+
} catch (RuntimeException e) {
96+
System.out.println(e.getMessage());
97+
}
98+
try {
99+
db.commit(); // No transaction in progress
100+
} catch (RuntimeException e) {
101+
System.out.println(e.getMessage());
102+
}
103+
try {
104+
db.rollback(); // No transaction in progress
105+
} catch (RuntimeException e) {
106+
System.out.println(e.getMessage());
107+
}
108+
109+
db.clear();
110+
111+
System.out.println("===================");
112+
113+
db.begin();
114+
db.set("a", "Hello");
115+
db.begin();
116+
db.set("a", "World");
117+
System.out.println(db.get("a")); // World
118+
db.commit();
119+
System.out.println(db.get("a")); // World
120+
System.out.println(db.count()); // 1
121+
122+
}
123+
}

src/Graphs/Kahn.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package Graphs;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.PriorityQueue;
6+
7+
public class Kahn {
8+
9+
private int V;
10+
private final List<List<Integer>> adjList;
11+
private final int[] indegree;
12+
13+
public Kahn(int V) {
14+
this.V = V;
15+
adjList = new ArrayList<>();
16+
for (int i = 0; i < V; i++) {
17+
adjList.add(new ArrayList<>());
18+
}
19+
indegree = new int[V];
20+
}
21+
22+
public void addEdge(int source, int destination) {
23+
adjList.get(source).add(destination);
24+
indegree[destination]++;
25+
}
26+
27+
public List<Integer> topologicalSort() {
28+
if (V == 0) {
29+
return new ArrayList<>();
30+
}
31+
32+
List<Integer> result = new ArrayList<>();
33+
PriorityQueue<Integer> queue = new PriorityQueue<>();
34+
35+
for (int i = 0; i < V; i++) {
36+
if (indegree[i] == 0) {
37+
queue.add(i);
38+
}
39+
}
40+
41+
if (queue.isEmpty()) {
42+
return new ArrayList<>();
43+
}
44+
45+
while (!queue.isEmpty()) {
46+
int current = queue.poll();
47+
result.add(current);
48+
for (int neighbor : adjList.get(current)) {
49+
indegree[neighbor]--;
50+
if (indegree[neighbor] == 0) {
51+
queue.add(neighbor);
52+
}
53+
}
54+
}
55+
56+
return result;
57+
}
58+
public static void main(String[] args) {
59+
Kahn graph = new Kahn(6);
60+
graph.addEdge(5, 2);
61+
graph.addEdge(5, 0);
62+
graph.addEdge(4, 0);
63+
graph.addEdge(4, 1);
64+
graph.addEdge(2, 3);
65+
graph.addEdge(3, 1);
66+
List<Integer> integers = graph.topologicalSort();
67+
System.out.println(integers);
68+
69+
int[] nums = new int[5];
70+
String[] strings = new String[5];
71+
72+
73+
}
74+
}
75+
76+
/**
77+
* 5 - 2
78+
* 5 - 0
79+
* 4 - 0
80+
* 4 - 1
81+
* 2 - 3
82+
* 3 - 1
83+
*/

src/Trees/ZigZagTraversal.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package Trees;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.ArrayList;
5+
import java.util.Deque;
6+
import java.util.List;
7+
8+
public class ZigZagTraversal {
9+
10+
public List<List<Integer>> zigzagLevelOrder(TreeNode<Integer> root) {
11+
if (root == null) {
12+
return null;
13+
}
14+
15+
List<List<Integer>> result = new ArrayList<>();
16+
Deque<TreeNode<Integer>> queue = new ArrayDeque<>();
17+
boolean leftToRight = false;
18+
19+
queue.add(root);
20+
21+
while (!queue.isEmpty()) {
22+
List<Integer> list = new ArrayList<>();
23+
int size = queue.size();
24+
25+
for (var i = 0; i < size; i++) {
26+
if (leftToRight) {
27+
TreeNode<Integer> current = queue.pollFirst();
28+
list.add(current.getData());
29+
30+
if (current.getLeft() != null) {
31+
queue.addLast(current.getLeft());
32+
}
33+
if (current.getRight() != null) {
34+
queue.addLast(current.getRight());
35+
}
36+
} else {
37+
TreeNode<Integer> current = queue.pollLast();
38+
list.add(current.getData());
39+
40+
if (current.getRight() != null) {
41+
queue.addFirst(current.getRight());
42+
}
43+
if (current.getLeft() != null) {
44+
queue.addFirst(current.getLeft());
45+
}
46+
}
47+
}
48+
49+
leftToRight = !leftToRight;
50+
result.add(list);
51+
}
52+
return result;
53+
}
54+
55+
public static void main(String[] args) {
56+
TreeNode<Integer> root = new TreeNode<>(1);
57+
root.setLeft(new TreeNode<>(2));
58+
root.setRight(new TreeNode<>(3));
59+
root.getLeft().setLeft(new TreeNode<>(4));
60+
root.getLeft().setRight(new TreeNode<>(5));
61+
root.getRight().setLeft(new TreeNode<>(6));
62+
root.getRight().setRight(new TreeNode<>(7));
63+
64+
ZigZagTraversal zigZagTraversal = new ZigZagTraversal();
65+
List<List<Integer>> result = zigZagTraversal.zigzagLevelOrder(root);
66+
System.out.println(result);
67+
68+
}
69+
70+
}

0 commit comments

Comments
 (0)