Skip to content

Commit 88c5c6d

Browse files
authored
Added tasks 239-394
1 parent 4e1ca1d commit 88c5c6d

File tree

21 files changed

+635
-0
lines changed

21 files changed

+635
-0
lines changed

README.md

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

151151
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
152152
|-|-|-|-|-|-
153+
| 0394 |[Decode String](src/main/python/g0301_0400/s0394_decode_string/Solution.py)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Big_O_Time_O(n)_Space_O(n) | 28 | 91.14
153154

154155
#### Day 15 Heap
155156

@@ -220,6 +221,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
220221

221222
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
222223
|-|-|-|-|-|-
224+
| 0322 |[Coin Change](src/main/python/g0301_0400/s0322_coin_change/Solution.py)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Breadth_First_Search, Big_O_Time_O(m\*n)_Space_O(amount) | 488 | 96.38
223225

224226
#### Day 13 Dynamic Programming
225227

@@ -279,6 +281,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
279281
| 0003 |[Longest Substring Without Repeating Characters](src/main/python/g0001_0100/s0003_longest_substring_without_repeating_characters/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n)_Space_O(1) | 45 | 93.32
280282
| 0020 |[Valid Parentheses](src/main/python/g0001_0100/s0020_valid_parentheses/Solution.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Stack, Big_O_Time_O(n)_Space_O(n) | 24 | 98.16
281283
| 0005 |[Longest Palindromic Substring](src/main/python/g0001_0100/s0005_longest_palindromic_substring/Solution.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 78 | 97.43
284+
| 0394 |[Decode String](src/main/python/g0301_0400/s0394_decode_string/Solution.py)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Big_O_Time_O(n)_Space_O(n) | 28 | 91.14
282285

283286
#### Udemy Binary Search
284287

@@ -289,7 +292,9 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
289292

290293
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
291294
|-|-|-|-|-|-
295+
| 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
292296
| 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
297+
| 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
293298

294299
#### Udemy Two Pointers
295300

@@ -340,6 +345,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
340345

341346
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
342347
|-|-|-|-|-|-
348+
| 0300 |[Longest Increasing Subsequence](src/main/python/g0201_0300/s0300_longest_increasing_subsequence/Solution.py)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Binary_Search, Big_O_Time_O(n\*log_n)_Space_O(n) | 58 | 96.98
343349
| 1143 |[Longest Common Subsequence](src/main/python/g1101_1200/s1143_longest_common_subsequence/Solution.py)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n\*m)_Space_O(n\*m) | 452 | 81.00
344350
| 0010 |[Regular Expression Matching](src/main/python/g0001_0100/s0010_regular_expression_matching/Solution.py)| Hard | Top_Interview_Questions, String, Dynamic_Programming, Recursion, Big_O_Time_O(m\*n)_Space_O(m\*n) | 23 | 99.97
345351

@@ -354,6 +360,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
354360

355361
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
356362
|-|-|-|-|-|-
363+
| 0338 |[Counting Bits](src/main/python/g0301_0400/s0338_counting_bits/Solution.py)| Easy | Dynamic_Programming, Bit_Manipulation, Big_O_Time_O(num)_Space_O(num) | 55 | 88.80
357364

358365
#### Udemy Design
359366

@@ -457,6 +464,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
457464

458465
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
459466
|-|-|-|-|-|-
467+
| 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, Big_O_Time_O(n+m)_Space_O(1) | 130 | 91.49
460468

461469
#### Day 5 Array
462470

@@ -543,6 +551,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
543551

544552
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
545553
|-|-|-|-|-|-
554+
| 0347 |[Top K Frequent Elements](src/main/python/g0301_0400/s0347_top_k_frequent_elements/Solution.py)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Divide_and_Conquer, Quickselect, Bucket_Sort, Big_O_Time_O(n\*log(n))_Space_O(k) | 86 | 71.64
546555

547556
#### Day 21 Heap Priority Queue
548557

@@ -565,6 +574,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
565574

566575
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
567576
|-|-|-|-|-|-
577+
| 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
568578

569579
#### Day 4 Two Pointers
570580

@@ -711,6 +721,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
711721

712722
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
713723
|-|-|-|-|-|-
724+
| 0300 |[Longest Increasing Subsequence](src/main/python/g0201_0300/s0300_longest_increasing_subsequence/Solution.py)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Binary_Search, Big_O_Time_O(n\*log_n)_Space_O(n) | 58 | 96.98
714725

715726
#### Day 17 Dynamic Programming
716727

@@ -722,6 +733,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
722733

723734
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
724735
|-|-|-|-|-|-
736+
| 0322 |[Coin Change](src/main/python/g0301_0400/s0322_coin_change/Solution.py)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Breadth_First_Search, Big_O_Time_O(m\*n)_Space_O(amount) | 488 | 96.38
725737

726738
#### Day 19 Bit Manipulation
727739

@@ -816,6 +828,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
816828

817829
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
818830
|-|-|-|-|-|-
831+
| 0300 |[Longest Increasing Subsequence](src/main/python/g0201_0300/s0300_longest_increasing_subsequence/Solution.py)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Binary_Search, Big_O_Time_O(n\*log_n)_Space_O(n) | 58 | 96.98
819832

820833
#### Day 4
821834

@@ -826,6 +839,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
826839

827840
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
828841
|-|-|-|-|-|-
842+
| 0287 |[Find the Duplicate Number](src/main/python/g0201_0300/s0287_find_the_duplicate_number/Solution.py)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Two_Pointers, Bit_Manipulation, Big_O_Time_O(n)_Space_O(n) | 453 | 81.73
829843

830844
#### Day 6
831845

@@ -841,6 +855,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
841855

842856
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
843857
|-|-|-|-|-|-
858+
| 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, Big_O_Time_O(n+m)_Space_O(1) | 130 | 91.49
844859

845860
#### Day 9
846861

@@ -994,6 +1009,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
9941009

9951010
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
9961011
|-|-|-|-|-|-
1012+
| 0300 |[Longest Increasing Subsequence](src/main/python/g0201_0300/s0300_longest_increasing_subsequence/Solution.py)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Binary_Search, Big_O_Time_O(n\*log_n)_Space_O(n) | 58 | 96.98
9971013

9981014
#### Day 19
9991015

@@ -1005,6 +1021,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
10051021

10061022
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10071023
|-|-|-|-|-|-
1024+
| 0322 |[Coin Change](src/main/python/g0301_0400/s0322_coin_change/Solution.py)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Breadth_First_Search, Big_O_Time_O(m\*n)_Space_O(amount) | 488 | 96.38
10081025

10091026
#### Day 21
10101027

@@ -1042,6 +1059,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
10421059

10431060
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10441061
|-|-|-|-|-|-
1062+
| 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
10451063

10461064
#### Day 7 Array
10471065

@@ -1264,6 +1282,16 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
12641282
| 0438 |[Find All Anagrams in a String](src/main/python/g0401_0500/s0438_find_all_anagrams_in_a_string/Solution.py)| Medium | Top_100_Liked_Questions, String, Hash_Table, Sliding_Window, Algorithm_II_Day_5_Sliding_Window, Programming_Skills_II_Day_12, Level_1_Day_12_Sliding_Window/Two_Pointer, Big_O_Time_O(n+m)_Space_O(1) | 100 | 50.22
12651283
| 0437 |[Path Sum III](src/main/python/g0401_0500/s0437_path_sum_iii/Solution.py)| Medium | Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree, Big_O_Time_O(n)_Space_O(n) | 264 | 30.25
12661284
| 0416 |[Partition Equal Subset Sum](src/main/python/g0401_0500/s0416_partition_equal_subset_sum/Solution.py)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Level_2_Day_13_Dynamic_Programming, Big_O_Time_O(n\*sums)_Space_O(n\*sums) | 550 | 64.93
1285+
| 0394 |[Decode String](src/main/python/g0301_0400/s0394_decode_string/Solution.py)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Level_1_Day_14_Stack, Udemy_Strings, Big_O_Time_O(n)_Space_O(n) | 28 | 91.14
1286+
| 0347 |[Top K Frequent Elements](src/main/python/g0301_0400/s0347_top_k_frequent_elements/Solution.py)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Sorting, Heap_Priority_Queue, Counting, Divide_and_Conquer, Quickselect, Bucket_Sort, Data_Structure_II_Day_20_Heap_Priority_Queue, Big_O_Time_O(n\*log(n))_Space_O(k) | 86 | 71.64
1287+
| 0338 |[Counting Bits](src/main/python/g0301_0400/s0338_counting_bits/Solution.py)| Easy | Dynamic_Programming, Bit_Manipulation, Udemy_Bit_Manipulation, Big_O_Time_O(num)_Space_O(num) | 55 | 88.80
1288+
| 0322 |[Coin Change](src/main/python/g0301_0400/s0322_coin_change/Solution.py)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Breadth_First_Search, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_20, Level_2_Day_12_Dynamic_Programming, Big_O_Time_O(m\*n)_Space_O(amount) | 488 | 96.38
1289+
| 0300 |[Longest Increasing Subsequence](src/main/python/g0201_0300/s0300_longest_increasing_subsequence/Solution.py)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Binary_Search, Algorithm_II_Day_16_Dynamic_Programming, Binary_Search_II_Day_3, Dynamic_Programming_I_Day_18, Udemy_Dynamic_Programming, Big_O_Time_O(n\*log_n)_Space_O(n) | 58 | 96.98
1290+
| 0295 |[Find Median from Data Stream](src/main/python/g0201_0300/s0295_find_median_from_data_stream/MedianFinder.py)| Hard | Top_100_Liked_Questions, Sorting, Two_Pointers, Design, Heap_Priority_Queue, Data_Stream, Big_O_Time_O(n\*log_n)_Space_O(n) | 351 | 89.30
1291+
| 0287 |[Find the Duplicate Number](src/main/python/g0201_0300/s0287_find_the_duplicate_number/Solution.py)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Two_Pointers, Bit_Manipulation, Binary_Search_II_Day_5, Big_O_Time_O(n)_Space_O(n) | 453 | 81.73
1292+
| 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
1293+
| 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
1294+
| 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
12671295
| 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
12681296
| 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
12691297
| 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
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# #Hard #Top_100_Liked_Questions #Array #Heap_Priority_Queue #Sliding_Window #Queue
2+
# #Monotonic_Queue #Udemy_Arrays #Big_O_Time_O(n*k)_Space_O(n+k)
3+
# #2024_06_08_Time_1093_ms_(66.83%)_Space_32_MB_(94.89%)
4+
5+
from collections import deque
6+
7+
class Solution:
8+
def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
9+
n = len(nums)
10+
res = [0] * (n - k + 1)
11+
dq = deque()
12+
i, j = 0, 0
13+
x = 0
14+
15+
while j < len(nums):
16+
while dq and dq[-1] < nums[j]:
17+
dq.pop()
18+
dq.append(nums[j])
19+
20+
if j - i + 1 == k:
21+
res[x] = dq[0]
22+
x += 1
23+
if dq[0] == nums[i]:
24+
dq.popleft()
25+
i += 1
26+
j += 1
27+
28+
return res
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
239\. Sliding Window Maximum
2+
3+
Hard
4+
5+
You are given an array of integers `nums`, there is a sliding window of size `k` which is moving from the very left of the array to the very right. You can only see the `k` numbers in the window. Each time the sliding window moves right by one position.
6+
7+
Return _the max sliding window_.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,3,-1,-3,5,3,6,7], k = 3
12+
13+
**Output:** [3,3,5,5,6,7]
14+
15+
**Explanation:**
16+
17+
Window position Max
18+
--------------- -----
19+
[1 3 -1] -3 5 3 6 7 3
20+
1 [3 -1 -3] 5 3 6 7 3
21+
1 3 [-1 -3 5] 3 6 7 5
22+
1 3 -1 [-3 5 3] 6 7 5
23+
1 3 -1 -3 [5 3 6] 7 6
24+
1 3 -1 -3 5 [3 6 7] 7
25+
26+
**Example 2:**
27+
28+
**Input:** nums = [1], k = 1
29+
30+
**Output:** [1]
31+
32+
**Example 3:**
33+
34+
**Input:** nums = [1,-1], k = 1
35+
36+
**Output:** [1,-1]
37+
38+
**Example 4:**
39+
40+
**Input:** nums = [9,11], k = 2
41+
42+
**Output:** [11]
43+
44+
**Example 5:**
45+
46+
**Input:** nums = [4,-2], k = 2
47+
48+
**Output:** [4]
49+
50+
**Constraints:**
51+
52+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
53+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
54+
* `1 <= k <= nums.length`
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# #Medium #Top_100_Liked_Questions #Array #Binary_Search #Matrix #Divide_and_Conquer
2+
# #Data_Structure_II_Day_4_Array #Binary_Search_II_Day_8 #Big_O_Time_O(n+m)_Space_O(1)
3+
# #2024_06_08_Time_130_ms_(91.49%)_Space_22.8_MB_(65.80%)
4+
5+
class Solution:
6+
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
7+
if not matrix or not matrix[0]:
8+
return False
9+
10+
r, c = 0, len(matrix[0]) - 1
11+
12+
while r < len(matrix) and c >= 0:
13+
if matrix[r][c] == target:
14+
return True
15+
elif matrix[r][c] > target:
16+
c -= 1
17+
else:
18+
r += 1
19+
20+
return False
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
240\. Search a 2D Matrix II
2+
3+
Medium
4+
5+
Write an efficient algorithm that searches for a `target` value in an `m x n` integer `matrix`. The `matrix` has the following properties:
6+
7+
* Integers in each row are sorted in ascending from left to right.
8+
* Integers in each column are sorted in ascending from top to bottom.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/11/24/searchgrid2.jpg)
13+
14+
**Input:** matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
15+
16+
**Output:** true
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2020/11/24/searchgrid.jpg)
21+
22+
**Input:** matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
23+
24+
**Output:** false
25+
26+
**Constraints:**
27+
28+
* `m == matrix.length`
29+
* `n == matrix[i].length`
30+
* `1 <= n, m <= 300`
31+
* <code>-10<sup>9</sup> <= matrix[i][j] <= 10<sup>9</sup></code>
32+
* All the integers in each row are **sorted** in ascending order.
33+
* All the integers in each column are **sorted** in ascending order.
34+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# #Easy #Top_100_Liked_Questions #Array #Two_Pointers #Algorithm_I_Day_3_Two_Pointers
2+
# #Programming_Skills_I_Day_6_Array #Udemy_Arrays #Big_O_Time_O(n)_Space_O(1)
3+
# #2024_06_08_Time_113_ms_(97.70%)_Space_17.8_MB_(99.81%)
4+
5+
class Solution:
6+
def moveZeroes(self, nums: List[int]) -> None:
7+
"""
8+
Do not return anything, modify nums in-place instead.
9+
"""
10+
firstZero = 0
11+
for i in range(len(nums)):
12+
if nums[i] != 0:
13+
self.swap(firstZero, i, nums)
14+
firstZero += 1
15+
16+
def swap(self, index1: int, index2: int, nums: List[int]) -> None:
17+
nums[index1], nums[index2] = nums[index2], nums[index1]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
283\. Move Zeroes
2+
3+
Easy
4+
5+
Given an integer array `nums`, move all `0`'s to the end of it while maintaining the relative order of the non-zero elements.
6+
7+
**Note** that you must do this in-place without making a copy of the array.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [0,1,0,3,12]
12+
13+
**Output:** [1,3,12,0,0]
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [0]
18+
19+
**Output:** [0]
20+
21+
**Constraints:**
22+
23+
* <code>1 <= nums.length <= 10<sup>4</sup></code>
24+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
25+
26+
**Follow up:** Could you minimize the total number of operations done?
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# #Medium #Top_100_Liked_Questions #Array #Binary_Search #Two_Pointers #Bit_Manipulation
2+
# #Binary_Search_II_Day_5 #Big_O_Time_O(n)_Space_O(n)
3+
# #2024_06_08_Time_453_ms_(81.73%)_Space_31.1_MB_(24.78%)
4+
5+
class Solution:
6+
def findDuplicate(self, nums: List[int]) -> int:
7+
arr = [0] * (len(nums) + 1)
8+
for num in nums:
9+
arr[num] += 1
10+
if arr[num] == 2:
11+
return num
12+
return 0

0 commit comments

Comments
 (0)