289-Game-Of-Life
https://leetcode.com/problems/game-of-life
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def gameOfLife(self, board: List[List[int]]) -> None:
m = len(board)
n = len(board[0])
for i in range(m):
for j in range(n):
ones = 0
for x …
Read More
29-Divide-Two-Integers
https://leetcode.com/problems/divide-two-integers
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def divide(self, dividend: int, divisor: int) -> int:
if dividend == -2**31 and divisor == -1:
return 2**31 - 1
sign = -1 if (dividend > 0) ^ (divisor > 0) else 1
ans …
Read More
290-Word-Pattern
https://leetcode.com/problems/word-pattern
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def wordPattern(self, pattern: str, str: str) -> bool:
t = str.split()
return [*map(pattern.index, pattern)] == [*map(t.index, t)]
new Solution().wordPattern()
Score: 5
Read More
291-Word-Pattern-Ii
https://leetcode.com/problems/word-pattern-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def wordPatternMatch(self, pattern: str, s: str) -> bool:
def isMatch(i: int, j: int, charToString: Dict[chr, str], seen: Set[str]) -> bool:
if i == len(pattern) and j == len …
Read More
292-Nim-Game
https://leetcode.com/problems/nim-game
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def canWinNim(self, n: int) -> bool:
return n % 4 != 0
new Solution().canWinNim()
Score: 5
Read More
293-Flip-Game
https://leetcode.com/problems/flip-game
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def generatePossibleNextMoves(self, currentState: str) -> List[str]:
return [currentState[:i] + '--' + currentState[i + 2:]
for i, (a, b) in enumerate(zip(currentState, currentState[1:]))
if a == '+' and b == '+']
Read More
294-Flip-Game-Ii
https://leetcode.com/problems/flip-game-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
@functools.lru_cache(None)
def canWin(self, currentState: str) -> bool:
# If any of currentState[i:i + 2] == "++" and your friend can't win after
# Changing currentState[i:i + 2] to …
Read More
296-Best-Meeting-Point
https://leetcode.com/problems/best-meeting-point
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minTotalDistance(self, grid: List[List[int]]) -> int:
m = len(grid)
n = len(grid[0])
# I indices s.t. grid[i][j] == 1
I = [i for i in range …
Read More
298-Binary-Tree-Longest-Consecutive-Sequence
https://leetcode.com/problems/binary-tree-longest-consecutive-sequence
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def longestConsecutive(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
def dfs(root: Optional[TreeNode], target: int, length: int, maxLength: int) -> int:
if not root:
return maxLength …
Read More
299-Bulls-And-Cows
https://leetcode.com/problems/bulls-and-cows
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def getHint(self, secret: str, guess: str) -> str:
bulls = sum(map(operator.eq, secret, guess))
bovine = sum(min(secret.count(x), guess.count(x)) for x in set(guess …
Read More