Skip to content

Commit 150a1d9

Browse files
authored
Added tasks 31-35
1 parent d500513 commit 150a1d9

File tree

16 files changed

+461
-2
lines changed

16 files changed

+461
-2
lines changed

README.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
3030

3131
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
3232
|-|-|-|-|-|-
33+
| 0035 |[Search Insert Position](src/main/js/g0001_0100/s0035_search_insert_position/solution.js)| Easy | Top_100_Liked_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
3334

3435
#### Day 2 Two Pointers
3536

@@ -105,6 +106,8 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
105106

106107
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
107108
|-|-|-|-|-|-
109+
| 0034 |[Find First and Last Position of Element in Sorted Array](src/main/js/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
110+
| 0033 |[Search in Rotated Sorted Array](src/main/js/g0001_0100/s0033_search_in_rotated_sorted_array/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
108111

109112
#### Day 2 Binary Search
110113

@@ -222,6 +225,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
222225

223226
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
224227
|-|-|-|-|-|-
228+
| 0035 |[Search Insert Position](src/main/js/g0001_0100/s0035_search_insert_position/solution.js)| Easy | Top_100_Liked_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
225229

226230
#### Day 3
227231

@@ -237,6 +241,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
237241

238242
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
239243
|-|-|-|-|-|-
244+
| 0034 |[Find First and Last Position of Element in Sorted Array](src/main/js/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
240245

241246
#### Day 6
242247

@@ -267,6 +272,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
267272

268273
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
269274
|-|-|-|-|-|-
275+
| 0033 |[Search in Rotated Sorted Array](src/main/js/g0001_0100/s0033_search_in_rotated_sorted_array/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
270276

271277
#### Day 12
272278

@@ -893,6 +899,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
893899

894900
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
895901
|-|-|-|-|-|-
902+
| 0033 |[Search in Rotated Sorted Array](src/main/js/g0001_0100/s0033_search_in_rotated_sorted_array/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
896903

897904
#### Day 9 Binary Search Tree
898905

@@ -961,7 +968,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
961968

962969
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
963970
|-|-|-|-|-|-
964-
| 0007 |[Reverse Integer](src/main/js/g0001_0100/s0007_reverse_integer/solution.js)| Medium | Top_Interview_Questions, Math | 66 | 87.91
971+
| 0007 |[Reverse Integer](src/main/js/g0001_0100/s0007_reverse_integer/solution.js)| Medium | Top_Interview_Questions, Math | 67 | 85.38
965972
| 0009 |[Palindrome Number](src/main/js/g0001_0100/s0009_palindrome_number/solution.js)| Easy | Math | 4 | 97.14
966973

967974
#### Udemy Strings
@@ -976,6 +983,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
976983

977984
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
978985
|-|-|-|-|-|-
986+
| 0033 |[Search in Rotated Sorted Array](src/main/js/g0001_0100/s0033_search_in_rotated_sorted_array/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
979987

980988
#### Udemy Arrays
981989

@@ -1241,6 +1249,11 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
12411249

12421250
| # | Title | Difficulty | Tag | Time, ms | Time, %
12431251
|------|----------------|-------------|-------------|----------|---------
1252+
| 0035 |[Search Insert Position](src/main/js/g0001_0100/s0035_search_insert_position/solution.js)| 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) | 0 | 100.00
1253+
| 0034 |[Find First and Last Position of Element in Sorted Array](src/main/js/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array/solution.js)| 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) | 0 | 100.00
1254+
| 0033 |[Search in Rotated Sorted Array](src/main/js/g0001_0100/s0033_search_in_rotated_sorted_array/solution.js)| 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) | 0 | 100.00
1255+
| 0032 |[Longest Valid Parentheses](src/main/js/g0001_0100/s0032_longest_valid_parentheses/solution.js)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming, Stack, Big_O_Time_O(n)_Space_O(1) | 1 | 96.21
1256+
| 0031 |[Next Permutation](src/main/js/g0001_0100/s0031_next_permutation/solution.js)| Medium | Top_100_Liked_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
12441257
| 0025 |[Reverse Nodes in k-Group](src/main/js/g0001_0100/s0025_reverse_nodes_in_k_group/solution.js)| 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) | 0 | 100.00
12451258
| 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, Data_Structure_II_Day_12_Linked_List, Udemy_Linked_List, Big_O_Time_O(n)_Space_O(1) | 1 | 100.00
12461259
| 0023 |[Merge k Sorted Lists](src/main/js/g0001_0100/s0023_merge_k_sorted_lists/solution.js)| 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)) | 3 | 99.39
@@ -1254,7 +1267,7 @@ JavaScript-based LeetCode algorithm problem solutions, regularly updated.
12541267
| 0010 |[Regular Expression Matching](src/main/js/g0001_0100/s0010_regular_expression_matching/solution.js)| Hard | Top_Interview_Questions, String, Dynamic_Programming, Recursion, Udemy_Dynamic_Programming, Big_O_Time_O(m\*n)_Space_O(m\*n) | 4 | 95.52
12551268
| 0009 |[Palindrome Number](src/main/js/g0001_0100/s0009_palindrome_number/solution.js)| Easy | Math, Udemy_Integers | 4 | 97.14
12561269
| 0008 |[String to Integer (atoi)](src/main/js/g0001_0100/s0008_string_to_integer_atoi/solution.js)| Medium | Top_Interview_Questions, String | 1 | 96.63
1257-
| 0007 |[Reverse Integer](src/main/js/g0001_0100/s0007_reverse_integer/solution.js)| Medium | Top_Interview_Questions, Math, Udemy_Integers | 66 | 87.91
1270+
| 0007 |[Reverse Integer](src/main/js/g0001_0100/s0007_reverse_integer/solution.js)| Medium | Top_Interview_Questions, Math, Udemy_Integers | 67 | 85.38
12581271
| 0006 |[Zigzag Conversion](src/main/js/g0001_0100/s0006_zigzag_conversion/solution.js)| Medium | String | 2 | 99.71
12591272
| 0005 |[Longest Palindromic Substring](src/main/js/g0001_0100/s0005_longest_palindromic_substring/solution.js)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Data_Structure_II_Day_9_String, Algorithm_II_Day_14_Dynamic_Programming, Dynamic_Programming_I_Day_17, Udemy_Strings, Big_O_Time_O(n)_Space_O(n) | 10 | 99.54
12601273
| 0004 |[Median of Two Sorted Arrays](src/main/js/g0001_0100/s0004_median_of_two_sorted_arrays/solution.js)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Divide_and_Conquer, Big_O_Time_O(log(min(N,M)))_Space_O(1), AI_can_be_used_to_solve_the_task | 3 | 91.90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
31\. Next Permutation
2+
3+
Medium
4+
5+
A **permutation** of an array of integers is an arrangement of its members into a sequence or linear order.
6+
7+
* For example, for `arr = [1,2,3]`, the following are all the permutations of `arr`: `[1,2,3], [1,3,2], [2, 1, 3], [2, 3, 1], [3,1,2], [3,2,1]`.
8+
9+
The **next permutation** of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the **next permutation** of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order).
10+
11+
* For example, the next permutation of `arr = [1,2,3]` is `[1,3,2]`.
12+
* Similarly, the next permutation of `arr = [2,3,1]` is `[3,1,2]`.
13+
* While the next permutation of `arr = [3,2,1]` is `[1,2,3]` because `[3,2,1]` does not have a lexicographical larger rearrangement.
14+
15+
Given an array of integers `nums`, _find the next permutation of_ `nums`.
16+
17+
The replacement must be **[in place](http://en.wikipedia.org/wiki/In-place_algorithm)** and use only constant extra memory.
18+
19+
**Example 1:**
20+
21+
**Input:** nums = [1,2,3]
22+
23+
**Output:** [1,3,2]
24+
25+
**Example 2:**
26+
27+
**Input:** nums = [3,2,1]
28+
29+
**Output:** [1,2,3]
30+
31+
**Example 3:**
32+
33+
**Input:** nums = [1,1,5]
34+
35+
**Output:** [1,5,1]
36+
37+
**Constraints:**
38+
39+
* `1 <= nums.length <= 100`
40+
* `0 <= nums[i] <= 100`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// #Medium #Top_100_Liked_Questions #Array #Two_Pointers #Big_O_Time_O(n)_Space_O(1)
2+
// #2024_12_05_Time_0_ms_(100.00%)_Space_52.3_MB_(8.66%)
3+
4+
/**
5+
* @param {number[]} nums
6+
* @return {void} Do not return anything, modify nums in-place instead.
7+
*/
8+
var nextPermutation = function(nums) {
9+
if (!nums || nums.length <= 1) {
10+
return
11+
}
12+
13+
let i = nums.length - 2
14+
15+
// Find the first index `i` where nums[i] < nums[i + 1]
16+
while (i >= 0 && nums[i] >= nums[i + 1]) {
17+
i--
18+
}
19+
20+
if (i >= 0) {
21+
// Find the smallest number larger than nums[i] to swap with
22+
let j = nums.length - 1
23+
while (nums[j] <= nums[i]) {
24+
j--
25+
}
26+
swap(nums, i, j)
27+
}
28+
29+
// Reverse the portion of the array from index `i + 1` to the end
30+
reverse(nums, i + 1, nums.length - 1)
31+
};
32+
33+
function swap(nums, i, j) {
34+
const temp = nums[i]
35+
nums[i] = nums[j]
36+
nums[j] = temp
37+
}
38+
39+
function reverse(nums, start, end) {
40+
while (start < end) {
41+
swap(nums, start++, end--)
42+
}
43+
}
44+
45+
export { nextPermutation }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
32\. Longest Valid Parentheses
2+
3+
Hard
4+
5+
Given a string containing just the characters `'('` and `')'`, find the length of the longest valid (well-formed) parentheses substring.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "(()"
10+
11+
**Output:** 2
12+
13+
**Explanation:** The longest valid parentheses substring is "()".
14+
15+
**Example 2:**
16+
17+
**Input:** s = ")()())"
18+
19+
**Output:** 4
20+
21+
**Explanation:** The longest valid parentheses substring is "()()".
22+
23+
**Example 3:**
24+
25+
**Input:** s = ""
26+
27+
**Output:** 0
28+
29+
**Constraints:**
30+
31+
* <code>0 <= s.length <= 3 * 10<sup>4</sup></code>
32+
* `s[i]` is `'('`, or `')'`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// #Hard #Top_100_Liked_Questions #String #Dynamic_Programming #Stack #Big_O_Time_O(n)_Space_O(1)
2+
// #2024_12_05_Time_1_ms_(96.21%)_Space_52.5_MB_(15.11%)
3+
4+
/**
5+
* @param {string} s
6+
* @return {number}
7+
*/
8+
var longestValidParentheses = function (s) {
9+
const n = s.length
10+
const f = new Array(n + 1).fill(0)
11+
for (let i = 2; i <= n; ++i) {
12+
if (s[i - 1] === ')') {
13+
if (s[i - 2] === '(') {
14+
f[i] = f[i - 2] + 2
15+
} else {
16+
const j = i - f[i - 1] - 1
17+
if (j && s[j - 1] === '(') {
18+
f[i] = f[i - 1] + 2 + f[j - 1]
19+
}
20+
}
21+
}
22+
}
23+
return Math.max(...f)
24+
};
25+
26+
export { longestValidParentheses }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
33\. Search in Rotated Sorted Array
2+
3+
Medium
4+
5+
There is an integer array `nums` sorted in ascending order (with **distinct** values).
6+
7+
Prior to being passed to your function, `nums` is **possibly rotated** at an unknown pivot index `k` (`1 <= k < nums.length`) such that the resulting array is `[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]` (**0-indexed**). For example, `[0,1,2,4,5,6,7]` might be rotated at pivot index `3` and become `[4,5,6,7,0,1,2]`.
8+
9+
Given the array `nums` **after** the possible rotation and an integer `target`, return _the index of_ `target` _if it is in_ `nums`_, or_ `-1` _if it is not in_ `nums`.
10+
11+
You must write an algorithm with `O(log n)` runtime complexity.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [4,5,6,7,0,1,2], target = 0
16+
17+
**Output:** 4
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [4,5,6,7,0,1,2], target = 3
22+
23+
**Output:** -1
24+
25+
**Example 3:**
26+
27+
**Input:** nums = [1], target = 0
28+
29+
**Output:** -1
30+
31+
**Constraints:**
32+
33+
* `1 <= nums.length <= 5000`
34+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
35+
* All values of `nums` are **unique**.
36+
* `nums` is an ascending array that is possibly rotated.
37+
* <code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search
2+
// #Algorithm_II_Day_1_Binary_Search #Binary_Search_I_Day_11 #Level_2_Day_8_Binary_Search
3+
// #Udemy_Binary_Search #Big_O_Time_O(log_n)_Space_O(1)
4+
// #2024_12_05_Time_0_ms_(100.00%)_Space_49.3_MB_(21.97%)
5+
6+
/**
7+
* @param {number[]} nums
8+
* @param {number} target
9+
* @return {number}
10+
*/
11+
var search = function(nums, target) {
12+
let lo = 0
13+
let hi = nums.length - 1
14+
while (lo <= hi) {
15+
const mid = Math.floor((hi - lo) / 2) + lo
16+
if (nums[mid] === target) {
17+
return mid
18+
}
19+
if (nums[lo] <= nums[mid]) {
20+
// Target is in the sorted left half
21+
if (nums[lo] <= target && target <= nums[mid]) {
22+
hi = mid - 1
23+
} else {
24+
lo = mid + 1
25+
}
26+
} else if (nums[mid] <= target && target <= nums[hi]) {
27+
lo = mid + 1
28+
} else {
29+
hi = mid - 1
30+
}
31+
}
32+
return -1
33+
};
34+
35+
export { search }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
34\. Find First and Last Position of Element in Sorted Array
2+
3+
Medium
4+
5+
Given an array of integers `nums` sorted in non-decreasing order, find the starting and ending position of a given `target` value.
6+
7+
If `target` is not found in the array, return `[-1, -1]`.
8+
9+
You must write an algorithm with `O(log n)` runtime complexity.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [5,7,7,8,8,10], target = 8
14+
15+
**Output:** [3,4]
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [5,7,7,8,8,10], target = 6
20+
21+
**Output:** [-1,-1]
22+
23+
**Example 3:**
24+
25+
**Input:** nums = [], target = 0
26+
27+
**Output:** [-1,-1]
28+
29+
**Constraints:**
30+
31+
* <code>0 <= nums.length <= 10<sup>5</sup></code>
32+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
33+
* `nums` is a non-decreasing array.
34+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search
2+
// #Algorithm_II_Day_1_Binary_Search #Binary_Search_I_Day_5 #Big_O_Time_O(log_n)_Space_O(1)
3+
// #2024_12_05_Time_0_ms_(100.00%)_Space_49.9_MB_(40.62%)
4+
5+
/**
6+
* @param {number[]} nums
7+
* @param {number} target
8+
* @return {number[]}
9+
*/
10+
var searchRange = function(nums, target) {
11+
return [
12+
helper(nums, target, false),
13+
helper(nums, target, true)
14+
]
15+
}
16+
17+
function helper(nums, target, equals) {
18+
let l = 0, r = nums.length - 1
19+
let result = -1
20+
21+
while (l <= r) {
22+
const mid = Math.floor((l + r) / 2)
23+
if (nums[mid] === target) {
24+
result = mid
25+
}
26+
if (nums[mid] < target || (nums[mid] === target && equals)) {
27+
l = mid + 1
28+
} else {
29+
r = mid - 1
30+
}
31+
}
32+
33+
return result
34+
};
35+
36+
export { searchRange }

0 commit comments

Comments
 (0)