Skip to content

Commit e90a24e

Browse files
committed
update
1 parent fb83da9 commit e90a24e

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

✅ Pattern 04 : Merge Intervals.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ minMeetingRooms([[4,5], [2,3], [2,4], [3,5]])//2, We will need one room for [2,3
519519
520520
> Given a list of intervals representing the arrival and departure times of trains to a train station, our goal is to find the minimum number of platforms required for the train station so that no train has to wait.
521521
522-
Both of these problems can be solved using the approach discussed above.
522+
Both of these problems can be solved using the `approach` discussed above.
523523

524524

525525
## 🌟 Maximum CPU Load (hard)

✅ Pattern 05: Cyclic Sort.md

+22-20
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# Pattern 5: Cyclic Sort
22

3-
This pattern describes an interesting approach to deal with problems involving arrays containing numbers in a given range. For example, take the following problem:
3+
This pattern describes an interesting approach to deal with problems involving arrays containing numbers in a given range.
44

5-
>You are given an unsorted array containing numbers taken from the range 1 to ‘n’. The array can have duplicates, which means that some numbers will be missing. Find all the missing numbers.
5+
### For example, take the following problem:
6+
7+
>You are given an unsorted array containing numbers taken from the range `1` to `n` The array can have duplicates, which means that some numbers will be missing. Find all the missing numbers.
68
79
To efficiently solve this problem, we can use the fact that the input array contains numbers in the range of `1` to `n`.
810
For example, to efficiently sort the array, we can try placing each number in its correct place, i.e., placing `1` at index `0`, placing `2` at index `1`, and so on. Once we are done with the sorting, we can iterate the array to find all indices that are missing the correct numbers. These will be our required numbers.
@@ -46,20 +48,20 @@ cyclicSort ([2, 6, 4, 3, 1, 5])
4648
cyclicSort ([1, 5, 6, 4, 3, 2])
4749
````
4850
- The time complexity of the above algorithm is `O(n)`. Although we are not incrementing the index `i` when swapping the numbers, this will result in more than `n` iterations of the loop, but in the worst-case scenario, the while loop will swap a total of `n-1` numbers and once a number is at its correct index, we will move on to the next number by incrementing `i`. So overall, our algorithm will take `O(n) + O(n-1)` which is asymptotically equivalent to `O(n)`.
49-
-The algorithm runs in constant space `O(1)`.
51+
- The algorithm runs in constant space `O(1)`.
5052

5153
## Find the Missing Number (easy)
5254
https://leetcode.com/problems/missing-number/
5355

5456
> We are given an array containing `n` distinct numbers taken from the range `0` to `n`. Since the array has only `n` numbers out of the total `n+1` numbers, find the missing number.
5557
56-
This problem follows the <b>Cyclic Sort</b> pattern. Since the input array contains unique numbers from the range `0` to `n`, we can use a similar strategy as discussed in <b>Cyclic Sort</b> to place the numbers on their correct index. Once we have every number in its correct place, we can iterate the array to find the index which does not have the correct number, and that index will be our missing number.
58+
This problem follows the <b>[Cyclic Sort pattern](#pattern-5-cyclic-sort)</b>. Since the input array contains unique numbers from the range `0` to `n`, we can use a similar strategy as discussed in <b>[ <b>[Cyclic Sort pattern](#pattern-5-cyclic-sort)</b> pattern](#pattern-5-cyclic-sort)</b> to place the numbers on their correct index. Once we have every number in its correct place, we can iterate the array to find the index which does not have the correct number, and that index will be our missing number.
5759

58-
However, there are two differences with Cyclic Sort:
59-
1. In this problem, the numbers are ranged from `0` to `n`, compared to `1` to `n` in the Cyclic Sort. This will make two changes in our algorithm:
60-
- In this problem, each number should be equal to its index, compared to `index - 1` in the <b>Cyclic Sort</b>. Therefore => `nums[i] == nums[nums[i]]`
60+
However, there are two differences with <b>[Cyclic Sort pattern](#pattern-5-cyclic-sort)</b>:
61+
1. In this problem, the numbers are ranged from `0` to `n`, compared to `1` to `n` in the <b>[Cyclic Sort pattern](#pattern-5-cyclic-sort)</b>. This will make two changes in our algorithm:
62+
- In this problem, each number should be equal to its index, compared to `index - 1` in the <b>[Cyclic Sort pattern](#pattern-5-cyclic-sort)</b>. Therefore => `nums[i] == nums[nums[i]]`
6163
- Since the array will have `n` numbers, which means array indices will range from `0` to `n-1`. Therefore, we will ignore the number `n` as we can’t place it in the array, so => `nums[i] < nums.length`
62-
2. Say we are at index `i`. If we swap the number at index `i` to place it at the correct index, we can still have the wrong number at index `i`. This was true in <b>Cyclic Sort</b> too. It didn’t cause any problems in <b>Cyclic Sort</b> as over there, we made sure to place one number at its correct place in each step, but that wouldn’t be enough in this problem as we have one extra number due to the larger range. Therefore, we will not move to the next number after the swap until we have a correct number at the index `i`.
64+
2. Say we are at index `i`. If we swap the number at index `i` to place it at the correct index, we can still have the wrong number at index `i`. This was true in <b>[Cyclic Sort pattern](#pattern-5-cyclic-sort)</b> too. It didn’t cause any problems in <b>[Cyclic Sort pattern](#pattern-5-cyclic-sort)</b> as over there, we made sure to place one number at its correct place in each step, but that wouldn’t be enough in this problem as we have one extra number due to the larger range. Therefore, we will not move to the next number after the swap until we have a correct number at the index `i`.
6365

6466
````js
6567
function findMissingNumber(nums) {
@@ -96,11 +98,11 @@ findMissingNumber([8, 3, 5, 2, 4, 6, 0, 1])//7
9698
## Find all Missing Numbers (easy)
9799
https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/
98100

99-
> We are given an unsorted array containing numbers taken from the range `1 to n`. The array can have duplicates, which means some numbers will be missing. Find all those missing numbers.
101+
> We are given an unsorted array containing numbers taken from the range `1` to `n`. The array can have duplicates, which means some numbers will be missing. Find all those missing numbers.
100102
101-
This problem follows the <b>Cyclic Sort</b> pattern and shares similarities with <b>Find the Missing Number</b> with one difference. In this problem, there can be many duplicates whereas in <b>Find the Missing Number</b> there were no duplicates and the range was greater than the length of the array.
103+
This problem follows the <b>[Cyclic Sort pattern](#pattern-5-cyclic-sort)</b> and shares similarities with <b>Find the Missing Number</b> with one difference. In this problem, there can be many duplicates whereas in <b>Find the Missing Number</b> there were no duplicates and the range was greater than the length of the array.
102104

103-
However, we will follow a similar approach though as discussed in <b>Find the Missing Number</b> to place the numbers on their correct indices. Once we are done with the cyclic sort we will iterate the array to find all indices that are missing the correct numbers.
105+
However, we will follow a similar approach though as discussed in <b>Find the Missing Number</b> to place the numbers on their correct indices. Once we are done with the <b>Cyclic Sort</b> we will iterate the array to find all indices that are missing the correct numbers.
104106

105107
````js
106108
function findMissingNumbers(nums) {
@@ -138,9 +140,9 @@ findMissingNumbers([2, 3, 2, 1])//4
138140
## Find the Duplicate Number (easy)
139141
https://leetcode.com/problems/find-the-duplicate-number/
140142

141-
> We are given an unsorted array containing n+1’ numbers taken from the range 1 to ‘n’. The array has only one duplicate but it can be repeated multiple times. <b>Find that duplicate number without using any extra space</b>. You are, however, allowed to modify the input array.
143+
> We are given an unsorted array containing `n+1’ numbers taken from the range `1` to `n` The array has only one duplicate but it can be repeated multiple times. <b>Find that duplicate number without using any extra space</b>. You are, however, allowed to modify the input array.
142144
143-
This problem follows the <b>Cyclic Sort</b> pattern and shares similarities with <b>Find the Missing Number</b>. Following a similar approach, we will try to place each number on its correct index. Since there is only one duplicate, if while swapping the number with its index both the numbers being swapped are same, we have found our duplicate!
145+
This problem follows the <b>[Cyclic Sort pattern](#pattern-5-cyclic-sort)</b> and shares similarities with <b>Find the Missing Number</b>. Following a similar approach, we will try to place each number on its correct index. Since there is only one duplicate, if while swapping the number with its index both the numbers being swapped are same, we have found our duplicate!
144146

145147
````js
146148
function findDuplicate(nums) {
@@ -172,7 +174,7 @@ findDuplicate([2, 4, 1, 4, 4])//4
172174

173175
> Can we solve the above problem in `O(1)` space and without modifying the input array?
174176
175-
While doing the cyclic sort, we realized that the array will have a cycle due to the duplicate number and that the start of the cycle will always point to the duplicate number. This means that we can use the <b>fast & the slow</b> pointer method to find the duplicate number or the start of the cycle similar to Start of <b>LinkedList</b> Cycle.
177+
While doing the <b>Cyclic Sort</b>, we realized that the array will have a cycle due to the duplicate number and that the start of the cycle will always point to the duplicate number. This means that we can use the <b>fast & the slow</b> pointer method to find the duplicate number or the start of the cycle similar to Start of <b>LinkedList</b> Cycle.
176178
````js
177179
function findDuplicate(nums) {
178180
//using fast & slow pointer method
@@ -222,7 +224,7 @@ https://leetcode.com/problems/find-all-duplicates-in-an-array/
222224

223225
> We are given an unsorted array containing `n` numbers taken from the range `1` to `n`. The array has some numbers appearing twice, <b>find all these duplicate numbers without using any extra space</b>.
224226
225-
This problem follows the <b>Cyclic Sort</b> pattern and shares similarities with <b>Find the Duplicate Number</b>. Following a similar approach, we will place each number at its correct index. After that, we will iterate through the array to find all numbers that are not at the correct indices. All these numbers are duplicates.
227+
This problem follows the <b>[Cyclic Sort pattern](#pattern-5-cyclic-sort)</b> and shares similarities with <b>Find the Duplicate Number</b>. Following a similar approach, we will place each number at its correct index. After that, we will iterate through the array to find all numbers that are not at the correct indices. All these numbers are duplicates.
226228

227229
````js
228230
function findAllDuplicates(nums) {
@@ -296,9 +298,9 @@ https://leetcode.com/problems/first-missing-positive/
296298

297299
> Given an unsorted array containing numbers, find the <b>smallest missing positive number</b> in it.
298300
299-
This problem follows the <b>Cyclic Sort</b> pattern and shares similarities with <b>Find the Missing Number</b> with one big difference. In this problem, the numbers are not bound by any range so we can have any number in the input array.
301+
This problem follows the <b>[Cyclic Sort pattern](#pattern-5-cyclic-sort)</b> and shares similarities with <b>Find the Missing Number</b> with one big difference. In this problem, the numbers are not bound by any range so we can have any number in the input array.
300302

301-
However, we will follow a similar approach though as discussed in <b>Find the Missing Number</b> to place the numbers on their correct indices and ignore all numbers that are out of the range of the array (i.e., all negative numbers and all numbers greater than or equal to the length of the array). Once we are done with the cyclic sort we will iterate the array and the first index that does not have the correct number will be the smallest missing positive number!
303+
However, we will follow a similar approach though as discussed in <b>Find the Missing Number</b> to place the numbers on their correct indices and ignore all numbers that are out of the range of the array (i.e., all negative numbers and all numbers greater than or equal to the length of the array). Once we are done with the <b>Cyclic Sort</b> we will iterate the array and the first index that does not have the correct number will be the smallest missing positive number!
302304

303305
````js
304306
function findFirstSmallestMissingPositive(nums) {
@@ -336,15 +338,15 @@ findFirstSmallestMissingPositive([3, 2, 5, 1])//4
336338
https://leetcode.com/problems/kth-missing-positive-number/
337339
> Given an unsorted array containing numbers and a number `K`, find the first `K` missing positive numbers in the array.
338340
339-
This problem follows the <b>Cyclic Sort</b> pattern and shares similarities with <b>Find the Smallest Missing Positive Number</b>. The only difference is that, in this problem, we need to find the first `K` missing numbers compared to only the first missing number.
341+
This problem follows the <b>[Cyclic Sort pattern](#pattern-5-cyclic-sort)</b> and shares similarities with <b>Find the Smallest Missing Positive Number</b>. The only difference is that, in this problem, we need to find the first `K` missing numbers compared to only the first missing number.
340342

341-
We will follow a similar approach as discussed in <b>Find the Smallest Missing Positive Number</b> to place the numbers on their correct indices and ignore all numbers that are out of the range of the array. Once we are done with the cyclic sort we will iterate through the array to find indices that do not have the correct numbers.
343+
We will follow a similar approach as discussed in <b>Find the Smallest Missing Positive Number</b> to place the numbers on their correct indices and ignore all numbers that are out of the range of the array. Once we are done with the <b>Cyclic Sort</b> we will iterate through the array to find indices that do not have the correct numbers.
342344

343345
If we are not able to find `K` missing numbers from the array, we need to add additional numbers to the output array. To find these additional numbers we will use the length of the array. For example, if the length of the array is `4`, the next missing numbers will be `4, 5, 6` and so on. One tricky aspect is that any of these additional numbers could be part of the array. Remember, while sorting, we ignored all numbers that are greater than or equal to the length of the array. So all indices that have the missing numbers could possibly have these additional numbers. To handle this, we must keep track of all numbers from those indices that have missing numbers. Let’s understand this with an example:
344346
````
345347
nums: [2, 1, 3, 6, 5], k =2
346348
````
347-
After the cyclic sort our array will look like:
349+
After the <b>Cyclic Sort</b> our array will look like:
348350
````
349351
nums: [1, 2, 3, 6, 5]
350352
````

0 commit comments

Comments
 (0)