Skip to content

Commit 20b1fbf

Browse files
committed
Update
1 parent 43ffdd1 commit 20b1fbf

16 files changed

+228
-14
lines changed

src/binary_tree/BinaryTreePostorderTraversal.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public List<Integer> postorderTraversalViaTwoStacks(TreeNode root) {
2222

2323
stack.push(root);
2424
while (!stack.isEmpty()) {
25-
TreeNode current = stack.poll();
25+
TreeNode current = stack.pop();
2626
out.push(current);
2727

2828
if (current.left != null) stack.push(current.left);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package binary_tree;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/count-good-nodes-in-binary-tree
5+
* Difficulty: Medium
6+
* Time complexity: O(n)
7+
* Space complexity: O(h)
8+
*/
9+
public class CountGoodNodesInBinaryTree {
10+
11+
public int goodNodes(TreeNode root) {
12+
return count(root, Integer.MIN_VALUE);
13+
}
14+
15+
private int count(TreeNode root, int max) {
16+
if (root == null) return 0;
17+
18+
max = Math.max(root.val, max);
19+
20+
int current = root.val >= max ? 1 : 0;
21+
int left = count(root.left, max);
22+
int right = count(root.right, max);
23+
24+
return current + left + right;
25+
}
26+
27+
private static class TreeNode {
28+
int val;
29+
TreeNode left;
30+
TreeNode right;
31+
}
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package binary_tree;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/sum-root-to-leaf-numbers
5+
* Difficulty: Medium
6+
* Time complexity: O(n)
7+
* Space complexity: O(h)
8+
*/
9+
public class SumRootToLeafNodes {
10+
11+
public int sumNumbers(TreeNode root) {
12+
return sum(root, 0);
13+
}
14+
15+
private int sum(TreeNode root, int sum) {
16+
if (root == null) return 0;
17+
18+
sum = sum * 10 + root.val;
19+
if (root.left == null && root.right == null) {
20+
return sum;
21+
}
22+
23+
return sum(root.left, sum) + sum(root.right, sum);
24+
}
25+
26+
private static class TreeNode {
27+
int val;
28+
TreeNode left;
29+
TreeNode right;
30+
}
31+
}

src/graph/CourseSchedule2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private boolean hasCycle(Map<Integer, List<Integer>> adjList, int start) {
4848
stack.push(start);
4949

5050
while (!stack.isEmpty()) {
51-
int current = stack.poll();
51+
int current = stack.pop();
5252

5353
if (visited[current] == 0) {
5454
visited[current] = 1;

src/graph/MaxAreaOfIsland.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ private int dfs(int[][] grid, int[] start) {
3737

3838
int count = 0;
3939
while (!stack.isEmpty()) {
40-
int[] current = stack.poll();
40+
int[] current = stack.pop();
4141

4242
if (visited[current[0]][current[1]] == 0) {
4343
visited[current[0]][current[1]] = 1;

src/graph/PacificAtlanticWaterFlow.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private void dfs(int[][] heights, int[][] visited, int[] start) {
4949
stack.push(start);
5050

5151
while (!stack.isEmpty()) {
52-
int[] current = stack.poll();
52+
int[] current = stack.pop();
5353

5454
if (visited[current[0]][current[1]] == 0) {
5555
stack.push(current);
@@ -100,7 +100,7 @@ private boolean canFlowBothWays(int[][] heights, int[] start) {
100100
stack.push(start);
101101

102102
while (!stack.isEmpty()) {
103-
int[] current = stack.poll();
103+
int[] current = stack.pop();
104104

105105
if (visited[current[0]][current[1]] == 0) {
106106
visited[current[0]][current[1]] = 1;

src/graph/SurroundedRegions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private boolean isSurrounded(char[][] board, int[] start, int component) {
4747

4848
boolean isSurrounded = true;
4949
while (!stack.isEmpty()) {
50-
int[] current = stack.poll();
50+
int[] current = stack.pop();
5151

5252
if (visited[current[0]][current[1]] == 0) {
5353
visited[current[0]][current[1]] = 1;

src/graph/WordSearch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private boolean isExist(char[][] board, String word, int[] start) {
4242
return true;
4343
}
4444

45-
int[] current = stack.poll();
45+
int[] current = stack.pop();
4646

4747
if (visited[current[0]][current[1]] == 0) {
4848
if (board[current[0]][current[1]] != word.charAt(pointer)) {

src/array/InsertInterval.java renamed to src/interval/InsertInterval.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package array;
1+
package interval;
22

33
import java.util.ArrayList;
44
import java.util.List;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package interval;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/interval-list-intersections
8+
* Difficulty: Medium
9+
* Time complexity: O(n)
10+
* Space complexity: O(n)
11+
*/
12+
public class IntervalListIntersections {
13+
14+
public int[][] intervalIntersection(int[][] firstList, int[][] secondList) {
15+
List<int[]> intersections = new ArrayList<>();
16+
17+
int firstPointer = 0;
18+
int secondPointer = 0;
19+
20+
while (firstPointer < firstList.length && secondPointer < secondList.length) {
21+
int[] firstInterval = firstList[firstPointer];
22+
int[] secondInterval = secondList[secondPointer];
23+
24+
if (firstInterval[0] <= secondInterval[1] && secondInterval[0] <= firstInterval[1]) {
25+
intersections.add(new int[]{
26+
Math.max(firstInterval[0], secondInterval[0]),
27+
Math.min(firstInterval[1], secondInterval[1])});
28+
}
29+
30+
if (firstInterval[1] < secondInterval[1]) {
31+
firstPointer++;
32+
} else {
33+
secondPointer++;
34+
}
35+
}
36+
37+
return intersections.toArray(new int[0][]);
38+
}
39+
}

src/array/MeetingRooms.java renamed to src/interval/MeetingRooms.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package array;
1+
package interval;
22

33
import java.util.Arrays;
44
import java.util.Comparator;

src/interval/MeetingRooms2.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package interval;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Description: https://leetcode.com/problems/meeting-rooms-ii
7+
* Difficulty: Medium
8+
* Time complexity: O(nlog n)
9+
* Space complexity: O(n)
10+
*/
11+
public class MeetingRooms2 {
12+
13+
public int minMeetingRooms(Interval[] intervals) {
14+
int[] starts = new int[intervals.length];
15+
int[] ends = new int[intervals.length];
16+
17+
for (int i = 0; i < intervals.length; i++) {
18+
starts[i] = intervals[i].start;
19+
ends[i] = intervals[i].end;
20+
}
21+
22+
Arrays.sort(starts);
23+
Arrays.sort(ends);
24+
25+
int startPointer = 0;
26+
int endPointer = 0;
27+
int busyRoomsCount = 0;
28+
int maxCount = 0;
29+
30+
while (startPointer < starts.length) {
31+
if (starts[startPointer] < ends[endPointer]) { // a new meeting has started, while the previous one didn't end
32+
busyRoomsCount++;
33+
startPointer++;
34+
} else { // a meeting has ended
35+
busyRoomsCount--;
36+
endPointer++;
37+
}
38+
39+
maxCount = Math.max(busyRoomsCount, maxCount);
40+
}
41+
42+
return maxCount;
43+
}
44+
45+
private static class Interval {
46+
int start;
47+
int end;
48+
}
49+
}

src/array/MergeIntervals.java renamed to src/interval/MergeIntervals.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package array;
1+
package interval;
22

33
import java.util.ArrayList;
44
import java.util.Arrays;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package interval;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Description: https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons
7+
* Difficulty: Medium
8+
* Time complexity: O(nlog n)
9+
* Space complexity: O(1)
10+
*/
11+
public class MinimumNumberOfArrowsToBurstBalloons {
12+
13+
public int findMinArrowShots(int[][] points) {
14+
if (points.length < 2) return points.length;
15+
16+
Arrays.sort(points, (p1, p2) -> Integer.compare(p1[0], p2[0]));
17+
18+
int end = points[0][1];
19+
int nonOverlapingIntervals = 1;
20+
21+
for (int[] point : points) {
22+
if (point[0] > end) {
23+
nonOverlapingIntervals++;
24+
end = point[1];
25+
} else {
26+
end = Math.min(end, point[1]);
27+
}
28+
}
29+
30+
return nonOverlapingIntervals;
31+
}
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package interval;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Description: https://leetcode.com/problems/non-overlapping-intervals
7+
* Difficulty: Medium
8+
* Time complexity: O(nlog n)
9+
* Space complexity: O(1)
10+
*/
11+
public class NonOverlappingIntervals {
12+
13+
public int eraseOverlapIntervals(int[][] intervals) {
14+
if (intervals.length < 2) return 0;
15+
16+
Arrays.sort(intervals, (i1, i2) -> Integer.compare(i1[0], i2[0]));
17+
18+
int end = intervals[0][1];
19+
int nonOverlappingIntervals = 1;
20+
for (int[] current : intervals) {
21+
if (current[0] >= end) {
22+
nonOverlappingIntervals++;
23+
end = current[1];
24+
} else {
25+
end = Math.min(end, current[1]);
26+
}
27+
}
28+
29+
return intervals.length - nonOverlappingIntervals;
30+
}
31+
}

src/stack/MinStack.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ private static class Node {
4040
Node next;
4141
int min;
4242

43-
public Node(int v, Node n, int m) {
44-
val = v;
45-
next = n;
46-
min = m;
43+
public Node(int val, Node node, int min) {
44+
this.val = val;
45+
this.next = node;
46+
this.min = min;
4747
}
4848
}
4949
}

0 commit comments

Comments
 (0)