Skip to content

Commit 18d6d54

Browse files
committed
update
1 parent 51684cf commit 18d6d54

12 files changed

+263
-39
lines changed

leetcode_solved/[editing]leetcode_0289_Game_of_Life.cpp

-6
This file was deleted.

leetcode_solved/[editing]leetcode_0337_House_Robber_III.cpp

-15
This file was deleted.

leetcode_solved/[editing]leetcode_1017_Convert_to_Base_-2.cpp

-6
This file was deleted.

leetcode_solved/[editing]leetcode_1041_Robot_Bounded_In_Circle.cpp

-6
This file was deleted.

leetcode_solved/[editing]leetcode_1262_Greatest_Sum_Divisible_by_Three.cpp

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// AC: Runtime: 1 ms, faster than 23.27% of Java online submissions for Game of Life.
2+
// Memory Usage: 38.9 MB, less than 8.21% of Java online submissions for Game of Life.
3+
// .
4+
// T:O(9 * n) ~ O(n), S:O(1)
5+
//
6+
class Solution {
7+
public void gameOfLife(int[][] board) {
8+
int row = board.length, col = board[0].length;
9+
for (int i = 0; i < row; i++) {
10+
for (int j = 0; j < col; j++) {
11+
int countOne = 0;
12+
for (int t = Math.max(0, i - 1); t <= Math.min(row - 1, i + 1); t++) {
13+
for (int k = Math.max(0, j - 1); k <= Math.min(col - 1, j + 1); k++) {
14+
if (t == i && k == j) {
15+
continue;
16+
}
17+
if (board[t][k] == 1 || board[t][k] == -1) {
18+
countOne++;
19+
}
20+
}
21+
}
22+
if (board[i][j] == 1) {
23+
if (countOne < 2) {
24+
// live cell will dead
25+
board[i][j] = -1;
26+
} else if (countOne == 2 || countOne == 3) {
27+
// live
28+
} else {
29+
// over-population, die
30+
board[i][j] = -1;
31+
}
32+
} else {
33+
if (countOne == 3) {
34+
// dead cell will birth
35+
board[i][j] = -2;
36+
}
37+
}
38+
}
39+
}
40+
41+
for (int i = 0; i < row; i++) {
42+
for (int j = 0; j < col; j++) {
43+
if (board[i][j] == -1) {
44+
board[i][j] = 0;
45+
}
46+
if (board[i][j] == -2) {
47+
board[i][j] = 1;
48+
}
49+
}
50+
}
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// AC: Runtime: 2 ms, faster than 41.52% of Java online submissions for House Robber III.
2+
// Memory Usage: 39 MB, less than 19.68% of Java online submissions for House Robber III.
3+
// recursive. if rob root, then rob root's left and right subtree's childs. if not rob root, then rob root's left and right subtrees.
4+
// T:O(n), S:O(n)
5+
//
6+
class Solution {
7+
private HashMap<TreeNode, Integer> record;
8+
public int rob(TreeNode root) {
9+
record = new HashMap<>();
10+
return solve(root);
11+
}
12+
13+
private int solve(TreeNode root) {
14+
if (root == null) {
15+
return 0;
16+
}
17+
if (root.left == null && root.right == null) {
18+
return root.val;
19+
}
20+
if (record.containsKey(root)) {
21+
return record.get(root);
22+
}
23+
int val1 = root.val;
24+
if (root.left != null) {
25+
val1 += solve(root.left.left) + solve(root.left.right);
26+
}
27+
if (root.right != null) {
28+
val1 += solve(root.right.left) + solve(root.right.right);
29+
}
30+
31+
int val2 = solve(root.left) + solve(root.right);
32+
33+
int ret = Math.max(val1, val2);
34+
record.put(root, ret);
35+
36+
return ret;
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// AC: Runtime: 1 ms, faster than 63.70% of Java online submissions for Convert to Base -2.
2+
// Memory Usage: 38.5 MB, less than 6.67% of Java online submissions for Convert to Base -2.
3+
// some tricks to use bit manipulation
4+
// T:O(logn), S:O(logn)
5+
//
6+
class Solution {
7+
public String baseNeg2(int n) {
8+
if (n == 0) {
9+
return "0";
10+
}
11+
StringBuilder ret = new StringBuilder();
12+
while (n != 0) {
13+
ret.append(n & 1);
14+
n = -(n >> 1);
15+
}
16+
17+
return ret.reverse().toString();
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// AC: Runtime: 16 ms, faster than 5.52% of Java online submissions for Robot Bounded In Circle.
2+
// Memory Usage: 40.5 MB, less than 5.04% of Java online submissions for Robot Bounded In Circle.
3+
// using at most 4-loop to check instructions can make how many moves. using two loop to check whether it forms a circle.
4+
// T:O(n), S:O(n)
5+
//
6+
class Solution {
7+
public boolean isRobotBounded(String instructions) {
8+
HashSet<String> record = new HashSet<>();
9+
int curX = 0, curY = 0, dir = 0;
10+
record.add(curX + "#" + curY);
11+
int[] count = new int[2];
12+
for (int loop = 0; loop < 2; loop++) {
13+
for (int i = 0; i < 4; i++) {
14+
for (char c : instructions.toCharArray()) {
15+
if (c == 'G') {
16+
if (dir == 0) {
17+
curY++;
18+
} else if (dir == 1) {
19+
curX++;
20+
} else if (dir == 2) {
21+
curY--;
22+
} else {
23+
curX--;
24+
}
25+
26+
// check loop
27+
String temp = curX + "#" + curY;
28+
record.add(temp);
29+
} else if (c == 'L') {
30+
dir = (dir == 0) ? 3 : dir - 1;
31+
} else {
32+
dir = (dir == 3) ? 0 : dir + 1;
33+
}
34+
}
35+
}
36+
count[loop] = record.size();
37+
}
38+
39+
return count[0] == count[1];
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// AC: Runtime: 10 ms, faster than 45.69% of Java online submissions for Greatest Sum Divisible by Three.
2+
// Memory Usage: 44.7 MB, less than 47.20% of Java online submissions for Greatest Sum Divisible by Three.
3+
// .
4+
// T:O(nlogn), S:O(logn)
5+
//
6+
class Solution {
7+
public int maxSumDivThree(int[] nums) {
8+
Arrays.sort(nums);
9+
int sum = 0, firstModOne = -1, firstModTwo = -1, secondModOne = -1, secondModTwo = -1;
10+
for (int i : nums) {
11+
sum += i;
12+
}
13+
if (sum % 3 == 1) {
14+
for (int num : nums) {
15+
if (num % 3 == 1) {
16+
if (firstModTwo != -1 && secondModTwo != -1) {
17+
return sum - Math.min(num, firstModTwo + secondModTwo);
18+
} else {
19+
return sum - num;
20+
}
21+
} else if (num % 3 == 2) {
22+
if (firstModTwo == -1) {
23+
firstModTwo = num;
24+
} else if (secondModTwo == -1) {
25+
secondModTwo = num;
26+
}
27+
}
28+
}
29+
30+
return (firstModTwo != -1 && secondModTwo != -1) ? sum - firstModTwo - secondModTwo : 0;
31+
} else if (sum % 3 == 2) {
32+
for (int num : nums) {
33+
if (num % 3 == 1) {
34+
if (firstModOne == -1) {
35+
firstModOne = num;
36+
} else if (secondModOne == -1) {
37+
secondModOne = num;
38+
}
39+
} else if (num % 3 == 2) {
40+
if (firstModOne != -1 && secondModOne != -1) {
41+
return sum - Math.min(num, firstModOne + secondModOne);
42+
} else {
43+
return sum - num;
44+
}
45+
}
46+
}
47+
48+
return (firstModOne != -1 && secondModOne != -1) ? sum - firstModOne - secondModOne : 0;
49+
} else {
50+
return sum;
51+
}
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// AC: Runtime: 3 ms, faster than 100.00% of Java online submissions for Minimum Difference Between Highest and Lowest of K Scores.
2+
// Memory Usage: 38.8 MB, less than 100.00% of Java online submissions for Minimum Difference Between Highest and Lowest of K Scores.
3+
// sort and compare nums[i + k - 1] - nums[i].
4+
// T:O(nlogn), S:O(logn)
5+
//
6+
class Solution {
7+
public int minimumDifference(int[] nums, int k) {
8+
int size = nums.length, ret = Integer.MAX_VALUE;
9+
Arrays.sort(nums);
10+
for (int i = 0; i < size - k + 1; i++) {
11+
ret = Math.min(ret, nums[i + k - 1] - nums[i]);
12+
}
13+
14+
return ret;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// AC: Runtime: 20 ms, faster than 75.00% of Java online submissions for Find the Kth Largest Integer in the Array.
2+
// Memory Usage: 47.5 MB, less than 50.00% of Java online submissions for Find the Kth Largest Integer in the Array.
3+
// treemap
4+
// T:O(nlogn), S:O(n)
5+
//
6+
class Solution {
7+
public String kthLargestNumber(String[] nums, int k) {
8+
TreeMap<Integer, List<String>> record = new TreeMap<>(new Comparator<Integer>() {
9+
@Override
10+
public int compare(Integer o1, Integer o2) {
11+
return o2 - o1;
12+
}
13+
});
14+
for (String str: nums) {
15+
int len = str.length();
16+
if (record.containsKey(len)) {
17+
record.get(len).add(str);
18+
} else {
19+
List<String> tempList = new ArrayList<>();
20+
tempList.add(str);
21+
record.put(len, tempList);
22+
}
23+
}
24+
25+
int count = 0;
26+
for (int len: record.keySet()) {
27+
int lenCount = record.get(len).size();
28+
if (count + lenCount >= k) {
29+
// return list
30+
record.get(len).sort(new Comparator<String>() {
31+
@Override
32+
public int compare(String o1, String o2) {
33+
return o2.compareTo(o1);
34+
}
35+
});
36+
return record.get(len).get(k - count - 1);
37+
} else {
38+
count += lenCount;
39+
}
40+
}
41+
42+
return nums[0];
43+
}
44+
}

0 commit comments

Comments
 (0)