Skip to content

Commit 7b55ad7

Browse files
committed
Add some solutions to new problems
16. 3Sum Cloest 221. Maximal Square 45. Jump Game II 653. Two Sum IV Input Is A BST
1 parent 2ee5c12 commit 7b55ad7

8 files changed

+196
-5
lines changed

leetcode/16.3sum-closest.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## [16. 3Sum Closest](https://leetcode.com/problems/3sum-closest/)
2+
3+
The idea is similar to [15. 3Sum](../leetcode/15.3sum.md) with some modification.
4+
5+
> Nice explanation: https://leetcode.com/problems/3sum-closest/solutions/7883/c-solution-o-n-2-using-sort/
6+
7+
```kotlin
8+
fun threeSumClosest(nums: IntArray, target: Int): Int {
9+
nums.sort()
10+
var resultSum = Int.MAX_VALUE
11+
for (i in 0 until nums.size) {
12+
val first = nums[i]
13+
14+
var secondIndex = i + 1
15+
var thirdIndex = nums.size - 1
16+
while (secondIndex < thirdIndex) {
17+
val sum = first + nums[secondIndex] + nums[thirdIndex]
18+
val diff = abs(sum - target)
19+
if (sum == target) return sum
20+
if (abs(resultSum - target) > diff) {
21+
resultSum = sum
22+
}
23+
if (sum > target) {
24+
thirdIndex--
25+
} else if (sum < target) {
26+
secondIndex++
27+
}
28+
}
29+
}
30+
return resultSum
31+
}
32+
```
33+
34+
* **Time Complexity**: `O(n^2)`.
35+
* **Space Complexity**: `O(lg n)` for sorting.

leetcode/207.course-schedule.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,9 @@ class Solution {
4343
topologicalSort.push(source)
4444
}
4545
}
46-
```
46+
```
47+
48+
### Clarification Questions
49+
1. Are all the prerequisites pair unique?
50+
2. What if `prerequisites` is empty? (but `numCourses` is 1 or greater than 1)
51+
3. Do all the prerequisite pairs cover all courses from 0 to `numCourses - 1`.

leetcode/221.maximal-square.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
## [221. Maximal Square](https://leetcode.com/problems/maximal-square/)
2+
3+
### Brute Force
4+
```kotlin
5+
fun maximalSquare(matrix: Array<CharArray>): Int {
6+
val m = matrix.size
7+
val n = matrix[0].size
8+
9+
var maxSquare = 0
10+
for (row in 0 until m) {
11+
for (col in 0 until n) {
12+
var i = row
13+
var j = col
14+
var currentLength = if (matrix[i][j] == '1') 1 else 0
15+
if (currentLength == 0) continue
16+
while (i + 1 < m && j + 1 < n && matrix[i + 1][j + 1] == '1') {
17+
var allOnes = true
18+
for (r in row..i) {
19+
if (matrix[r][j + 1] == '0') {
20+
allOnes = false
21+
break
22+
}
23+
}
24+
if (!allOnes) break
25+
for (c in col..j) {
26+
if (matrix[i + 1][c] == '0') {
27+
allOnes = false
28+
break
29+
}
30+
}
31+
32+
if (allOnes) {
33+
currentLength++
34+
} else {
35+
break
36+
}
37+
i++
38+
j++
39+
}
40+
41+
maxSquare = max(maxSquare, currentLength * currentLength)
42+
}
43+
}
44+
return maxSquare
45+
}
46+
```
47+
48+
### Dynamic Programming
49+
```kotlin
50+
fun maximalSquare(matrix: Array<CharArray>): Int {
51+
val dp = Array(matrix.size) { _ -> IntArray(matrix[0].size) }
52+
var longestLength = 0
53+
for (i in 0 until matrix.size) {
54+
for (j in 0 until matrix[0].size) {
55+
dp[i][j] = if (matrix[i][j] == '0') 0 else {
56+
if (i == 0 || j == 0) 1
57+
else min(
58+
dp[i - 1][j - 1],
59+
min(dp[i - 1][j], dp[i][j - 1])
60+
) + 1
61+
}
62+
longestLength = max(longestLength, dp[i][j])
63+
}
64+
}
65+
return longestLength * longestLength
66+
}
67+
```

leetcode/33.search-in-rotated-sorted-array.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ fun search(nums: IntArray, target: Int): Int {
1717
while (left <= right) {
1818
val middle = left + (right - left) / 2
1919
if (target == nums[middle]) return middle
20-
else if (nums[left] <= nums[middle]) {
20+
21+
// Or we can exchange the logic for `if (nums[middle] <= nums[right])`, it leads to the same accepted result.
22+
if (nums[left] <= nums[middle]) {
2123
if (target in nums[left]..nums[middle])
2224
right = middle - 1
2325
else
@@ -33,4 +35,36 @@ fun search(nums: IntArray, target: Int): Int {
3335
}
3436
```
3537

36-
Another way to implement the binary search
38+
## Corner Cases
39+
```js
40+
[3,1]
41+
target = 0
42+
```
43+
44+
If you implement the following logic, it leads TLE:
45+
```kotlin
46+
if (nums[left] < nums[middle]) {
47+
if (target in nums[left]..nums[middle])
48+
right = middle - 1
49+
else
50+
left = middle + 1
51+
} else {
52+
if (target in nums[middle]..nums[right])
53+
left = middle + 1
54+
else
55+
right = middle - 1
56+
}
57+
58+
// Or
59+
if (nums[left] < nums[middle]) {
60+
if (target in nums[left]..nums[middle])
61+
right = middle - 1
62+
else
63+
left = middle + 1
64+
} else if (nums[middle] <= nums[right]) {
65+
if (target in nums[middle]..nums[right])
66+
left = middle + 1
67+
else
68+
right = middle - 1
69+
}
70+
```

leetcode/34.find-first-and-last-position-of-element-in-sorted-array.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,6 @@ private fun findEndingPosition(nums: IntArray, target: Int): Int {
134134
right = middle - 1
135135
}
136136
}
137-
return if (found) left - 1 else - 1
137+
return if (found) right else - 1
138138
}
139139
```

leetcode/45.jump-game-ii.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## [45. Jump Game II]()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## [653. Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/)
2+
3+
### Inorder + Two Pointers
4+
```kotlin
5+
private val inorder = mutableListOf<Int>()
6+
7+
fun findTarget(root: TreeNode?, k: Int): Boolean {
8+
inorder(root)
9+
var left = 0
10+
var right = inorder.size - 1
11+
while (left < right) {
12+
val sum = inorder[left] + inorder[right]
13+
if (sum == k) return true
14+
if (sum < k) left++
15+
else right--
16+
}
17+
return false
18+
}
19+
20+
private fun inorder(root: TreeNode?) {
21+
if (root == null) return
22+
if (root.left != null) inorder(root.left)
23+
inorder.add(root.`val`)
24+
if (root.right != null) inorder(root.right)
25+
}
26+
```
27+
28+
* **Time Complexity**: `O(n)`.
29+
* **Space Complexity**: `O(n)`.
30+
31+
### Hash Table
32+
```kotlin
33+
private val hashSet = hashSetOf<Int>()
34+
35+
fun findTarget(root: TreeNode?, k: Int): Boolean {
36+
if (root == null) return false
37+
val remaining = k - root.`val`
38+
if (hashSet.contains(remaining)) return true
39+
hashSet.add(root.`val`)
40+
hashSet.add(remaining)
41+
return findTarget(root.left, k) || findTarget(root.right, k)
42+
}
43+
```
44+
45+
* **Time Complexity**: `O(n)`.
46+
* **Space Complexity**: `O(n)`.

topics/problems-solutions.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
|[977. Squares of a Sorted Array](../leetcode/977.squares-of-a-sorted-array.md)|Easy|
77
|[283. Move Zeroes](../leetcode/283.move-zeros.md)|Easy|
88
|[15. 3Sum](../leetcode/15.3sum.md)|Medium|
9+
|[16. 3Sum Closest](../leetcode/16.3sum-closest.md)|Medium|
910
|[1. Two Sum](../leetcode/1.two-sum.md)|Easy|
1011
|[217. Contains Duplicate](../leetcode/217.contains-duplicate.md)|Easy|
1112
|[350. Intersection of Two Arrays II](../leetcode/350.intersection-of-two-arrays-ii.md)|Easy|
@@ -26,6 +27,7 @@
2627
|[674. Longest Continuous Increasing Subsequence](../leetcode/674.longest-continuous-increasing-subsequence.md)|Easy|
2728
|[76. Minimum Window Substring](../leetcode/76.minimum-window-substring.md)|Hard|
2829
|[239. Sliding Window Maximum](../leetcode/239.sliding-window-maximium.md)|Hard|
30+
|[221. Maximal Square](../leetcode/221.maximal-square.md)|Medium|
2931

3032
> * https://leetcode.com/problems/find-all-duplicates-in-an-array/ 6k m
3133
> * https://leetcode.com/problems/merge-intervals/ 14k m
@@ -142,6 +144,7 @@
142144
|[98. Validate Binary Search Tree](../leetcode/98.validate-binary-search-tree.md)|Medium|
143145
|[230. Kth Smallest Element in a BST](../leetcode/230.kth-smallest-element-in-a-bst.md)|Medium|
144146
|[235. Lowest Common Ancestor of a Binary Search Tree](../leetcode/235.lowest-common-acestor-of-a-binary-search-tree.md)|Easy|
147+
|[653. Two Sum IV - Input is a BST](../leetcode/653.two-sum-iv-input-is-a-bst.md)|Easy|
145148

146149
> * https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/ 4k m
147150
> * https://leetcode.com/problems/serialize-and-deserialize-bst/ 2k m
@@ -217,6 +220,7 @@
217220
|[152. Maximum Product Subarray](../leetcode/153.maximum-product-subarray.md)|Medium|
218221
|[32. Longest Valid Parentheses](../leetcode/32.longest-valid-parentheses.md)|Hard|
219222
|[55. Jump Game](../leetcode/55.jump-game.md)|Medium|
223+
|[45. Jump Game II](../leetcode/45.jump-game-ii.md)|Medium|
220224
|[139. Word Break](../leetcode/139.word-break.md)|Medium|
221225
|[64. Minimum Path Sum](../leetcode/64.minimum-path-sum.md)|Medium|
222226
|[279. Perfect Squares](../leetcode/279.perfect-squares.md)|Medium|
@@ -244,7 +248,6 @@
244248
> * https://leetcode.com/problems/ones-and-zeroes/ 4k m
245249
> * https://leetcode.com/problems/integer-break/ 3k m
246250
> * https://leetcode.com/problems/triangle/ 6k m
247-
> * https://leetcode.com/problems/maximal-square/ 7k m
248251
249252
### LCS
250253
> * https://leetcode.com/problems/distinct-subsequences/ 4k h

0 commit comments

Comments
 (0)