Skip to content

Commit d8a632e

Browse files
committed
Completed migration of github.com/manojbaishya/dojo.net
1 parent f3578c7 commit d8a632e

19 files changed

Lines changed: 1383 additions & 89 deletions

.idea/csv-editor.xml

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/dictionaries/project.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
Computer Science problems in Java.
22

3-
### Migration status - dojo.net
4-
Combine single method classes in one file
5-
1. GiftingGroups.cs [WIP]
6-
2. ItemsInContainers.cs [WIP]
7-
3. LongestSubstringWithoutRepeatingCharacters.cs [WIP]
8-
4. MedianOfTwoSortedArrays.cs [WIP]
9-
5. OptimizingBoxWeights.cs [WIP]
10-
6. PatternsIterationsAndConditionals.cs [WIP]
11-
7. Recursion.cs [WIP]
12-
8. SlidingWindow.cs [WIP]
3+
### Migration status - dojo.net — Done ✅
134

145
## Important and Useful LibrariesX
156
* JDK Standard Library — https://docs.oracle.com/en/java/javase/24/docs/api/index.html

src/main/java/org/dojo/leetcode/AmazonTransactionLogs.java

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package org.dojo.leetcode;
2+
3+
import static java.lang.Math.max;
4+
import static java.lang.Math.min;
5+
6+
public class BinarySearch {
7+
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
8+
// S = Smaller array
9+
// B = Bigger array
10+
int[] S = nums1.length <= nums2.length ? nums1 : nums2;
11+
int[] B = nums1.length <= nums2.length ? nums2 : nums1;
12+
13+
// Let M be the merged array = [..S, ..B]
14+
// Size of a merged array is
15+
int MSize = S.length + B.length;
16+
17+
// IMPORTANT: Median finding logic depends on whether M has even or odd count of elements
18+
boolean isEven = MSize % 2 == 0;
19+
20+
// INVARIANT: The median of the merged array divides it into a one and only one correct symmetry:
21+
// 50% count on the left partition and 50% on the right partition;
22+
// if isEven, then M == LeftPartitionSize x 2 (due to integer division),
23+
// else !isEven, then M == LeftPartitionSize x 2 - 1 (M + 1 is even)
24+
// so for odd sized M, LeftPartitionSize = RightPartionSize + 1, this influences the formula for
25+
// finding median for odd sized M
26+
int LEFTPARTITIONSIZE = (MSize + 1) >> 1; // CONSTANT
27+
28+
// Binary search to find the correct element count from S (left partition of S)
29+
// which goes into the left partition of M
30+
// Let SLCount = correct number of elements from S (left partition of S)
31+
// which goes into the left partition of M
32+
// Then BLCount = correct number of elements from B (left partition of B)
33+
// which goes into the left partition of M
34+
// Lemma: LeftPartitionSize (INVARIANT) = SLCount + BLCount;
35+
// Binary search to determine correct SLCount, over the smaller array to reduce time complexity
36+
int lowerBoundSLCount = 0, upperBoundSLCount = S.length;
37+
38+
while (lowerBoundSLCount <= upperBoundSLCount) {
39+
// Test element counts (also partition boundaries) of both S and B
40+
// which goes into LeftPartition of M
41+
int testSLCount = (lowerBoundSLCount + upperBoundSLCount) >> 1;
42+
int testBLCount = LEFTPARTITIONSIZE - testSLCount;
43+
44+
// Values for the above element counts
45+
int SLMAX = Integer.MIN_VALUE, BLMAX = Integer.MIN_VALUE;
46+
int SRMIN = Integer.MAX_VALUE, BRMIN = Integer.MAX_VALUE;
47+
48+
// testSLCount is size, hence rightmost value in the Left Partition of S has index testSLCount - 1
49+
// Guard for
50+
if (testSLCount - 1 >= 0) SLMAX = S[testSLCount - 1];
51+
// Hence, leftmost value in the Right Partition of S has index testSLCount
52+
// Guard for
53+
if (testSLCount < S.length) SRMIN = S[testSLCount];
54+
// testBLCount is size, hence rightmost value in the Left Partition of B has index testBLCount - 1
55+
// Guard for
56+
if (testBLCount - 1 >= 0) BLMAX = B[testBLCount - 1];
57+
// Hence, leftmost value in the Right Partition of B has index testBLCount
58+
// Guard for
59+
if (testBLCount < B.length) BRMIN = B[testBLCount];
60+
61+
/*
62+
* Iteration of partitionings to find the correct symmetry:
63+
* Rotate top (small) and bottom (big) arrays to arrive at the correct partitioning
64+
* S0, S1, S2 ... S[testSLCount - 1] or SLMAX | SRMIN or S[testSLCount], S[testSLCount + 1], ... S[S.length - 1]
65+
* B0, B1, B2, ....... B[testBLCount - 1] or BLMAX | BRMIN or B[testBLCount], B[testBLCount + 1], ...... B[B.length - 1]
66+
*/
67+
68+
if (SLMAX <= BRMIN && BLMAX <= SRMIN)
69+
return isEven ? calculateMedian(SLMAX, BLMAX, SRMIN, BRMIN) : calculateMedian(SLMAX, BLMAX);
70+
71+
// Counter Clockwise rotation, take higher count of elements from S and
72+
// take lower count of elements from B to maintain Left Partition Size invariant and
73+
// to maintain sorted order of M
74+
else if (BLMAX > SRMIN) lowerBoundSLCount = testSLCount + 1;
75+
// Clockwise rotation, take lower count of elements from S and
76+
// take higher count of elements from B to maintain Left Partition Size invariant
77+
// to maintain sorted order of M
78+
else if (SLMAX > BRMIN) upperBoundSLCount = testSLCount - 1;
79+
}
80+
81+
// This case is never executed.
82+
return 0.0;
83+
}
84+
85+
/**
86+
* Calculate median of combined array M from S and B, when M.length is even.
87+
*
88+
* @param SLMAX maximum value from left partition of smaller array
89+
* @param BLMAX maximum value from left partition of bigger array
90+
* @param SRMIN minimum value from right partition of smaller array
91+
* @param BRMIN minimum value from right partition of bigger array
92+
* @return A double valued median of combined array M from S and B, when M.length is even
93+
*/
94+
private static double calculateMedian(int SLMAX, int BLMAX, int SRMIN, int BRMIN) {
95+
return (max(SLMAX, BLMAX) + min(SRMIN, BRMIN)) / 2.0;
96+
}
97+
98+
/**
99+
* Calculate median of combined array M from S and B, when M.length is odd.
100+
*
101+
* @param SLMAX maximum value from left partition of smaller array
102+
* @param BLMAX maximum value from left partition of bigger array
103+
* @return A double valued median of combined array M from S and B, when M.length is odd
104+
*/
105+
private static double calculateMedian(int SLMAX, int BLMAX) {
106+
return max(SLMAX, BLMAX);
107+
}
108+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package org.dojo.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.Queue;
7+
8+
public class Graphs {
9+
public int countGroupsBFS(List<String> related) {
10+
List<List<Integer>> G = getAdjacencyList(related);
11+
boolean[] visitedNodes = new boolean[G.size()];
12+
int groups = 0;
13+
14+
for (int k = 0; k < visitedNodes.length; k++) {
15+
if (!visitedNodes[k]) {
16+
BFS(k, G, visitedNodes);
17+
groups += 1;
18+
}
19+
}
20+
21+
return groups;
22+
}
23+
24+
public int countGroupsDFS(List<String> related) {
25+
List<List<Integer>> G = getAdjacencyList(related);
26+
boolean[] visitedNodes = new boolean[G.size()];
27+
int groups = 0;
28+
29+
for (int k = 0; k < visitedNodes.length; k++) {
30+
if (!visitedNodes[k]) {
31+
DFS(k, G, visitedNodes);
32+
groups += 1;
33+
}
34+
}
35+
36+
return groups;
37+
}
38+
39+
private List<List<Integer>> getAdjacencyList(List<String> related) {
40+
List<List<Integer>> G = new ArrayList<>();
41+
for (int k = 0; k < related.size(); k++) {
42+
G.add(new ArrayList<>());
43+
for (int m = k + 1; m < related.get(k).length(); m++) {
44+
if (Character.getNumericValue(related.get(k).charAt(m)) == 1) {
45+
G.get(k).add(m);
46+
}
47+
}
48+
}
49+
return G;
50+
}
51+
52+
private void DFS(int nodeId, List<List<Integer>> graph, boolean[] visitedNodes) {
53+
List<Integer> neighbours = graph.get(nodeId);
54+
visitedNodes[nodeId] = true;
55+
for (int P : neighbours) {
56+
if (!visitedNodes[P]) {
57+
DFS(P, graph, visitedNodes);
58+
}
59+
}
60+
}
61+
62+
private void BFS(int nodeId, List<List<Integer>> graph, boolean[] visitedNodes) {
63+
Queue<Integer> nodesToCheck = new LinkedList<>();
64+
nodesToCheck.offer(nodeId);
65+
visitedNodes[nodeId] = true;
66+
67+
while (!nodesToCheck.isEmpty()) {
68+
int node = nodesToCheck.poll();
69+
List<Integer> neighbours = graph.get(node);
70+
for (int item : neighbours) {
71+
if (!visitedNodes[item]) {
72+
nodesToCheck.offer(item);
73+
visitedNodes[item] = true;
74+
}
75+
}
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)