Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
# #2025_07_22_Time_5_ms_(93.09%)_Space_17.85_MB_(70.00%)

class Solution:
def convert(self, s: str, numRows: int) -> str:
def convert(self, s: str, num_rows: int) -> str:
s_len = len(s)
if numRows == 1:
if num_rows == 1:
return s

max_dist = numRows * 2 - 2
max_dist = num_rows * 2 - 2
buf = []

for i in range(numRows):
for i in range(num_rows):
index = i
if i == 0 or i == numRows - 1:
if i == 0 or i == num_rows - 1:
while index < s_len:
buf.append(s[index])
index += max_dist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

class SolutionTest(unittest.TestCase):
def test_isPalindrome(self):
self.assertEqual(Solution().isPalindrome(121), True)
self.assertTrue(Solution().isPalindrome(121))

def test_isPalindrome2(self):
self.assertEqual(Solution().isPalindrome(-121), False)
self.assertFalse(Solution().isPalindrome(-121))

def test_isPalindrome3(self):
self.assertEqual(Solution().isPalindrome(10), False)
self.assertFalse(Solution().isPalindrome(10))
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

class SolutionTest(unittest.TestCase):
def test_isMatch(self):
self.assertEqual(Solution().isMatch("aa", "a"), False)
self.assertFalse(Solution().isMatch("aa", "a"))

def test_isMatch2(self):
self.assertEqual(Solution().isMatch("aa", "a*"), True)
self.assertTrue(Solution().isMatch("aa", "a*"))

def test_isMatch3(self):
self.assertEqual(Solution().isMatch("ab", ".*"), True)
self.assertTrue(Solution().isMatch("ab", ".*"))

def test_isMatch4(self):
self.assertEqual(Solution().isMatch("aab", "c*a*b"), True)
self.assertTrue(Solution().isMatch("aab", "c*a*b"))

def test_isMatch5(self):
self.assertEqual(Solution().isMatch("mississippi", "mis*is*p*."), False)
self.assertFalse(Solution().isMatch("mississippi", "mis*is*p*."))
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ def searchInsert(self, nums: List[int], target: int) -> int:

elif guess < target:
low = mid + 1
if nums[-1] == guess:
if nums[-1] == guess or nums[low] > target:
return mid + 1
elif nums[low] > target:
return mid + 1
else:
high = mid - 1

Expand Down
6 changes: 3 additions & 3 deletions src/main/python/g0001_0100/s0051_n_queens/Solution0051.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def helper(self, n: int, row: int, pos: List[bool], pos2: List[int], ans: List[L
def construct(self, n: int, pos: List[int], ans: List[List[str]]):
sol = []
for r in range(n):
queenRow = ['.'] * n
queenRow[pos[r]] = 'Q'
sol.append(''.join(queenRow))
queen_row = ['.'] * n
queen_row[pos[r]] = 'Q'
sol.append(''.join(queen_row))
ans.append(sol)
14 changes: 7 additions & 7 deletions src/main/python/g0001_0100/s0052_n_queens_ii/Solution0052.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ def totalNQueens(self, n: int) -> int:
row = [False] * n
col = [False] * n
diagonal = [False] * (2 * n - 1)
antiDiagonal = [False] * (2 * n - 1)
return self._totalNQueens(n, 0, row, col, diagonal, antiDiagonal)
anti_diagonal = [False] * (2 * n - 1)
return self._totalNQueens(n, 0, row, col, diagonal, anti_diagonal)

def _totalNQueens(self, n, r, row, col, diagonal, antiDiagonal):
def _totalNQueens(self, n, r, row, col, diagonal, anti_diagonal):
if r == n:
return 1
count = 0
for c in range(n):
if not row[r] and not col[c] and not diagonal[r + c] and not antiDiagonal[r - c + n - 1]:
row[r] = col[c] = diagonal[r + c] = antiDiagonal[r - c + n - 1] = True
count += self._totalNQueens(n, r + 1, row, col, diagonal, antiDiagonal)
row[r] = col[c] = diagonal[r + c] = antiDiagonal[r - c + n - 1] = False
if not row[r] and not col[c] and not diagonal[r + c] and not anti_diagonal[r - c + n - 1]:
row[r] = col[c] = diagonal[r + c] = anti_diagonal[r - c + n - 1] = True
count += self._totalNQueens(n, r + 1, row, col, diagonal, anti_diagonal)
row[r] = col[c] = diagonal[r + c] = anti_diagonal[r - c + n - 1] = False
return count
10 changes: 5 additions & 5 deletions src/main/python/g0001_0100/s0057_insert_interval/Solution0057.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
from typing import List

class Solution:
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
def insert(self, intervals: List[List[int]], new_interval: List[int]) -> List[List[int]]:
n = len(intervals)
l = 0
r = n - 1
while l < n and newInterval[0] > intervals[l][1]:
while l < n and new_interval[0] > intervals[l][1]:
l += 1
while r >= 0 and newInterval[1] < intervals[r][0]:
while r >= 0 and new_interval[1] < intervals[r][0]:
r -= 1
res = [[0, 0] for _ in range(l + n - r)]
for i in range(l):
res[i] = intervals[i][:]
res[l][0] = min(newInterval[0], newInterval[0] if l == n else intervals[l][0])
res[l][1] = max(newInterval[1], newInterval[1] if r == -1 else intervals[r][1])
res[l][0] = min(new_interval[0], new_interval[0] if l == n else intervals[l][0])
res[l][1] = max(new_interval[1], new_interval[1] if r == -1 else intervals[r][1])
for i in range(l + 1, l + n - r):
j = r + 1 + i - l - 1
res[i] = intervals[j][:]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
return head
temp = head
# iterate and go to the K+1 th node from the end or count - K - 1 node from start
for i in range(1, count - times):
for _ in range(1, count - times):
if temp is not None:
temp = temp.next
new_head = None
Expand Down
30 changes: 15 additions & 15 deletions src/main/python/g0001_0100/s0063_unique_paths_ii/Solution0063.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@
from typing import List

class Solution:
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
def uniquePathsWithObstacles(self, obstacle_grid: List[List[int]]) -> int:
# if start point has obstacle, there's no path
if obstacleGrid[0][0] == 1:
if obstacle_grid[0][0] == 1:
return 0
obstacleGrid[0][0] = 1
m = len(obstacleGrid)
n = len(obstacleGrid[0])
obstacle_grid[0][0] = 1
m = len(obstacle_grid)
n = len(obstacle_grid[0])
for i in range(1, m):
if obstacleGrid[i][0] == 1:
obstacleGrid[i][0] = 0
if obstacle_grid[i][0] == 1:
obstacle_grid[i][0] = 0
else:
obstacleGrid[i][0] = obstacleGrid[i - 1][0]
obstacle_grid[i][0] = obstacle_grid[i - 1][0]
for j in range(1, n):
if obstacleGrid[0][j] == 1:
obstacleGrid[0][j] = 0
if obstacle_grid[0][j] == 1:
obstacle_grid[0][j] = 0
else:
obstacleGrid[0][j] = obstacleGrid[0][j - 1]
obstacle_grid[0][j] = obstacle_grid[0][j - 1]
for i in range(1, m):
for j in range(1, n):
if obstacleGrid[i][j] == 1:
obstacleGrid[i][j] = 0
if obstacle_grid[i][j] == 1:
obstacle_grid[i][j] = 0
else:
obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1]
return obstacleGrid[m - 1][n - 1]
obstacle_grid[i][j] = obstacle_grid[i - 1][j] + obstacle_grid[i][j - 1]
return obstacle_grid[m - 1][n - 1]
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import List

class Solution:
def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
def fullJustify(self, words: List[str], max_width: int) -> List[str]:
# Trying to gauge the number of lines so the list doesn't need to resize
output = []
# Setting string capacity also
Expand All @@ -18,18 +18,18 @@ def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
# tracking line length + #words
num_words_on_line += 1
# checking if the next word causes an overflow
if line_total + num_words_on_line + len(words[i + 1]) > maxWidth:
if line_total + num_words_on_line + len(words[i + 1]) > max_width:
# if only one word fits on the line...
if num_words_on_line == 1:
# append word
sb.append(words[i])
# pad right with spaces
while line_total < maxWidth:
while line_total < max_width:
sb.append(' ')
line_total += 1
else:
# of extra spaces
extra_sp = (maxWidth - line_total) % (num_words_on_line - 1)
extra_sp = (max_width - line_total) % (num_words_on_line - 1)
# Creating the line
for j in range(start_word, start_word + num_words_on_line - 1):
# appending the word
Expand All @@ -39,7 +39,7 @@ def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
sb.append(' ')
extra_sp -= 1
# appending the rest of the required spaces
max_spaces = max(0, (maxWidth - line_total) // (num_words_on_line - 1))
max_spaces = max(0, (max_width - line_total) // (num_words_on_line - 1))
sb.append(' ' * max_spaces)
# appending the last word of the line
sb.append(words[start_word + num_words_on_line - 1])
Expand All @@ -57,11 +57,11 @@ def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
for i in range(start_word, len(words)):
line_total += len(words[i])
sb.append(words[i])
if line_total < maxWidth:
if line_total < max_width:
sb.append(' ')
line_total += 1
# padding right side with spaces
while line_total < maxWidth:
while line_total < max_width:
sb.append(' ')
line_total += 1
# add the final line to output list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ def solve(self, str1, i, str2, j, dp):

def minDistance(self, word1: str, word2: str) -> int:
m,n = len(word1), len(word2)
dp=[[-1]*(n+1) for i in range(m+1)]
dp=[[-1] * (n+1) for _ in range(m + 1)]
return self.solve(word1, m, word2, n, dp)
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@

class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
endRow = len(matrix)
endCol = len(matrix[0])
targetRow = 0
end_row = len(matrix)
end_col = len(matrix[0])
target_row = 0
result = False

for i in range(endRow):
if matrix[i][endCol - 1] >= target:
targetRow = i
for i in range(end_row):
if matrix[i][end_col - 1] >= target:
target_row = i
break

for i in range(endCol):
if matrix[targetRow][i] == target:
for i in range(end_col):
if matrix[target_row][i] == target:
result = True
break

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ def largestRectangleArea(self, heights: List[int]) -> int:
for i,h in enumerate(heights):
if h > heights[lefts[-1]]:
lefts.append(i)
elif h == heights[lefts[-1]]:
pass
elif h < heights[lefts[-1]]:
while lefts and h < heights[lefts[-1]]:
left = lefts.pop()
Expand Down
10 changes: 5 additions & 5 deletions src/main/python/g0101_0200/s0101_symmetric_tree/Solution0101.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def isSymmetric(self, root: Optional[TreeNode]) -> bool:
return True
return self.helper(root.left, root.right)

def helper(self, leftNode: TreeNode, rightNode: TreeNode) -> bool:
if leftNode is None or rightNode is None:
return leftNode is None and rightNode is None
if leftNode.val != rightNode.val:
def helper(self, left_node: TreeNode, right_node: TreeNode) -> bool:
if left_node is None or right_node is None:
return left_node is None and right_node is None
if left_node.val != right_node.val:
return False
return self.helper(leftNode.left, rightNode.right) and self.helper(leftNode.right, rightNode.left)
return self.helper(left_node.left, right_node.right) and self.helper(left_node.right, right_node.left)
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self):
def get(self, key: int) -> int:
return self.map[key]

def answer(self, preorder: List[int], inorder: List[int], start: int, end: int) -> TreeNode:
def answer(self, preorder: List[int], inorder: List[int], start: int, end: int) -> Optional[TreeNode]:
if start > end or self.j > len(preorder):
return None
value = preorder[self.j]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNo
post_index = [len(postorder) - 1]
return self._helper(inorder, postorder, in_index, post_index, float('inf'))

def _helper(self, inorder: List[int], postorder: List[int], index: List[int], p_index: List[int], target: int) -> Optional[TreeNode]:
def _helper(self, inorder: List[int], postorder: List[int], index: List[int], p_index: List[int], target: float) -> Optional[TreeNode]:
if index[0] < 0 or (index[0] < len(inorder) and inorder[index[0]] == target):
return None
root = TreeNode(postorder[p_index[0]])
Expand Down
6 changes: 3 additions & 3 deletions src/main/python/g0101_0200/s0112_path_sum/Solution0112.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ def __init__(self, val=0, left=None, right=None):
self.right = right

class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
def hasPathSum(self, root: Optional[TreeNode], target_sum: int) -> bool:
if root is None:
return False
if targetSum == root.val and root.left is None and root.right is None:
if target_sum == root.val and root.left is None and root.right is None:
return True
return self.hasPathSum(root.left, targetSum - root.val) or self.hasPathSum(root.right, targetSum - root.val)
return self.hasPathSum(root.left, target_sum - root.val) or self.hasPathSum(root.right, target_sum - root.val)
12 changes: 6 additions & 6 deletions src/main/python/g0101_0200/s0127_word_ladder/Solution0127.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
from typing import List, Set

class Solution:
def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
def ladderLength(self, begin_word: str, end_word: str, word_list: List[str]) -> int:
begin_set = set()
end_set = set()
word_set = set(wordList)
word_set = set(word_list)
visited = set()
if endWord not in wordList:
if end_word not in word_list:
return 0
length = 1
str_len = len(beginWord)
begin_set.add(beginWord)
end_set.add(endWord)
str_len = len(begin_word)
begin_set.add(begin_word)
end_set.add(end_word)
while begin_set and end_set:
if len(begin_set) > len(end_set):
begin_set, end_set = end_set, begin_set
Expand Down
4 changes: 2 additions & 2 deletions src/main/python/g0101_0200/s0139_word_break/Solution0139.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import List

class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
def wordBreak(self, s: str, word_dict: List[str]) -> bool:
n = len(s)
dp = [-1] * n

Expand All @@ -15,7 +15,7 @@ def solve(i, s):
return True
if dp[i] != -1:
return dp[i] == 1
for w in wordDict:
for w in word_dict:
sz = len(w)
if i - sz + 1 >= 0 and s[i - sz + 1 : i + 1] == w:
if solve(i - sz, s):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def maxPoints(self, points: List[List[int]]) -> int:
hashmap[m].add(i)
hashmap[m].add(j)
else:
hashmap[m] = set([i, j])
hashmap[m] = {i, j}
result = max(len(hashmap[m]), result)

return result