Skip to content

Commit 67d2e69

Browse files
committed
Solutions added for LeetCode 304, 881, 1376
1 parent 17b2aab commit 67d2e69

File tree

6 files changed

+201
-0
lines changed

6 files changed

+201
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.sean.array;
2+
3+
import java.util.Arrays;
4+
5+
/***
6+
* 881. Boats to Save People
7+
*/
8+
public class BoatCounter {
9+
public int numRescueBoats(int[] people, int limit) {
10+
// 2 pointers
11+
Arrays.sort(people);
12+
13+
int len = people.length;
14+
int left = 0, right = len - 1;
15+
int cnt = 0;
16+
17+
while (left < right) {
18+
int l = people[left];
19+
int r = people[right];
20+
21+
if (r >= limit) {
22+
right--;
23+
cnt++;
24+
} else {
25+
if (l + r <= limit) {
26+
right--;
27+
left++;
28+
cnt++;
29+
} else { // >
30+
right--;
31+
cnt++;
32+
}
33+
}
34+
}
35+
if (left == right)
36+
cnt++;
37+
38+
return cnt;
39+
}
40+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.sean.array;
2+
3+
/***
4+
* 304. Range Sum Query 2D - Immutable
5+
*/
6+
public class NumMatrix {
7+
private int[][] sumCache;
8+
9+
public NumMatrix(int[][] matrix) {
10+
int rowCnt = matrix.length;
11+
int colCnt = matrix[0].length;
12+
sumCache = new int[rowCnt][colCnt];
13+
14+
for (int i = 0; i < rowCnt; i++) {
15+
sumCache[i][0] = (i > 0 ? sumCache[i - 1][0] : 0) + matrix[i][0];
16+
}
17+
18+
for (int j = 0; j < colCnt; j++) {
19+
sumCache[0][j] = (j > 0 ? sumCache[0][j - 1] : 0) + matrix[0][j];
20+
}
21+
22+
for (int i = 1; i < rowCnt; i++) {
23+
for (int j = 1; j < colCnt; j++) {
24+
sumCache[i][j] = sumCache[i - 1][j] + sumCache[i][j - 1] + matrix[i][j] - sumCache[i - 1][j - 1];
25+
}
26+
}
27+
}
28+
29+
public int sumRegion(int row1, int col1, int row2, int col2) {
30+
// 0 <= row1 <= row2 < m
31+
int res = sumCache[row2][col2];
32+
if (row1 > 0) {
33+
res -= sumCache[row1 - 1][col2];
34+
}
35+
if (col1 > 0) {
36+
res -= sumCache[row2][col1 - 1];
37+
}
38+
if (row1 > 0 && col1 > 0) {
39+
res += sumCache[row1 - 1][col1 - 1];
40+
}
41+
return res;
42+
}
43+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.sean.tree;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/***
7+
* 1376. Time Needed to Inform All Employees
8+
*/
9+
public class InformTime {
10+
private int timeCost;
11+
12+
private void numOfMinutesHelper(int pos, List<List<Integer>> g, int[] informTime, int sumSoFar) {
13+
if (g.get(pos).isEmpty()) {
14+
timeCost = Math.max(timeCost, sumSoFar);
15+
return;
16+
}
17+
18+
for (int e : g.get(pos)) {
19+
numOfMinutesHelper(e, g, informTime, sumSoFar + informTime[e]);
20+
}
21+
}
22+
23+
public int numOfMinutes(int n, int headID, int[] manager, int[] informTime) {
24+
// DFS max sum from root to leaf
25+
if (n == 1)
26+
return 0;
27+
28+
List<List<Integer>> graph = new ArrayList<>();
29+
for (int i = 0; i < n; i++) {
30+
graph.add(new ArrayList<>());
31+
}
32+
33+
for (int j = 0; j < manager.length; j++) {
34+
int mgr = manager[j];
35+
if (mgr != -1) {
36+
graph.get(mgr).add(j);
37+
}
38+
}
39+
40+
numOfMinutesHelper(headID, graph, informTime, informTime[headID]);
41+
42+
return timeCost;
43+
}
44+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.sean.array;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class BoatCounterTest {
9+
10+
private BoatCounter counter;
11+
12+
@Before
13+
public void setUp() throws Exception {
14+
counter = new BoatCounter();
15+
}
16+
17+
@Test
18+
public void numRescueBoats() {
19+
assertEquals(2, counter.numRescueBoats(new int[]{5, 1, 4, 2}, 6));
20+
}
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.sean.array;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class NumMatrixTest {
9+
10+
private NumMatrix matrix;
11+
12+
@Before
13+
public void setUp() throws Exception {
14+
matrix = new NumMatrix(new int[][]{
15+
new int[]{3, 0, 1, 4, 2},
16+
new int[]{5, 6, 3, 2, 1},
17+
new int[]{1, 2, 0, 1, 5},
18+
new int[]{4, 1, 0, 1, 7},
19+
new int[]{1, 0, 3, 0, 5}
20+
});
21+
}
22+
23+
@Test
24+
public void sumRegion() {
25+
assertEquals(8, matrix.sumRegion(2, 1, 4, 3));
26+
assertEquals(11, matrix.sumRegion(1, 1, 2, 2));
27+
assertEquals(12, matrix.sumRegion(1, 2, 2, 4));
28+
}
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.sean.tree;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class InformTimeTest {
9+
10+
private InformTime informTime;
11+
12+
@Before
13+
public void setUp() throws Exception {
14+
informTime = new InformTime();
15+
}
16+
17+
@Test
18+
public void numOfMinutes() {
19+
int minutes = informTime.numOfMinutes(11, 4, new int[]{5, 9, 6, 10, -1, 8, 9, 1, 9, 3, 4},
20+
new int[]{0, 213, 0, 253, 686, 170, 975, 0, 261, 309, 337});
21+
22+
assertEquals(2560, minutes);
23+
}
24+
}

0 commit comments

Comments
 (0)