82-Remove-Duplicates-From-Sorted-List-Ii

Sat 17 May 2025

https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

820-Short-Encoding-Of-Words

Sat 17 May 2025

https://leetcode.com/problems/short-encoding-of-words

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

822-Card-Flipping-Game

Sat 17 May 2025

https://leetcode.com/problems/card-flipping-game

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

823-Binary-Trees-With-Factors

Sat 17 May 2025

https://leetcode.com/problems/binary-trees-with-factors

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

824-Goat-Latin

Sat 17 May 2025

https://leetcode.com/problems/goat-latin

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

825-Friends-Of-Appropriate-Ages

Sat 17 May 2025

https://leetcode.com/problems/friends-of-appropriate-ages

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

826-Most-Profit-Assigning-Work

Sat 17 May 2025

https://leetcode.com/problems/most-profit-assigning-work

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More

828-Count-Unique-Characters-Of-All-Substrings-Of-A-Given-String

Sat 17 May 2025

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"))
from typing import List
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 …

Category: leetcode

Read More

829-Consecutive-Numbers-Sum

Sat 17 May 2025

https://leetcode.com/problems/consecutive-numbers-sum

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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
new …

Category: leetcode

Read More

83-Remove-Duplicates-From-Sorted-List

Sat 17 May 2025

https://leetcode.com/problems/remove-duplicates-from-sorted-list

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
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 …

Category: leetcode

Read More
Page 62 of 77

« Prev Next »