From 6ac3a45caff47bee7dcea9b92964c12825d6be44 Mon Sep 17 00:00:00 2001 From: ThanhNIT Date: Tue, 8 Apr 2025 20:10:20 +0700 Subject: [PATCH] Added tasks 149-167 --- README.md | 17 ++++++ .../s0149_max_points_on_a_line/readme.md | 28 ++++++++++ .../s0149_max_points_on_a_line/solution.ts | 31 +++++++++++ .../readme.md | 48 +++++++++++++++++ .../solution.ts | 24 +++++++++ .../s0151_reverse_words_in_a_string/readme.md | 53 +++++++++++++++++++ .../solution.ts | 13 +++++ .../s0162_find_peak_element/readme.md | 33 ++++++++++++ .../s0162_find_peak_element/solution.ts | 19 +++++++ .../readme.md | 41 ++++++++++++++ .../solution.ts | 21 ++++++++ .../solution.test.ts | 11 ++++ .../solution.test.ts | 15 ++++++ .../solution.test.ts | 15 ++++++ .../s0162_find_peak_element/solution.test.ts | 11 ++++ .../solution.test.ts | 15 ++++++ 16 files changed, 395 insertions(+) create mode 100644 src/main/ts/g0101_0200/s0149_max_points_on_a_line/readme.md create mode 100644 src/main/ts/g0101_0200/s0149_max_points_on_a_line/solution.ts create mode 100644 src/main/ts/g0101_0200/s0150_evaluate_reverse_polish_notation/readme.md create mode 100644 src/main/ts/g0101_0200/s0150_evaluate_reverse_polish_notation/solution.ts create mode 100644 src/main/ts/g0101_0200/s0151_reverse_words_in_a_string/readme.md create mode 100644 src/main/ts/g0101_0200/s0151_reverse_words_in_a_string/solution.ts create mode 100644 src/main/ts/g0101_0200/s0162_find_peak_element/readme.md create mode 100644 src/main/ts/g0101_0200/s0162_find_peak_element/solution.ts create mode 100644 src/main/ts/g0101_0200/s0167_two_sum_ii_input_array_is_sorted/readme.md create mode 100644 src/main/ts/g0101_0200/s0167_two_sum_ii_input_array_is_sorted/solution.ts create mode 100644 src/test/ts/g0101_0200/s0149_max_points_on_a_line/solution.test.ts create mode 100644 src/test/ts/g0101_0200/s0150_evaluate_reverse_polish_notation/solution.test.ts create mode 100644 src/test/ts/g0101_0200/s0151_reverse_words_in_a_string/solution.test.ts create mode 100644 src/test/ts/g0101_0200/s0162_find_peak_element/solution.test.ts create mode 100644 src/test/ts/g0101_0200/s0167_two_sum_ii_input_array_is_sorted/solution.test.ts diff --git a/README.md b/README.md index 4ba55e8..20ecd15 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 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 #### Day 8 @@ -160,6 +161,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 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 #### Day 13 @@ -410,6 +412,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 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 | 0066 |[Plus One](src/main/ts/g0001_0100/s0066_plus_one/solution.ts)| Easy | Top_Interview_Questions, Array, Math | 0 | 100.00 #### Day 4 @@ -871,6 +874,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | 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 | 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 | 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 +| 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 #### Udemy Binary Search @@ -1033,6 +1037,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | 0012 |[Integer to Roman](src/main/ts/g0001_0100/s0012_integer_to_roman/solution.ts)| Medium | String, Hash_Table, Math | 3 | 93.02 | 0058 |[Length of Last Word](src/main/ts/g0001_0100/s0058_length_of_last_word/solution.ts)| Easy | String | 0 | 100.00 | 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 +| 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 | 0006 |[Zigzag Conversion](src/main/ts/g0001_0100/s0006_zigzag_conversion/solution.ts)| Medium | String | 2 | 99.08 | 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 | 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. | | | | | | |-|-|-|-|-|- | 0125 |[Valid Palindrome](src/main/ts/g0101_0200/s0125_valid_palindrome/solution.ts)| Easy | Top_Interview_Questions, String, Two_Pointers | 0 | 100.00 +| 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 | 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 | 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 @@ -1084,6 +1090,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | 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 | 0071 |[Simplify Path](src/main/ts/g0001_0100/s0071_simplify_path/solution.ts)| Medium | String, Stack | 0 | 100.00 | 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 +| 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 #### Top Interview 150 Linked List @@ -1182,6 +1189,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. |-|-|-|-|-|- | 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 | 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 +| 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 | 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 | 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 | 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. | 0066 |[Plus One](src/main/ts/g0001_0100/s0066_plus_one/solution.ts)| Easy | Top_Interview_Questions, Array, Math | 0 | 100.00 | 0069 |[Sqrt(x)](src/main/ts/g0001_0100/s0069_sqrtx/solution.ts)| Hard | Array, String, Simulation | 0 | 100.00 | 0050 |[Pow(x, n)](src/main/ts/g0001_0100/s0050_powx_n/solution.ts)| Medium | Top_Interview_Questions, Math, Recursion | 0 | 100.00 +| 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 #### Top Interview 150 1D DP @@ -1475,6 +1484,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- | 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 +| 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 #### Day 4 Two Pointers @@ -1556,6 +1566,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- | 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 +| 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 #### Day 3 Two Pointers @@ -1671,6 +1682,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | | | | | | |-|-|-|-|-|- +| 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 ## Algorithms @@ -1710,10 +1722,15 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated. | 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 | 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 | 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 +| 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 +| 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 | 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 | 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 | 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 | 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 +| 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 +| 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 +| 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 | 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 | 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 | 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 diff --git a/src/main/ts/g0101_0200/s0149_max_points_on_a_line/readme.md b/src/main/ts/g0101_0200/s0149_max_points_on_a_line/readme.md new file mode 100644 index 0000000..99b2528 --- /dev/null +++ b/src/main/ts/g0101_0200/s0149_max_points_on_a_line/readme.md @@ -0,0 +1,28 @@ +149\. Max Points on a Line + +Hard + +Given an array of `points` where points[i] = [xi, yi] represents a point on the **X-Y** plane, return _the maximum number of points that lie on the same straight line_. + +**Example 1:** + +![](https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg) + +**Input:** points = [[1,1],[2,2],[3,3]] + +**Output:** 3 + +**Example 2:** + +![](https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg) + +**Input:** points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]] + +**Output:** 4 + +**Constraints:** + +* `1 <= points.length <= 300` +* `points[i].length == 2` +* -104 <= xi, yi <= 104 +* All the `points` are **unique**. diff --git a/src/main/ts/g0101_0200/s0149_max_points_on_a_line/solution.ts b/src/main/ts/g0101_0200/s0149_max_points_on_a_line/solution.ts new file mode 100644 index 0000000..ce5d847 --- /dev/null +++ b/src/main/ts/g0101_0200/s0149_max_points_on_a_line/solution.ts @@ -0,0 +1,31 @@ +// #Hard #Top_Interview_Questions #Array #Hash_Table #Math #Geometry #Algorithm_II_Day_21_Others +// #Top_Interview_150_Math #2025_04_08_Time_9_ms_(97.18%)_Space_61.36_MB_(70.42%) + +function maxPoints(points: number[][]): number { + if (points.length <= 2) { + return points.length + } + const map = new Map() + let result: number = 0 + for (let i = 0; i < points.length; i++) { + const [x0, y0] = points[i] + for (let j = i + 1; j < points.length; j++) { + const [x1, y1] = points[j] + let m: number + if (x0 === x1) { + m = Number.MAX_VALUE + } else if (y0 === y1) { + m = 0 + } else { + m = (y0 - y1) / (x0 - x1) + } + const nextM: number = map.has(m) ? map.get(m) + 1 : 2 + map.set(m, nextM) + result = Math.max(result, nextM) + } + map.clear() + } + return result +} + +export { maxPoints } diff --git a/src/main/ts/g0101_0200/s0150_evaluate_reverse_polish_notation/readme.md b/src/main/ts/g0101_0200/s0150_evaluate_reverse_polish_notation/readme.md new file mode 100644 index 0000000..1369eac --- /dev/null +++ b/src/main/ts/g0101_0200/s0150_evaluate_reverse_polish_notation/readme.md @@ -0,0 +1,48 @@ +150\. Evaluate Reverse Polish Notation + +Medium + +Evaluate the value of an arithmetic expression in [Reverse Polish Notation](http://en.wikipedia.org/wiki/Reverse_Polish_notation). + +Valid operators are `+`, `-`, `*`, and `/`. Each operand may be an integer or another expression. + +**Note** that division between two integers should truncate toward zero. + +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. + +**Example 1:** + +**Input:** tokens = ["2","1","+","3","\*"] + +**Output:** 9 + +**Explanation:** ((2 + 1) \* 3) = 9 + +**Example 2:** + +**Input:** tokens = ["4","13","5","/","+"] + +**Output:** 6 + +**Explanation:** (4 + (13 / 5)) = 6 + +**Example 3:** + +**Input:** tokens = ["10","6","9","3","+","-11","\*","/","\*","17","+","5","+"] + +**Output:** 22 + +**Explanation:** + + ((10 \* (6 / ((9 + 3) \* -11))) + 17) + 5 + = ((10 \* (6 / (12 \* -11))) + 17) + 5 + = ((10 \* (6 / -132)) + 17) + 5 + = ((10 \* 0) + 17) + 5 + = (0 + 17) + 5 + = 17 + 5 + = 22 + +**Constraints:** + +* 1 <= tokens.length <= 104 +* `tokens[i]` is either an operator: `"+"`, `"-"`, `"*"`, or `"/"`, or an integer in the range `[-200, 200]`. diff --git a/src/main/ts/g0101_0200/s0150_evaluate_reverse_polish_notation/solution.ts b/src/main/ts/g0101_0200/s0150_evaluate_reverse_polish_notation/solution.ts new file mode 100644 index 0000000..1e2ceda --- /dev/null +++ b/src/main/ts/g0101_0200/s0150_evaluate_reverse_polish_notation/solution.ts @@ -0,0 +1,24 @@ +// #Medium #Top_Interview_Questions #Array #Math #Stack #Programming_Skills_II_Day_3 +// #Top_Interview_150_Stack #2025_04_08_Time_1_ms_(97.48%)_Space_60.14_MB_(28.30%) + +function evalRPN(tokens: string[]): number { + const numberStack: number[] = [] + const isOperator = (val: string): boolean => val === '+' || val === '-' || val === '*' || val === '/' + for (let token of tokens) { + if (isOperator(token)) { + const right = numberStack.pop() as number + const left = numberStack.pop() as number + + if (token === '+') numberStack.push(left + right) + else if (token === '-') numberStack.push(left - right) + else if (token === '*') numberStack.push(left * right) + else if (token === '/') { + const result = left / right + numberStack.push(result < 0 ? Math.ceil(result) : Math.floor(result)) + } + } else numberStack.push(+token) + } + return numberStack.pop() || 0 +} + +export { evalRPN } diff --git a/src/main/ts/g0101_0200/s0151_reverse_words_in_a_string/readme.md b/src/main/ts/g0101_0200/s0151_reverse_words_in_a_string/readme.md new file mode 100644 index 0000000..1f89300 --- /dev/null +++ b/src/main/ts/g0101_0200/s0151_reverse_words_in_a_string/readme.md @@ -0,0 +1,53 @@ +151\. Reverse Words in a String + +Medium + +Given an input string `s`, reverse the order of the **words**. + +A **word** is defined as a sequence of non-space characters. The **words** in `s` will be separated by at least one space. + +Return _a string of the words in reverse order concatenated by a single space._ + +**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. + +**Example 1:** + +**Input:** s = "the sky is blue" + +**Output:** "blue is sky the" + +**Example 2:** + +**Input:** s = " hello world " + +**Output:** "world hello" + +**Explanation:** Your reversed string should not contain leading or trailing spaces. + +**Example 3:** + +**Input:** s = "a good example" + +**Output:** "example good a" + +**Explanation:** You need to reduce multiple spaces between two words to a single space in the reversed string. + +**Example 4:** + +**Input:** s = " Bob Loves Alice " + +**Output:** "Alice Loves Bob" + +**Example 5:** + +**Input:** s = "Alice does not even like bob" + +**Output:** "bob like even not does Alice" + +**Constraints:** + +* 1 <= s.length <= 104 +* `s` contains English letters (upper-case and lower-case), digits, and spaces `' '`. +* There is **at least one** word in `s`. + +**Follow-up:** If the string data type is mutable in your language, can you solve it **in-place** with `O(1)` extra space? diff --git a/src/main/ts/g0101_0200/s0151_reverse_words_in_a_string/solution.ts b/src/main/ts/g0101_0200/s0151_reverse_words_in_a_string/solution.ts new file mode 100644 index 0000000..8c4215f --- /dev/null +++ b/src/main/ts/g0101_0200/s0151_reverse_words_in_a_string/solution.ts @@ -0,0 +1,13 @@ +// #Medium #String #Two_Pointers #Udemy_Strings #Top_Interview_150_Array/String +// #2025_04_08_Time_0_ms_(100.00%)_Space_57.70_MB_(48.75%) + +function reverseWords(s: string): string { + return s + ?.trim() + ?.replaceAll(/\s{2,}/g, ' ') + ?.split(' ') + ?.reverse() + .join(' ') +} + +export { reverseWords } diff --git a/src/main/ts/g0101_0200/s0162_find_peak_element/readme.md b/src/main/ts/g0101_0200/s0162_find_peak_element/readme.md new file mode 100644 index 0000000..abe70d7 --- /dev/null +++ b/src/main/ts/g0101_0200/s0162_find_peak_element/readme.md @@ -0,0 +1,33 @@ +162\. Find Peak Element + +Medium + +A peak element is an element that is strictly greater than its neighbors. + +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**. + +You may imagine that `nums[-1] = nums[n] = -∞`. + +You must write an algorithm that runs in `O(log n)` time. + +**Example 1:** + +**Input:** nums = [1,2,3,1] + +**Output:** 2 + +**Explanation:** 3 is a peak element and your function should return the index number 2. + +**Example 2:** + +**Input:** nums = [1,2,1,3,5,6,4] + +**Output:** 5 + +**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. + +**Constraints:** + +* `1 <= nums.length <= 1000` +* -231 <= nums[i] <= 231 - 1 +* `nums[i] != nums[i + 1]` for all valid `i`. diff --git a/src/main/ts/g0101_0200/s0162_find_peak_element/solution.ts b/src/main/ts/g0101_0200/s0162_find_peak_element/solution.ts new file mode 100644 index 0000000..d79d342 --- /dev/null +++ b/src/main/ts/g0101_0200/s0162_find_peak_element/solution.ts @@ -0,0 +1,19 @@ +// #Medium #Top_Interview_Questions #Array #Binary_Search #Algorithm_II_Day_2_Binary_Search +// #Binary_Search_II_Day_12 #Top_Interview_150_Binary_Search +// #2025_04_08_Time_0_ms_(100.00%)_Space_55.40_MB_(60.85%) + +function findPeakElement(nums: number[]): number { + let start = 0 + let end = nums.length - 1 + while (start < end) { + const mid = start + Math.floor((end - start) / 2) + if (nums[mid + 1] > nums[mid]) { + start = mid + 1 + } else { + end = mid + } + } + return start +} + +export { findPeakElement } diff --git a/src/main/ts/g0101_0200/s0167_two_sum_ii_input_array_is_sorted/readme.md b/src/main/ts/g0101_0200/s0167_two_sum_ii_input_array_is_sorted/readme.md new file mode 100644 index 0000000..238d405 --- /dev/null +++ b/src/main/ts/g0101_0200/s0167_two_sum_ii_input_array_is_sorted/readme.md @@ -0,0 +1,41 @@ +167\. Two Sum II - Input Array Is Sorted + +Easy + +Given a **1-indexed** array of integers `numbers` that is already **_sorted in non-decreasing order_**, find two numbers such that they add up to a specific `target` number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length. + +Return _the indices of the two numbers,_ index1 _and_ index2_, **added by one** as an integer array_ [index1, index2] _of length 2._ + +The tests are generated such that there is **exactly one solution**. You **may not** use the same element twice. + +**Example 1:** + +**Input:** numbers = [2,7,11,15], target = 9 + +**Output:** [1,2] + +**Explanation:** The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2]. + +**Example 2:** + +**Input:** numbers = [2,3,4], target = 6 + +**Output:** [1,3] + +**Explanation:** The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3]. + +**Example 3:** + +**Input:** numbers = [\-1,0], target = -1 + +**Output:** [1,2] + +**Explanation:** The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2]. + +**Constraints:** + +* 2 <= numbers.length <= 3 * 104 +* `-1000 <= numbers[i] <= 1000` +* `numbers` is sorted in **non-decreasing order**. +* `-1000 <= target <= 1000` +* The tests are generated such that there is **exactly one solution**. diff --git a/src/main/ts/g0101_0200/s0167_two_sum_ii_input_array_is_sorted/solution.ts b/src/main/ts/g0101_0200/s0167_two_sum_ii_input_array_is_sorted/solution.ts new file mode 100644 index 0000000..ee66d03 --- /dev/null +++ b/src/main/ts/g0101_0200/s0167_two_sum_ii_input_array_is_sorted/solution.ts @@ -0,0 +1,21 @@ +// #Medium #Array #Binary_Search #Two_Pointers #Algorithm_I_Day_3_Two_Pointers +// #Binary_Search_I_Day_7 #Top_Interview_150_Two_Pointers +// #2025_04_08_Time_0_ms_(100.00%)_Space_56.49_MB_(68.78%) + +function twoSum(numbers: number[], target: number): number[] { + let i = 0 + let j = numbers.length - 1 + while (i < j) { + const sum = numbers[i] + numbers[j] + if (sum === target) { + return [i + 1, j + 1] + } else if (sum < target) { + i++ + } else { + j-- + } + } + return [] +} + +export { twoSum } diff --git a/src/test/ts/g0101_0200/s0149_max_points_on_a_line/solution.test.ts b/src/test/ts/g0101_0200/s0149_max_points_on_a_line/solution.test.ts new file mode 100644 index 0000000..84b5c45 --- /dev/null +++ b/src/test/ts/g0101_0200/s0149_max_points_on_a_line/solution.test.ts @@ -0,0 +1,11 @@ +// tslint:disable:no-magic-numbers +import { maxPoints } from 'src/main/ts/g0101_0200/s0149_max_points_on_a_line/solution' +import { expect, test } from 'vitest' + +test('maxPoints', () => { + expect(maxPoints([[1,1],[2,2],[3,3]])).toEqual(3) +}) + +test('maxPoints2', () => { + expect(maxPoints([[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]])).toEqual(4) +}) diff --git a/src/test/ts/g0101_0200/s0150_evaluate_reverse_polish_notation/solution.test.ts b/src/test/ts/g0101_0200/s0150_evaluate_reverse_polish_notation/solution.test.ts new file mode 100644 index 0000000..2a4048f --- /dev/null +++ b/src/test/ts/g0101_0200/s0150_evaluate_reverse_polish_notation/solution.test.ts @@ -0,0 +1,15 @@ +// tslint:disable:no-magic-numbers +import { evalRPN } from 'src/main/ts/g0101_0200/s0150_evaluate_reverse_polish_notation/solution' +import { expect, test } from 'vitest' + +test('evalRPN', () => { + expect(evalRPN(['2', '1', '+', '3', '*'])).toEqual(9) +}) + +test('evalRPN2', () => { + expect(evalRPN(['4', '13', '5', '/', '+'])).toEqual(6) +}) + +test('evalRPN3', () => { + expect(evalRPN(['10', '6', '9', '3', '+', '-11', '*', '/', '*', '17', '+', '5', '+'])).toEqual(22) +}) diff --git a/src/test/ts/g0101_0200/s0151_reverse_words_in_a_string/solution.test.ts b/src/test/ts/g0101_0200/s0151_reverse_words_in_a_string/solution.test.ts new file mode 100644 index 0000000..5477310 --- /dev/null +++ b/src/test/ts/g0101_0200/s0151_reverse_words_in_a_string/solution.test.ts @@ -0,0 +1,15 @@ +// tslint:disable:no-magic-numbers +import { reverseWords } from 'src/main/ts/g0101_0200/s0151_reverse_words_in_a_string/solution' +import { expect, test } from 'vitest' + +test('reverseWords', () => { + expect(reverseWords('the sky is blue')).toEqual('blue is sky the') +}) + +test('reverseWords2', () => { + expect(reverseWords(' hello world ')).toEqual('world hello') +}) + +test('reverseWords3', () => { + expect(reverseWords('a good example')).toEqual('example good a') +}) diff --git a/src/test/ts/g0101_0200/s0162_find_peak_element/solution.test.ts b/src/test/ts/g0101_0200/s0162_find_peak_element/solution.test.ts new file mode 100644 index 0000000..b6fedd9 --- /dev/null +++ b/src/test/ts/g0101_0200/s0162_find_peak_element/solution.test.ts @@ -0,0 +1,11 @@ +// tslint:disable:no-magic-numbers +import { findPeakElement } from 'src/main/ts/g0101_0200/s0162_find_peak_element/solution' +import { expect, test } from 'vitest' + +test('findPeakElement', () => { + expect(findPeakElement([1,2,3,1])).toEqual(2) +}) + +test('findPeakElement2', () => { + expect(findPeakElement([1,2,1,3,5,6,4])).toEqual(5) +}) diff --git a/src/test/ts/g0101_0200/s0167_two_sum_ii_input_array_is_sorted/solution.test.ts b/src/test/ts/g0101_0200/s0167_two_sum_ii_input_array_is_sorted/solution.test.ts new file mode 100644 index 0000000..58aea70 --- /dev/null +++ b/src/test/ts/g0101_0200/s0167_two_sum_ii_input_array_is_sorted/solution.test.ts @@ -0,0 +1,15 @@ +// tslint:disable:no-magic-numbers +import { twoSum } from 'src/main/ts/g0101_0200/s0167_two_sum_ii_input_array_is_sorted/solution' +import { expect, test } from 'vitest' + +test('twoSum', () => { + expect(twoSum([2, 7, 11, 15], 9)).toEqual([1, 2]) +}) + +test('twoSum2', () => { + expect(twoSum([2, 3, 4], 6)).toEqual([1, 3]) +}) + +test('twoSum3', () => { + expect(twoSum([-1, 0], -1)).toEqual([1, 2]) +})