Skip to content

Commit 4a3e3ac

Browse files
committed
LC : add #31, #1608, #2812 and refine #140, #463, #827
1 parent af850e3 commit 4a3e3ac

11 files changed

+421
-80
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.sean.array;
2+
3+
/***
4+
* 31. Next Permutation
5+
*/
6+
public class NextPermutation {
7+
public void nextPermutation(int[] nums) {
8+
// [] 4 3 2 1
9+
// 1 [2] 3
10+
// xxx [4] 7 5 3
11+
12+
int len = nums.length;
13+
int i;
14+
for (i = len - 1; i >= 1; i--) {
15+
if (nums[i] > nums[i - 1]) {
16+
break;
17+
}
18+
}
19+
if (i == 0) { // already sorted in DESC
20+
reverse(nums, 0, len - 1);
21+
return;
22+
}
23+
24+
i--; // the one to be swapped
25+
for (int j = len - 1; j > i; j--) {
26+
if (nums[j] > nums[i]) {
27+
swap(nums, i, j);
28+
break;
29+
}
30+
}
31+
32+
// reorder the [i+1, len) part as they're already sorted in reverse order
33+
reverse(nums, i + 1, len - 1);
34+
}
35+
36+
private void reverse(int[] nums, int l, int r) {
37+
while (l < r) {
38+
swap(nums, l, r);
39+
l++;
40+
r--;
41+
}
42+
}
43+
44+
private static void swap(int[] nums, int l, int r) {
45+
int tmp = nums[l];
46+
nums[l] = nums[r];
47+
nums[r] = tmp;
48+
}
49+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.sean.array;
2+
3+
/***
4+
* 1608. Special Array With X Elements Greater Than or Equal X
5+
*/
6+
public class NumInSpecialArray {
7+
// #region BinSearch, time complexity O(N*lgN)
8+
public int specialArray0(int[] nums) {
9+
// 0 <= nums[i] <= 1000
10+
// 1 <= nums.length <= 100
11+
12+
// left : 0
13+
// right : 1000
14+
// binSearch
15+
int left = 0;
16+
int right = nums.length;
17+
while (left <= right) {
18+
int mid = (left + right) / 2;
19+
int cnt = numOfNonSmaller(nums, mid);
20+
if (cnt == mid) {
21+
return mid;
22+
} else if (cnt < mid) {
23+
right = mid - 1;
24+
} else {
25+
left = mid + 1;
26+
}
27+
}
28+
return -1;
29+
}
30+
// #endregion
31+
32+
private int numOfNonSmaller(int[] nums, int x) {
33+
int cnt = 0;
34+
for (int i : nums) {
35+
if (i >= x)
36+
cnt++;
37+
}
38+
return cnt;
39+
}
40+
41+
42+
// #region Counting sort + prefix sum, time complexity O(N)
43+
public int specialArray(int[] nums) {
44+
int len = nums.length;
45+
int[] counters = new int[len + 1];
46+
for (int i = 0; i < len; i++) {
47+
if(nums[i] >= len)
48+
counters[len] += 1;
49+
else
50+
counters[nums[i]] += 1;
51+
52+
}
53+
int count = 0;
54+
for (int i = len; i >= 1; i--) {
55+
count += counters[i];
56+
if(i == count)
57+
return i;
58+
}
59+
return -1;
60+
}
61+
// #endregion
62+
}

src/main/java/org/sean/backtracking/WordBreakII.java

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,39 @@
66
* 140. Word Break II
77
*/
88
public class WordBreakII {
9-
private List<String> outputs = new ArrayList<>();
10-
11-
private void lookupWords(
12-
String s, List<String> wordDict, int currIndex, LinkedList<String> words) {
13-
int len = s.length();
14-
if (currIndex >= len) {
15-
int totalLen = words.stream().mapToInt(String::length).sum();
16-
if (totalLen == len) {
17-
StringBuilder builder = new StringBuilder();
18-
for (int i = 0; i < words.size(); i++) {
19-
builder.append(words.get(i));
20-
if (i != words.size() - 1) builder.append(' ');
21-
}
22-
outputs.add(builder.toString());
9+
List<String> out = new ArrayList<>();
10+
11+
private void lookupWords(String s, int pos, List<String> parts, int size, Set<String> set) {
12+
if (pos >= s.length()) {
13+
//int len = parts.stream().mapToInt(value -> value.length()).sum();
14+
if (size == s.length()) {
15+
out.add(String.join(" ", parts));
2316
}
2417
return;
2518
}
26-
for (int j = currIndex + 1; j <= len; j++) {
27-
String sub = s.substring(currIndex, j);
28-
if (wordDict.contains(sub)) {
29-
words.add(sub);
30-
lookupWords(s, wordDict, j, words);
31-
words.removeLast();
19+
20+
for (int i = pos; i < s.length(); i++) {
21+
String word = s.substring(pos, i + 1);
22+
if (set.contains(word)) {
23+
parts.add(word);
24+
lookupWords(s, i + 1, parts, size + word.length(), set);
25+
parts.remove(parts.size() - 1);
3226
}
3327
}
3428
}
3529

3630
public List<String> wordBreak(String s, List<String> wordDict) {
3731
if (s == null || s.length() == 0) return Collections.emptyList();
3832

39-
if (s.length() == 1) {
40-
if (wordDict.contains(s)) return Collections.singletonList(s);
41-
else return Collections.emptyList();
42-
}
33+
// all such possible sentences in any order.
34+
HashSet<String> dict = new HashSet<>(wordDict);
35+
// catsanddog
36+
// cat{sanddog}
37+
// cats{anddog}
38+
// ...
4339

44-
lookupWords(s, wordDict, 0, new LinkedList<>());
40+
lookupWords(s, 0, new ArrayList<>(), 0, dict);
4541

46-
return outputs;
42+
return out;
4743
}
4844
}

src/main/java/org/sean/backtracking/IslandPerimeterCalculator.java renamed to src/main/java/org/sean/graph/IslandPerimeterCalculator.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
1-
package org.sean.backtracking;
1+
package org.sean.graph;
22

33
// 463. Island Perimeter
44
public class IslandPerimeterCalculator {
5+
56
public int islandPerimeter(int[][] grid) {
7+
int rowCnt = grid.length;
8+
int colCnt = grid[0].length;
9+
10+
int res = 0;
11+
for (int i = 0; i < rowCnt; i++) {
12+
for (int j = 0; j < colCnt; j++) {
13+
int cell = grid[i][j];
14+
15+
if (cell == 1) {
16+
for (int[] mv : offsets) {
17+
int nR = i + mv[0];
18+
int nC = j + mv[1];
19+
20+
if (isInvalidPos(nR, nC, grid) || grid[nR][nC] == 0) {
21+
res++;
22+
}
23+
}
24+
}
25+
}
26+
}
27+
return res;
28+
}
29+
30+
public int islandPerimeter0(int[][] grid) {
631
int row = grid.length;
732
int col = grid[0].length;
833

src/main/java/org/sean/array/LargeIsland.java renamed to src/main/java/org/sean/graph/LargeIsland.java

Lines changed: 32 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,56 @@
1-
package org.sean.array;
1+
package org.sean.graph;
22

33
import java.util.*;
44

55
/***
66
* 827. Making A Large Island
77
*/
88
public class LargeIsland {
9-
private boolean[][] visited;
10-
private final int[][] moves = new int[][]{{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
11-
private final List<int[]> targetCoordination = new ArrayList<>();
9+
private final int[][] moves = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
1210

13-
private int maxSize = 0;
11+
private int dfs(int[][] grid, int i, int j, int order, int cnt) {
1412

15-
// <areaNum, maxArea>
16-
Map<Integer, Integer> map = new HashMap<>();
13+
int n = grid.length;
14+
grid[i][j] = order;
1715

18-
// color and count
19-
private int traverse(int[][] grid, int r, int c, int curr, int order) {
20-
if (!isValidPos(grid, r, c) || visited[r][c]) return curr;
21-
22-
visited[r][c] = true;
23-
24-
if (grid[r][c] == 1) {
25-
curr++;
26-
27-
// color the area
28-
grid[r][c] = order;
29-
30-
for (int[] move : moves) {
31-
curr += traverse(grid, r + move[0], c + move[1], curr, order) - curr;
16+
for (int[] move : moves) {
17+
int nR = i + move[0];
18+
int nC = j + move[1];
19+
if (nR >= 0 && nR < n && nC >= 0 && nC < n && grid[nR][nC] == 1) {
20+
grid[nR][nC] = order;
21+
cnt += dfs(grid, nR, nC, order, 1);
3222
}
3323
}
34-
35-
return curr;
24+
return cnt;
3625
}
3726

38-
private boolean isValidPos(int[][] grid, int r, int c) {
39-
int length = grid.length;
40-
if (r >= length || r < 0 || c >= grid[0].length || c < 0)
41-
return false;
42-
return true;
43-
}
27+
// <order, size>
28+
private Map<Integer, Integer> map = new HashMap<>();
4429

30+
// Large Island
4531
public int largestIsland(int[][] grid) {
46-
if (grid.length == 1)
47-
return 1;
32+
// dfs find all the SCCs
33+
// mark it and cnt the area
34+
// for each 0, check the max
4835

49-
int len = grid.length;
50-
for (int i = 0; i < len; i++) {
51-
for (int j = 0; j < len; j++) {
36+
int order = 2;
37+
int n = grid.length;
38+
for (int i = 0; i < n; i++) {
39+
for (int j = 0; j < n; j++) {
5240
if (grid[i][j] == 1) {
53-
targetCoordination.add(new int[]{i, j});
41+
int sum = dfs(grid, i, j, order, 1);
42+
map.put(order, sum);
43+
44+
//System.out.printf("[%d:%d] - %d\n", i, j, sum);
45+
order++;
5446
}
5547
}
5648
}
57-
if (targetCoordination.isEmpty()) // all zeros!
58-
return 1;
59-
if (targetCoordination.size() == len * len)
60-
return len * len;
61-
62-
visited = new boolean[len][len];
63-
64-
int order = 2;
65-
for (int[] pair : targetCoordination) {
6649

67-
int r = pair[0];
68-
int c = pair[1];
69-
int islandSize = traverse(grid, r, c, 0, order);
70-
if (islandSize > 0)
71-
map.put(order, islandSize);
72-
73-
order++;
74-
}
50+
if (map.isEmpty())
51+
return 1;
7552

53+
int maxSize = 0, len = n;
7654
for (int i = 0; i < len; i++) {
7755
for (int j = 0; j < len; j++) {
7856
if (grid[i][j] == 0) {
@@ -102,7 +80,8 @@ public int largestIsland(int[][] grid) {
10280
}
10381
}
10482

105-
return maxSize;
83+
84+
return maxSize == 0 ? n * n : maxSize;
10685
}
10786

10887
public static void main(String[] args) {

0 commit comments

Comments
 (0)