20-Valid-Parentheses
https://leetcode.com/problems/valid-parentheses
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isValid(self, s: str) -> bool:
stack = []
for c in s:
if c == '(':
stack.append(')')
elif c == '{':
stack.append('}')
elif c == '[':
stack.append(']')
elif not stack or stack.pop …
Read More
200-Number-Of-Islands
https://leetcode.com/problems/number-of-islands
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
m = len(grid)
n = len(grid[0])
dirs = [0, 1, 0, -1, 0]
def bfs(r, c):
q = deque([(r, c …
Read More
201-Bitwise-And-Of-Numbers-Range
https://leetcode.com/problems/bitwise-and-of-numbers-range
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def rangeBitwiseAnd(self, m: int, n: int) -> int:
return self.rangeBitwiseAnd(m >> 1, n >> 1) << 1 if m < n else m
new Solution().rangeBitwiseAnd()
Score: 5
Read More
202-Happy-Number
https://leetcode.com/problems/happy-number
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isHappy(self, n: int) -> bool:
def squaredSum(n: int) -> bool:
summ = 0
while n:
summ += pow(n % 10, 2)
n //= 10
return summ
slow = squaredSum(n)
fast = squaredSum …
Read More
203-Remove-Linked-List-Elements
https://leetcode.com/problems/remove-linked-list-elements
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def removeElements(self, head: ListNode, val: int) -> ListNode:
dummy = ListNode(0, head)
prev = dummy
while head:
if head.val != val:
prev.next = head
prev = prev.next
head = head.next …
Read More
204-Count-Primes
https://leetcode.com/problems/count-primes
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def countPrimes(self, n: int) -> int:
if n <= 2:
return 0
isPrime = [False] * 2 + [True] * (n - 2)
for i in range(2, int(n**0.5) + 1):
if isPrime …
Read More
205-Isomorphic-Strings
https://leetcode.com/problems/isomorphic-strings
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
return [*map(s.index, s)] == [*map(t.index, t)]
new Solution().isIsomorphic()
Score: 5
Read More
206-Reverse-Linked-List
https://leetcode.com/problems/reverse-linked-list
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head or not head.next:
return head
newHead = self.reverseList(head.next)
head.next.next = head
head.next = None …
Read More
207-Course-Schedule
https://leetcode.com/problems/course-schedule
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from enum import Enum
class State(Enum):
kInit = 0
kVisiting = 1
kVisited = 2
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
graph = [[] for _ in range(numCourses …
Read More
209-Minimum-Size-Subarray-Sum
https://leetcode.com/problems/minimum-size-subarray-sum
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minSubArrayLen(self, s: int, nums: List[int]) -> int:
ans = math.inf
summ = 0
j = 0
for i, num in enumerate(nums):
summ += num
while summ >= s:
ans = min …
Read More