55-Jump-Game
https://leetcode.com/problems/jump-game
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def canJump(self, nums: List[int]) -> bool:
i = 0
reach = 0
while i < len(nums) and i <= reach:
reach = max(reach, i + nums[i])
i += 1
return i == len …
Read More
551-Student-Attendance-Record-I
https://leetcode.com/problems/student-attendance-record-i
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def checkRecord(self, s: str) -> bool:
return s.count('A') <= 1 and 'LLL' not in s
new Solution().checkRecord()
Score: 5
Read More
552-Student-Attendance-Record-Ii
https://leetcode.com/problems/student-attendance-record-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def checkRecord(self, n: int) -> int:
kMod = 1_000_000_007
# dp[i][j] := length so far w/ i A's and the latest chars are j L's
dp = [[0 …
Read More
553-Optimal-Division
https://leetcode.com/problems/optimal-division
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def optimalDivision(self, nums: List[int]) -> str:
ans = str(nums[0])
if len(nums) == 1:
return ans
if len(nums) == 2:
return ans + '/' + str(nums[1])
ans += '/(' + str(nums …
Read More
554-Brick-Wall
https://leetcode.com/problems/brick-wall
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def leastBricks(self, wall: List[List[int]]) -> int:
maxFreq = 0
count = defaultdict(int)
for row in wall:
prefix = 0
for i in range(len(row) - 1):
prefix += row[i …
Read More
555-Split-Concatenated-Strings
https://leetcode.com/problems/split-concatenated-strings
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def splitLoopedString(self, strs: List[str]) -> str:
ans = ''
sortedStrs = [max(s, s[::-1]) for s in strs]
for i, sortedStr in enumerate(sortedStrs):
for s in (sortedStr, sortedStr[::-1 …
Read More
556-Next-Greater-Element-Iii
https://leetcode.com/problems/next-greater-element-iii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def nextGreaterElement(self, n: int) -> int:
def nextPermutation(s: List[chr]) -> str:
i = len(s) - 2
while i >= 0:
if s[i] < s[i + 1]:
break
i -= 1
if …
Read More
559-Maximum-Depth-Of-N-Ary-Tree
https://leetcode.com/problems/maximum-depth-of-n-ary-tree
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxDepth(self, root: 'Node') -> int:
if not root:
return 0
if not root.children:
return 1
return 1 + max(self.maxDepth(child) for child in root.children)
Read More
56-Merge-Intervals
https://leetcode.com/problems/merge-intervals
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
ans = []
for interval in sorted(intervals):
if not ans or ans[-1][1] < interval[0]:
ans.append(interval)
else …
Read More
560-Subarray-Sum-Equals-K
https://leetcode.com/problems/subarray-sum-equals-k
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def subarraySum(self, nums: List[int], k: int) -> int:
ans = 0
prefix = 0
count = Counter({0: 1})
for num in nums:
prefix += num
ans += count[prefix - k]
count[prefix …
Read More