Skip to content

Commit dcae267

Browse files
committed
update
1 parent 9fae610 commit dcae267

3 files changed

+20
-20
lines changed

✅ Pattern 02: Two Pointers.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ pair_with_targetsum([3, 3], 6)//[0, 1]
106106
## Remove Duplicates (easy)
107107
https://leetcode.com/problems/remove-duplicates-from-sorted-array/
108108

109-
> Given an array of sorted numbers, <b>remove all duplicates</b> from it. You should <b>not use any extra space</b>; after removing the duplicates in-place return the length of the subarray that has no duplicate in it.
109+
> Given an array of sorted numbers, <b>remove all duplicates</b> from it. You should <b>not use any extra space</b>; after removing the duplicates </i>in-place</i> return the length of the subarray that has no duplicate in it.
110110
111-
In this problem, we need to remove the duplicates in-place such that the resultant length of the array remains sorted. As the input array is sorted, therefore, one way to do this is to shift the elements left whenever we encounter duplicates. In other words, we will keep one pointer for iterating the array and one pointer for placing the next non-duplicate number. So our algorithm will be to iterate the array and whenever we see a non-duplicate number we move it next to the last non-duplicate number we’ve seen.
111+
In this problem, we need to remove the duplicates </i>in-place</i> such that the resultant length of the array remains sorted. As the input array is sorted, therefore, one way to do this is to shift the elements left whenever we encounter duplicates. In other words, we will keep one pointer for iterating the array and one pointer for placing the next non-duplicate number. So our algorithm will be to iterate the array and whenever we see a non-duplicate number we move it next to the last non-duplicate number we’ve seen.
112112

113113
* Assume the input is sorted
114114
````js
@@ -141,7 +141,7 @@ removeDuplicates([2, 2, 2, 11])//2, The first two elements after removing the du
141141
### Remove Element
142142
https://leetcode.com/problems/remove-element/
143143

144-
> Given an unsorted array of numbers and a target `key`, remove all instances of `key` in-place and return the new length of the array.
144+
> Given an unsorted array of numbers and a target `key`, remove all instances of `key` </i>in-place</i> and return the new length of the array.
145145
146146
````js
147147
function removeElement(arr, key) {
@@ -388,7 +388,7 @@ tripletWithSmallerSum ([], 0)//0
388388
tripletWithSmallerSum ([0], 0)//0
389389
````
390390
- Sorting the array will take `O(N * logN)`. The `searchPair()` will take `O(N)`. So, overall `searchTriplets()` will take `O(N * logN + N^2)`, which is asymptotically equivalent to `O(N^2)`.
391-
- The space complexity of the above algorithm will be `O(N)` which is required for sorting if we are not using an in-place sorting algorithm.
391+
- The space complexity of the above algorithm will be `O(N)` which is required for sorting if we are not using an </i>in-place</i> sorting algorithm.
392392

393393
> Write a function to return the list of all such triplets instead of the count. How will the time complexity change in this case?
394394
@@ -471,7 +471,7 @@ findSubarrays([10, 5, 2, 6], 100)//The 8 subarrays that have product less than 1
471471
## Dutch National Flag Problem (medium)
472472
https://leetcode.com/problems/sort-colors/
473473

474-
> Given an array containing `0`s, `1`s and `2`s, sort the array in-place. You should treat numbers of the array as objects, hence, we can’t count `0`s, `1`s, and `2`s to recreate the array.
474+
> Given an array containing `0`s, `1`s and `2`s, sort the array </i>in-place</i>. You should treat numbers of the array as objects, hence, we can’t count `0`s, `1`s, and `2`s to recreate the array.
475475
476476
The flag of the Netherlands consists of three colors: red, white and blue; and since our input array also consists of three different numbers that is why it is called <b>Dutch National Flag problem</b>.
477477

✅ Pattern 03: Fast & Slow pointers.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ https://leetcode.com/problems/happy-number/
223223

224224
Any number will be called a <b>happy number</b> if, after repeatedly replacing it with a number equal to the <b>sum of the square of all of its digits, leads us to number `1`</b>. All other <b>(not-happy)</b> numbers will never reach `1`. Instead, they will be stuck in a cycle of numbers which does not include `1`.
225225

226-
The process, defined above, to find out if a number is a <b>happy number</b> or not, always ends in a cycle. If the number is a <b>happy number</b>, the process will be stuck in a cycle on number ‘1,’ and if the number is not a <b>happy number</b> then the process will be stuck in a cycle with a set of numbers. As we saw in Example-2 while determining if ‘12’ is a <b>happy number</b> or not, our process will get stuck in a cycle with the following numbers: `89 -> 145 -> 42 -> 20 -> 4 -> 16 -> 37 -> 58 -> 89`
226+
The process, defined above, to find out if a number is a <b>happy number</b> or not, always ends in a cycle. If the number is a <b>happy number</b>, the process will be stuck in a cycle on number `1`, and if the number is not a <b>happy number</b> then the process will be stuck in a cycle with a set of numbers. As we saw in Example-2 while determining if `12` is a <b>happy number</b> or not, our process will get stuck in a cycle with the following numbers: `89 -> 145 -> 42 -> 20 -> 4 -> 16 -> 37 -> 58 -> 89`
227227

228228
We saw in the <b>LinkedList Cycle</b> problem that we can use the <b>Fast & Slow</b> pointers method to find a cycle among a set of elements. As we have described above, each number will definitely have a cycle. Therefore, we will use the same <i>fast</i> & <i>slow pointer</i> strategy to find the cycle and once the cycle is found, we will see if the cycle is stuck on number `1` to find out if the number is happy or not.
229229

@@ -281,7 +281,7 @@ function findSquareSum(num) {
281281
11. 1² + 6² = 1 + 36 = 37
282282
12. 3² + 7² = 9 + 49 = 58
283283
13. 5² + 8²= 25 + 64 = 89
284-
Step ‘13’ leads us back to step ‘5’ as the number becomes equal to 89’, this means that we can never reach `1`, therefore, ‘12’ is not a <b>happy number</b>.
284+
Step `13` leads us back to step `5` as the number becomes equal to `89’, this means that we can never reach `1`, therefore, `12` is not a <b>happy number</b>.
285285

286286
`findHappyNumber(19)//true`
287287

@@ -298,8 +298,8 @@ Step ‘13’ leads us back to step ‘5’ as the number becomes equal to ‘89
298298
- The time complexity of the algorithm is difficult to determine. However we know the fact that all <b>unhappy number</b>s eventually get stuck in the cycle: 4 -> 16 -> 37 -> 58 -> 89 -> 145 -> 42 -> 20 -> 4
299299

300300
This sequence behavior tells us two things:
301-
1. If the number `N` is less than or equal to 1000, then we reach the cycle or `1` in at most 1001 steps.
302-
2. For `N > 1000`, suppose the number has `M` digits and the next number is `N1`. From the above Wikipedia link, we know that the sum of the squares of the digits of `N` is at most 9²M, or `81M`(this will happen when all digits of `N` are `9`).
301+
1. If the number `N` is less than or equal to `1000`, then we reach the cycle or `1` in at most `1001` steps.
302+
2. For `N > 1000`, suppose the number has `M` digits and the next number is `N1`. From the above Wikipedia link, we know that the sum of the squares of the digits of `N` is at most `9²M`, or `81M`(this will happen when all digits of `N` are `9`).
303303

304304
This means:
305305
1. `N1 < 81M`
@@ -458,9 +458,9 @@ console.log(`Is palindrome: ${isPalindromicLinkedList(head)}`)
458458
https://leetcode.com/problems/reorder-list/
459459

460460

461-
> Given the head of a Singly <b>LinkedList</b>, write a method to modify the <b>LinkedList</b> such that the <b>nodes from the second half of the <b>LinkedList</b> are inserted alternately to the nodes from the first half in reverse order</b>. So if the <b>LinkedList</b> has nodes 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null, your method should return 1 -> 6 -> 2 -> 5 -> 3 -> 4 -> null.
461+
> Given the head of a Singly <b>LinkedList</b>, write a method to modify the <b>LinkedList</b> such that the <b>nodes from the second half of the <b>LinkedList</b> are inserted alternately to the nodes from the first half in reverse order</b>. So if the <b>LinkedList</b> has nodes `1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null`, your method should return `1 -> 6 -> 2 -> 5 -> 3 -> 4 -> null`.
462462
>
463-
> Your algorithm should not use any extra space and the input <b>LinkedList</b> should be modified in-place.
463+
> Your algorithm should not use any extra space and the input <b>LinkedList</b> should be modified </i>in-place</i>.
464464
465465
### Example 1:
466466
````
@@ -561,7 +561,7 @@ head.printList()
561561
## 🌟 Cycle in a Circular Array (hard)
562562
https://leetcode.com/problems/circular-array-loop/
563563

564-
We are given an array containing positive and negative numbers. Suppose the array contains a number ‘M’ at a particular index. Now, if ‘M’ is positive we will move forward ‘M’ indices and if ‘M’ is negative move backwards ‘M’ indices. You should assume that the <b>array is circular</b> which means two things:
564+
We are given an array containing positive and negative numbers. Suppose the array contains a number `M` at a particular index. Now, if `M` is positive we will move forward `M` indices and if `M` is negative move backwards `M` indices. You should assume that the <b>array is circular</b> which means two things:
565565
1. If, while moving forward, we reach the end of the array, we will jump to the first element to continue the movement.
566566
2. If, while moving backward, we reach the beginning of the array, we will jump to the last element to continue the movement.
567567
Write a method to determine <b>if the array has a cycle</b>. The cycle should have more than one element and should follow one direction which means the cycle should not contain both forward and backward movements.

✅ Pattern 06: In-place Reversal of a LinkedList.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Pattern 6: In-place Reversal of a LinkedList
22

3-
In a lot of problems, we are asked to reverse the links between a set of nodes of a <b>LinkedList</b>. Often, the constraint is that we need to do this in-place, i.e., using the existing node objects and without using extra memory.
3+
In a lot of problems, we are asked to reverse the links between a set of nodes of a <b>LinkedList</b>. Often, the constraint is that we need to do this </i>in-place</i>, i.e., using the existing node objects and without using extra memory.
44

5-
<b>In-place Reversal of a LinkedList pattern</b> describes an efficient way to solve the above problem.
5+
<b></i>in-place</i> Reversal of a LinkedList pattern</b> describes an efficient way to solve the above problem.
66

77
## Reverse a LinkedList (easy)
88
https://leetcode.com/problems/reverse-linked-list/
@@ -62,14 +62,14 @@ console.log(`Nodes of original LinkedList are: ${head.printList()}`)
6262
console.log(`Nodes of reversed LinkedList are: ${reverse(head).printList()}`)
6363
````
6464

65-
- The time complexity of our algorithm will be `O(N)` where `N` is the total number of nodes in the <b>LinkedList</b>.
65+
- The time complexity of our algorithm will be `O(N)` where `N` is the total number of nodes in the <b>LinkedList</b>.
6666
- We only used constant space, therefore, the space complexity of our algorithm is `O(1)`.
6767

6868
## Reverse a Sub-list (medium)
6969
https://leetcode.com/problems/reverse-linked-list-ii/
7070
> Given the head of a <b>LinkedList</b> and two positions `p` and `q`, reverse the <b>LinkedList</b> from position `p` to `q`.
7171
72-
The problem follows the <b>In-place Reversal</b> of a <b>LinkedList</b> pattern. We can use a similar approach as discussed in <b>Reverse a LinkedList</b>. Here are the steps we need to follow:
72+
The problem follows the <b></i>in-place</i> Reversal</b> of a <b>LinkedList</b> pattern. We can use a similar approach as discussed in <b>Reverse a LinkedList</b>. Here are the steps we need to follow:
7373
1. Skip the first `p-1` nodes, to reach the node at position `p`.
7474
2. Remember the node at position `p-1` to be used later to connect with the reversed sub-list.
7575
3. Next, reverse the nodes from `p` to `q` using the same approach discussed in <b>Reverse a LinkedList</b>.
@@ -187,7 +187,7 @@ https://leetcode.com/problems/reverse-nodes-in-k-group/
187187
> Given the head of a <b>LinkedList</b> and a number ‘k’, <b>reverse every ‘k’ sized sub-list</b> starting from the head.
188188
> If, in the end, you are left with a sub-list with less than ‘k’ elements, reverse it too.
189189
190-
The problem follows the <b>In-place Reversal of a LinkedList</b> pattern and is quite similar to <b>Reverse a Sub-list</b>. The only difference is that we have to reverse all the sub-lists. We can use the same approach, starting with the first sub-list (i.e. `p=1, q=k`) and keep reversing all the sublists of size ‘k’.
190+
The problem follows the <b></i>in-place</i> Reversal of a LinkedList</b> pattern and is quite similar to <b>Reverse a Sub-list</b>. The only difference is that we have to reverse all the sub-lists. We can use the same approach, starting with the first sub-list (i.e. `p=1, q=k`) and keep reversing all the sublists of size ‘k’.
191191

192192
````js
193193
class Node {
@@ -275,7 +275,7 @@ console.log(`Nodes of reversed LinkedList are: ${reverseEveryKElements(head, 3).
275275
>
276276
> If, in the end, you are left with a sub-list with less than `K` elements, reverse it too.
277277
278-
The problem follows the <b>In-place Reversal of a LinkedList</b> pattern and is quite similar to <b>Reverse every K-element Sub-list</b>. The only difference is that we have to skip `K` alternating elements. We can follow a similar approach, and in each iteration after reversing `K` elements, we will skip the next `K` elements.
278+
The problem follows the <b></i>in-place</i> Reversal of a LinkedList</b> pattern and is quite similar to <b>Reverse every K-element Sub-list</b>. The only difference is that we have to skip `K` alternating elements. We can follow a similar approach, and in each iteration after reversing `K` elements, we will skip the next `K` elements.
279279

280280
````class Node {
281281
constructor(value, next = null) {
@@ -359,7 +359,7 @@ process.stdout.write('Nodes of reversed LinkedList are: ');
359359
result.printList();
360360
````
361361

362-
- The time complexity of our algorithm will be `O(N)`where `N` is the total number of nodes in the <b>LinkedList</b>.
362+
- The time complexity of our algorithm will be `O(N)`where `N` is the total number of nodes in the <b>LinkedList</b>.
363363
- We only used constant space, therefore, the space complexity of our algorithm is `O(1)`.
364364
## 🌟 Rotate a LinkedList (medium)
365365
https://leetcode.com/problems/rotate-list/
@@ -433,5 +433,5 @@ head.next.next.next.next.next = new Node(6)
433433
console.log(`Nodes of original LinkedList are: ${head.getList()}`)
434434
console.log(`Nodes of rotated LinkedList are: ${rotate(head, 3).getList()}`)
435435
````
436-
- The time complexity of our algorithm will be `O(N)` where `N` is the total number of nodes in the <b>LinkedList</b>.
436+
- The time complexity of our algorithm will be `O(N)` where `N` is the total number of nodes in the <b>LinkedList</b>.
437437
- We only used constant space, therefore, the space complexity of our algorithm is `O(1)`.

0 commit comments

Comments
 (0)