Skip to content

Commit 6bc849e

Browse files
committed
Update
1 parent d0635d7 commit 6bc849e

14 files changed

+624
-1
lines changed

src/array/ContainerWithMostWater.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package array;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/container-with-most-water
5+
* Difficulty: Medium
6+
* Time complexity: O(n)
7+
* Space complexity: O(1)
8+
*/
9+
public class ContainerWithMostWater {
10+
11+
public int maxArea(int[] height) {
12+
int left = 0;
13+
int right = height.length - 1;
14+
15+
int max = 0;
16+
while (left < right) {
17+
int space = Math.min(height[left], height[right]) * (right - left);
18+
max = Math.max(space, max);
19+
20+
if (height[left] > height[right]) {
21+
right--;
22+
} else {
23+
left++;
24+
}
25+
}
26+
27+
return max;
28+
}
29+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package array;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/longest-consecutive-sequence
8+
* Difficulty: Medium
9+
* Time complexity: O(n)
10+
* Space complexity: O(n)
11+
*/
12+
public class LongestConsecutiveSequence {
13+
14+
public int longestConsecutive(int[] nums) {
15+
Set<Integer> numsSet = new HashSet<>(nums.length);
16+
for (int num : nums) {
17+
numsSet.add(num);
18+
}
19+
20+
int max = 0;
21+
for (int num : nums) {
22+
if (numsSet.contains(num - 1)) continue; // num is not the start of a sequence
23+
24+
int count = 1;
25+
int next = num + 1;
26+
while (numsSet.contains(next)) {
27+
count++;
28+
next++;
29+
}
30+
31+
max = Math.max(count, max);
32+
}
33+
34+
return max;
35+
}
36+
}

src/array/SubarraySumEqualsK.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package array;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/subarray-sum-equals-k
8+
* Difficulty: Medium
9+
* Time complexity: O(n)
10+
* Space complexity: O(n)
11+
*/
12+
public class SubarraySumEqualsK {
13+
14+
public int subarraySum(int[] nums, int targetSum) {
15+
Map<Long, Integer> prefixSumFrequencyMap = new HashMap<>();
16+
17+
int count = 0;
18+
long currentSum = 0;
19+
for (int num : nums) {
20+
currentSum += num;
21+
22+
int currentSubarray = currentSum == targetSum ? 1 : 0;
23+
int previousSubarrays = prefixSumFrequencyMap.getOrDefault(currentSum - targetSum, 0);
24+
25+
count += currentSubarray + previousSubarrays;
26+
27+
prefixSumFrequencyMap.merge(currentSum, 1, Integer::sum);
28+
}
29+
30+
return count;
31+
}
32+
}

src/array/TreeSum.java renamed to src/array/ThreeSum.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Time complexity: O(n^2)
99
* Space complexity: O(n)
1010
*/
11-
public class TreeSum {
11+
public class ThreeSum {
1212

1313
public List<List<Integer>> threeSumViaTwoPointers(int[] nums) {
1414
Set<List<Integer>> result = new HashSet<>();

src/array/TwoSum2.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package array;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted
5+
* Difficulty: Medium
6+
* Time complexity: O(n)
7+
* Space complexity: O(1)
8+
*/
9+
public class TwoSum2 {
10+
11+
public int[] twoSum(int[] numbers, int target) {
12+
int left = 0;
13+
int right = numbers.length - 1;
14+
15+
while (left < right) {
16+
int sum = numbers[left] + numbers[right];
17+
18+
if (sum > target) {
19+
right--;
20+
} else if (sum < target) {
21+
left++;
22+
} else {
23+
return new int[] {left + 1, right + 1};
24+
}
25+
}
26+
27+
throw new RuntimeException();
28+
}
29+
}

src/binary_search/Search2DMatrix.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package binary_search;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/search-a-2d-matrix
5+
* Difficulty: Medium
6+
* Time complexity: O(log n + log m)
7+
* Space complexity: O(1)
8+
*/
9+
public class Search2DMatrix {
10+
11+
public boolean searchMatrix(int[][] matrix, int target) {
12+
int row = findRowNumber(matrix, target);
13+
return containsTarget(matrix[row], target);
14+
}
15+
16+
private int findRowNumber(int[][] matrix, int target) {
17+
int left = 0;
18+
int right = matrix.length - 1;
19+
20+
while (left < right) {
21+
int mid = left + (right - left) / 2;
22+
23+
if (target > matrix[mid][matrix[0].length - 1]){
24+
left = mid + 1;
25+
} else if (target < matrix[mid][0]) {
26+
right = mid - 1;
27+
} else {
28+
return mid;
29+
}
30+
}
31+
32+
return left;
33+
}
34+
35+
private boolean containsTarget(int[] row, int target) {
36+
int left = 0;
37+
int right = row.length - 1;
38+
39+
while (left <= right) {
40+
int mid = left + (right - left) / 2;
41+
42+
if (target > row[mid]) {
43+
left = mid + 1;
44+
} else if (target < row[mid]) {
45+
right = mid - 1;
46+
} else {
47+
return true;
48+
}
49+
}
50+
51+
return false;
52+
}
53+
}

src/binary_tree/PathSum.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package binary_tree;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/path-sum
5+
* Difficulty: Easy
6+
* Time complexity: O(n)
7+
* Space complexity: O(h)
8+
*/
9+
public class PathSum {
10+
11+
public boolean hasPathSum(TreeNode root, int remains) {
12+
if (root == null) return false;
13+
14+
remains -= root.val;
15+
if (root.left == null && root.right == null) {
16+
return remains == 0;
17+
}
18+
19+
return hasPathSum(root.right, remains) || hasPathSum(root.left, remains);
20+
}
21+
22+
private static class TreeNode {
23+
int val;
24+
TreeNode left;
25+
TreeNode right;
26+
}
27+
}

src/binary_tree/PathSum2.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package binary_tree;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/path-sum-ii
8+
* Difficulty: Medium
9+
* Time complexity: O(n)
10+
* Space complexity: O(n)
11+
*/
12+
public class PathSum2 {
13+
14+
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
15+
List<List<Integer>> paths = new ArrayList<>();
16+
pathSum(root, new ArrayList<>(), targetSum, paths);
17+
return paths;
18+
}
19+
20+
private void pathSum(
21+
TreeNode root,
22+
List<Integer> currentPath,
23+
int remains,
24+
List<List<Integer>> paths) {
25+
if (root == null) return;
26+
27+
currentPath.add(root.val);
28+
29+
if (root.left == null && root.right == null && remains == root.val) {
30+
paths.add(new ArrayList<>(currentPath));
31+
} else {
32+
pathSum(root.left, currentPath, remains - root.val, paths);
33+
pathSum(root.right, currentPath, remains - root.val, paths);
34+
}
35+
36+
currentPath.remove(currentPath.size() - 1);
37+
}
38+
39+
private static class TreeNode {
40+
int val;
41+
TreeNode left;
42+
TreeNode right;
43+
}
44+
}

src/binary_tree/PathSum3.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package binary_tree;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/path-sum-iii
8+
* Difficulty: Medium
9+
*/
10+
public class PathSum3 {
11+
12+
public int pathSumViaMemoization(TreeNode root, int targetSum) {
13+
return pathSum(root, 0L, targetSum, new HashMap<>());
14+
}
15+
16+
/**
17+
* Time complexity: O(n)
18+
* Space complexity: O(n)
19+
*/
20+
private int pathSum(
21+
TreeNode root,
22+
long currentSum,
23+
int targetSum,
24+
Map<Long, Integer> prefixSumFrequencies) {
25+
if (root == null) return 0;
26+
27+
currentSum += root.val;
28+
29+
int currentPath = currentSum == targetSum ? 1 : 0;
30+
int previousPaths = prefixSumFrequencies.getOrDefault(currentSum - targetSum, 0);
31+
32+
prefixSumFrequencies.merge(currentSum, 1, Integer::sum);
33+
34+
int result = currentPath
35+
+ previousPaths
36+
+ pathSum(root.left, currentSum, targetSum, prefixSumFrequencies)
37+
+ pathSum(root.right, currentSum, targetSum, prefixSumFrequencies);
38+
39+
prefixSumFrequencies.merge(currentSum, -1, Integer::sum);
40+
41+
return result;
42+
}
43+
44+
/**
45+
* Time complexity: O(n^2)
46+
* Space complexity: O(h)
47+
*/
48+
public int pathSum(TreeNode root, int remains) {
49+
if (root == null) return 0;
50+
51+
return pathSumStartingWith(root, remains)
52+
+ pathSum(root.left, remains)
53+
+ pathSum(root.right, remains);
54+
}
55+
56+
private int pathSumStartingWith(TreeNode root, int remains) {
57+
if (root == null) return 0;
58+
59+
return (remains == root.val ? 1 : 0)
60+
+ pathSumStartingWith(root.left, remains - root.val)
61+
+ pathSumStartingWith(root.right, remains - root.val);
62+
}
63+
64+
private static class TreeNode {
65+
int val;
66+
TreeNode left;
67+
TreeNode right;
68+
}
69+
}

0 commit comments

Comments
 (0)