74-Search-A-2D-Matrix
https://leetcode.com/problems/search-a-2d-matrix
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
if not matrix:
return False
m = len(matrix)
n = len(matrix[0])
l = 0
r = m * n
while l …
Read More
744-Find-Smallest-Letter-Greater-Than-Target
https://leetcode.com/problems/find-smallest-letter-greater-than-target
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def nextGreatestLetter(self, letters: List[str], target: str) -> str:
l = 0
r = len(letters)
while l < r:
m = (l + r) >> 1
if letters[m] <= target:
l = m + 1
else …
Read More
746-Min-Cost-Climbing-Stairs
https://leetcode.com/problems/min-cost-climbing-stairs
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minCostClimbingStairs(self, cost: List[int]) -> int:
cost.append(0)
for i in range(2, len(cost)):
cost[i] += min(cost[i - 1], cost[i - 2])
return cost[-1 …
Read More
747-Largest-Number-At-Least-Twice-Of-Others
https://leetcode.com/problems/largest-number-at-least-twice-of-others
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def dominantIndex(self, nums: List[int]) -> int:
max = 0
secondMax = 0
for i, num in enumerate(nums):
if num > max:
secondMax = max
max = num
ans = i
elif num > secondMax …
Read More
748-Shortest-Completing-Word
https://leetcode.com/problems/shortest-completing-word
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def shortestCompletingWord(self, licensePlate: str, words: List[str]) -> str:
def isMatch(word: str) -> bool:
wordCount = Counter(word)
return False if any(wordCount[i] < count[i] for i in string …
Read More
75-Sort-Colors
https://leetcode.com/problems/sort-colors
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def sortColors(self, nums: List[int]) -> None:
zero = -1
one = -1
two = -1
for num in nums:
if num == 0:
two += 1
one += 1
zero += 1
nums[two] = 2 …
Read More
751-Ip-To-Cidr
https://leetcode.com/problems/ip-to-cidr
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def ipToCIDR(self, ip: str, n: int) -> List[str]:
ans = []
num = self._getNum(ip.split('.'))
while n > 0:
lowbit = num & -num
count = self._maxLow(n) if lowbit == 0 else …
Read More
753-Cracking-The-Safe
https://leetcode.com/problems/cracking-the-safe
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def crackSafe(self, n: int, k: int) -> str:
passwordSize = k**n
path = '0' * n
seen = set()
seen.add(path)
def dfs(path: str) -> str:
if len(seen) == passwordSize:
return …
Read More
754-Reach-A-Number
https://leetcode.com/problems/reach-a-number
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def reachNumber(self, target: int) -> int:
ans = 0
pos = 0
target = abs(target)
while pos < target:
ans += 1
pos += ans
while (pos - target) & 1:
ans += 1
pos += ans
return …
Read More
756-Pyramid-Transition-Matrix
https://leetcode.com/problems/pyramid-transition-matrix
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def pyramidTransition(self, bottom: str, allowed: List[str]) -> bool:
prefixToBlocks = defaultdict(list)
for a in allowed:
prefixToBlocks[a[:2]].append(a[2])
def dfs(row: str, nextRow: str, i …
Read More