561-Array-Partition

Sat 17 May 2025

https://leetcode.com/problems/array-partition

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def arrayPairSum(self, nums: List[int]) -> int:
    return sum(sorted(nums)[::2])
new Solution().arrayPairSum()

Score: 5

Category: leetcode

Read More

562-Longest-Line-Of-Consecutive-One-In-Matrix

Sat 17 May 2025

https://leetcode.com/problems/longest-line-of-consecutive-one-in-matrix

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def longestLine(self, mat: List[List[int]]) -> int:
    m = len(mat)
    n = len(mat[0])
    ans = 0
    # dp[i][j][0] := horizontal
    # dp[i][j][1] := vertical
    # dp[i …

Category: leetcode

Read More

563-Binary-Tree-Tilt

Sat 17 May 2025

https://leetcode.com/problems/binary-tree-tilt

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def findTilt(self, root: Optional[TreeNode]) -> int:
    ans = 0

    def summ(root: Optional[TreeNode]) -> None:
      nonlocal ans
      if not root:
        return 0

      l = summ(root.left)
      r = summ(root …

Category: leetcode

Read More

564-Find-The-Closest-Palindrome

Sat 17 May 2025

https://leetcode.com/problems/find-the-closest-palindrome

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def nearestPalindromic(self, n: str) -> str:
    def getPalindromes(s: str) -> tuple:
      num = int(s)
      k = len(s)
      palindromes = []
      half = s[0:(k + 1) // 2]
      reversedHalf = half[:k // 2][::-1 …

Category: leetcode

Read More

565-Array-Nesting

Sat 17 May 2025

https://leetcode.com/problems/array-nesting

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def arrayNesting(self, nums: List[int]) -> int:
    ans = 0

    for num in nums:
      if num == -1:
        continue
      index = num
      count = 0
      while nums[index] != -1:
        temp = index
        index = nums …

Category: leetcode

Read More

566-Reshape-The-Matrix

Sat 17 May 2025

https://leetcode.com/problems/reshape-the-matrix

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def matrixReshape(self, nums: List[List[int]], r: int, c: int) -> List[List[int]]:
    if nums == [] or r * c != len(nums) * len(nums[0]):
      return nums

    ans = [[0 for …

Category: leetcode

Read More

57-Insert-Interval

Sat 17 May 2025

https://leetcode.com/problems/insert-interval

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
    n = len(intervals)
    ans = []
    i = 0

    while i < n and intervals[i][1] < newInterval[0]:
      ans …

Category: leetcode

Read More

573-Squirrel-Simulation

Sat 17 May 2025

https://leetcode.com/problems/squirrel-simulation

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

Category: leetcode

Read More

575-Distribute-Candies

Sat 17 May 2025

https://leetcode.com/problems/distribute-candies

import pyutil as pyu
pyu.get_local_pyinfo()
print(pyu.ps2("python-dotenv"))
from typing import List
class Solution:
  def distributeCandies(self, candies: List[int]) -> int:
    return min(len(candies) // 2, len(set(candies)))
new Solution().distributeCandies()

Score: 5

Category: leetcode

Read More

58-Length-Of-Last-Word

Sat 17 May 2025

https://leetcode.com/problems/length-of-last-word

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

Category: leetcode

Read More
Page 44 of 77

« Prev Next »