356-Line-Reflection
https://leetcode.com/problems/line-reflection
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isReflected(self, points: List[List[int]]) -> bool:
minX = math.inf
maxX = -math.inf
seen = set()
for x, y in points:
minX = min(minX, x)
maxX = max(maxX, x …
Read More
357-Count-Numbers-With-Unique-Digits
https://leetcode.com/problems/count-numbers-with-unique-digits
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def countNumbersWithUniqueDigits(self, n: int) -> int:
if n == 0:
return 1
ans = 10
uniqueDigits = 9
availableNum = 9
while n > 1 and availableNum > 0:
uniqueDigits *= availableNum
ans += uniqueDigits
n -= 1 …
Read More
358-Rearrange-String-K-Distance-Apart
https://leetcode.com/problems/rearrange-string-k-distance-apart
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def rearrangeString(self, s: str, k: int) -> str:
n = len(s)
ans = []
count = Counter(s)
valid = Counter() # valid[i] := the leftmost index i can appear
# Returns the letter that …
Read More
36-Valid-Sudoku
https://leetcode.com/problems/valid-sudoku
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
seen = set()
for i in range(9):
for j in range(9):
c = board[i][j]
if c == '.':
continue
if c …
Read More
360-Sort-Transformed-Array
https://leetcode.com/problems/sort-transformed-array
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def sortTransformedArray(self, nums: List[int], a: int, b: int, c: int) -> List[int]:
n = len(nums)
upward = a > 0
ans = [0] * n
# The concavity of f only depends …
Read More
361-Bomb-Enemy
https://leetcode.com/problems/bomb-enemy
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxKilledEnemies(self, grid: List[List[chr]]) -> int:
m = len(grid)
n = len(grid[0])
enemyCount = 0
# dp[i][j] := max enemies grid[i][j] can kill
dp = [[0 …
Read More
365-Water-And-Jug-Problem
https://leetcode.com/problems/water-and-jug-problem
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def canMeasureWater(self, jug1Capacity: int, jug2Capacity: int, targetCapacity: int) -> bool:
return targetCapacity == 0 or \
jug1Capacity + jug2Capacity >= targetCapacity and \
targetCapacity % gcd(jug1Capacity, jug2Capacity) == 0
new Solution().canMeasureWater()
Score: 5
Read More
367-Valid-Perfect-Square
https://leetcode.com/problems/valid-perfect-square
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isPerfectSquare(self, num: int) -> bool:
l = 1
r = num
while l < r:
m = (l + r) // 2
if m >= num / m:
r = m
else:
l = m + 1
return l …
Read More
368-Largest-Divisible-Subset
https://leetcode.com/problems/largest-divisible-subset
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def largestDivisibleSubset(self, nums: List[int]) -> List[int]:
n = len(nums)
ans = []
count = [1] * n
prevIndex = [-1] * n
maxCount = 0
index = -1
nums.sort()
for i, num in enumerate …
Read More
37-Sudoku-Solver
https://leetcode.com/problems/sudoku-solver
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def solveSudoku(self, board: List[List[str]]) -> None:
def isValid(row: int, col: int, c: chr) -> bool:
for i in range(9):
if board[i][col] == c or \
board …
Read More