Skip to content

Commit 656c338

Browse files
authored
Added tasks 149-167
1 parent 4cb99fa commit 656c338

File tree

16 files changed

+395
-0
lines changed

16 files changed

+395
-0
lines changed

README.md

+17
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
6767

6868
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
6969
|-|-|-|-|-|-
70+
| 0167 |[Two Sum II - Input Array Is Sorted](src/main/ts/g0101_0200/s0167_two_sum_ii_input_array_is_sorted/solution.ts)| Medium | Array, Binary_Search, Two_Pointers | 0 | 100.00
7071

7172
#### Day 8
7273

@@ -160,6 +161,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
160161

161162
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
162163
|-|-|-|-|-|-
164+
| 0162 |[Find Peak Element](src/main/ts/g0101_0200/s0162_find_peak_element/solution.ts)| Medium | Top_Interview_Questions, Array, Binary_Search | 0 | 100.00
163165

164166
#### Day 13
165167

@@ -410,6 +412,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
410412

411413
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
412414
|-|-|-|-|-|-
415+
| 0150 |[Evaluate Reverse Polish Notation](src/main/ts/g0101_0200/s0150_evaluate_reverse_polish_notation/solution.ts)| Medium | Top_Interview_Questions, Array, Math, Stack | 1 | 97.48
413416
| 0066 |[Plus One](src/main/ts/g0001_0100/s0066_plus_one/solution.ts)| Easy | Top_Interview_Questions, Array, Math | 0 | 100.00
414417

415418
#### Day 4
@@ -871,6 +874,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
871874
| 0005 |[Longest Palindromic Substring](src/main/ts/g0001_0100/s0005_longest_palindromic_substring/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Big_O_Time_O(n)_Space_O(n) | 8 | 99.14
872875
| 0394 |[Decode String](src/main/ts/g0301_0400/s0394_decode_string/solution.ts)| Medium | Top_100_Liked_Questions, String, Stack, Recursion, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
873876
| 0049 |[Group Anagrams](src/main/ts/g0001_0100/s0049_group_anagrams/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, String, Hash_Table, Sorting, Big_O_Time_O(n\*k_log_k)_Space_O(n) | 27 | 78.99
877+
| 0151 |[Reverse Words in a String](src/main/ts/g0101_0200/s0151_reverse_words_in_a_string/solution.ts)| Medium | String, Two_Pointers | 0 | 100.00
874878

875879
#### Udemy Binary Search
876880

@@ -1033,6 +1037,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
10331037
| 0012 |[Integer to Roman](src/main/ts/g0001_0100/s0012_integer_to_roman/solution.ts)| Medium | String, Hash_Table, Math | 3 | 93.02
10341038
| 0058 |[Length of Last Word](src/main/ts/g0001_0100/s0058_length_of_last_word/solution.ts)| Easy | String | 0 | 100.00
10351039
| 0014 |[Longest Common Prefix](src/main/ts/g0001_0100/s0014_longest_common_prefix/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String | 0 | 100.00
1040+
| 0151 |[Reverse Words in a String](src/main/ts/g0101_0200/s0151_reverse_words_in_a_string/solution.ts)| Medium | String, Two_Pointers | 0 | 100.00
10361041
| 0006 |[Zigzag Conversion](src/main/ts/g0001_0100/s0006_zigzag_conversion/solution.ts)| Medium | String | 2 | 99.08
10371042
| 0028 |[Implement strStr()](src/main/ts/g0001_0100/s0028_find_the_index_of_the_first_occurrence_in_a_string/solution.ts)| Easy | Top_Interview_Questions, String, Two_Pointers, String_Matching | 0 | 100.00
10381043
| 0068 |[Text Justification](src/main/ts/g0001_0100/s0068_text_justification/solution.ts)| Hard | Array, String, Simulation | 0 | 100.00
@@ -1042,6 +1047,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
10421047
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10431048
|-|-|-|-|-|-
10441049
| 0125 |[Valid Palindrome](src/main/ts/g0101_0200/s0125_valid_palindrome/solution.ts)| Easy | Top_Interview_Questions, String, Two_Pointers | 0 | 100.00
1050+
| 0167 |[Two Sum II - Input Array Is Sorted](src/main/ts/g0101_0200/s0167_two_sum_ii_input_array_is_sorted/solution.ts)| Medium | Array, Binary_Search, Two_Pointers | 0 | 100.00
10451051
| 0011 |[Container With Most Water](src/main/ts/g0001_0100/s0011_container_with_most_water/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Greedy, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 2 | 80.13
10461052
| 0015 |[3Sum](src/main/ts/g0001_0100/s0015_3sum/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n\*log(n))_Space_O(n^2) | 30 | 91.56
10471053

@@ -1084,6 +1090,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
10841090
| 0020 |[Valid Parentheses](src/main/ts/g0001_0100/s0020_valid_parentheses/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Stack, Big_O_Time_O(n)_Space_O(n) | 1 | 86.85
10851091
| 0071 |[Simplify Path](src/main/ts/g0001_0100/s0071_simplify_path/solution.ts)| Medium | String, Stack | 0 | 100.00
10861092
| 0155 |[Min Stack](src/main/ts/g0101_0200/s0155_min_stack/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Stack, Design, Big_O_Time_O(1)_Space_O(N) | 5 | 99.10
1093+
| 0150 |[Evaluate Reverse Polish Notation](src/main/ts/g0101_0200/s0150_evaluate_reverse_polish_notation/solution.ts)| Medium | Top_Interview_Questions, Array, Math, Stack | 1 | 97.48
10871094

10881095
#### Top Interview 150 Linked List
10891096

@@ -1182,6 +1189,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
11821189
|-|-|-|-|-|-
11831190
| 0035 |[Search Insert Position](src/main/ts/g0001_0100/s0035_search_insert_position/solution.ts)| Easy | Top_100_Liked_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
11841191
| 0074 |[Search a 2D Matrix](src/main/ts/g0001_0100/s0074_search_a_2d_matrix/solution.ts)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Matrix, Big_O_Time_O(endRow+endCol)_Space_O(1) | 0 | 100.00
1192+
| 0162 |[Find Peak Element](src/main/ts/g0101_0200/s0162_find_peak_element/solution.ts)| Medium | Top_Interview_Questions, Array, Binary_Search | 0 | 100.00
11851193
| 0033 |[Search in Rotated Sorted Array](src/main/ts/g0001_0100/s0033_search_in_rotated_sorted_array/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
11861194
| 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/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Binary_Search, Big_O_Time_O(log_n)_Space_O(1) | 0 | 100.00
11871195
| 0153 |[Find Minimum in Rotated Sorted Array](src/main/ts/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/solution.ts)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Big_O_Time_O(log_N)_Space_O(log_N) | 0 | 100.00
@@ -1210,6 +1218,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
12101218
| 0066 |[Plus One](src/main/ts/g0001_0100/s0066_plus_one/solution.ts)| Easy | Top_Interview_Questions, Array, Math | 0 | 100.00
12111219
| 0069 |[Sqrt(x)](src/main/ts/g0001_0100/s0069_sqrtx/solution.ts)| Hard | Array, String, Simulation | 0 | 100.00
12121220
| 0050 |[Pow(x, n)](src/main/ts/g0001_0100/s0050_powx_n/solution.ts)| Medium | Top_Interview_Questions, Math, Recursion | 0 | 100.00
1221+
| 0149 |[Max Points on a Line](src/main/ts/g0101_0200/s0149_max_points_on_a_line/solution.ts)| Hard | Top_Interview_Questions, Array, Hash_Table, Math, Geometry | 9 | 97.18
12131222

12141223
#### Top Interview 150 1D DP
12151224

@@ -1475,6 +1484,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
14751484
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
14761485
|-|-|-|-|-|-
14771486
| 0283 |[Move Zeroes](src/main/ts/g0201_0300/s0283_move_zeroes/solution.ts)| Easy | Top_100_Liked_Questions, Array, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 1 | 82.86
1487+
| 0167 |[Two Sum II - Input Array Is Sorted](src/main/ts/g0101_0200/s0167_two_sum_ii_input_array_is_sorted/solution.ts)| Medium | Array, Binary_Search, Two_Pointers | 0 | 100.00
14781488

14791489
#### Day 4 Two Pointers
14801490

@@ -1556,6 +1566,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
15561566
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
15571567
|-|-|-|-|-|-
15581568
| 0153 |[Find Minimum in Rotated Sorted Array](src/main/ts/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/solution.ts)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Big_O_Time_O(log_N)_Space_O(log_N) | 0 | 100.00
1569+
| 0162 |[Find Peak Element](src/main/ts/g0101_0200/s0162_find_peak_element/solution.ts)| Medium | Top_Interview_Questions, Array, Binary_Search | 0 | 100.00
15591570

15601571
#### Day 3 Two Pointers
15611572

@@ -1671,6 +1682,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
16711682

16721683
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
16731684
|-|-|-|-|-|-
1685+
| 0149 |[Max Points on a Line](src/main/ts/g0101_0200/s0149_max_points_on_a_line/solution.ts)| Hard | Top_Interview_Questions, Array, Hash_Table, Math, Geometry | 9 | 97.18
16741686

16751687
## Algorithms
16761688

@@ -1710,10 +1722,15 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
17101722
| 0198 |[House Robber](src/main/ts/g0101_0200/s0198_house_robber/solution.ts)| 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, Top_Interview_150_1D_DP, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
17111723
| 0189 |[Rotate Array](src/main/ts/g0101_0200/s0189_rotate_array/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Two_Pointers, Algorithm_I_Day_2_Two_Pointers, Udemy_Arrays, Top_Interview_150_Array/String, Big_O_Time_O(n)_Space_O(1) | 1 | 86.17
17121724
| 0169 |[Majority Element](src/main/ts/g0101_0200/s0169_majority_element/solution.ts)| 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, Top_Interview_150_Array/String, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
1725+
| 0167 |[Two Sum II - Input Array Is Sorted](src/main/ts/g0101_0200/s0167_two_sum_ii_input_array_is_sorted/solution.ts)| Medium | Array, Binary_Search, Two_Pointers, Algorithm_I_Day_3_Two_Pointers, Binary_Search_I_Day_7, Top_Interview_150_Two_Pointers | 0 | 100.00
1726+
| 0162 |[Find Peak Element](src/main/ts/g0101_0200/s0162_find_peak_element/solution.ts)| Medium | Top_Interview_Questions, Array, Binary_Search, Algorithm_II_Day_2_Binary_Search, Binary_Search_II_Day_12, Top_Interview_150_Binary_Search | 0 | 100.00
17131727
| 0160 |[Intersection of Two Linked Lists](src/main/ts/g0101_0200/s0160_intersection_of_two_linked_lists/solution.ts)| 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) | 65 | 72.36
17141728
| 0155 |[Min Stack](src/main/ts/g0101_0200/s0155_min_stack/solution.ts)| 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, Top_Interview_150_Stack, Big_O_Time_O(1)_Space_O(N) | 5 | 99.10
17151729
| 0153 |[Find Minimum in Rotated Sorted Array](src/main/ts/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/solution.ts)| Medium | Top_100_Liked_Questions, Array, Binary_Search, Algorithm_II_Day_2_Binary_Search, Binary_Search_I_Day_12, Udemy_Binary_Search, Top_Interview_150_Binary_Search, Big_O_Time_O(log_N)_Space_O(log_N) | 0 | 100.00
17161730
| 0152 |[Maximum Product Subarray](src/main/ts/g0101_0200/s0152_maximum_product_subarray/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Dynamic_Programming_I_Day_6, Level_2_Day_13_Dynamic_Programming, Udemy_Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 0 | 100.00
1731+
| 0151 |[Reverse Words in a String](src/main/ts/g0101_0200/s0151_reverse_words_in_a_string/solution.ts)| Medium | String, Two_Pointers, Udemy_Strings, Top_Interview_150_Array/String | 0 | 100.00
1732+
| 0150 |[Evaluate Reverse Polish Notation](src/main/ts/g0101_0200/s0150_evaluate_reverse_polish_notation/solution.ts)| Medium | Top_Interview_Questions, Array, Math, Stack, Programming_Skills_II_Day_3, Top_Interview_150_Stack | 1 | 97.48
1733+
| 0149 |[Max Points on a Line](src/main/ts/g0101_0200/s0149_max_points_on_a_line/solution.ts)| Hard | Top_Interview_Questions, Array, Hash_Table, Math, Geometry, Algorithm_II_Day_21_Others, Top_Interview_150_Math | 9 | 97.18
17171734
| 0148 |[Sort List](src/main/ts/g0101_0200/s0148_sort_list/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Sorting, Two_Pointers, Linked_List, Divide_and_Conquer, Merge_Sort, Level_2_Day_4_Linked_List, Top_Interview_150_Divide_and_Conquer, Big_O_Time_O(log(N))_Space_O(log(N)) | 36 | 44.94
17181735
| 0146 |[LRU Cache](src/main/ts/g0101_0200/s0146_lru_cache/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Design, Linked_List, Doubly_Linked_List, Udemy_Linked_List, Top_Interview_150_Linked_List, Big_O_Time_O(1)_Space_O(capacity) | 97 | 81.52
17191736
| 0142 |[Linked List Cycle II](src/main/ts/g0101_0200/s0142_linked_list_cycle_ii/solution.ts)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Data_Structure_II_Day_10_Linked_List, Level_1_Day_4_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 51 | 76.99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
149\. Max Points on a Line
2+
3+
Hard
4+
5+
Given an array of `points` where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the **X-Y** plane, return _the maximum number of points that lie on the same straight line_.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg)
10+
11+
**Input:** points = [[1,1],[2,2],[3,3]]
12+
13+
**Output:** 3
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg)
18+
19+
**Input:** points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
20+
21+
**Output:** 4
22+
23+
**Constraints:**
24+
25+
* `1 <= points.length <= 300`
26+
* `points[i].length == 2`
27+
* <code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code>
28+
* All the `points` are **unique**.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// #Hard #Top_Interview_Questions #Array #Hash_Table #Math #Geometry #Algorithm_II_Day_21_Others
2+
// #Top_Interview_150_Math #2025_04_08_Time_9_ms_(97.18%)_Space_61.36_MB_(70.42%)
3+
4+
function maxPoints(points: number[][]): number {
5+
if (points.length <= 2) {
6+
return points.length
7+
}
8+
const map = new Map<number, number>()
9+
let result: number = 0
10+
for (let i = 0; i < points.length; i++) {
11+
const [x0, y0] = points[i]
12+
for (let j = i + 1; j < points.length; j++) {
13+
const [x1, y1] = points[j]
14+
let m: number
15+
if (x0 === x1) {
16+
m = Number.MAX_VALUE
17+
} else if (y0 === y1) {
18+
m = 0
19+
} else {
20+
m = (y0 - y1) / (x0 - x1)
21+
}
22+
const nextM: number = map.has(m) ? map.get(m) + 1 : 2
23+
map.set(m, nextM)
24+
result = Math.max(result, nextM)
25+
}
26+
map.clear()
27+
}
28+
return result
29+
}
30+
31+
export { maxPoints }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
150\. Evaluate Reverse Polish Notation
2+
3+
Medium
4+
5+
Evaluate the value of an arithmetic expression in [Reverse Polish Notation](http://en.wikipedia.org/wiki/Reverse_Polish_notation).
6+
7+
Valid operators are `+`, `-`, `*`, and `/`. Each operand may be an integer or another expression.
8+
9+
**Note** that division between two integers should truncate toward zero.
10+
11+
It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result, and there will not be any division by zero operation.
12+
13+
**Example 1:**
14+
15+
**Input:** tokens = ["2","1","+","3","\*"]
16+
17+
**Output:** 9
18+
19+
**Explanation:** ((2 + 1) \* 3) = 9
20+
21+
**Example 2:**
22+
23+
**Input:** tokens = ["4","13","5","/","+"]
24+
25+
**Output:** 6
26+
27+
**Explanation:** (4 + (13 / 5)) = 6
28+
29+
**Example 3:**
30+
31+
**Input:** tokens = ["10","6","9","3","+","-11","\*","/","\*","17","+","5","+"]
32+
33+
**Output:** 22
34+
35+
**Explanation:**
36+
37+
((10 \* (6 / ((9 + 3) \* -11))) + 17) + 5
38+
= ((10 \* (6 / (12 \* -11))) + 17) + 5
39+
= ((10 \* (6 / -132)) + 17) + 5
40+
= ((10 \* 0) + 17) + 5
41+
= (0 + 17) + 5
42+
= 17 + 5
43+
= 22
44+
45+
**Constraints:**
46+
47+
* <code>1 <= tokens.length <= 10<sup>4</sup></code>
48+
* `tokens[i]` is either an operator: `"+"`, `"-"`, `"*"`, or `"/"`, or an integer in the range `[-200, 200]`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// #Medium #Top_Interview_Questions #Array #Math #Stack #Programming_Skills_II_Day_3
2+
// #Top_Interview_150_Stack #2025_04_08_Time_1_ms_(97.48%)_Space_60.14_MB_(28.30%)
3+
4+
function evalRPN(tokens: string[]): number {
5+
const numberStack: number[] = []
6+
const isOperator = (val: string): boolean => val === '+' || val === '-' || val === '*' || val === '/'
7+
for (let token of tokens) {
8+
if (isOperator(token)) {
9+
const right = numberStack.pop() as number
10+
const left = numberStack.pop() as number
11+
12+
if (token === '+') numberStack.push(left + right)
13+
else if (token === '-') numberStack.push(left - right)
14+
else if (token === '*') numberStack.push(left * right)
15+
else if (token === '/') {
16+
const result = left / right
17+
numberStack.push(result < 0 ? Math.ceil(result) : Math.floor(result))
18+
}
19+
} else numberStack.push(+token)
20+
}
21+
return numberStack.pop() || 0
22+
}
23+
24+
export { evalRPN }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
151\. Reverse Words in a String
2+
3+
Medium
4+
5+
Given an input string `s`, reverse the order of the **words**.
6+
7+
A **word** is defined as a sequence of non-space characters. The **words** in `s` will be separated by at least one space.
8+
9+
Return _a string of the words in reverse order concatenated by a single space._
10+
11+
**Note** that `s` may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.
12+
13+
**Example 1:**
14+
15+
**Input:** s = "the sky is blue"
16+
17+
**Output:** "blue is sky the"
18+
19+
**Example 2:**
20+
21+
**Input:** s = " hello world "
22+
23+
**Output:** "world hello"
24+
25+
**Explanation:** Your reversed string should not contain leading or trailing spaces.
26+
27+
**Example 3:**
28+
29+
**Input:** s = "a good example"
30+
31+
**Output:** "example good a"
32+
33+
**Explanation:** You need to reduce multiple spaces between two words to a single space in the reversed string.
34+
35+
**Example 4:**
36+
37+
**Input:** s = " Bob Loves Alice "
38+
39+
**Output:** "Alice Loves Bob"
40+
41+
**Example 5:**
42+
43+
**Input:** s = "Alice does not even like bob"
44+
45+
**Output:** "bob like even not does Alice"
46+
47+
**Constraints:**
48+
49+
* <code>1 <= s.length <= 10<sup>4</sup></code>
50+
* `s` contains English letters (upper-case and lower-case), digits, and spaces `' '`.
51+
* There is **at least one** word in `s`.
52+
53+
**Follow-up:** If the string data type is mutable in your language, can you solve it **in-place** with `O(1)` extra space?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// #Medium #String #Two_Pointers #Udemy_Strings #Top_Interview_150_Array/String
2+
// #2025_04_08_Time_0_ms_(100.00%)_Space_57.70_MB_(48.75%)
3+
4+
function reverseWords(s: string): string {
5+
return s
6+
?.trim()
7+
?.replaceAll(/\s{2,}/g, ' ')
8+
?.split(' ')
9+
?.reverse()
10+
.join(' ')
11+
}
12+
13+
export { reverseWords }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
162\. Find Peak Element
2+
3+
Medium
4+
5+
A peak element is an element that is strictly greater than its neighbors.
6+
7+
Given an integer array `nums`, find a peak element, and return its index. If the array contains multiple peaks, return the index to **any of the peaks**.
8+
9+
You may imagine that `nums[-1] = nums[n] = -∞`.
10+
11+
You must write an algorithm that runs in `O(log n)` time.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [1,2,3,1]
16+
17+
**Output:** 2
18+
19+
**Explanation:** 3 is a peak element and your function should return the index number 2.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [1,2,1,3,5,6,4]
24+
25+
**Output:** 5
26+
27+
**Explanation:** Your function can return either index number 1 where the peak element is 2, or index number 5 where the peak element is 6.
28+
29+
**Constraints:**
30+
31+
* `1 <= nums.length <= 1000`
32+
* <code>-2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1</code>
33+
* `nums[i] != nums[i + 1]` for all valid `i`.

0 commit comments

Comments
 (0)