Merge branch 'main' of git.bumpsoo.dev:bumpsoo/leetpycode

This commit is contained in:
bumpsoo 2024-11-02 22:58:22 +09:00
commit d74f67ea6b
2 changed files with 45 additions and 0 deletions

View file

@ -0,0 +1,17 @@
# https://leetcode.com/problems/delete-characters-to-make-fancy-string
class Solution:
def makeFancyString(self, s: str) -> str:
l = 0
old = None
result = []
for ch in s:
if old == None or old != ch:
old = ch
l = 1
result.append(old)
elif old == ch and l <= 2:
result.append(old)
l += 1
return ''.join(result)

View file

@ -0,0 +1,28 @@
# https://leetcode.com/problems/maximum-number-of-moves-in-a-grid
from typing import List
DIRS = [[-1, 1], [0, 1], [1, 1]]
class Solution:
def maxMoves(self, grid: List[List[int]]) -> int:
memo = [[-1] * len(l) for l in grid]
def recurse(y, x, curr: int) -> int:
ret = curr
if memo[y][x] != -1:
return memo[y][x]
for [new_y, new_x] in DIRS:
new_y += y
new_x += x
if new_y >= len(grid) or \
new_y < 0 or \
new_x >= len(grid[0]) or \
new_x < 0 or \
grid[new_y][new_x] <= grid[y][x]:
continue
ret = max(ret, recurse(new_y, new_x, curr + 1))
memo[y][x] = ret
return ret
ret = 0
for y in range(len(grid)):
ret = max(ret, recurse(y, 0, 0))
return ret