133-Clone-Graph
https://leetcode.com/problems/clone-graph
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def cloneGraph(self, node: 'Node') -> 'Node':
if not node:
return None
q = deque([node])
map = {node: Node(node.val)}
while q:
u = q.popleft()
for v in u.neighbors …
Read More
134-Gas-Station
https://leetcode.com/problems/gas-station
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
ans = 0
net = 0
summ = 0
for i in range(len(gas)):
net += gas[i] - cost[i]
summ += gas …
Read More
135-Candy
https://leetcode.com/problems/candy
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def candy(self, ratings: List[int]) -> int:
n = len(ratings)
ans = 0
l = [1] * n
r = [1] * n
for i in range(1, n):
if ratings[i] > ratings[i …
Read More
136-Single-Number
https://leetcode.com/problems/single-number
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def singleNumber(self, nums: List[int]) -> int:
return functools.reduce(lambda x, y: x ^ y, nums, 0)
new Solution().singleNumber()
Score: 5
Read More
137-Single-Number-Ii
https://leetcode.com/problems/single-number-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def singleNumber(self, nums: List[int]) -> int:
ones = 0
twos = 0
for num in nums:
ones ^= num & ~twos
twos ^= num & ~ones
return ones
new Solution().singleNumber()
Score: 5
Read More
138-Copy-List-With-Random-Pointer
https://leetcode.com/problems/copy-list-with-random-pointer
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def copyRandomList(self, head: 'Node') -> 'Node':
if not head:
return None
if head in self.map:
return self.map[head]
newNode = Node(head.val)
self.map[head] = newNode
newNode …
Read More
139-Word-Break
https://leetcode.com/problems/word-break
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
wordSet = set(wordDict)
@functools.lru_cache(None)
def wordBreak(s: str) -> bool:
if s in wordSet:
return True
return any(s …
Read More
14-Longest-Common-Prefix
https://leetcode.com/problems/longest-common-prefix
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def longestCommonPrefix(self, strs: List[str]) -> str:
if not strs:
return ''
for i in range(len(strs[0])):
for j in range(1, len(strs)):
if i == len(strs …
Read More
140-Word-Break-Ii
https://leetcode.com/problems/word-break-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
wordSet = set(wordDict)
@functools.lru_cache(None)
def wordBreak(s: str) -> List[str]:
ans = []
# 1 <= len(prefix) < len(s)
for …
Read More
141-Linked-List-Cycle
https://leetcode.com/problems/linked-list-cycle
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def hasCycle(self, head: ListNode) -> bool:
slow = head
fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False …
Read More