812-Largest-Triangle-Area
https://leetcode.com/problems/largest-triangle-area
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def largestTriangleArea(self, points: List[List[int]]) -> float:
ans = 0
for Ax, Ay in points:
for Bx, By in points:
for Cx, Cy in points:
ans = max(ans, 0 …
Read More
814-Binary-Tree-Pruning
https://leetcode.com/problems/binary-tree-pruning
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def pruneTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if not root:
return None
root.left = self.pruneTree(root.left)
root.right = self.pruneTree(root.right)
if not root.left …
Read More
815-Bus-Routes
https://leetcode.com/problems/bus-routes
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numBusesToDestination(self, routes: List[List[int]], S: int, T: int) -> int:
if S == T:
return 0
graph = defaultdict(list)
usedBuses = set()
for i in range(len(routes)):
for …
Read More
816-Ambiguous-Coordinates
https://leetcode.com/problems/ambiguous-coordinates
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def ambiguousCoordinates(self, S: str) -> List[str]:
def splits(S: str) -> List[str]:
if not S or len(S) > 1 and S[0] == S[-1] == '0':
return []
if S …
Read More
817-Linked-List-Components
https://leetcode.com/problems/linked-list-components
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def numComponents(self, head: ListNode, G: List[int]) -> int:
ans = 0
G = set(G)
while head:
if head.val in G and (head.next == None or head.next.val …
Read More
819-Most-Common-Word
https://leetcode.com/problems/most-common-word
import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
class Solution:
def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
banned = set(banned)
words = re.findall(r'\w+', paragraph.lower())
return Counter(word for word in words if word not …
Read More
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