Skip to content

Commit 8d71052

Browse files
committed
update java solution
1 parent 785165b commit 8d71052

File tree

4 files changed

+89
-3
lines changed

4 files changed

+89
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ Feel free to raise issues or post in discussions.
236236
| 214 | | [1472. Design Browser History](https://leetcode.com/problems/design-browser-history/) | [H]array | maintain array's range dynamically | [Python]([H]array/[H]array/1472-design-browser-history.py) |
237237
| 215 | | [163. Missing Ranges](https://leetcode.com/problems/missing-ranges/) | [H]array | pre-process the array | [Python]([H]array/[H]array/163-missing-ranges.py) |
238238
| 216 | O | [56. Merge Intervals](https://leetcode.com/problems/merge-intervals/) | [H]array-line-sweep | compare two intervals each round | [Python]([H]array/[H]array-line-sweep/56-merge-intervals.py) |
239-
| 217 | O | [57. Insert Interval](https://leetcode.com/problems/insert-interval/) | [H]array-line-sweep | compare two intervals each round | [Python]([H]array/[H]array-line-sweep/57-insert-interval.py) |
239+
| 217 | O | [57. Insert Interval](https://leetcode.com/problems/insert-interval/) | [H]array-line-sweep | compare two intervals each round | [Python]([H]array/[H]array-line-sweep/57-insert-interval.py) [Java]([H]array/[H]array-line-sweep/57-insert-interval.java) |
240240
| 218 | | [1272. Remove Interval](https://leetcode.com/problems/remove-interval/) | [H]array-line-sweep | compare two intervals each round | [Python]([H]array/[H]array-line-sweep/1272-remove-interval.py) |
241241
| 219 | O | [252. Meeting Rooms](https://leetcode.com/problems/meeting-rooms/) | [H]array-line-sweep | compare two intervals each round | [Python]([H]array/[H]array-line-sweep/252-meeting-rooms.py) [Java]([H]array/[H]array-line-sweep/252-meeting-rooms.java) |
242242
| 220 | O | [435. Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/) | [H]array-line-sweep | compare two intervals each round | [Python]([H]array/[H]array-line-sweep/435-non-overlapping-intervals.py) |
@@ -398,7 +398,7 @@ Feel free to raise issues or post in discussions.
398398
| 376 | | [909. Snakes and Ladders](https://leetcode.com/problems/snakes-and-ladders/) | [K]graph-bfs | bfs with single source | [Python]([K]graph/[K]graph-bfs/909-snakes-and-ladders.py) |
399399
| 377 | | [749. Contain Virus](https://leetcode.com/problems/contain-virus/) | [K]graph-bfs | bfs with single source | [Python]([K]graph/[K]graph-bfs/749-contain-virus.py) |
400400
| 378 | | [694. Number of Distinct Islands](https://leetcode.com/problems/number-of-distinct-islands/) | [K]graph-bfs | bfs with single source | [Python]([K]graph/[K]graph-bfs/694-number-of-distinct-islands.py) |
401-
| 379 | O | [542. 01 Matrix](https://leetcode.com/problems/01-matrix/) | [K]graph-bfs | bfs with multiple sources | [Python]([K]graph/[K]graph-bfs/542-01-matrix.py) |
401+
| 379 | O | [542. 01 Matrix](https://leetcode.com/problems/01-matrix/) | [K]graph-bfs | bfs with multiple sources | [Python]([K]graph/[K]graph-bfs/542-01-matrix.py) [Java]([K]graph/[K]graph-bfs/542-01-matrix.java) |
402402
| 380 | O | [417. Pacific Atlantic Water Flow](https://leetcode.com/problems/pacific-atlantic-water-flow/) | [K]graph-bfs | bfs with multiple sources | [Python]([K]graph/[K]graph-bfs/417-pacific-atlantic-water-flow.py) |
403403
| 381 | O | [994. Rotting Oranges](https://leetcode.com/problems/rotting-oranges/) | [K]graph-bfs | bfs with multiple sources | [Python]([K]graph/[K]graph-bfs/994-rotting-oranges.py) |
404404
| 382 | | [286. Walls and Gates](https://leetcode.com/problems/walls-and-gates/) | [K]graph-bfs | bfs with multiple sources | [Python]([K]graph/[K]graph-bfs/286-walls-and-gates.py) |
@@ -499,7 +499,7 @@ Feel free to raise issues or post in discussions.
499499
| 477 | | [647. Palindromic Substrings](https://leetcode.com/problems/palindromic-substrings/) | [M]dynamic-programming | interval (start from short interval) | [Python]([M]dynamic-programming/[M]dynamic-programming/647-palindromic-substrings.py) |
500500
| 478 | | [1547. Minimum Cost to Cut a Stick](https://leetcode.com/problems/minimum-cost-to-cut-a-stick/) | [M]dynamic-programming | interval (start from short interval) | [Python]([M]dynamic-programming/[M]dynamic-programming/1547-minimum-cost-to-cut-a-stick.py) |
501501
| 479 | | [greedy]([N]greedy/greedy.md) | [N]greedy | | |
502-
| 480 | O | [53. Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) | [N]greedy | greedy | [Python]([N]greedy/[N]greedy/53-maximum-subarray.py) |
502+
| 480 | O | [53. Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) | [N]greedy | greedy | [Python]([N]greedy/[N]greedy/53-maximum-subarray.py) [Java]([N]greedy/[N]greedy/53-maximum-subarray.java) |
503503
| 481 | O | [134. Gas Station](https://leetcode.com/problems/gas-station/) | [N]greedy | greedy | [Python]([N]greedy/[N]greedy/134-gas-station.py) |
504504
| 482 | O | [621. Task Scheduler](https://leetcode.com/problems/task-scheduler/) | [N]greedy | greedy | [Python]([N]greedy/[N]greedy/621-task-scheduler.py) |
505505
| 483 | | [45. Jump Game II](https://leetcode.com/problems/jump-game-ii/) | [N]greedy | greedy | [Python]([N]greedy/[N]greedy/45-jump-game-ii.py) |
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int[][] insert(int[][] intervals, int[] newInterval) {
3+
int prevS = newInterval[0];
4+
int prevE = newInterval[1];
5+
ArrayList<int[]> res = new ArrayList<>();
6+
7+
for (int i = 0; i < intervals.length; i++) {
8+
int curS = intervals[i][0];
9+
int curE = intervals[i][1];
10+
if (prevE < curS) {
11+
res.add(new int[]{prevS, prevE});
12+
res.addAll(Arrays.asList(Arrays.copyOfRange(intervals, i, intervals.length)));
13+
return res.toArray(new int[res.size()][2]);
14+
} else if (prevS > curE) {
15+
res.add(new int[]{curS, curE});
16+
} else {
17+
prevS = Math.min(prevS, curS);
18+
prevE = Math.max(prevE, curE);
19+
}
20+
}
21+
res.add(new int[]{prevS, prevE});
22+
return res.toArray(new int[res.size()][2]);
23+
}
24+
}
25+
26+
// time O(n)
27+
// space O(n), due to output list
28+
// using array and line sweep and compare two intervals each round
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public int[][] updateMatrix(int[][] mat) {
3+
int rows = mat.length;
4+
int cols = mat[0].length;
5+
int[][] res = new int[rows][cols];
6+
7+
ArrayDeque<int[]> queue = new ArrayDeque<>();
8+
HashSet<Integer> visited = new HashSet<>();
9+
10+
for (int r = 0; r < rows; r++) {
11+
for (int c = 0; c < cols; c++) {
12+
if (mat[r][c] == 0) {
13+
queue.offer(new int[]{r, c, 0});
14+
visited.add(r * cols + c);
15+
}
16+
}
17+
}
18+
19+
while (!queue.isEmpty()) {
20+
int[] rcs = queue.poll();
21+
int r = rcs[0];
22+
int c = rcs[1];
23+
int s = rcs[2];
24+
res[r][c] = s;
25+
for (int[] nextrc: new int[][]{{r + 1, c}, {r - 1, c}, {r, c + 1}, {r, c - 1}}) {
26+
int nextr = nextrc[0];
27+
int nextc = nextrc[1];
28+
if (0 <= nextr && nextr < rows && 0 <= nextc && nextc < cols && !visited.contains(nextr * cols + nextc)) {
29+
queue.offer(new int[]{nextr, nextc, s + 1});
30+
visited.add(nextr * cols + nextc);
31+
}
32+
}
33+
}
34+
return res;
35+
}
36+
}
37+
38+
// time O(RC)
39+
// space O(RC)
40+
// using graph and bfs with multiple sources
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int maxSubArray(int[] nums) {
3+
int res = Integer.MIN_VALUE;
4+
int total = 0;
5+
for (int num: nums) {
6+
total += num;
7+
res = Math.max(res, total);
8+
if (total < 0) {
9+
total = 0;
10+
}
11+
}
12+
return res;
13+
}
14+
}
15+
16+
// time O(n)
17+
// space O(1)
18+
// using greedy

0 commit comments

Comments
 (0)