Skip to content

Commit aac3441

Browse files
committed
Added 4 solutions & modified 2 solutions
1 parent 13bbb63 commit aac3441

6 files changed

+186
-24
lines changed

Concurrency/Print in Order.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class Foo {
2+
3+
private int counter = 1;
4+
private String mutex = "";
5+
public Foo() {
6+
7+
}
8+
9+
public void first(Runnable printFirst) throws InterruptedException {
10+
11+
// printFirst.run() outputs "first". Do not change or remove this line.
12+
boolean flag = true;
13+
while (flag) {
14+
synchronized(mutex) {
15+
if (counter == 1) {
16+
printFirst.run();
17+
counter++;
18+
flag = false;
19+
}
20+
}
21+
}
22+
}
23+
24+
public void second(Runnable printSecond) throws InterruptedException {
25+
26+
// printSecond.run() outputs "second". Do not change or remove this line.
27+
boolean flag = true;
28+
while (flag) {
29+
synchronized(mutex) {
30+
if (counter == 2) {
31+
printSecond.run();
32+
counter++;
33+
flag = false;
34+
}
35+
}
36+
}
37+
}
38+
39+
public void third(Runnable printThird) throws InterruptedException {
40+
41+
// printThird.run() outputs "third". Do not change or remove this line.
42+
boolean flag = true;
43+
while (flag) {
44+
synchronized(mutex) {
45+
if (counter == 3) {
46+
printThird.run();
47+
counter++;
48+
flag = false;
49+
}
50+
}
51+
}
52+
}
53+
}

Easy/Flood Fill.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
class Solution {
2+
public int[][] dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
23
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
3-
int color = image[sr][sc];
4-
if (color != newColor) {
5-
dfs(image, sr, sc, color, newColor);
6-
}
7-
4+
dfs(image, sr, sc, newColor, image[sr][sc]);
85
return image;
96
}
107

11-
public void dfs(int[][] image, int sr, int sc, int color, int newColor) {
12-
if (image[sr][sc] == color) {
13-
image[sr][sc] = newColor;
14-
if (sr >= 1) dfs(image, sr-1, sc, color, newColor);
15-
if (sc >= 1) dfs(image, sr, sc-1, color, newColor);
16-
if (sr+1 < image.length) dfs(image, sr+1, sc, color, newColor);
17-
if (sc+1 < image[0].length) dfs(image, sr, sc+1, color, newColor);
8+
private void dfs(int[][] image, int x, int y, int newColor, int oldColor) {
9+
if(x < 0 || x >= image.length || y < 0 || y >= image[0].length || image[x][y] == newColor) {
10+
return;
1811
}
19-
}
12+
13+
if (image[x][y] == oldColor) {
14+
image[x][y] = newColor;
15+
for (int[] dir : dirs) {
16+
dfs(image, x + dir[0], y + dir[1], newColor, oldColor);
17+
}
18+
}
19+
}
2020
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public String gcdOfStrings(String str1, String str2) {
3+
return stringGcd(str1, str2);
4+
}
5+
6+
private String stringGcd(String s1, String s2) {
7+
if (s1.length() == 0) {
8+
return s2;
9+
}
10+
11+
if (s2.length() == 0) {
12+
return s1;
13+
}
14+
15+
if (s1.equals(s2)) {
16+
return s1;
17+
}
18+
19+
if (s1.length() > s2.length()) {
20+
for (int i = 0; i < s2.length(); i++) {
21+
if (s1.charAt(i) != s2.charAt(i)) {
22+
return "";
23+
}
24+
}
25+
26+
String temp = s1.substring(s2.length());
27+
return stringGcd(temp, s2);
28+
}
29+
30+
if (s2.length() > s1.length()) {
31+
return stringGcd(s2, s1);
32+
}
33+
34+
return "";
35+
}
36+
}

Easy/Rotate String.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
class Solution {
22
public boolean rotateString(String A, String B) {
3-
if (A.isEmpty() && B.isEmpty()) return true;
4-
int n = A.length();
5-
int shiftCount = 0;
6-
7-
while(shiftCount < n) {
8-
if (A.equals(B)) {
9-
return true;
10-
}
11-
A = A.substring(1, n) + A.substring(0,1);
12-
shiftCount++;
3+
if (A.length() != B.length()) {
4+
return false;
135
}
146

15-
return false;
7+
return (A + A).contains(B);
168
}
179
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public TreeNode sufficientSubset(TreeNode root, int limit) {
12+
if (root == null) {
13+
return null;
14+
}
15+
16+
if (root.left == null && root.right == null) {
17+
return root.val < limit ? null : root;
18+
}
19+
20+
root.left = sufficientSubset(root.left, limit - root.val);
21+
root.right = sufficientSubset(root.right, limit - root.val);
22+
23+
return root.left == root.right ? null : root;
24+
}
25+
}

Medium/Majority Element II.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class Solution {
2+
public List<Integer> majorityElement(int[] nums) {
3+
if (nums.length == 0) {
4+
return new ArrayList<>();
5+
}
6+
7+
int num1 = nums[0];
8+
int num2 = nums[0];
9+
int count1 = 0;
10+
int count2 = 0;
11+
12+
for (int i = 0; i < nums.length; i++) {
13+
if (nums[i] == num1) {
14+
count1++;
15+
}
16+
else if (nums[i] == num2) {
17+
count2++;
18+
}
19+
else if (count1 == 0) {
20+
num1 = nums[i];
21+
count1 = 1;
22+
}
23+
else if (count2 == 0) {
24+
num2 = nums[i];
25+
count2 = 1;
26+
}
27+
else {
28+
count1--;
29+
count2--;
30+
}
31+
}
32+
33+
count1 = 0;
34+
count2 = 0;
35+
36+
for (int i = 0; i< nums.length; i++) {
37+
if (nums[i] == num1) {
38+
count1++;
39+
}
40+
else if (nums[i] == num2) {
41+
count2++;
42+
}
43+
}
44+
45+
List<Integer> ans = new ArrayList<>();
46+
if (count1 > nums.length / 3) {
47+
ans.add(num1);
48+
}
49+
50+
if (count2 > nums.length / 3) {
51+
ans.add(num2);
52+
}
53+
54+
return ans;
55+
}
56+
}

0 commit comments

Comments
 (0)