Skip to content

Commit 9a33707

Browse files
authored
Added tasks 57-66
1 parent 4ddc75e commit 9a33707

File tree

16 files changed

+421
-0
lines changed

16 files changed

+421
-0
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
287287
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
288288
|-|-|-|-|-|-
289289
| 0062 |[Unique Paths](src/main/ts/g0001_0100/s0062_unique_paths/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Combinatorics, Big_O_Time_O(m\*n)_Space_O(m\*n) | 0 | 100.00
290+
| 0063 |[Unique Paths II](src/main/ts/g0001_0100/s0063_unique_paths_ii/solution.ts)| Medium | Array, Dynamic_Programming, Matrix | 0 | 100.00
290291

291292
#### Day 16
292293

@@ -406,6 +407,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
406407

407408
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
408409
|-|-|-|-|-|-
410+
| 0066 |[Plus One](src/main/ts/g0001_0100/s0066_plus_one/solution.ts)| Easy | Top_Interview_Questions, Array, Math | 0 | 100.00
409411

410412
#### Day 4
411413

@@ -422,6 +424,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
422424
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
423425
|-|-|-|-|-|-
424426
| 0739 |[Daily Temperatures](src/main/ts/g0701_0800/s0739_daily_temperatures/solution.ts)| Medium | Top_100_Liked_Questions, Array, Stack, Monotonic_Stack, Big_O_Time_O(n)_Space_O(n) | 18 | 80.57
427+
| 0058 |[Length of Last Word](src/main/ts/g0001_0100/s0058_length_of_last_word/solution.ts)| Easy | String | 0 | 100.00
425428

426429
#### Day 7
427430

@@ -478,6 +481,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
478481

479482
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
480483
|-|-|-|-|-|-
484+
| 0061 |[Rotate List](src/main/ts/g0001_0100/s0061_rotate_list/solution.ts)| Medium | Two_Pointers, Linked_List | 0 | 100.00
481485

482486
#### Day 17
483487

@@ -820,6 +824,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
820824

821825
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
822826
|-|-|-|-|-|-
827+
| 0057 |[Insert Interval](src/main/ts/g0001_0100/s0057_insert_interval/solution.ts)| Medium | Array | 0 | 100.00
823828
| 0056 |[Merge Intervals](src/main/ts/g0001_0100/s0056_merge_intervals/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Big_O_Time_O(n_log_n)_Space_O(n) | 7 | 87.99
824829

825830
#### Day 18 Stack
@@ -875,9 +880,11 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
875880
| 0121 |[Best Time to Buy and Sell Stock](src/main/ts/g0101_0200/s0121_best_time_to_buy_and_sell_stock/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 1 | 96.44
876881
| 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
877882
| 0001 |[Two Sum](src/main/ts/g0001_0100/s0001_two_sum/solution.ts)| 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.70
883+
| 0058 |[Length of Last Word](src/main/ts/g0001_0100/s0058_length_of_last_word/solution.ts)| Easy | String | 0 | 100.00
878884
| 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, Big_O_Time_O(n)_Space_O(1) | 1 | 86.17
879885
| 0055 |[Jump Game](src/main/ts/g0001_0100/s0055_jump_game/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
880886
| 0075 |[Sort Colors](src/main/ts/g0001_0100/s0075_sort_colors/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Two_Pointers, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
887+
| 0066 |[Plus One](src/main/ts/g0001_0100/s0066_plus_one/solution.ts)| Easy | Top_Interview_Questions, Array, Math | 0 | 100.00
881888
| 0238 |[Product of Array Except Self](src/main/ts/g0201_0300/s0238_product_of_array_except_self/solution.ts)| Medium | Top_100_Liked_Questions, Array, Prefix_Sum, Big_O_Time_O(n^2)_Space_O(n) | 3 | 92.81
882889
| 0041 |[First Missing Positive](src/main/ts/g0001_0100/s0041_first_missing_positive/solution.ts)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Big_O_Time_O(n)_Space_O(n) | 1 | 99.11
883890
| 0239 |[Sliding Window Maximum](src/main/ts/g0201_0300/s0239_sliding_window_maximum/solution.ts)| Hard | Top_100_Liked_Questions, Array, Heap_Priority_Queue, Sliding_Window, Queue, Monotonic_Queue, Big_O_Time_O(n\*k)_Space_O(n+k) | 26 | 99.07
@@ -917,6 +924,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
917924
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
918925
|-|-|-|-|-|-
919926
| 0114 |[Flatten Binary Tree to Linked List](src/main/ts/g0101_0200/s0114_flatten_binary_tree_to_linked_list/solution.ts)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Stack, Linked_List, Big_O_Time_O(N)_Space_O(N) | 0 | 100.00
927+
| 0061 |[Rotate List](src/main/ts/g0001_0100/s0061_rotate_list/solution.ts)| Medium | Two_Pointers, Linked_List | 0 | 100.00
920928
| 0024 |[Swap Nodes in Pairs](src/main/ts/g0001_0100/s0024_swap_nodes_in_pairs/solution.ts)| Medium | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
921929
| 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, Big_O_Time_O(N)_Space_O(1) | 51 | 76.99
922930
| 0141 |[Linked List Cycle](src/main/ts/g0101_0200/s0141_linked_list_cycle/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Two_Pointers, Linked_List, Big_O_Time_O(N)_Space_O(1) | 47 | 89.37
@@ -1006,6 +1014,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
10061014
| 0042 |[Trapping Rain Water](src/main/ts/g0001_0100/s0042_trapping_rain_water/solution.ts)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Two_Pointers, Stack, Monotonic_Stack, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
10071015
| 0013 |[Roman to Integer](src/main/ts/g0001_0100/s0013_roman_to_integer/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Math | 3 | 94.58
10081016
| 0012 |[Integer to Roman](src/main/ts/g0001_0100/s0012_integer_to_roman/solution.ts)| Medium | String, Hash_Table, Math | 3 | 93.02
1017+
| 0058 |[Length of Last Word](src/main/ts/g0001_0100/s0058_length_of_last_word/solution.ts)| Easy | String | 0 | 100.00
10091018
| 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
10101019
| 0006 |[Zigzag Conversion](src/main/ts/g0001_0100/s0006_zigzag_conversion/solution.ts)| Medium | String | 2 | 99.08
10111020
| 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
@@ -1047,6 +1056,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
10471056
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
10481057
|-|-|-|-|-|-
10491058
| 0056 |[Merge Intervals](src/main/ts/g0001_0100/s0056_merge_intervals/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Big_O_Time_O(n_log_n)_Space_O(n) | 7 | 87.99
1059+
| 0057 |[Insert Interval](src/main/ts/g0001_0100/s0057_insert_interval/solution.ts)| Medium | Array | 0 | 100.00
10501060

10511061
#### Top Interview 150 Stack
10521062

@@ -1065,6 +1075,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
10651075
| 0138 |[Copy List with Random Pointer](src/main/ts/g0101_0200/s0138_copy_list_with_random_pointer/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Linked_List, Big_O_Time_O(N)_Space_O(N) | 49 | 72.42
10661076
| 0025 |[Reverse Nodes in k-Group](src/main/ts/g0001_0100/s0025_reverse_nodes_in_k_group/solution.ts)| Hard | Top_100_Liked_Questions, Linked_List, Recursion, Big_O_Time_O(n)_Space_O(k) | 0 | 100.00
10671077
| 0019 |[Remove Nth Node From End of List](src/main/ts/g0001_0100/s0019_remove_nth_node_from_end_of_list/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Two_Pointers, Linked_List, Big_O_Time_O(L)_Space_O(L) | 0 | 100.00
1078+
| 0061 |[Rotate List](src/main/ts/g0001_0100/s0061_rotate_list/solution.ts)| Medium | Two_Pointers, Linked_List | 0 | 100.00
10681079
| 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, Big_O_Time_O(1)_Space_O(capacity) | 97 | 81.52
10691080

10701081
#### Top Interview 150 Binary Tree General
@@ -1163,6 +1174,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
11631174
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11641175
|-|-|-|-|-|-
11651176
| 0009 |[Palindrome Number](src/main/ts/g0001_0100/s0009_palindrome_number/solution.ts)| Easy | Math | 3 | 99.14
1177+
| 0066 |[Plus One](src/main/ts/g0001_0100/s0066_plus_one/solution.ts)| Easy | Top_Interview_Questions, Array, Math | 0 | 100.00
11661178
| 0050 |[Pow(x, n)](src/main/ts/g0001_0100/s0050_powx_n/solution.ts)| Medium | Top_Interview_Questions, Math, Recursion | 0 | 100.00
11671179

11681180
#### Top Interview 150 1D DP
@@ -1180,6 +1192,7 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
11801192
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
11811193
|-|-|-|-|-|-
11821194
| 0064 |[Minimum Path Sum](src/main/ts/g0001_0100/s0064_minimum_path_sum/solution.ts)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 4 | 70.73
1195+
| 0063 |[Unique Paths II](src/main/ts/g0001_0100/s0063_unique_paths_ii/solution.ts)| Medium | Array, Dynamic_Programming, Matrix | 0 | 100.00
11831196
| 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
11841197
| 0072 |[Edit Distance](src/main/ts/g0001_0100/s0072_edit_distance/solution.ts)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Big_O_Time_O(n^2)_Space_O(n2) | 6 | 93.83
11851198
| 0221 |[Maximal Square](src/main/ts/g0201_0300/s0221_maximal_square/solution.ts)| Medium | Array, Dynamic_Programming, Matrix, Big_O_Time_O(m\*n)_Space_O(m\*n) | 18 | 59.02
@@ -1683,8 +1696,13 @@ TypeScript-based LeetCode algorithm problem solutions, regularly updated.
16831696
| 0073 |[Set Matrix Zeroes](src/main/ts/g0001_0100/s0073_set_matrix_zeroes/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Hash_Table, Matrix, Udemy_2D_Arrays/Matrix, Top_Interview_150_Matrix, Big_O_Time_O(m\*n)_Space_O(1) | 4 | 50.63
16841697
| 0072 |[Edit Distance](src/main/ts/g0001_0100/s0072_edit_distance/solution.ts)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming, Top_Interview_150_Multidimensional_DP, Big_O_Time_O(n^2)_Space_O(n2) | 6 | 93.83
16851698
| 0070 |[Climbing Stairs](src/main/ts/g0001_0100/s0070_climbing_stairs/solution.ts)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_2, Level_1_Day_10_Dynamic_Programming, Udemy_Dynamic_Programming, Top_Interview_150_1D_DP, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
1699+
| 0066 |[Plus One](src/main/ts/g0001_0100/s0066_plus_one/solution.ts)| Easy | Top_Interview_Questions, Array, Math, Programming_Skills_II_Day_3, Udemy_Arrays, Top_Interview_150_Math | 0 | 100.00
16861700
| 0064 |[Minimum Path Sum](src/main/ts/g0001_0100/s0064_minimum_path_sum/solution.ts)| Medium | Top_100_Liked_Questions, Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_16, Udemy_Dynamic_Programming, Top_Interview_150_Multidimensional_DP, Big_O_Time_O(m\*n)_Space_O(m\*n) | 4 | 70.73
1701+
| 0063 |[Unique Paths II](src/main/ts/g0001_0100/s0063_unique_paths_ii/solution.ts)| Medium | Array, Dynamic_Programming, Matrix, Dynamic_Programming_I_Day_15, Top_Interview_150_Multidimensional_DP | 0 | 100.00
16871702
| 0062 |[Unique Paths](src/main/ts/g0001_0100/s0062_unique_paths/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Combinatorics, Algorithm_II_Day_13_Dynamic_Programming, Dynamic_Programming_I_Day_15, Level_1_Day_11_Dynamic_Programming, Big_O_Time_O(m\*n)_Space_O(m\*n) | 0 | 100.00
1703+
| 0061 |[Rotate List](src/main/ts/g0001_0100/s0061_rotate_list/solution.ts)| Medium | Two_Pointers, Linked_List, Programming_Skills_II_Day_16, Udemy_Linked_List, Top_Interview_150_Linked_List | 0 | 100.00
1704+
| 0058 |[Length of Last Word](src/main/ts/g0001_0100/s0058_length_of_last_word/solution.ts)| Easy | String, Programming_Skills_II_Day_6, Udemy_Arrays, Top_Interview_150_Array/String | 0 | 100.00
1705+
| 0057 |[Insert Interval](src/main/ts/g0001_0100/s0057_insert_interval/solution.ts)| Medium | Array, Level_2_Day_17_Interval, Top_Interview_150_Intervals | 0 | 100.00
16881706
| 0056 |[Merge Intervals](src/main/ts/g0001_0100/s0056_merge_intervals/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Sorting, Data_Structure_II_Day_2_Array, Level_2_Day_17_Interval, Udemy_2D_Arrays/Matrix, Top_Interview_150_Intervals, Big_O_Time_O(n_log_n)_Space_O(n) | 7 | 87.99
16891707
| 0055 |[Jump Game](src/main/ts/g0001_0100/s0055_jump_game/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Algorithm_II_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_4, Udemy_Arrays, Top_Interview_150_Array/String, Big_O_Time_O(n)_Space_O(1) | 0 | 100.00
16901708
| 0054 |[Spiral Matrix](src/main/ts/g0001_0100/s0054_spiral_matrix/solution.ts)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Matrix, Simulation, Programming_Skills_II_Day_8, Level_2_Day_1_Implementation/Simulation, Udemy_2D_Arrays/Matrix, Top_Interview_150_Matrix | 0 | 100.00
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
57\. Insert Interval
2+
3+
Medium
4+
5+
You are given an array of non-overlapping intervals `intervals` where <code>intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> represent the start and the end of the <code>i<sup>th</sup></code> interval and `intervals` is sorted in ascending order by <code>start<sub>i</sub></code>. You are also given an interval `newInterval = [start, end]` that represents the start and end of another interval.
6+
7+
Insert `newInterval` into `intervals` such that `intervals` is still sorted in ascending order by <code>start<sub>i</sub></code> and `intervals` still does not have any overlapping intervals (merge overlapping intervals if necessary).
8+
9+
Return `intervals` _after the insertion_.
10+
11+
**Example 1:**
12+
13+
**Input:** intervals = [[1,3],[6,9]], newInterval = [2,5]
14+
15+
**Output:** [[1,5],[6,9]]
16+
17+
**Example 2:**
18+
19+
**Input:** intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
20+
21+
**Output:** [[1,2],[3,10],[12,16]]
22+
23+
**Explanation:** Because the new interval `[4,8]` overlaps with `[3,5],[6,7],[8,10]`.
24+
25+
**Example 3:**
26+
27+
**Input:** intervals = [], newInterval = [5,7]
28+
29+
**Output:** [[5,7]]
30+
31+
**Example 4:**
32+
33+
**Input:** intervals = [[1,5]], newInterval = [2,3]
34+
35+
**Output:** [[1,5]]
36+
37+
**Example 5:**
38+
39+
**Input:** intervals = [[1,5]], newInterval = [2,7]
40+
41+
**Output:** [[1,7]]
42+
43+
**Constraints:**
44+
45+
* <code>0 <= intervals.length <= 10<sup>4</sup></code>
46+
* `intervals[i].length == 2`
47+
* <code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>5</sup></code>
48+
* `intervals` is sorted by <code>start<sub>i</sub></code> in **ascending** order.
49+
* `newInterval.length == 2`
50+
* <code>0 <= start <= end <= 10<sup>5</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// #Medium #Array #Level_2_Day_17_Interval #Top_Interview_150_Intervals
2+
// #2025_04_02_Time_0_ms_(100.00%)_Space_58.54_MB_(66.77%)
3+
4+
function insert(intervals: number[][], newInterval: number[]): number[][] {
5+
const n = intervals.length
6+
let l = 0
7+
let r = n - 1
8+
while (l < n && newInterval[0] > intervals[l][1]) {
9+
l++
10+
}
11+
while (r >= 0 && newInterval[1] < intervals[r][0]) {
12+
r--
13+
}
14+
const res: number[][] = new Array(l + n - r).fill(0).map(() => new Array(2))
15+
for (let i = 0; i < l; i++) {
16+
res[i] = [...intervals[i]]
17+
}
18+
res[l] = [
19+
Math.min(newInterval[0], l === n ? newInterval[0] : intervals[l][0]),
20+
Math.max(newInterval[1], r === -1 ? newInterval[1] : intervals[r][1])
21+
]
22+
for (let i = l + 1, j = r + 1; j < n; i++, j++) {
23+
res[i] = intervals[j]
24+
}
25+
return res
26+
}
27+
28+
export { insert }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
58\. Length of Last Word
2+
3+
Easy
4+
5+
Given a string `s` consisting of some words separated by some number of spaces, return _the length of the **last** word in the string._
6+
7+
A **word** is a maximal substring consisting of non-space characters only.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "Hello World"
12+
13+
**Output:** 5
14+
15+
**Explanation:** The last word is "World" with length 5.
16+
17+
**Example 2:**
18+
19+
**Input:** s = " fly me to the moon "
20+
21+
**Output:** 4
22+
23+
**Explanation:** The last word is "moon" with length 4.
24+
25+
**Example 3:**
26+
27+
**Input:** s = "luffy is still joyboy"
28+
29+
**Output:** 6
30+
31+
**Explanation:** The last word is "joyboy" with length 6.
32+
33+
**Constraints:**
34+
35+
* <code>1 <= s.length <= 10<sup>4</sup></code>
36+
* `s` consists of only English letters and spaces `' '`.
37+
* There will be at least one word in `s`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// #Easy #String #Programming_Skills_II_Day_6 #Udemy_Arrays #Top_Interview_150_Array/String
2+
// #2025_04_02_Time_0_ms_(100.00%)_Space_56.32_MB_(7.86%)
3+
4+
function lengthOfLastWord(s: string): number {
5+
let len = 0
6+
for (let i = s.length - 1; i >= 0; i--) {
7+
const ch = s[i]
8+
if (ch === ' ' && len > 0) {
9+
break
10+
} else if (ch !== ' ') {
11+
len++
12+
}
13+
}
14+
return len
15+
}
16+
17+
export { lengthOfLastWord }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
61\. Rotate List
2+
3+
Medium
4+
5+
Given the `head` of a linked list, rotate the list to the right by `k` places.
6+
7+
**Example 1:**
8+
9+
![](https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg)
10+
11+
**Input:** head = [1,2,3,4,5], k = 2
12+
13+
**Output:** [4,5,1,2,3]
14+
15+
**Example 2:**
16+
17+
![](https://assets.leetcode.com/uploads/2020/11/13/roate2.jpg)
18+
19+
**Input:** head = [0,1,2], k = 4
20+
21+
**Output:** [2,0,1]
22+
23+
**Constraints:**
24+
25+
* The number of nodes in the list is in the range `[0, 500]`.
26+
* `-100 <= Node.val <= 100`
27+
* <code>0 <= k <= 2 * 10<sup>9</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// #Medium #Two_Pointers #Linked_List #Programming_Skills_II_Day_16 #Udemy_Linked_List
2+
// #Top_Interview_150_Linked_List #2025_04_02_Time_0_ms_(100.00%)_Space_58.23_MB_(55.89%)
3+
4+
/**
5+
* Definition for singly-linked list.
6+
* class ListNode {
7+
* val: number
8+
* next: ListNode | null
9+
* constructor(val?: number, next?: ListNode | null) {
10+
* this.val = (val===undefined ? 0 : val)
11+
* this.next = (next===undefined ? null : next)
12+
* }
13+
* }
14+
*/
15+
function rotateRight(head: ListNode | null, k: number): ListNode | null {
16+
if (!head || k === 0) {
17+
return head
18+
}
19+
let tail: ListNode | null = head
20+
let count = 1
21+
while (tail.next) {
22+
count++
23+
tail = tail.next
24+
}
25+
let times = k % count
26+
if (times === 0) {
27+
return head
28+
}
29+
let temp: ListNode | null = head
30+
for (let i = 1; i < count - times; i++) {
31+
temp = temp!.next
32+
}
33+
let newHead = temp!.next
34+
temp!.next = null
35+
tail!.next = head
36+
return newHead
37+
}
38+
39+
export { rotateRight }

0 commit comments

Comments
 (0)