Skip to content

Commit 57abe7f

Browse files
committed
Added 2 solutions to weekly contest
1 parent 5fc23cc commit 57abe7f

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public static int partitionDisjoint(int[] A) {
3+
int[] min = new int[A.length];
4+
int minVal = Integer.MAX_VALUE;
5+
6+
for (int i=A.length-1; i>=0; i--) {
7+
minVal = Math.min(minVal, A[i]);
8+
min[i] = minVal;
9+
}
10+
11+
int[] max = new int[A.length];
12+
int maxVal = Integer.MIN_VALUE;
13+
14+
for (int i=0; i<A.length; i++) {
15+
maxVal = Math.max(maxVal, A[i]);
16+
max[i] = maxVal;
17+
18+
if (i < A.length-1 && max[i] <= min[i+1]) {
19+
return i + 1;
20+
}
21+
}
22+
23+
return A.length;
24+
}
25+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Solution {
2+
public static boolean hasGroupsSizeX(int[] deck) {
3+
Map<Integer, Integer> map = new HashMap<>();
4+
5+
for (int card: deck) {
6+
map.put(card, map.getOrDefault(card, 0) + 1);
7+
}
8+
9+
int X = -1;
10+
11+
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
12+
if (entry.getValue() < 2) {
13+
return false;
14+
}
15+
16+
if (X == -1) {
17+
X = entry.getValue();
18+
}
19+
else {
20+
X = findGCD(X, entry.getValue());
21+
}
22+
}
23+
24+
if (X == 1) {
25+
return false;
26+
}
27+
28+
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
29+
if (entry.getValue() % X != 0) {
30+
return false;
31+
}
32+
}
33+
34+
return true;
35+
}
36+
37+
private static int findGCD(int n1, int n2) {
38+
while(n1 != n2) {
39+
if(n1 > n2)
40+
n1 -= n2;
41+
else
42+
n2 -= n1;
43+
}
44+
45+
return n1;
46+
}
47+
}

Medium/Search a 2D Matrix.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public static boolean searchMatrix(int[][] matrix, int target) {
3+
4+
if (matrix.length == 0) {
5+
return false;
6+
}
7+
8+
for (int i=0; i<matrix.length; i++) {
9+
int start = 0;
10+
int end = matrix[i].length-1;
11+
12+
while (start <= end) {
13+
int mid = (start + end)/2;
14+
15+
if (matrix[i][mid] == target) {
16+
return true;
17+
}
18+
else if (matrix[i][mid] > target) {
19+
end = mid - 1;
20+
}
21+
else {
22+
start = mid + 1;
23+
}
24+
}
25+
}
26+
27+
return false;
28+
}
29+
}

0 commit comments

Comments
 (0)