Skip to content

Commit 8257395

Browse files
authored
Added tasks 31-35
1 parent eea057a commit 8257395

File tree

6 files changed

+378
-0
lines changed

6 files changed

+378
-0
lines changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135

136136
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
137137
|-|-|-|-|-|-
138+
| 0035 |[Search Insert Position](src/main/ts/g0001_0100/s0035_search_insert_position)| Easy | Top_100_Liked_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 51 | 80.95
138139

139140
#### Day 2 Two Pointers
140141

@@ -210,6 +211,8 @@
210211

211212
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
212213
|-|-|-|-|-|-
214+
| 0034 |[Find First and Last Position of Element in Sorted Array](src/main/ts/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 45 | 98.05
215+
| 0033 |[Search in Rotated Sorted Array](src/main/ts/g0001_0100/s0033_search_in_rotated_sorted_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 43 | 98.54
213216

214217
#### Day 2 Binary Search
215218

@@ -327,6 +330,7 @@
327330

328331
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
329332
|-|-|-|-|-|-
333+
| 0035 |[Search Insert Position](src/main/ts/g0001_0100/s0035_search_insert_position)| Easy | Top_100_Liked_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 51 | 80.95
330334

331335
#### Day 3
332336

@@ -342,6 +346,7 @@
342346

343347
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
344348
|-|-|-|-|-|-
349+
| 0034 |[Find First and Last Position of Element in Sorted Array](src/main/ts/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 45 | 98.05
345350

346351
#### Day 6
347352

@@ -372,6 +377,7 @@
372377

373378
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
374379
|-|-|-|-|-|-
380+
| 0033 |[Search in Rotated Sorted Array](src/main/ts/g0001_0100/s0033_search_in_rotated_sorted_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 43 | 98.54
375381

376382
#### Day 12
377383

@@ -998,6 +1004,7 @@
9981004

9991005
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10001006
|-|-|-|-|-|-
1007+
| 0033 |[Search in Rotated Sorted Array](src/main/ts/g0001_0100/s0033_search_in_rotated_sorted_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 43 | 98.54
10011008

10021009
#### Day 9 Binary Search Tree
10031010

@@ -1081,6 +1088,7 @@
10811088

10821089
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10831090
|-|-|-|-|-|-
1091+
| 0033 |[Search in Rotated Sorted Array](src/main/ts/g0001_0100/s0033_search_in_rotated_sorted_array)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 43 | 98.54
10841092

10851093
#### Udemy Arrays
10861094

@@ -1234,6 +1242,11 @@
12341242

12351243
| # | Title | Difficulty | Tag | Time, ms | Time, %
12361244
|------|----------------|-------------|-------------|----------|--------
1245+
| 0035 |[Search Insert Position](src/main/ts/g0001_0100/s0035_search_insert_position)| 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) | 51 | 80.95
1246+
| 0034 |[Find First and Last Position of Element in Sorted Array](src/main/ts/g0001_0100/s0034_find_first_and_last_position_of_element_in_sorted_array)| 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) | 45 | 98.05
1247+
| 0033 |[Search in Rotated Sorted Array](src/main/ts/g0001_0100/s0033_search_in_rotated_sorted_array)| 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) | 43 | 98.54
1248+
| 0032 |[Longest Valid Parentheses](src/main/ts/g0001_0100/s0032_longest_valid_parentheses)| Hard | Top_100_Liked_Questions, String, Dynamic_Programming, Stack, Big_O_Time_O(n)_Space_O(1) | 56 | 89.31
1249+
| 0031 |[Next Permutation](src/main/ts/g0001_0100/s0031_next_permutation)| Medium | Top_100_Liked_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 47 | 99.30
12371250
| 0025 |[Reverse Nodes in k-Group](src/main/ts/g0001_0100/s0025_reverse_nodes_in_k_group)| 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) | 74 | 66.83
12381251
| 0024 |[Swap Nodes in Pairs](src/main/ts/g0001_0100/s0024_swap_nodes_in_pairs)| 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) | 59 | 62.67
12391252
| 0023 |[Merge k Sorted Lists](src/main/ts/g0001_0100/s0023_merge_k_sorted_lists)| 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)) | 75 | 97.06
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 31\. Next Permutation
5+
6+
Medium
7+
8+
A **permutation** of an array of integers is an arrangement of its members into a sequence or linear order.
9+
10+
- 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]`.
11+
12+
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).
13+
14+
- For example, the next permutation of `arr = [1,2,3]` is `[1,3,2]`.
15+
- Similarly, the next permutation of `arr = [2,3,1]` is `[3,1,2]`.
16+
- While the next permutation of `arr = [3,2,1]` is `[1,2,3]` because `[3,2,1]` does not have a lexicographical larger rearrangement.
17+
18+
Given an array of integers `nums`, _find the next permutation of_ `nums`.
19+
20+
The replacement must be **[in place](http://en.wikipedia.org/wiki/In-place_algorithm)** and use only constant extra memory.
21+
22+
**Example 1:**
23+
24+
**Input:** nums = [1,2,3]
25+
26+
**Output:** [1,3,2]
27+
28+
**Example 2:**
29+
30+
**Input:** nums = [3,2,1]
31+
32+
**Output:** [1,2,3]
33+
34+
**Example 3:**
35+
36+
**Input:** nums = [1,1,5]
37+
38+
**Output:** [1,5,1]
39+
40+
**Constraints:**
41+
42+
- `1 <= nums.length <= 100`
43+
- `0 <= nums[i] <= 100`
44+
45+
## Solution
46+
47+
```typescript
48+
/*
49+
Do not return anything, modify nums in-place instead.
50+
*/
51+
function nextPermutation(nums: number[]): void {
52+
let swapperIndex: number | null = null
53+
54+
for (let ind = nums.length - 1; ind >= 0 && swapperIndex == null; ind--) {
55+
if (nums[ind] > nums[ind - 1]) {
56+
swapperIndex = ind - 1
57+
}
58+
}
59+
60+
if (swapperIndex == null) nums.sort((a, b) => a - b)
61+
else {
62+
nums.splice(swapperIndex + 1, nums.length, ...nums.slice(swapperIndex + 1, nums.length).sort((a, b) => a - b))
63+
let indToBringForward = swapperIndex + 1
64+
while (nums[indToBringForward] <= nums[swapperIndex]) ++indToBringForward
65+
let swappedNumber = nums[swapperIndex]
66+
nums[swapperIndex] = nums[indToBringForward]
67+
nums[indToBringForward] = swappedNumber
68+
}
69+
}
70+
71+
export { nextPermutation }
72+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 32\. Longest Valid Parentheses
5+
6+
Hard
7+
8+
Given a string containing just the characters `'('` and `')'`, find the length of the longest valid (well-formed) parentheses substring.
9+
10+
**Example 1:**
11+
12+
**Input:** s = "(()"
13+
14+
**Output:** 2
15+
16+
**Explanation:** The longest valid parentheses substring is "()".
17+
18+
**Example 2:**
19+
20+
**Input:** s = ")()())"
21+
22+
**Output:** 4
23+
24+
**Explanation:** The longest valid parentheses substring is "()()".
25+
26+
**Example 3:**
27+
28+
**Input:** s = ""
29+
30+
**Output:** 0
31+
32+
**Constraints:**
33+
34+
- <code>0 <= s.length <= 3 \* 10<sup>4</sup></code>
35+
- `s[i]` is `'('`, or `')'`.
36+
37+
## Solution
38+
39+
```typescript
40+
function longestValidParentheses(s: string): number {
41+
let open = 0
42+
let close = 0
43+
let max = 0
44+
for (let char of s) {
45+
if (char === '(') {
46+
open++
47+
} else {
48+
close++
49+
}
50+
if (open === close) {
51+
max = Math.max(max, open + close)
52+
} else if (close > open) {
53+
open = 0
54+
close = 0
55+
}
56+
}
57+
open = 0
58+
close = 0
59+
for (let i = s.length - 1; i >= 0; i--) {
60+
if (s[i] === '(') {
61+
open++
62+
} else {
63+
close++
64+
}
65+
if (open === close) {
66+
max = Math.max(max, open + close)
67+
} else if (open > close) {
68+
open = 0
69+
close = 0
70+
}
71+
}
72+
return max
73+
}
74+
75+
export { longestValidParentheses }
76+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 33\. Search in Rotated Sorted Array
5+
6+
Medium
7+
8+
There is an integer array `nums` sorted in ascending order (with **distinct** values).
9+
10+
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]`.
11+
12+
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`.
13+
14+
You must write an algorithm with `O(log n)` runtime complexity.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [4,5,6,7,0,1,2], target = 0
19+
20+
**Output:** 4
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [4,5,6,7,0,1,2], target = 3
25+
26+
**Output:** -1
27+
28+
**Example 3:**
29+
30+
**Input:** nums = [1], target = 0
31+
32+
**Output:** -1
33+
34+
**Constraints:**
35+
36+
* `1 <= nums.length <= 5000`
37+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
38+
* All values of `nums` are **unique**.
39+
* `nums` is an ascending array that is possibly rotated.
40+
* <code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code>
41+
42+
## Solution
43+
44+
```typescript
45+
function search(nums: number[], target: number): number { //NOSONAR
46+
let lowEnd: number = 0
47+
let highEnd: number = nums.length - 1
48+
while (lowEnd <= highEnd) {
49+
let half: number = Math.floor(lowEnd + (highEnd - lowEnd) / 2)
50+
if (target === nums[half]) {
51+
return half
52+
}
53+
if (nums[lowEnd] <= nums[half]) {
54+
if (target < nums[half] && target >= nums[lowEnd]) {
55+
highEnd = half
56+
} else {
57+
lowEnd = half + 1
58+
}
59+
} else if (target >= nums[half] && target <= nums[highEnd]) {
60+
lowEnd = half + 1
61+
} else {
62+
highEnd = half
63+
}
64+
}
65+
return -1
66+
}
67+
68+
export { search }
69+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)
3+
4+
## 34\. Find First and Last Position of Element in Sorted Array
5+
6+
Medium
7+
8+
Given an array of integers `nums` sorted in non-decreasing order, find the starting and ending position of a given `target` value.
9+
10+
If `target` is not found in the array, return `[-1, -1]`.
11+
12+
You must write an algorithm with `O(log n)` runtime complexity.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [5,7,7,8,8,10], target = 8
17+
18+
**Output:** [3,4]
19+
20+
**Example 2:**
21+
22+
**Input:** nums = [5,7,7,8,8,10], target = 6
23+
24+
**Output:** [-1,-1]
25+
26+
**Example 3:**
27+
28+
**Input:** nums = [], target = 0
29+
30+
**Output:** [-1,-1]
31+
32+
**Constraints:**
33+
34+
* <code>0 <= nums.length <= 10<sup>5</sup></code>
35+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
36+
* `nums` is a non-decreasing array.
37+
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
38+
39+
## Solution
40+
41+
```typescript
42+
function searchRange(nums: number[], target: number): number[] { //NOSONAR
43+
let first = -1
44+
let last = -1
45+
let left1 = 0
46+
let left2 = left1
47+
let right1 = nums.length - 1
48+
let right2 = right1
49+
50+
while (left1 <= right1 || left2 <= right2) {
51+
if (left1 <= right1) {
52+
let mid1 = Math.floor((left1 + right1) / 2)
53+
if (nums[mid1] == target) {
54+
first = mid1
55+
right1 = mid1 - 1
56+
} else if (nums[mid1] < target) {
57+
left1 = mid1 + 1
58+
} else {
59+
right1 = mid1 - 1
60+
}
61+
}
62+
63+
if (left2 <= right2) {
64+
let mid2 = Math.floor((left2 + right2) / 2)
65+
if (nums[mid2] == target) {
66+
last = mid2
67+
left2 = mid2 + 1
68+
} else if (nums[mid2] < target) {
69+
left2 = mid2 + 1
70+
} else {
71+
right2 = mid2 - 1
72+
}
73+
}
74+
}
75+
return [first, last]
76+
}
77+
78+
export { searchRange }
79+
```

0 commit comments

Comments
 (0)