Skip to content

Commit 6138ba8

Browse files
authored
Added tasks 169-206
1 parent 5b3eeeb commit 6138ba8

File tree

16 files changed

+474
-0
lines changed

16 files changed

+474
-0
lines changed

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
3636

3737
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
3838
|-|-|-|-|-|-
39+
| 0189 |[Rotate Array](src/main/js/g0101_0200/s0189_rotate_array/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
3940

4041
#### Day 3 Two Pointers
4142

@@ -79,6 +80,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
7980
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
8081
|-|-|-|-|-|-
8182
| 0021 |[Merge Two Sorted Lists](src/main/js/g0001_0100/s0021_merge_two_sorted_lists/solution.js)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 0 | 100.00
83+
| 0206 |[Reverse Linked List](src/main/js/g0201_0300/s0206_reverse_linked_list/solution.js)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
8284

8385
#### Day 11 Recursion Backtracking
8486

@@ -91,6 +93,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
9193
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
9294
|-|-|-|-|-|-
9395
| 0070 |[Climbing Stairs](src/main/js/g0001_0100/s0070_climbing_stairs/solution.js)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
96+
| 0198 |[House Robber](src/main/js/g0101_0200/s0198_house_robber/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
9497

9598
#### Day 13 Bit Manipulation
9699

@@ -140,6 +143,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
140143

141144
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
142145
|-|-|-|-|-|-
146+
| 0200 |[Number of Islands](src/main/js/g0101_0200/s0200_number_of_islands/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 62 | 90.80
143147

144148
#### Day 7 Breadth First Search Depth First Search
145149

@@ -413,6 +417,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
413417

414418
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
415419
|-|-|-|-|-|-
420+
| 0198 |[House Robber](src/main/js/g0101_0200/s0198_house_robber/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
416421

417422
#### Day 4
418423

@@ -692,6 +697,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
692697

693698
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
694699
|-|-|-|-|-|-
700+
| 0200 |[Number of Islands](src/main/js/g0101_0200/s0200_number_of_islands/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 62 | 90.80
695701

696702
#### Day 2 Matrix Related Problems
697703

@@ -827,6 +833,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
827833
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
828834
|-|-|-|-|-|-
829835
| 0021 |[Merge Two Sorted Lists](src/main/js/g0001_0100/s0021_merge_two_sorted_lists/solution.js)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 0 | 100.00
836+
| 0206 |[Reverse Linked List](src/main/js/g0201_0300/s0206_reverse_linked_list/solution.js)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
830837

831838
#### Day 4 Linked List
832839

@@ -861,6 +868,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
861868

862869
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
863870
|-|-|-|-|-|-
871+
| 0200 |[Number of Islands](src/main/js/g0101_0200/s0200_number_of_islands/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 62 | 90.80
864872

865873
#### Day 10 Dynamic Programming
866874

@@ -960,6 +968,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
960968

961969
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
962970
|-|-|-|-|-|-
971+
| 0198 |[House Robber](src/main/js/g0101_0200/s0198_house_robber/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
963972

964973
#### Day 13 Dynamic Programming
965974

@@ -1041,6 +1050,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
10411050
|-|-|-|-|-|-
10421051
| 0121 |[Best Time to Buy and Sell Stock](src/main/js/g0101_0200/s0121_best_time_to_buy_and_sell_stock/solution.js)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 1 | 97.34
10431052
| 0001 |[Two Sum](src/main/js/g0001_0100/s0001_two_sum/solution.js)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n), AI_can_be_used_to_solve_the_task | 1 | 89.15
1053+
| 0189 |[Rotate Array](src/main/js/g0101_0200/s0189_rotate_array/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
10441054
| 0055 |[Jump Game](src/main/js/g0001_0100/s0055_jump_game/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
10451055
| 0075 |[Sort Colors](src/main/js/g0001_0100/s0075_sort_colors/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
10461056
| 0041 |[First Missing Positive](src/main/js/g0001_0100/s0041_first_missing_positive/solution.js)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 1 | 98.86
@@ -1057,6 +1067,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
10571067
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10581068
|-|-|-|-|-|-
10591069
| 0053 |[Maximum Subarray](src/main/js/g0001_0100/s0053_maximum_subarray/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer, Big_O_Time_O(n)_Space_O(1) | 1 | 85.69
1070+
| 0169 |[Majority Element](src/main/js/g0101_0200/s0169_majority_element/solution.js)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Counting, Divide_and_Conquer, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
10601071

10611072
#### Udemy Sorting Algorithms
10621073

@@ -1080,6 +1091,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
10801091
| 0024 |[Swap Nodes in Pairs](src/main/js/g0001_0100/s0024_swap_nodes_in_pairs/solution.js)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 1 | 100.00
10811092
| 0142 |[Linked List Cycle II](src/main/js/g0101_0200/s0142_linked_list_cycle_ii/solution.js)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 58 | 92.76
10821093
| 0141 |[Linked List Cycle](src/main/js/g0101_0200/s0141_linked_list_cycle/solution.js)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 49 | 99.06
1094+
| 0206 |[Reverse Linked List](src/main/js/g0201_0300/s0206_reverse_linked_list/solution.js)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
10831095
| 0021 |[Merge Two Sorted Lists](src/main/js/g0001_0100/s0021_merge_two_sorted_lists/solution.js)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(m+n)_Space_O(m+n) | 0 | 100.00
10841096
| 0160 |[Intersection of Two Linked Lists](src/main/js/g0101_0200/s0160_intersection_of_two_linked_lists/solution.js)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(M+N)_Space_O(1) | 68 | 93.67
10851097
| 0138 |[Copy List with Random Pointer](src/main/js/g0101_0200/s0138_copy_list_with_random_pointer/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Big_O_Time_O(N)_Space_O(N) | 43 | 97.07
@@ -1105,13 +1117,15 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
11051117

11061118
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11071119
|-|-|-|-|-|-
1120+
| 0200 |[Number of Islands](src/main/js/g0101_0200/s0200_number_of_islands/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Big_O_Time_O(M\*N)_Space_O(M\*N) | 62 | 90.80
11081121

11091122
#### Udemy Dynamic Programming
11101123

11111124
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11121125
|-|-|-|-|-|-
11131126
| 0139 |[Word Break](src/main/js/g0101_0200/s0139_word_break/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Dynamic_Programming, Trie, Memoization, Big_O_Time_O(M+max\*N)_Space_O(M+N+max) | 0 | 100.00
11141127
| 0152 |[Maximum Product Subarray](src/main/js/g0101_0200/s0152_maximum_product_subarray/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
1128+
| 0198 |[House Robber](src/main/js/g0101_0200/s0198_house_robber/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
11151129
| 0070 |[Climbing Stairs](src/main/js/g0001_0100/s0070_climbing_stairs/solution.js)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
11161130
| 0064 |[Minimum Path Sum](src/main/js/g0001_0100/s0064_minimum_path_sum/solution.js)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 3 | 83.07
11171131
| 0072 |[Edit Distance](src/main/js/g0001_0100/s0072_edit_distance/solution.js)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 5 | 99.01
@@ -1185,6 +1199,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
11851199

11861200
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11871201
|-|-|-|-|-|-
1202+
| 0206 |[Reverse Linked List](src/main/js/g0201_0300/s0206_reverse_linked_list/solution.js)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
11881203

11891204
#### Day 9 Stack Queue
11901205

@@ -1229,6 +1244,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
12291244
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12301245
|-|-|-|-|-|-
12311246
| 0136 |[Single Number](src/main/js/g0101_0200/s0136_single_number/solution.js)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Bit_Manipulation, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
1247+
| 0169 |[Majority Element](src/main/js/g0101_0200/s0169_majority_element/solution.js)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Counting, Divide_and_Conquer, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
12321248
| 0015 |[3Sum](src/main/js/g0001_0100/s0015_3sum/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n\*log(n))_Space_O(n^2) | 31 | 91.92
12331249

12341250
#### Day 2 Array
@@ -1347,6 +1363,11 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
13471363

13481364
| # | Title | Difficulty | Tag | Time, ms | Time, %
13491365
|------|----------------|-------------|-------------|----------|---------
1366+
| 0206 |[Reverse Linked List](src/main/js/g0201_0300/s0206_reverse_linked_list/solution.js)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Linked_List, Recursion, Data_Structure_I_Day_8_Linked_List, Algorithm_I_Day_10_Recursion_Backtracking, Level_1_Day_3_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
1367+
| 0200 |[Number of Islands](src/main/js/g0101_0200/s0200_number_of_islands/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find, Algorithm_II_Day_6_Breadth_First_Search_Depth_First_Search, Graph_Theory_I_Day_1_Matrix_Related_Problems, Level_1_Day_9_Graph/BFS/DFS, Udemy_Graph, Big_O_Time_O(M\*N)_Space_O(M\*N) | 62 | 90.80
1368+
| 0198 |[House Robber](src/main/js/g0101_0200/s0198_house_robber/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_3, Level_2_Day_12_Dynamic_Programming, Udemy_Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
1369+
| 0189 |[Rotate Array](src/main/js/g0101_0200/s0189_rotate_array/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers, Algorithm_I_Day_2_Two_Pointers, Udemy_Arrays, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
1370+
| 0169 |[Majority Element](src/main/js/g0101_0200/s0169_majority_element/solution.js)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Sorting, Counting, Divide_and_Conquer, Data_Structure_II_Day_1_Array, Udemy_Famous_Algorithm, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
13501371
| 0160 |[Intersection of Two Linked Lists](src/main/js/g0101_0200/s0160_intersection_of_two_linked_lists/solution.js)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Data_Structure_II_Day_11_Linked_List, Udemy_Linked_List, Big_O_Time_O(M+N)_Space_O(1) | 68 | 93.67
13511372
| 0155 |[Min Stack](src/main/js/g0101_0200/s0155_min_stack/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Stack, Design, Data_Structure_II_Day_14_Stack_Queue, Programming_Skills_II_Day_18, Level_2_Day_16_Design, Udemy_Design, Big_O_Time_O(1)_Space_O(N) | 6 | 99.32
13521373
| 0153 |[Find Minimum in Rotated Sorted Array](src/main/js/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/solution.js)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Algorithm_II_Day_2_Binary_Search, Binary_Search_I_Day_12, Udemy_Binary_Search, Big_O_Time_O(log_N)_Space_O(log_N) | 0 | 100.00
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
169\. Majority Element
2+
3+
Easy
4+
5+
Given an array `nums` of size `n`, return _the majority element_.
6+
7+
The majority element is the element that appears more than `⌊n / 2⌋` times. You may assume that the majority element always exists in the array.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [3,2,3]
12+
13+
**Output:** 3
14+
15+
**Example 2:**
16+
17+
**Input:** nums = [2,2,1,1,1,2,2]
18+
19+
**Output:** 2
20+
21+
**Constraints:**
22+
23+
* `n == nums.length`
24+
* <code>1 <= n <= 5 * 10<sup>4</sup></code>
25+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
26+
27+
**Follow-up:** Could you solve the problem in linear time and in `O(1)` space?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Sorting #Counting
2+
// #Divide_and_Conquer #Data_Structure_II_Day_1_Array #Udemy_Famous_Algorithm
3+
// #Big_O_Time_O(n)_Space_O(1) #2024_12_17_Time_0_ms_(100.00%)_Space_51.8_MB_(69.63%)
4+
5+
/**
6+
* @param {number[]} nums
7+
* @return {number}
8+
*/
9+
var majorityElement = function(arr) {
10+
let count = 1;
11+
let majority = arr[0];
12+
13+
// For Potential Majority Element
14+
for (let i = 1; i < arr.length; i++) {
15+
if (arr[i] === majority) {
16+
count++;
17+
} else if (count > 1) {
18+
count--;
19+
} else {
20+
majority = arr[i];
21+
}
22+
}
23+
24+
// For Confirmation
25+
count = 0;
26+
for (const num of arr) {
27+
if (num === majority) {
28+
count++;
29+
}
30+
}
31+
32+
if (count >= Math.floor(arr.length / 2) + 1) {
33+
return majority;
34+
} else {
35+
return -1;
36+
}
37+
};
38+
39+
export { majorityElement }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
189\. Rotate Array
2+
3+
Medium
4+
5+
Given an array, rotate the array to the right by `k` steps, where `k` is non-negative.
6+
7+
**Example 1:**
8+
9+
**Input:** nums = [1,2,3,4,5,6,7], k = 3
10+
11+
**Output:** [5,6,7,1,2,3,4]
12+
13+
**Explanation:**
14+
15+
rotate 1 steps to the right: [7,1,2,3,4,5,6]
16+
17+
rotate 2 steps to the right: [6,7,1,2,3,4,5]
18+
19+
rotate 3 steps to the right: [5,6,7,1,2,3,4]
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [-1,-100,3,99], k = 2
24+
25+
**Output:** [3,99,-1,-100]
26+
27+
**Explanation:**
28+
29+
rotate 1 steps to the right: [99,-1,-100,3]
30+
31+
rotate 2 steps to the right: [3,99,-1,-100]
32+
33+
**Constraints:**
34+
35+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
36+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
37+
* <code>0 <= k <= 10<sup>5</sup></code>
38+
39+
**Follow up:**
40+
41+
* Try to come up with as many solutions as you can. There are at least **three** different ways to solve this problem.
42+
* Could you do it in-place with `O(1)` extra space?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Math #Two_Pointers
2+
// #Algorithm_I_Day_2_Two_Pointers #Udemy_Arrays #Big_O_Time_O(n)_Space_O(1)
3+
// #2024_12_17_Time_0_ms_(100.00%)_Space_60.8_MB_(38.22%)
4+
5+
/**
6+
* @param {number[]} nums
7+
* @param {number} k
8+
* @return {void} Do not return anything, modify nums in-place instead.
9+
*/
10+
var reverse = function(nums, l, r) {
11+
while (l <= r) {
12+
let temp = nums[l];
13+
nums[l] = nums[r];
14+
nums[r] = temp;
15+
l++;
16+
r--;
17+
}
18+
};
19+
20+
const rotate = function(nums, k) {
21+
const n = nums.length;
22+
const t = n - (k % n);
23+
reverse(nums, 0, t - 1);
24+
reverse(nums, t, n - 1);
25+
reverse(nums, 0, n - 1);
26+
};
27+
28+
export { rotate }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
198\. House Robber
2+
3+
Medium
4+
5+
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and **it will automatically contact the police if two adjacent houses were broken into on the same night**.
6+
7+
Given an integer array `nums` representing the amount of money of each house, return _the maximum amount of money you can rob tonight **without alerting the police**_.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,2,3,1]
12+
13+
**Output:** 4
14+
15+
**Explanation:** Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4.
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [2,7,9,3,1]
20+
21+
**Output:** 12
22+
23+
**Explanation:** Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12.
24+
25+
**Constraints:**
26+
27+
* `1 <= nums.length <= 100`
28+
* `0 <= nums[i] <= 400`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Dynamic_Programming
2+
// #Algorithm_I_Day_12_Dynamic_Programming #Dynamic_Programming_I_Day_3
3+
// #Level_2_Day_12_Dynamic_Programming #Udemy_Dynamic_Programming #Big_O_Time_O(n)_Space_O(n)
4+
// #2024_12_17_Time_0_ms_(100.00%)_Space_49_MB_(33.76%)
5+
6+
/**
7+
* @param {number[]} nums
8+
* @return {number}
9+
*/
10+
var rob = function(nums) {
11+
if (nums.length === 0) {
12+
return 0;
13+
}
14+
if (nums.length === 1) {
15+
return nums[0];
16+
}
17+
if (nums.length === 2) {
18+
return Math.max(nums[0], nums[1]);
19+
}
20+
21+
const profit = new Array(nums.length);
22+
profit[0] = nums[0];
23+
profit[1] = Math.max(nums[0], nums[1]);
24+
25+
for (let i = 2; i < nums.length; i++) {
26+
profit[i] = Math.max(profit[i - 1], nums[i] + profit[i - 2]);
27+
}
28+
29+
return profit[nums.length - 1];
30+
};
31+
32+
export { rob }

0 commit comments

Comments
 (0)