Skip to content

Commit cc7af24

Browse files
committed
feat(python): solutions
1 parent c626a8a commit cc7af24

File tree

4 files changed

+148
-1
lines changed

4 files changed

+148
-1
lines changed

JavaScript/0054. Spiral Matrix.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ const spiralOrder = (matrix) => {
6868
y += dirs[dir][1];
6969
res.push(matrix[x][y]);
7070
}
71-
7271
steps[dir % 2]--;
7372
dir = (dir + 1) % 4;
7473
}

Python/0054. Spiral Matrix.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Given an m x n matrix, return all elements of the matrix in spiral order.
2+
#
3+
# Example 1:
4+
#
5+
# Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
6+
# Output: [1,2,3,6,9,8,7,4,5]
7+
#
8+
# Example 2:
9+
#
10+
# Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
11+
# Output: [1,2,3,4,8,12,11,10,9,5,6,7]
12+
#
13+
# Constraints:
14+
#
15+
# m == matrix.length
16+
# n == matrix[i].length
17+
# 1 <= m, n <= 10
18+
# -100 <= matrix[i][j] <= 100
19+
20+
21+
# Directions
22+
# Similar
23+
# 54. Spiral Matrix
24+
# 59. Spiral Matrix II
25+
#
26+
# https://leetcode.com/problems/spiral-matrix/discuss/20573/A-concise-C++-implementation-based-on-Directions
27+
#
28+
# When traversing the matrix in the spiral order, at any time we follow one out of the following four directions:
29+
# RIGHT DOWN LEFT UP. Suppose we are working on a 5 x 3 matrix as such:
30+
# 0 1 2 3 4 5
31+
# 6 7 8 9 10
32+
# 11 12 13 14 15
33+
#
34+
# Imagine a cursor starts off at (0, -1), i.e. the position at '0', then we can achieve the spiral order by doing
35+
# the following:
36+
# 1. Go right 5 times
37+
# 2. Go down 2 times
38+
# 3. Go left 4 times
39+
# 4. Go up 1 times.
40+
# 5. Go right 3 times
41+
# 6. Go down 0 times -> quit
42+
#
43+
# Notice that the directions we choose always follow the order 'right -> down -> left -> up', and for horizontal
44+
# movements, the number of shifts follows: { 5, 4, 3 }, and vertical movements follows { 2, 1, 0 }.
45+
# Thus, we can make use of a direction matrix that records the offset for all directions, then an array of two
46+
# elements that stores the number of shifts for horizontal and vertical movements, respectively. This way, we really
47+
# just need one for loop instead of four.
48+
class Solution:
49+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
50+
dirs = [(0, 1), (1, 0), (0, -1), (-1, 0)]
51+
steps = [len(matrix[0]), len(matrix) - 1]
52+
dir = 0
53+
x = 0
54+
y = -1
55+
res = []
56+
while steps[dir % 2] > 0:
57+
for _ in range(steps[dir % 2]):
58+
x += dirs[dir][0]
59+
y += dirs[dir][1]
60+
res.append(matrix[x][y])
61+
steps[dir % 2] -= 1
62+
dir = (dir + 1) % 4
63+
return res

Python/0055. Jump Game.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.
2+
# Return true if you can reach the last index, or false otherwise.
3+
#
4+
# Example 1:
5+
#
6+
# Input: nums = [2,3,1,1,4]
7+
# Output: true
8+
# Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
9+
#
10+
# Example 2:
11+
#
12+
# Input: nums = [3,2,1,0,4]
13+
# Output: false
14+
# Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index.
15+
#
16+
# Constraints:
17+
#
18+
# 1 <= nums.length <= 10^4
19+
# 0 <= nums[i] <= 10^5
20+
21+
22+
# Time O(n)
23+
# Space O(1)
24+
class Solution:
25+
def canJump(self, nums: List[int]) -> bool:
26+
max_reach = 0
27+
for i, n in enumerate(nums):
28+
if max_reach < i: # max_reach cannot reach position i
29+
return False
30+
max_reach = max(max_reach, i + n)
31+
return True

Python/0059. Spiral Matrix II.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.
2+
#
3+
# Example 1:
4+
#
5+
# Input: n = 3
6+
# Output: [[1,2,3],[8,9,4],[7,6,5]]
7+
#
8+
# Example 2:
9+
#
10+
# Input: n = 1
11+
# Output: [[1]]
12+
#
13+
# Constraints:
14+
#
15+
# 1 <= n <= 20
16+
17+
18+
# Directions
19+
# Similar
20+
# 54. Spiral Matrix
21+
# 59. Spiral Matrix II
22+
#
23+
# When traversing the matrix in the spiral order, at any time we follow one out of the following four directions:
24+
# RIGHT DOWN LEFT UP. Suppose we are working on a 5 x 3 matrix as such:
25+
# 0 1 2 3 4 5
26+
# 6 7 8 9 10
27+
# 11 12 13 14 15
28+
#
29+
# Imagine a cursor starts off at (0, -1), i.e. the position at '0', then we can achieve the spiral order by doing
30+
# the following:
31+
# 1. Go right 5 times
32+
# 2. Go down 2 times
33+
# 3. Go left 4 times
34+
# 4. Go up 1 times.
35+
# 5. Go right 3 times
36+
# 6. Go down 0 times -> quit
37+
class Solution:
38+
def generateMatrix(self, n: int) -> List[List[int]]:
39+
matrix = [[None] * n for _ in range(n)]
40+
dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]]
41+
steps = [n, n - 1]
42+
dir = 0
43+
x = 0
44+
y = -1
45+
num = 1
46+
while steps[dir % 2] > 0:
47+
for _ in range(steps[dir % 2]):
48+
x += dirs[dir][0]
49+
y += dirs[dir][1]
50+
matrix[x][y] = num
51+
num += 1
52+
steps[dir % 2] -= 1
53+
dir = (dir + 1) % 4
54+
return matrix

0 commit comments

Comments
 (0)