487-Max-Consecutive-Ones-Ii
https://leetcode.com/problems/max-consecutive-ones-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findMaxConsecutiveOnes(self, nums: List[int]) -> int:
ans = 0
zeros = 0
l = 0
for r, num in enumerate(nums):
if num == 0:
zeros += 1
while zeros == 2:
if nums …
Read More
488-Zuma-Game
https://leetcode.com/problems/zuma-game
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findMinStep(self, board: str, hand: str) -> int:
def deDup(board):
start = 0 # Start index of a color sequenece
for i, c in enumerate(board):
if c != board[start …
Read More
49-Group-Anagrams
https://leetcode.com/problems/group-anagrams
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
dict = defaultdict(list)
for str in strs:
key = ''.join(sorted(str))
dict[key].append(str)
return dict.values()
Read More
490-The-Maze
https://leetcode.com/problems/the-maze
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def hasPath(self, maze: List[List[int]], start: List[int], destination: List[int]) -> bool:
m = len(maze)
n = len(maze[0])
dirs = [0, 1, 0, -1, 0]
q = deque …
Read More
491-Increasing-Subsequences
https://leetcode.com/problems/increasing-subsequences
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findSubsequences(self, nums: List[int]) -> List[List[int]]:
ans = []
def dfs(s: int, path: List[int]) -> None:
if len(path) > 1:
ans.append(path)
used = set()
for i …
Read More
494-Target-Sum
https://leetcode.com/problems/target-sum
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findTargetSumWays(self, nums: List[int], target: int) -> int:
summ = sum(nums)
if summ < abs(target) or (summ + target) & 1:
return 0
def knapsack(target: int) -> int:
# dp[i …
Read More
495-Teemo-Attacking
https://leetcode.com/problems/teemo-attacking
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findPoisonedDuration(self, timeSeries: List[int], duration: int) -> int:
if duration == 0:
return 0
ans = 0
for i in range(0, len(timeSeries) - 1):
ans += min(timeSeries[i + 1 …
Read More
496-Next-Greater-Element-I
https://leetcode.com/problems/next-greater-element-i
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def nextGreaterElement(self, nums1: List[int], nums2: List[int]) -> List[int]:
numToNextGreater = {}
stack = []
for num in nums2:
while stack and stack[-1] < num:
numToNextGreater[stack.pop()] = num
stack.append …
Read More
497-Random-Point-In-Non-Overlapping-Rectangles
https://leetcode.com/problems/random-point-in-non-overlapping-rectangles
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def __init__(self, rects: List[List[int]]):
self.rects = rects
self.areas = list(itertools.accumulate(
[(x2 - x1 + 1) * (y2 - y1 + 1) for x1, y1, x2, y2 in rects]))
def …
Read More
499-The-Maze-Iii
https://leetcode.com/problems/the-maze-iii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findShortestWay(self, maze: List[List[int]], ball: List[int], hole: List[int]) -> str:
ans = "impossible"
minSteps = math.inf
def dfs(i: int, j: int, dx: int, dy: int …
Read More