Skip to content

Commit f6d69ae

Browse files
committed
2 parents 644ee91 + 8aafb11 commit f6d69ae

File tree

5 files changed

+161
-0
lines changed

5 files changed

+161
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public int numIslands(char[][] g) {
3+
int rows = g.length;
4+
int cols = g[0].length;
5+
int[] DIRS = {1,0,-1,0,1};
6+
int res = 0;
7+
Set<Pair<Integer, Integer>> visit = new HashSet<>();
8+
class Helper {
9+
LinkedList<Pair<Integer, Integer>> q = new LinkedList<>();
10+
void markOff(Pair<Integer, Integer> coord) {
11+
q.add(coord);
12+
while (!q.isEmpty()) {
13+
Pair<Integer, Integer> popCoord = q.poll();
14+
int popR = popCoord.getKey();
15+
int popC = popCoord.getValue();
16+
for (int i=0; i<DIRS.length-1; i++) {
17+
int newR = DIRS[i] + popR;
18+
int newC = DIRS[i + 1] + popC;
19+
Pair<Integer, Integer> newCoord = new Pair<>(newR, newC);
20+
if (newR >= 0 && newR < rows &&
21+
newC >= 0 && newC < cols &&
22+
g[newR][newC] == '1' &&
23+
!visit.contains(newCoord)) {
24+
visit.add(newCoord);
25+
q.add(newCoord);
26+
}
27+
}
28+
}
29+
}
30+
}
31+
Helper helper = new Helper();
32+
for (int r=0; r<rows; r++) {
33+
for (int c=0; c<cols; c++) {
34+
Pair<Integer, Integer> coord = new Pair<>(r, c);
35+
if (g[r][c] == '1' && !visit.contains(coord)) {
36+
helper.markOff(coord);
37+
res++;
38+
}
39+
}
40+
}
41+
return res;
42+
}
43+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int search(int[] nums, int target) {
3+
int size = nums.length;
4+
int start = 0;
5+
int end = size - 1;
6+
while (start <= end) {
7+
int mid = (end + start) / 2;
8+
if (nums[mid] == target) return mid;
9+
if (nums[start] > nums[mid]) {
10+
if (target > nums[end] ||
11+
target < nums[mid]) end = mid - 1;
12+
else start = mid + 1;
13+
} else {
14+
if (target < nums[start] ||
15+
target > nums[mid]) start = mid + 1;
16+
else end = mid - 1;
17+
}
18+
}
19+
20+
return -1;
21+
}
22+
} //TC: O(logn), SC: O(1)

LeetCode/39. Combination Sum.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public List<List<Integer>> combinationSum(int[] cands, int target) {
3+
List<List<Integer>> res = new ArrayList<>();
4+
class Helper {
5+
void backtrack(int i, List<Integer> cur, int diff) {
6+
if (diff == 0) res.add(new ArrayList<>(cur));
7+
if (i >= cands.length || diff <= 0) return;
8+
cur.add(cands[i]);
9+
backtrack(i, cur, diff - cands[i]);
10+
cur.remove(cur.size() - 1);
11+
backtrack(i + 1, cur, diff);
12+
}
13+
}
14+
new Helper().backtrack(0, new ArrayList<>(), target);
15+
return res;
16+
}
17+
} //TC: O(N^(M/T + 1)), SC: O(T/M), where T is the target value, N is the number of candidates, M is the smallest candidate among all the given integers
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public boolean isValidBST(TreeNode root) {
18+
LinkedList<TreeNode> st = new LinkedList<>();
19+
TreeNode cur = root;
20+
TreeNode small = null;
21+
while (cur != null || !st.isEmpty()) {
22+
while (cur != null) {
23+
st.push(cur);
24+
cur = cur.left;
25+
}
26+
cur = st.poll();
27+
if (small != null && cur.val <= small.val) return false;
28+
small = cur;
29+
cur = cur.right;
30+
}
31+
return true;
32+
}
33+
} //TC: O(n), SC: O(height of tree), where n is node count

LeetCode/994. Rotting Oranges.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution {
2+
public int orangesRotting(int[][] g) {
3+
int[] DIRS = {1, 0, -1, 0, 1};
4+
int rows = g.length;
5+
int cols = g[0].length;
6+
7+
int res = 0;
8+
int fresh = 0;
9+
LinkedList<Pair<Integer,Integer>> q = new LinkedList<>();
10+
11+
for (int r=0; r<rows; r++) {
12+
for (int c=0; c<cols; c++) {
13+
Pair<Integer,Integer> coord = new Pair<>(r, c);
14+
if (g[r][c] == 2) {
15+
q.add(coord);
16+
} else if (g[r][c] == 1) fresh++;
17+
}
18+
}
19+
20+
while (!q.isEmpty() && fresh > 0) {
21+
int size = q.size();
22+
for (int i=0; i<size; i++) {
23+
Pair<Integer,Integer> coord = q.poll();
24+
int r = coord.getKey();
25+
int c = coord.getValue();
26+
27+
for (int d=0; d<DIRS.length-1; d++) {
28+
int nr = r + DIRS[d];
29+
int nc = c + DIRS[d + 1];
30+
Pair<Integer,Integer> newCoord = new Pair<>(nr, nc);
31+
if (nr >= 0 && nr < rows &&
32+
nc >= 0 && nc < cols &&
33+
g[nr][nc] == 1) {
34+
q.add(newCoord);
35+
g[nr][nc] = 2;
36+
fresh--;
37+
}
38+
}
39+
}
40+
res++;
41+
}
42+
43+
if (fresh > 0) return -1;
44+
return res;
45+
}
46+
}

0 commit comments

Comments
 (0)