Skip to content

Commit 51b85d6

Browse files
committed
Fix some typos
1 parent 787b04d commit 51b85d6

File tree

8 files changed

+128
-53
lines changed

8 files changed

+128
-53
lines changed

leetcode/15.3sum.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fun threeSum(nums: IntArray): List<List<Int>> {
4242
}
4343

4444
// Two sum is less than target, means negative number is too negative, move forward left pointer to "smaller" negative number
45-
if (nums[left] + nums[right] < target) {
45+
else if (nums[left] + nums[right] < target) {
4646
left++
4747
} else {
4848
right--

leetcode/264.ugly-number-ii.md

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ What's the next number? It would be the smallest number **all the previously exi
44

55
We assume there is one number shown, which is `1`, the next number is `min(2 * 1, 3 * 1, 5 * 1) && not shown before`, that is 2, and you can think that comes from `min(2 * dp[1], 3 * dp[1], 5 * dp[1])`.
66

7-
Again, what's the next number? now we only need to consider `min(2 * 2, 3 * 1, 5 * 1)`, that would be `min(2 * dp[2], 3 * dp[1], 5 * dp[1])`, that means we have three pointers (for 2, 3, 5 respectively) points to the previous existing number, then we move the pointer if we use that in the round.
7+
Again, what's the next number? Now we only need to consider `min(2 * 2, 3 * 1, 5 * 1)`, that would be `min(2 * dp[2], 3 * dp[1], 5 * dp[1])`, that means we have three pointers (for 2, 3, 5 respectively) points to the previous existing number, then we move the pointer if we use that in the round.
88

99
```js
1010
n=1, dp[1] = 1 // base case
@@ -51,33 +51,27 @@ fun nthUglyNumber(n: Int): Int {
5151
* **Space Complexity**: `O(n)`.
5252

5353
### Heap
54-
5554
We maintain a min heap and add `2x`, `3x` and `5x` into heap (make sure avoiding duplicate), the result will be the peek.
5655

5756
```kotlin
5857
fun nthUglyNumber(n: Int): Int {
59-
val factors = intArrayOf(2, 3, 5)
58+
val factors = listOf(2L, 3L, 5L)
6059
val minHeap = PriorityQueue<Long>()
6160
val seen = hashSetOf<Long>()
62-
63-
fun checkAndAdd(peek: Long) {
61+
var number = 1L
62+
minHeap.add(number)
63+
seen.add(number)
64+
for (i in 0 until n) {
65+
number = minHeap.poll()
6466
factors.forEach { factor ->
65-
val newValue = (peek * factor).toLong()
66-
if (!seen.contains(newValue)) {
67-
minHeap.add(newValue)
68-
seen.add(newValue)
67+
val next = factor * number
68+
if (!seen.contains(next)) {
69+
minHeap.add(next)
70+
seen.add(next)
6971
}
7072
}
7173
}
72-
73-
var result = 1
74-
minHeap.add(1)
75-
for (i in 2..n) {
76-
val peek = minHeap.poll()
77-
checkAndAdd(peek)
78-
result = minHeap.peek().toInt()
79-
}
80-
return result
74+
return number.toInt()
8175
}
8276
```
8377

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

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
We have to solve with `O(lg n)` time complexity, so we will start from binary search, but we have to decide the left or right part to search for next round, and since the array might be rotated, it won't be sorted for both left and right part, we have modify some logic here:
44

5-
In each round of binary search, we will split the `array[start..end]` into two parts: `[start..middle - 1]` and `[middle + 1..end]` and the key point here is that **one of the two parts will be sorted, we have to find the sorted part (the starting value <= the ending value of that part) and we have to check if the target is in that sorted part, if yes, just search that part, otherwise, search another part.
5+
In each round of binary search, we will split the `array[start..end]` into two parts: `[start..middle - 1]` and `[middle + 1..end]` and the key point here is that **one of the two parts will be sorted**, we have to find the sorted part (the starting value <= the ending value of that part) and we have to check if the target is in that sorted part, if yes, just search that part, otherwise, search another part.
66

77
For example, nums = `[6, 7, 0, 1, 2, 5]`, target = 3:
88
* The `middle` = 1, left part = `[6, 7, 0]`, right part = `[2, 5]`.
@@ -12,26 +12,22 @@ For example, nums = `[6, 7, 0, 1, 2, 5]`, target = 3:
1212

1313
```kotlin
1414
fun search(nums: IntArray, target: Int): Int {
15-
return binarySearch(nums, target, 0, nums.size - 1)
16-
}
17-
18-
private fun binarySearch(nums: IntArray, target: Int, start: Int, end: Int): Int {
19-
if (start > end) return -1
20-
21-
val middle = start + (end - start) / 2
22-
if (target == nums[middle]) return middle
23-
24-
// Left part is sorted
25-
if (nums[start] <= nums[middle]) {
26-
// The target is within this part
27-
if (target in nums[start]..nums[middle]) return binarySearch(nums, target, start, middle - 1)
28-
// Otherwise, we search another part.
29-
else return binarySearch(nums, target, middle + 1, end)
30-
}
31-
// Right part is sorted
32-
if (nums[middle] <= nums[end]) {
33-
if (target in nums[middle]..nums[end]) return binarySearch(nums, target, middle + 1, end)
34-
else return binarySearch(nums, target, start, middle - 1)
15+
var left = 0
16+
var right = nums.size - 1
17+
while (left <= right) {
18+
val middle = left + (right - left) / 2
19+
if (target == nums[middle]) return middle
20+
else if (nums[left] <= nums[middle]) {
21+
if (target in nums[left]..nums[middle])
22+
right = middle - 1
23+
else
24+
left = middle + 1
25+
} else {
26+
if (target in nums[middle]..nums[right])
27+
left = middle + 1
28+
else
29+
right = middle - 1
30+
}
3531
}
3632
return -1
3733
}

leetcode/622.design-circular-queue.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ class MyCircularQueue(k: Int) {
1111
fun enQueue(value: Int): Boolean {
1212
if (isFull()) return false
1313
rear = (rear + 1) % arrays.size
14+
// Or we can rotate the pointer to the beginning
15+
// rear++
16+
// if (rear >= k) rear = 0
1417
arrays[rear] = value
1518
size++
1619
return true
@@ -19,6 +22,9 @@ class MyCircularQueue(k: Int) {
1922
fun deQueue(): Boolean {
2023
if (isEmpty()) return false
2124
front = (front + 1) % arrays.size
25+
// Or we can rotate the pointer
26+
// front++
27+
// if (front >= k) front = 0
2228
size--
2329
return true
2430
}

leetcode/707.design-linked-list.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,89 @@ class MyLinkedList() {
173173
previous?.next = current?.next
174174
}
175175
}
176+
```
177+
178+
## With Sentinel
179+
```kotlin
180+
data class Node(
181+
var value: Int,
182+
var next: Node? = null
183+
)
184+
185+
class MyLinkedList() {
186+
187+
private var head: Node? = null
188+
189+
fun get(index: Int): Int {
190+
var i = 0
191+
var current: Node? = head
192+
while (i < index) {
193+
current = current?.next
194+
i++
195+
}
196+
return current?.value ?: -1
197+
}
198+
199+
fun addAtHead(`val`: Int) {
200+
val newNode = Node(`val`, next = head)
201+
head = newNode
202+
}
203+
204+
fun addAtTail(`val`: Int) {
205+
val sentinel = Node(-1, next = head)
206+
var previous: Node? = sentinel
207+
var current: Node? = head
208+
while (current != null) {
209+
previous = current
210+
current = current.next
211+
}
212+
val newNode = Node(`val`, current)
213+
previous?.next = newNode
214+
215+
// Remember to update the head
216+
head = sentinel.next
217+
}
218+
219+
fun addAtIndex(index: Int, `val`: Int) {
220+
val sentinel = Node(-1, next = head)
221+
var previous: Node? = sentinel
222+
var current: Node? = head
223+
var i = 0
224+
while (i < index) {
225+
previous = current
226+
current = current?.next
227+
i++
228+
}
229+
val newNode = Node(`val`, next = current)
230+
previous?.next = newNode
231+
232+
// Remember to update the head
233+
head = sentinel.next
234+
}
235+
236+
fun deleteAtIndex(index: Int) {
237+
val sentinel = Node(-1, next = head)
238+
var previous: Node? = sentinel
239+
var current: Node? = head
240+
var i = 0
241+
while (i < index) {
242+
previous = current
243+
current = current?.next
244+
i++
245+
}
246+
previous?.next = current?.next
247+
// Remember to update the head
248+
head = sentinel.next
249+
}
250+
}
251+
252+
/**
253+
* Your MyLinkedList object will be instantiated and called as such:
254+
* var obj = MyLinkedList()
255+
* var param_1 = obj.get(index)
256+
* obj.addAtHead(`val`)
257+
* obj.addAtTail(`val`)
258+
* obj.addAtIndex(index,`val`)
259+
* obj.deleteAtIndex(index)
260+
*/
176261
```

topics/array.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ Comparison to Linked List, see [Linked List](../topics/linked-list.md) topic.
7676
* For in-place operation or `O(1)` space complexity, use array itself as a hash table. For example, the value of array ranges from 1 to `n`, where `n` is the size of array, then we can use the *index* to represent.
7777

7878
## Two Pointers Approach
79-
* Fast/slow (read/write) pointers: [283. Move Zeroes](../leetcode/283.move-zeros.md) (
79+
* Fast/slow (read/write) pointers: [283. Move Zeroes](../leetcode/283.move-zeros.md)
8080
* Left/right pointers: [977. Squares of a Sorted Array](../leetcode/977.squares-of-a-sorted-array.md)
8181

8282
## Sliding Window Approach

topics/graph.md

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@ A graph `G = (V, E)`, consists of `V` (set of *vertices*) and `E` (*edges* of ve
99
1010
![Graph](../media/graph.png)
1111

12-
* G1: Undirected
13-
* V(G1) = {1, 2, 3, 4, 5, 6}
14-
* E(G1) = {(1, 2), (1, 4), (1, 5), (1, 6), (2, 4), (3, 4), (3, 5), (3, 6)}
15-
* G2: Directed
16-
* V(G2) = {1, 2, 3, 4, 5, 6}
17-
* E(G2) = {(1, 2), (3, 4), (4, 5), (6, 1), (6, 3)}
18-
19-
> V and E are set, the item in the set is unordered.
20-
2112
## Representation
2213
We can represent a graph `G = (V, E)` in adjacency list or matrix.
2314

@@ -44,7 +35,7 @@ A1 = [
4435
* The vertices in each adjacency list are typically stored in a arbitrary order.
4536
* For both undirected and directed graph, the amount of memory is `Θ(|V| + |E|)` space complexity. (`|V| + |E|` for directed, `|V| + 2 * |E|` for undirected)
4637
* It takes `Ω(|V|)` time to determine if an edge `(x, y)` is in the graph. (Loop for each vertices takes `O(1)` and `O(|V|)` for searching the adjacent vertices of the vertex `x`)
47-
* We prefer adjacency matrix when the graph are *sparse*.
38+
* We prefer adjacency list when the graph are *sparse*.
4839
* We also can associate *weight* on the edge by storing the weight on the node of the adjacency list. (linked list node can attach extra properties)
4940

5041
> |V| means the size of V.
@@ -91,6 +82,8 @@ S1 = {
9182
}
9283
```
9384

85+
> [Representation comparision](https://www.geeksforgeeks.org/comparison-between-adjacency-list-and-adjacency-matrix-representation-of-graph/)
86+
9487
## Breadth-first Search (BFS)
9588
Given a graph `G = (V, E)` (undirected or directed) and source `s`, we "discover" every vertex that is reachable from `s`. It visits all vertices at level `k` before visiting level `k + 1`. It computes the *distance* from `s` to each reachable vertex, and produces a *breadth-first tree* (shortest path) with root `s` with all reachable vertices.
9689

topics/stack-queue.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ class LinkedListQueue<T>: Queue<T> {
189189
if (isEmpty()) return null
190190
val value = head?.data
191191
head = head?.next
192-
if (isEmpty()) rear = null
193192
size--
193+
if (isEmpty()) rear = null
194194
return value
195195
}
196196

@@ -252,7 +252,8 @@ class DynamicArrayQueue<T>: Queue<T> {
252252
}
253253
```
254254

255-
> Verify if it's still true.
255+
> TODO: Verify if it's still true.
256+
256257
There is a drawback from the above implementation, our size is limited even if we dequeue all elements (we move `head` to the end of array when dequeue, but won't start from 0 again). To solve this case, we introduce [*Circular Queue*](../leetcode/622.design-circular-queue.md).
257258

258259
## Tips for [Problem Solving](../problems/problems-solutions.md#stack--queue)

0 commit comments

Comments
 (0)