Skip to content

Commit c011056

Browse files
committed
Update
1 parent 385b229 commit c011056

17 files changed

+792
-68
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package array;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Description: https://leetcode.com/problems/intersection-of-two-arrays
7+
* Difficulty: Easy
8+
*/
9+
public class IntersectionOfTwoArrays {
10+
11+
/**
12+
* Time complexity: O(m + n)
13+
* Space complexity: O(min(m, n))
14+
*/
15+
public int[] intersectionViaSet(int[] small, int[] large) {
16+
if (small.length > large.length) {
17+
return intersectionViaSet(large, small);
18+
}
19+
20+
Set<Integer> smallSet = new HashSet<>();
21+
for (int num : small) {
22+
smallSet.add(num);
23+
}
24+
25+
List<Integer> intersection = new ArrayList<>();
26+
for (int num : large) {
27+
if (smallSet.remove(num)) {
28+
intersection.add(num);
29+
}
30+
}
31+
32+
return intersection.stream().mapToInt(v -> v).toArray();
33+
}
34+
35+
/**
36+
* Time complexity: O(nlog n + mlog m)
37+
* Space complexity: O(log n + log m)
38+
*/
39+
public int[] intersectionViaSorting(int[] nums1, int[] nums2) {
40+
Arrays.sort(nums1);
41+
Arrays.sort(nums2);
42+
43+
int first = 0;
44+
int second = 0;
45+
46+
Set<Integer> intersection = new HashSet<>();
47+
while (first < nums1.length && second < nums2.length) {
48+
if (nums1[first] > nums2[second]) {
49+
second++;
50+
} else if (nums1[first] < nums2[second]) {
51+
first++;
52+
} else {
53+
intersection.add(nums1[first]);
54+
first++;
55+
second++;
56+
}
57+
}
58+
59+
return intersection.stream().mapToInt(v -> v).toArray();
60+
}
61+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package array;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Description: https://leetcode.com/problems/intersection-of-two-arrays-ii
7+
* Difficulty: Easy
8+
*/
9+
public class IntersectionOfTwoArrays2 {
10+
11+
/**
12+
* Time complexity: O(m + n)
13+
* Space complexity: O(min(m, n))
14+
*/
15+
public int[] intersectViaFreqMap(int[] small, int[] large) {
16+
if (small.length > large.length) {
17+
return intersectViaFreqMap(large, small); // to save memory on freqMap
18+
}
19+
20+
Map<Integer, Integer> freqMap = buildFrequencyMap(small);
21+
List<Integer> intersection = new ArrayList<>();
22+
for (int num : large) {
23+
int count = freqMap.getOrDefault(num, 0);
24+
if (count > 0) {
25+
intersection.add(num);
26+
freqMap.merge(num, -1, Integer::sum);
27+
}
28+
}
29+
30+
return intersection.stream().mapToInt(v -> v).toArray();
31+
}
32+
33+
private Map<Integer, Integer> buildFrequencyMap(int[] nums) {
34+
Map<Integer, Integer> freqMap = new HashMap<>();
35+
for (int num : nums) {
36+
freqMap.merge(num, 1, Integer::sum);
37+
}
38+
39+
return freqMap;
40+
}
41+
42+
/**
43+
* Time complexity: O(nlog n + mlog m)
44+
* Space complexity: O(log n + log m)
45+
*/
46+
public int[] intersectViaSorting(int[] nums1, int[] nums2) {
47+
Arrays.sort(nums1);
48+
Arrays.sort(nums2);
49+
50+
int first = 0;
51+
int second = 0;
52+
53+
List<Integer> intersection = new ArrayList<>();
54+
while (first < nums1.length && second < nums2.length) {
55+
if (nums1[first] > nums2[second]) {
56+
second++;
57+
} else if (nums1[first] < nums2[second]) {
58+
first++;
59+
} else {
60+
intersection.add(nums1[first]);
61+
first++;
62+
second++;
63+
}
64+
}
65+
66+
67+
return intersection.stream().mapToInt(v -> v).toArray();
68+
}
69+
}

src/array/TwoSum.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ public class TwoSum {
1313

1414
public int[] twoSum(int[] nums, int target) {
1515
Map<Integer, Integer> seen = new HashMap<>();
16-
seen.put(nums[0], 0);
1716

18-
for (int i = 1; i < nums.length; i++) {
19-
int pair = target - nums[i];
20-
Integer pairIndex = seen.get(pair);
21-
if (pairIndex != null) {
22-
return new int[]{i, pairIndex};
17+
for (int i = 0; i < nums.length; i++) {
18+
int possiblePair = target - nums[i];
19+
Integer possiblePairIndex = seen.get(possiblePair);
20+
if (possiblePairIndex != null) {
21+
return new int[] {i, possiblePairIndex};
2322
}
2423

2524
seen.put(nums[i], i);

src/binary/PowerOfTwo.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package binary;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/power-of-two
5+
* Difficulty: Easy
6+
*/
7+
public class PowerOfTwo {
8+
9+
/**
10+
* Time complexity: O(1)
11+
* Space complexity: O(1)
12+
*/
13+
public boolean isPowerOfTwoViaBitManipulation(int n) {
14+
// 4 = 100; 4 - 1 = 3 = 11; 100 & 11 = 0
15+
// 8 = 1000; 8 - 1 = 7 = 111; 1000 & 111 = 0
16+
return n > 0 && (n & (n - 1)) == 0;
17+
}
18+
19+
/**
20+
* Time complexity: O(log n)
21+
* Space complexity: O(log n)
22+
*/
23+
public boolean isPowerOfTwoViaBaseConversion(int n) {
24+
return Integer.toString(n, 2).matches("^10*$");
25+
}
26+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package binary_search_tree;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/inorder-successor-in-bst
5+
* Difficulty: Medium
6+
*/
7+
public class InorderSuccessorInBST {
8+
9+
/**
10+
* Time complexity: O(h)
11+
* Space complexity: O(1)
12+
*/
13+
public TreeNode inorderSuccessorViaIteration(TreeNode root, TreeNode p) {
14+
TreeNode successor = null;
15+
16+
while (root != null) {
17+
if (p.val >= root.val) {
18+
root = root.right;
19+
} else {
20+
successor = root;
21+
root = root.left;
22+
}
23+
}
24+
25+
return successor;
26+
}
27+
28+
/**
29+
* Time complexity: O(h)
30+
* Space complexity: O(h)
31+
*/
32+
public TreeNode inorderSuccessorViaRecursion(TreeNode root, TreeNode p) {
33+
return find(root, null, p);
34+
}
35+
36+
private TreeNode find(TreeNode current, TreeNode parent, TreeNode p) {
37+
if (current == null) return parent;
38+
39+
return p.val >= current.val
40+
? find(current.right, parent, p)
41+
: find(current.left, current, p);
42+
}
43+
44+
private static class TreeNode {
45+
int val;
46+
TreeNode left;
47+
TreeNode right;
48+
}
49+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package graph;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/as-far-from-land-as-possible
8+
* Difficulty: Medium
9+
* Time complexity: O(m * n)
10+
* Space complexity: O(m * n)
11+
*/
12+
public class AsFarFromLandAsPossible {
13+
14+
public int maxDistance(int[][] grid) {
15+
int[][] visited = new int[grid.length][grid[0].length];
16+
int[][] directions = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
17+
18+
Queue<int[]> planned = new LinkedList<>();
19+
for (int i = 0; i < grid.length; i++) {
20+
for (int j = 0; j < grid[0].length; j++) {
21+
if (grid[i][j] == 1) {
22+
visited[i][j] = 1;
23+
planned.offer(new int[]{i, j});
24+
}
25+
}
26+
}
27+
28+
// no land cells or no water cells
29+
if (planned.isEmpty() || planned.size() == grid.length * grid[0].length) {
30+
return -1;
31+
}
32+
33+
int distance = 0;
34+
while (!planned.isEmpty()) {
35+
int levelSize = planned.size();
36+
distance++;
37+
38+
for (int i = 0; i < levelSize; i++) {
39+
int[] current = planned.poll();
40+
for (int[] dir : directions) {
41+
int x = current[0] + dir[0];
42+
int y = current[1] + dir[1];
43+
if (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length
44+
&& visited[x][y] == 0) {
45+
visited[x][y] = 1;
46+
planned.offer(new int[]{x, y});
47+
}
48+
}
49+
}
50+
}
51+
52+
return distance - 1;
53+
}
54+
}

src/graph/CountSubIslands.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package graph;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/count-sub-islands
5+
* Difficulty: Medium
6+
* Time complexity: O(m * n)
7+
* Space complexity: O(m * n)
8+
*/
9+
public class CountSubIslands {
10+
11+
private final int[][] directions = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
12+
13+
public int countSubIslands(int[][] grid1, int[][] grid2) {
14+
int[][] visited = new int[grid2.length][grid2[0].length];
15+
int subIslands = 0;
16+
for (int i = 0; i < grid2.length; i++) {
17+
for (int j = 0; j < grid2[0].length; j++) {
18+
if (grid2[i][j] == 1 && visited[i][j] == 0 && isSubIsland(grid1, grid2, visited, i, j)) {
19+
subIslands++;
20+
}
21+
}
22+
}
23+
24+
return subIslands;
25+
}
26+
27+
private boolean isSubIsland(int[][] grid1, int[][] grid2, int[][] visited, int x, int y) {
28+
if (grid1[x][y] != 1) return false;
29+
30+
visited[x][y] = 1;
31+
boolean isSubIsland = true;
32+
for (int[] dir : directions) {
33+
int x1 = x + dir[0];
34+
int y1 = y + dir[1];
35+
36+
if (x1 >= 0 && x1 < grid2.length && y1 >= 0 && y1 < grid2[0].length
37+
&& visited[x1][y1] == 0 && grid2[x1][y1] == 1) {
38+
if (!isSubIsland(grid1, grid2, visited, x1, y1)) {
39+
isSubIsland = false;
40+
}
41+
}
42+
}
43+
44+
return isSubIsland;
45+
}
46+
}

src/graph/FloodFill.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
/**
44
* Description: https://leetcode.com/problems/flood-fill
55
* Difficulty: Easy
6-
* Time complexity: O(n)
7-
* Space complexity: O(n)
6+
* Time complexity: O(m * n)
7+
* Space complexity: O(m * n)
88
*/
99
public class FloodFill {
1010

src/graph/NumberOfClosedIslands.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package graph;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/number-of-closed-islands
5+
* Difficulty: Medium
6+
* Time complexity: O(m * n)
7+
* Space complexity: O(m * n)
8+
*/
9+
public class NumberOfClosedIslands {
10+
11+
private final int[][] directions = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
12+
13+
public int closedIsland(int[][] grid) {
14+
int[][] visited = new int[grid.length][grid[0].length];
15+
16+
int closedIslands = 0;
17+
for (int i = 0; i < grid.length; i++) {
18+
for (int j = 0; j < grid[0].length; j++) {
19+
if (grid[i][j] == 0 && visited[i][j] == 0 && isClosed(grid, visited, i, j)) {
20+
closedIslands++;
21+
}
22+
}
23+
}
24+
25+
return closedIslands;
26+
}
27+
28+
private boolean isClosed(int[][] grid, int[][] visited, int x, int y) {
29+
if (grid[x][y] == 1 || visited[x][y] == 1) {
30+
return true;
31+
}
32+
33+
visited[x][y] = 1;
34+
boolean isClosed = true;
35+
for (int[] dir : directions) {
36+
int x1 = x + dir[0];
37+
int y1 = y + dir[1];
38+
if (x1 < 0 || x1 >= grid.length || y1 < 0 || y1 >= grid[0].length
39+
|| !isClosed(grid, visited, x1, y1)) {
40+
isClosed = false;
41+
}
42+
}
43+
44+
return isClosed;
45+
}
46+
}

0 commit comments

Comments
 (0)