Skip to content

Commit

Permalink
feat(python): solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
hongbo-miao committed Apr 2, 2022
1 parent c626a8a commit cc7af24
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 1 deletion.
1 change: 0 additions & 1 deletion JavaScript/0054. Spiral Matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ const spiralOrder = (matrix) => {
y += dirs[dir][1];
res.push(matrix[x][y]);
}

steps[dir % 2]--;
dir = (dir + 1) % 4;
}
Expand Down
63 changes: 63 additions & 0 deletions Python/0054. Spiral Matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Given an m x n matrix, return all elements of the matrix in spiral order.
#
# Example 1:
#
# Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
# Output: [1,2,3,6,9,8,7,4,5]
#
# Example 2:
#
# Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
# Output: [1,2,3,4,8,12,11,10,9,5,6,7]
#
# Constraints:
#
# m == matrix.length
# n == matrix[i].length
# 1 <= m, n <= 10
# -100 <= matrix[i][j] <= 100


# Directions
# Similar
# 54. Spiral Matrix
# 59. Spiral Matrix II
#
# https://leetcode.com/problems/spiral-matrix/discuss/20573/A-concise-C++-implementation-based-on-Directions
#
# When traversing the matrix in the spiral order, at any time we follow one out of the following four directions:
# RIGHT DOWN LEFT UP. Suppose we are working on a 5 x 3 matrix as such:
# 0 1 2 3 4 5
# 6 7 8 9 10
# 11 12 13 14 15
#
# Imagine a cursor starts off at (0, -1), i.e. the position at '0', then we can achieve the spiral order by doing
# the following:
# 1. Go right 5 times
# 2. Go down 2 times
# 3. Go left 4 times
# 4. Go up 1 times.
# 5. Go right 3 times
# 6. Go down 0 times -> quit
#
# Notice that the directions we choose always follow the order 'right -> down -> left -> up', and for horizontal
# movements, the number of shifts follows: { 5, 4, 3 }, and vertical movements follows { 2, 1, 0 }.
# Thus, we can make use of a direction matrix that records the offset for all directions, then an array of two
# elements that stores the number of shifts for horizontal and vertical movements, respectively. This way, we really
# just need one for loop instead of four.
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
dirs = [(0, 1), (1, 0), (0, -1), (-1, 0)]
steps = [len(matrix[0]), len(matrix) - 1]
dir = 0
x = 0
y = -1
res = []
while steps[dir % 2] > 0:
for _ in range(steps[dir % 2]):
x += dirs[dir][0]
y += dirs[dir][1]
res.append(matrix[x][y])
steps[dir % 2] -= 1
dir = (dir + 1) % 4
return res
31 changes: 31 additions & 0 deletions Python/0055. Jump Game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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.
# Return true if you can reach the last index, or false otherwise.
#
# Example 1:
#
# Input: nums = [2,3,1,1,4]
# Output: true
# Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index.
#
# Example 2:
#
# Input: nums = [3,2,1,0,4]
# Output: false
# 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.
#
# Constraints:
#
# 1 <= nums.length <= 10^4
# 0 <= nums[i] <= 10^5


# Time O(n)
# Space O(1)
class Solution:
def canJump(self, nums: List[int]) -> bool:
max_reach = 0
for i, n in enumerate(nums):
if max_reach < i: # max_reach cannot reach position i
return False
max_reach = max(max_reach, i + n)
return True
54 changes: 54 additions & 0 deletions Python/0059. Spiral Matrix II.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.
#
# Example 1:
#
# Input: n = 3
# Output: [[1,2,3],[8,9,4],[7,6,5]]
#
# Example 2:
#
# Input: n = 1
# Output: [[1]]
#
# Constraints:
#
# 1 <= n <= 20


# Directions
# Similar
# 54. Spiral Matrix
# 59. Spiral Matrix II
#
# When traversing the matrix in the spiral order, at any time we follow one out of the following four directions:
# RIGHT DOWN LEFT UP. Suppose we are working on a 5 x 3 matrix as such:
# 0 1 2 3 4 5
# 6 7 8 9 10
# 11 12 13 14 15
#
# Imagine a cursor starts off at (0, -1), i.e. the position at '0', then we can achieve the spiral order by doing
# the following:
# 1. Go right 5 times
# 2. Go down 2 times
# 3. Go left 4 times
# 4. Go up 1 times.
# 5. Go right 3 times
# 6. Go down 0 times -> quit
class Solution:
def generateMatrix(self, n: int) -> List[List[int]]:
matrix = [[None] * n for _ in range(n)]
dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]]
steps = [n, n - 1]
dir = 0
x = 0
y = -1
num = 1
while steps[dir % 2] > 0:
for _ in range(steps[dir % 2]):
x += dirs[dir][0]
y += dirs[dir][1]
matrix[x][y] = num
num += 1
steps[dir % 2] -= 1
dir = (dir + 1) % 4
return matrix

0 comments on commit cc7af24

Please sign in to comment.