Skip to content

Commit 0209c09

Browse files
authored
Improved tasks 31-46
1 parent cb56b4c commit 0209c09

File tree

11 files changed

+616
-452
lines changed

11 files changed

+616
-452
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
201201

202202
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
203203
|-|-|-|-|-|-
204+
| 0033 |[Search in Rotated Sorted Array](src/main/python/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 36 | 91.71
204205

205206
#### Day 9 Binary Search Tree
206207

@@ -264,6 +265,8 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
264265

265266
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
266267
|-|-|-|-|-|-
268+
| 0039 |[Combination Sum](src/main/python/g0001_0100/s0039_combination_sum/Solution.py)| Medium | Top_100_Liked_Questions, Array, Backtracking, Big_O_Time_O(2^n)_Space_O(n+2^n) | 42 | 94.56
269+
| 0046 |[Permutations](src/main/python/g0001_0100/s0046_permutations/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Backtracking, Big_O_Time_O(n\*n!)_Space_O(n+n!) | 28 | 98.67
267270

268271
### Udemy
269272

@@ -287,19 +290,22 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
287290

288291
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
289292
|-|-|-|-|-|-
293+
| 0033 |[Search in Rotated Sorted Array](src/main/python/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 36 | 91.71
290294

291295
#### Udemy Arrays
292296

293297
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
294298
|-|-|-|-|-|-
295299
| 0283 |[Move Zeroes](src/main/python/g0201_0300/s0283_move_zeroes/Solution.py)| Easy | Top_100_Liked_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 113 | 97.70
296300
| 0001 |[Two Sum](src/main/python/g0001_0100/s0001_two_sum/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 62 | 53.52
301+
| 0041 |[First Missing Positive](src/main/python/g0001_0100/s0041_first_missing_positive/Solution.py)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 334 | 98.05
297302
| 0239 |[Sliding Window Maximum](src/main/python/g0201_0300/s0239_sliding_window_maximum/Solution.py)| Hard | Top_100_Liked_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue, Big_O_Time_O(n\*k)_Space_O(n+k) | 1093 | 66.83
298303

299304
#### Udemy Two Pointers
300305

301306
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
302307
|-|-|-|-|-|-
308+
| 0042 |[Trapping Rain Water](src/main/python/g0001_0100/s0042_trapping_rain_water/Solution.py)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Two_Pointers, Stack, Monotonic_Stack, Big_O_Time_O(n)_Space_O(1) | 93 | 87.70
303309
| 0015 |[3Sum](src/main/python/g0001_0100/s0015_3sum/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n\*log(n))_Space_O(n^2) | 683 | 63.27
304310

305311
#### Udemy Famous Algorithm
@@ -354,7 +360,9 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
354360
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
355361
|-|-|-|-|-|-
356362
| 0022 |[Generate Parentheses](src/main/python/g0001_0100/s0022_generate_parentheses/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(2^n)_Space_O(n) | 32 | 86.75
363+
| 0039 |[Combination Sum](src/main/python/g0001_0100/s0039_combination_sum/Solution.py)| Medium | Top_100_Liked_Questions, Array, Backtracking, Big_O_Time_O(2^n)_Space_O(n+2^n) | 42 | 94.56
357364
| 0017 |[Letter Combinations of a Phone Number](src/main/python/g0001_0100/s0017_letter_combinations_of_a_phone_number/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Backtracking, Big_O_Time_O(4^n)_Space_O(n) | 28 | 91.85
365+
| 0046 |[Permutations](src/main/python/g0001_0100/s0046_permutations/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Backtracking, Big_O_Time_O(n\*n!)_Space_O(n+n!) | 28 | 98.67
358366

359367
#### Udemy Bit Manipulation
360368

@@ -564,6 +572,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
564572

565573
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
566574
|-|-|-|-|-|-
575+
| 0035 |[Search Insert Position](src/main/python/g0001_0100/s0035_search_insert_position/Solution.py)| Easy | Top_100_Liked_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 41 | 94.38
567576

568577
#### Day 2 Two Pointers
569578

@@ -618,6 +627,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
618627

619628
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
620629
|-|-|-|-|-|-
630+
| 0046 |[Permutations](src/main/python/g0001_0100/s0046_permutations/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Backtracking, Big_O_Time_O(n\*n!)_Space_O(n+n!) | 28 | 98.67
621631

622632
#### Day 12 Dynamic Programming
623633

@@ -640,6 +650,8 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
640650

641651
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
642652
|-|-|-|-|-|-
653+
| 0034 |[Find First and Last Position of Element in Sorted Array](src/main/python/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 64 | 97.49
654+
| 0033 |[Search in Rotated Sorted Array](src/main/python/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 36 | 91.71
643655

644656
#### Day 2 Binary Search
645657

@@ -688,6 +700,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
688700

689701
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
690702
|-|-|-|-|-|-
703+
| 0039 |[Combination Sum](src/main/python/g0001_0100/s0039_combination_sum/Solution.py)| Medium | Top_100_Liked_Questions, Array, Backtracking, Big_O_Time_O(2^n)_Space_O(n+2^n) | 42 | 94.56
691704

692705
#### Day 11 Recursion Backtracking
693706

@@ -705,6 +718,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
705718

706719
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
707720
|-|-|-|-|-|-
721+
| 0045 |[Jump Game II](src/main/python/g0001_0100/s0045_jump_game_ii/Solution.py)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 89 | 99.30
708722

709723
#### Day 14 Dynamic Programming
710724

@@ -761,6 +775,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
761775

762776
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
763777
|-|-|-|-|-|-
778+
| 0035 |[Search Insert Position](src/main/python/g0001_0100/s0035_search_insert_position/Solution.py)| Easy | Top_100_Liked_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 41 | 94.38
764779

765780
#### Day 3
766781

@@ -776,6 +791,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
776791

777792
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
778793
|-|-|-|-|-|-
794+
| 0034 |[Find First and Last Position of Element in Sorted Array](src/main/python/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 64 | 97.49
779795

780796
#### Day 6
781797

@@ -806,6 +822,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
806822

807823
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
808824
|-|-|-|-|-|-
825+
| 0033 |[Search in Rotated Sorted Array](src/main/python/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 36 | 91.71
809826

810827
#### Day 12
811828

@@ -938,6 +955,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
938955

939956
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
940957
|-|-|-|-|-|-
958+
| 0045 |[Jump Game II](src/main/python/g0001_0100/s0045_jump_game_ii/Solution.py)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 89 | 99.30
941959

942960
#### Day 5
943961

@@ -963,6 +981,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
963981

964982
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
965983
|-|-|-|-|-|-
984+
| 0042 |[Trapping Rain Water](src/main/python/g0001_0100/s0042_trapping_rain_water/Solution.py)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Two_Pointers, Stack, Monotonic_Stack, Big_O_Time_O(n)_Space_O(1) | 93 | 87.70
966985

967986
#### Day 10
968987

@@ -1292,6 +1311,16 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
12921311
| 0283 |[Move Zeroes](src/main/python/g0201_0300/s0283_move_zeroes/Solution.py)| Easy | Top_100_Liked_Questions, Array, Two_Pointers, Algorithm_I_Day_3_Two_Pointers, Programming_Skills_I_Day_6_Array, Udemy_Arrays, Big_O_Time_O(n)_Space_O(1) | 113 | 97.70
12931312
| 0240 |[Search a 2D Matrix II](src/main/python/g0201_0300/s0240_search_a_2d_matrix_ii/Solution.py)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Divide_and_Conquer, Data_Structure_II_Day_4_Array, Binary_Search_II_Day_8, Big_O_Time_O(n+m)_Space_O(1) | 130 | 91.49
12941313
| 0239 |[Sliding Window Maximum](src/main/python/g0201_0300/s0239_sliding_window_maximum/Solution.py)| Hard | Top_100_Liked_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue, Udemy_Arrays, Big_O_Time_O(n\*k)_Space_O(n+k) | 1093 | 66.83
1314+
| 0046 |[Permutations](src/main/python/g0001_0100/s0046_permutations/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Backtracking, Algorithm_I_Day_11_Recursion_Backtracking, Level_2_Day_20_Brute_Force/Backtracking, Udemy_Backtracking/Recursion, Big_O_Time_O(n\*n!)_Space_O(n+n!) | 28 | 98.67
1315+
| 0045 |[Jump Game II](src/main/python/g0001_0100/s0045_jump_game_ii/Solution.py)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Greedy, Algorithm_II_Day_13_Dynamic_Programming, Dynamic_Programming_I_Day_4, Big_O_Time_O(n)_Space_O(1) | 89 | 99.30
1316+
| 0042 |[Trapping Rain Water](src/main/python/g0001_0100/s0042_trapping_rain_water/Solution.py)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Two_Pointers, Stack, Monotonic_Stack, Dynamic_Programming_I_Day_9, Udemy_Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 93 | 87.70
1317+
| 0041 |[First Missing Positive](src/main/python/g0001_0100/s0041_first_missing_positive/Solution.py)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Udemy_Arrays, Big_O_Time_O(n)_Space_O(n) | 334 | 98.05
1318+
| 0039 |[Combination Sum](src/main/python/g0001_0100/s0039_combination_sum/Solution.py)| Medium | Top_100_Liked_Questions, Array, Backtracking, Algorithm_II_Day_10_Recursion_Backtracking, Level_2_Day_20_Brute_Force/Backtracking, Udemy_Backtracking/Recursion, Big_O_Time_O(2^n)_Space_O(n+2^n) | 42 | 94.56
1319+
| 0035 |[Search Insert Position](src/main/python/g0001_0100/s0035_search_insert_position/Solution.py)| Easy | Top_100_Liked_Questions, Array, Binary_Search, Algorithm_I_Day_1_Binary_Search, Binary_Search_I_Day_2, Big_O_Time_O(log_n)_Space_O(1) | 41 | 94.38
1320+
| 0034 |[Find First and Last Position of Element in Sorted Array](src/main/python/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Algorithm_II_Day_1_Binary_Search, Binary_Search_I_Day_5, Big_O_Time_O(log_n)_Space_O(1) | 64 | 97.49
1321+
| 0033 |[Search in Rotated Sorted Array](src/main/python/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Algorithm_II_Day_1_Binary_Search, Binary_Search_I_Day_11, Level_2_Day_8_Binary_Search, Udemy_Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 36 | 91.71
1322+
| 0032 |[Longest Valid Parentheses](src/main/python/g0001_0100/s0032_longest_valid_parentheses/Solution.py)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming, Stack, Big_O_Time_O(n)_Space_O(1) | 36 | 89.25
1323+
| 0031 |[Next Permutation](src/main/python/g0001_0100/s0031_next_permutation/Solution.py)| Medium | Top_100_Liked_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 35 | 93.06
12951324
| 0025 |[Reverse Nodes in k-Group](src/main/python/g0001_0100/s0025_reverse_nodes_in_k_group/Solution.py)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Data_Structure_II_Day_13_Linked_List, Udemy_Linked_List, Big_O_Time_O(n)_Space_O(k) | 36 | 90.51
12961325
| 0024 |[Swap Nodes in Pairs](src/main/python/g0001_0100/s0024_swap_nodes_in_pairs/Solution.py)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Data_Structure_II_Day_12_Linked_List, Udemy_Linked_List, Big_O_Time_O(n)_Space_O(1) | 27 | 93.88
12971326
| 0023 |[Merge k Sorted Lists](src/main/python/g0001_0100/s0023_merge_k_sorted_lists/Solution.py)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Heap_Priority_Queue, Linked_List, Divide_and_Conquer, Merge_Sort, Big_O_Time_O(k\*n\*log(k))_Space_O(log(k)) | 61 | 98.91

src/main/python/g0001_0100/s0031_next_permutation/readme.md

Lines changed: 61 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -37,63 +37,66 @@ The replacement must be **[in place](http://en.wikipedia.org/wiki/In-place_algor
3737
* `1 <= nums.length <= 100`
3838
* `0 <= nums[i] <= 100`
3939

40-
To solve the "Next Permutation" problem in Java with a `Solution` class, we can follow these steps:
41-
42-
1. Define a `Solution` class.
43-
2. Define a method named `nextPermutation` that takes an integer array `nums` as input and modifies it to find the next permutation in lexicographic order.
44-
3. Find the first index `i` from the right such that `nums[i] > nums[i - 1]`. If no such index exists, reverse the entire array, as it's already the last permutation.
45-
4. Find the smallest index `j` from the right such that `nums[j] > nums[i - 1]`.
46-
5. Swap `nums[i - 1]` with `nums[j]`.
47-
6. Reverse the suffix of the array starting from index `i`.
48-
49-
Here's the implementation:
50-
51-
```java
52-
public class Solution {
53-
public void nextPermutation(int[] nums) {
54-
int n = nums.length;
55-
56-
// Step 1: Find the first index i from the right such that nums[i] > nums[i - 1]
57-
int i = n - 1;
58-
while (i > 0 && nums[i] <= nums[i - 1]) {
59-
i--;
60-
}
61-
62-
// Step 2: If no such index exists, reverse the entire array
63-
if (i == 0) {
64-
reverse(nums, 0, n - 1);
65-
return;
66-
}
67-
68-
// Step 3: Find the smallest index j from the right such that nums[j] > nums[i - 1]
69-
int j = n - 1;
70-
while (nums[j] <= nums[i - 1]) {
71-
j--;
72-
}
73-
74-
// Step 4: Swap nums[i - 1] with nums[j]
75-
swap(nums, i - 1, j);
76-
77-
// Step 5: Reverse the suffix of the array starting from index i
78-
reverse(nums, i, n - 1);
79-
}
80-
81-
// Helper method to reverse a portion of the array
82-
private void reverse(int[] nums, int start, int end) {
83-
while (start < end) {
84-
swap(nums, start, end);
85-
start++;
86-
end--;
87-
}
88-
}
89-
90-
// Helper method to swap two elements in the array
91-
private void swap(int[] nums, int i, int j) {
92-
int temp = nums[i];
93-
nums[i] = nums[j];
94-
nums[j] = temp;
95-
}
96-
}
40+
To solve the "Next Permutation" problem, you can use the following steps:
41+
42+
### Approach:
43+
44+
1. **Find the First Decreasing Element:**
45+
- Starting from the right, find the first pair of adjacent elements `nums[i]` and `nums[i+1]` where `nums[i] < nums[i+1]`. This element `nums[i]` is the first decreasing element.
46+
47+
2. **Find the Smallest Element to the Right of `nums[i]`:**
48+
- From the end, find the smallest element to the right of `nums[i]` that is greater than `nums[i]`. Let's call this element `nums[j]`.
49+
50+
3. **Swap `nums[i]` and `nums[j]`:**
51+
- Swap the elements `nums[i]` and `nums[j]`.
52+
53+
4. **Reverse the Subarray to the Right of `nums[i]`:**
54+
- Reverse the subarray to the right of `nums[i]` to ensure the smallest lexicographically next permutation.
55+
56+
### Python Code:
57+
58+
```python
59+
class Solution:
60+
def nextPermutation(self, nums):
61+
# Step 1: Find the first decreasing element
62+
i = len(nums) - 2
63+
while i >= 0 and nums[i] >= nums[i+1]:
64+
i -= 1
65+
66+
# Step 2: Find the smallest element to the right of nums[i] that is greater than nums[i]
67+
if i >= 0:
68+
j = len(nums) - 1
69+
while nums[j] <= nums[i]:
70+
j -= 1
71+
72+
# Step 3: Swap nums[i] and nums[j]
73+
nums[i], nums[j] = nums[j], nums[i]
74+
75+
# Step 4: Reverse the subarray to the right of nums[i]
76+
left, right = i + 1, len(nums) - 1
77+
while left < right:
78+
nums[left], nums[right] = nums[right], nums[left]
79+
left += 1
80+
right -= 1
81+
82+
# Example Usage:
83+
solution = Solution()
84+
85+
# Example 1:
86+
nums1 = [1, 2, 3]
87+
solution.nextPermutation(nums1) # Output: [1, 3, 2]
88+
89+
# Example 2:
90+
nums2 = [3, 2, 1]
91+
solution.nextPermutation(nums2) # Output: [1, 2, 3]
92+
93+
# Example 3:
94+
nums3 = [1, 1, 5]
95+
solution.nextPermutation(nums3) # Output: [1, 5, 1]
96+
97+
# Example 4:
98+
nums4 = [1]
99+
solution.nextPermutation(nums4) # Output: [1]
97100
```
98101

99-
This implementation provides a solution to the "Next Permutation" problem in Java. It finds the next lexicographically greater permutation of the given array `nums` and modifies it in place.
102+
This code defines a `Solution` class with a method `nextPermutation` that takes a list of integers `nums` and modifies it to the next lexicographically greater permutation. The example usage demonstrates how to create an instance of the `Solution` class and call the `nextPermutation` method with different inputs.

0 commit comments

Comments
 (0)