350-Intersection-Of-Two-Arrays-Ii
https://leetcode.com/problems/intersection-of-two-arrays-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
if len(nums1) > len(nums2):
return self.intersect(nums2, nums1)
ans = []
count = Counter(nums1)
for num in nums2 …
Read More
351-Android-Unlock-Patterns
https://leetcode.com/problems/android-unlock-patterns
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numberOfPatterns(self, m: int, n: int) -> int:
seen = set()
accross = [[0] * 10 for _ in range(10)]
accross[1][3] = accross[3][1] = 2
accross[1][7] = accross …
Read More
354-Russian-Doll-Envelopes
https://leetcode.com/problems/russian-doll-envelopes
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxEnvelopes(self, envelopes: List[List[int]]) -> int:
envelopes.sort(key=lambda x: (x[0], -x[1]))
# Same as 300. Longest Increasing Subsequence
ans = 0
dp = [0] * len(envelopes …
Read More
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-Ease-Of-Movement
import pyutil as pyu
pyu.get_local_pyinfo()
'conda env: ml312-2024; pyv: 3.12.7 | packaged by Anaconda, Inc. | (main, Oct 4 2024, 13:27:36) [GCC 11.2.0]'
print(pyu.ps2("yfinance pandas matplotlib"))
yfinance==0.2.51
pandas==2.2.3
matplotlib==3.9.3
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