82-Remove-Duplicates-From-Sorted-List-Ii
https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
dummy = ListNode(0, head)
prev = dummy
while head:
while head.next and head.val == head.next.val:
head = head.next
if prev.next …
Read More
820-Short-Encoding-Of-Words
https://leetcode.com/problems/short-encoding-of-words
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class TrieNode:
def __init__(self):
self.children: Dict[str, TrieNode] = defaultdict(TrieNode)
self.depth = 0
class Solution:
def minimumLengthEncoding(self, words: List[str]) -> int:
root = TrieNode()
leaves = []
def insert(word: str …
Read More
822-Card-Flipping-Game
https://leetcode.com/problems/card-flipping-game
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def flipgame(self, fronts: List[int], backs: List[int]) -> int:
same = {f for f, b in zip(fronts, backs) if f == b}
return min([num for num in fronts …
Read More
823-Binary-Trees-With-Factors
https://leetcode.com/problems/binary-trees-with-factors
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numFactoredBinaryTrees(self, arr: List[int]) -> int:
kMod = 1_000_000_007
n = len(arr)
# dp[i] := # Of binary trees with arr[i] as root
dp = [1] * n
arr.sort()
numToIndex …
Read More
824-Goat-Latin
https://leetcode.com/problems/goat-latin
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def toGoatLatin(self, S: str) -> str:
ans = ''
vowels = 'aeiouAEIOU'
words = S.split()
i = 1
for word in words:
if i > 1:
ans += ' '
if word[0] in vowels:
ans += word …
Read More
825-Friends-Of-Appropriate-Ages
https://leetcode.com/problems/friends-of-appropriate-ages
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numFriendRequests(self, ages: List[int]) -> int:
ans = 0
count = [0] * 121
for age in ages:
count[age] += 1
for i in range(15, 121):
ans += count[i] * (count …
Read More
826-Most-Profit-Assigning-Work
https://leetcode.com/problems/most-profit-assigning-work
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def maxProfitAssignment(self, difficulty: List[int], profit: List[int], worker: List[int]) -> int:
ans = 0
jobs = sorted(zip(difficulty, profit))
worker.sort(reverse=1)
i = 0
maxProfit = 0
for …
Read More
828-Count-Unique-Characters-Of-All-Substrings-Of-A-Given-String
https://leetcode.com/problems/count-unique-characters-of-all-substrings-of-a-given-string
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def uniqueLetterString(self, s: str) -> int:
ans = 0
count = 0
lastCount = [0] * 26
lastSeen = [-1] * 26
for i, c in enumerate(s):
c = ord(c) - ord('A')
currentCount = i …
Read More
829-Consecutive-Numbers-Sum
https://leetcode.com/problems/consecutive-numbers-sum
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def consecutiveNumbersSum(self, N: int) -> int:
ans = 0
i = 1
triangleNum = 1
while triangleNum <= N:
if (N - triangleNum) % i == 0:
ans += 1
i += 1
triangleNum += i
return ans
Read More
83-Remove-Duplicates-From-Sorted-List
https://leetcode.com/problems/remove-duplicates-from-sorted-list
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
curr = head
while curr:
while curr.next and curr.val == curr.next.val:
curr.next = curr.next.next
curr = curr.next
return head …
Read More