573-Squirrel-Simulation
https://leetcode.com/problems/squirrel-simulation
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minDistance(self, height: int, width: int, tree: List[int], squirrel: List[int], nuts: List[List[int]]) -> int:
def dist(a: List[int], b: List[int]) -> int:
return abs …
Read More
575-Distribute-Candies
https://leetcode.com/problems/distribute-candies
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def distributeCandies(self, candies: List[int]) -> int:
return min(len(candies) // 2, len(set(candies)))
new Solution().distributeCandies()
Score: 5
Read More
58-Length-Of-Last-Word
https://leetcode.com/problems/length-of-last-word
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def lengthOfLastWord(self, s: str) -> int:
i = len(s) - 1
while i >= 0 and s[i] == ' ':
i -= 1
lastIndex = i
while i >= 0 and s[i] != ' ':
i -= 1
return …
Read More
581-Shortest-Unsorted-Continuous-Subarray
https://leetcode.com/problems/shortest-unsorted-continuous-subarray
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def findUnsortedSubarray(self, nums: List[int]) -> int:
mini = math.inf
maxi = -math.inf
flag = False
for i in range(1, len(nums)):
if nums[i] < nums[i - 1]:
flag …
Read More
582-Kill-Process
https://leetcode.com/problems/kill-process
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def killProcess(self, pid: List[int], ppid: List[int], kill: int) -> List[int]:
ans = []
tree = defaultdict(list)
for v, u in zip(pid, ppid):
if u == 0:
continue
tree …
Read More
583-Delete-Operation-For-Two-Strings
https://leetcode.com/problems/delete-operation-for-two-strings
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def minDistance(self, word1: str, word2: str) -> int:
m = len(word1)
n = len(word2)
dp = [0] * (n + 1)
for j in range(n + 1):
dp[j] = j
for i …
Read More
587-Erect-The-Fence
https://leetcode.com/problems/erect-the-fence
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def outerTrees(self, trees: List[List[int]]) -> List[List[int]]:
hull = []
trees.sort(key=lambda x: (x[0], x[1]))
def cross(p: List[int], q: List[int], r …
Read More
59-Spiral-Matrix-Ii
https://leetcode.com/problems/spiral-matrix-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def generateMatrix(self, n: int) -> List[List[int]]:
ans = [[0] * n for _ in range(n)]
count = 1
for min in range(n // 2):
max = n - min - 1
for …
Read More
591-Tag-Validator
https://leetcode.com/problems/tag-validator
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def isValid(self, code: str) -> bool:
if code[0] != '<' or code[-1] != '>':
return False
containsTag = False
stack = []
def isValidCdata(s: str) -> bool:
return s.find('[CDATA[') == 0
def isValidTagName …
Read More
592-Fraction-Addition-And-Subtraction
https://leetcode.com/problems/fraction-addition-and-subtraction
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def fractionAddition(self, expression: str) -> str:
ints = list(map(int, re.findall('[+-]?[0-9]+', expression)))
A = 0
B = 1
for a, b in zip(ints[::2], ints[1::2]):
A …
Read More