Skip to content

Commit d9907b1

Browse files
authored
Added tasks 58-63
1 parent ccf123f commit d9907b1

File tree

16 files changed

+402
-16
lines changed

16 files changed

+402
-16
lines changed

README.md

Lines changed: 34 additions & 16 deletions
Large diffs are not rendered by default.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# #Easy #String #Programming_Skills_II_Day_6 #Udemy_Arrays #Top_Interview_150_Array/String
2+
# #2025_09_13_Time_0_ms_(100.00%)_Space_12.78_MB_(7.53%)
3+
4+
class Solution:
5+
def lengthOfLastWord(self, s: str) -> int:
6+
length = 0
7+
for i in range(len(s) - 1, -1, -1):
8+
ch = s[i]
9+
if ch == ' ' and length > 0:
10+
break
11+
elif ch != ' ':
12+
length += 1
13+
return length
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import unittest
2+
from Solution0058 import Solution
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_lengthOfLastWord(self):
6+
self.assertEqual(Solution().lengthOfLastWord("Hello World"), 5)
7+
8+
def test_lengthOfLastWord2(self):
9+
self.assertEqual(Solution().lengthOfLastWord(" fly me to the moon "), 4)
10+
11+
def test_lengthOfLastWord3(self):
12+
self.assertEqual(Solution().lengthOfLastWord("luffy is still joyboy"), 6)
Lines changed: 37 additions & 0 deletions
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`.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# #Medium #Two_Pointers #Linked_List #Programming_Skills_II_Day_16 #Udemy_Linked_List
2+
# #Top_Interview_150_Linked_List #2025_09_13_Time_0_ms_(100.00%)_Space_12.50_MB_(74.08%)
3+
4+
from typing import Optional
5+
6+
class ListNode:
7+
def __init__(self, val=0, next=None):
8+
self.val = val
9+
self.next = next
10+
11+
class Solution:
12+
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
13+
if head is None or k == 0:
14+
return head
15+
tail = head
16+
# find the count and let tail points to last node
17+
count = 1
18+
while tail is not None and tail.next is not None:
19+
count += 1
20+
tail = tail.next
21+
# calculate number of times to rotate by count modulas
22+
times = k % count
23+
if times == 0:
24+
return head
25+
temp = head
26+
# iterate and go to the K+1 th node from the end or count - K - 1 node from start
27+
for i in range(1, count - times):
28+
if temp is not None:
29+
temp = temp.next
30+
new_head = None
31+
if temp is not None and tail is not None:
32+
new_head = temp.next
33+
temp.next = None
34+
tail.next = head
35+
return new_head
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import unittest
2+
from Solution0061 import Solution, ListNode
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_rotateRight(self):
6+
head = ListNode(1)
7+
head.next = ListNode(2)
8+
head.next.next = ListNode(3)
9+
head.next.next.next = ListNode(4)
10+
head.next.next.next.next = ListNode(5)
11+
result = Solution().rotateRight(head, 2)
12+
# Convert to list for comparison
13+
result_list = []
14+
current = result
15+
while current:
16+
result_list.append(current.val)
17+
current = current.next
18+
self.assertEqual(result_list, [4, 5, 1, 2, 3])
19+
20+
def test_rotateRight2(self):
21+
head = ListNode(0)
22+
head.next = ListNode(1)
23+
head.next.next = ListNode(2)
24+
result = Solution().rotateRight(head, 4)
25+
# Convert to list for comparison
26+
result_list = []
27+
current = result
28+
while current:
29+
result_list.append(current.val)
30+
current = current.next
31+
self.assertEqual(result_list, [2, 0, 1])
Lines changed: 27 additions & 0 deletions
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>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# #Medium #Array #Dynamic_Programming #Matrix #Dynamic_Programming_I_Day_15
2+
# #Top_Interview_150_Multidimensional_DP #2025_09_13_Time_0_ms_(100.00%)_Space_12.44_MB_(66.99%)
3+
4+
from typing import List
5+
6+
class Solution:
7+
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
8+
# if start point has obstacle, there's no path
9+
if obstacleGrid[0][0] == 1:
10+
return 0
11+
obstacleGrid[0][0] = 1
12+
m = len(obstacleGrid)
13+
n = len(obstacleGrid[0])
14+
for i in range(1, m):
15+
if obstacleGrid[i][0] == 1:
16+
obstacleGrid[i][0] = 0
17+
else:
18+
obstacleGrid[i][0] = obstacleGrid[i - 1][0]
19+
for j in range(1, n):
20+
if obstacleGrid[0][j] == 1:
21+
obstacleGrid[0][j] = 0
22+
else:
23+
obstacleGrid[0][j] = obstacleGrid[0][j - 1]
24+
for i in range(1, m):
25+
for j in range(1, n):
26+
if obstacleGrid[i][j] == 1:
27+
obstacleGrid[i][j] = 0
28+
else:
29+
obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1]
30+
return obstacleGrid[m - 1][n - 1]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import unittest
2+
from Solution0063 import Solution
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_uniquePathsWithObstacles(self):
6+
obstacle_grid = [
7+
[0, 0, 0],
8+
[0, 1, 0],
9+
[0, 0, 0]
10+
]
11+
self.assertEqual(Solution().uniquePathsWithObstacles(obstacle_grid), 2)
12+
13+
def test_uniquePathsWithObstacles2(self):
14+
obstacle_grid = [[0, 1], [0, 0]]
15+
self.assertEqual(Solution().uniquePathsWithObstacles(obstacle_grid), 1)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
63\. Unique Paths II
2+
3+
Medium
4+
5+
A robot is located at the top-left corner of a `m x n` grid (marked 'Start' in the diagram below).
6+
7+
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
8+
9+
Now consider if some obstacles are added to the grids. How many unique paths would there be?
10+
11+
An obstacle and space is marked as `1` and `0` respectively in the grid.
12+
13+
**Example 1:**
14+
15+
![](https://assets.leetcode.com/uploads/2020/11/04/robot1.jpg)
16+
17+
**Input:** obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
18+
19+
**Output:** 2
20+
21+
**Explanation:** There is one obstacle in the middle of the 3x3 grid above. There are two ways to reach the bottom-right corner: 1. Right -> Right -> Down -> Down 2. Down -> Down -> Right -> Right
22+
23+
**Example 2:**
24+
25+
![](https://assets.leetcode.com/uploads/2020/11/04/robot2.jpg)
26+
27+
**Input:** obstacleGrid = [[0,1],[0,0]]
28+
29+
**Output:** 1
30+
31+
**Constraints:**
32+
33+
* `m == obstacleGrid.length`
34+
* `n == obstacleGrid[i].length`
35+
* `1 <= m, n <= 100`
36+
* `obstacleGrid[i][j]` is `0` or `1`.

0 commit comments

Comments
 (0)