TODO:
bisect.insort()main.cimport bisect
import copy
class Solution:
def insert(self, intervals: list[list[int]], newInterval: list[int]) -> list[list[int]]:
intervals = copy.deepcopy(intervals)
bisect.insort(intervals, newInterval.copy())
merged = []
for i in range(len(intervals) - 1):
if intervals[i][1] < intervals[i + 1][0]:
merged.append(intervals[i])
continue
intervals[i + 1][0] = intervals[i][0]
intervals[i + 1][1] = max(intervals[i][1], intervals[i + 1][1])
merged.append(intervals[-1])
return mergedimport math
class Solution:
def updateMatrix(self, mat: list[list[int]]) -> list[list[int]]:
num_rows = len(mat)
num_cols = len(mat[0])
dist = [[float("inf")] * num_cols for _ in range(num_rows)]
cells = []
for row in range(num_rows):
for col in range(num_cols):
if mat[row][col] == 0:
cells.append((row, col))
distance = 0
while cells:
next_cells = []
for row, col in cells:
if not (0 <= row < num_rows and 0 <= col < num_cols):
continue
if not math.isinf(dist[row][col]):
continue
dist[row][col] = distance
next_cells.append((row + 1, col))
next_cells.append((row - 1, col))
next_cells.append((row, col + 1))
next_cells.append((row, col - 1))
distance += 1
cells = next_cells
return distI get a little embarrassed when people celebrate my birthday.
ls /Volumes/RP2350cmake -S ~/Desktop/pico-c/blink -B ~/Desktop/pico-c/blink/build && make -C ~/Desktop/pico-c/blink/buildYou set up a full C toolchain for the Pico 2 W: CMake, the Arm GNU cross-compiler, and the Pico SDK with its submodules. Then you wrote your own CMakeLists.txt and main.c, built the firmware, and flashed it to a dedicated second board, which now blinks its LED from bare-metal C. Along the way you learned how C code becomes Arm Thumb-2 machine code, why the .uf2 runs from flash, and how to reflash through BOOTSEL.
import bisect
class Solution:
def insert(self, intervals: list[list[int]], newInterval: list[int]) -> list[list[int]]:
intervals = copy.deepcopy(intervals)
insert_pos = bisect.bisect_left(intervals, newInterval)
intervals.insert(insert_pos, newInterval)
merged = []
for i in range(len(intervals) - 1):
if intervals[i][1] < intervals[i + 1][0]:
merged.append(intervals[i])
continue
intervals[i + 1][0] = intervals[i][0]
intervals[i + 1][1] = max(intervals[i][1], intervals[i + 1][1])
merged.append(intervals[-1])
return mergedclass Solution:
def maxAreaOfIsland(self, grid: list[list[int]]) -> int:
num_rows = len(grid)
num_cols = len(grid[0])
visited = [[False] * num_cols for _ in range(num_rows)]
def count_area(row: int, col: int) -> int:
if not (0 <= row < num_rows and 0 <= col < num_cols):
return 0
if grid[row][col] == 0:
return 0
if visited[row][col]:
return 0
visited[row][col] = True
count = 1
count += count_area(row - 1, col)
count += count_area(row + 1, col)
count += count_area(row, col - 1)
count += count_area(row, col + 1)
return count
max_area = 0
for row in range(num_rows):
for col in range(num_cols):
max_area = max(max_area, count_area(row, col))
return max_areaclass Solution:
def insert(self, intervals: list[list[int]], newInterval: list[int]) -> list[list[int]]:
merged = []
merged_start, merged_end = newInterval
i = 0
while i < len(intervals) and intervals[i][1] < merged_start:
merged.append(intervals[i].copy())
i += 1
while i < len(intervals) and intervals[i][0] <= merged_end:
merged_end = max(merged_end, intervals[i][1])
merged_start = min(merged_start, intervals[i][0])
i += 1
merged.append([merged_start, merged_end])
while i < len(intervals):
merged.append(intervals[i].copy())
i += 1
return mergedimport copy
class Solution:
def insert(self, intervals: list[list[int]], newInterval: list[int]) -> list[list[int]]:
all_intervals = copy.deepcopy(intervals)
all_intervals.append(newInterval)
all_intervals.sort()
merged = []
for i in range(len(all_intervals) - 1):
if all_intervals[i][1] < all_intervals[i + 1][0]:
merged.append(all_intervals[i])
continue
all_intervals[i + 1] = [
min(all_intervals[i][0], all_intervals[i + 1][0]),
max(all_intervals[i][1], all_intervals[i + 1][1]),
]
merged.append(all_intervals[-1])
return mergedclass Solution:
def maxProfit(self, prices: list[int]) -> int:
max_profit = 0
min_price = float("inf")
for price in prices:
min_price = min(min_price, price)
max_profit = max(max_profit, price - min_price)
return max_profitclass Solution:
def maxProfit(self, prices: list[int]) -> int:
suffix_min = [float("inf")] * (len(prices) + 1)
for i in range(1, len(prices) + 1):
suffix_min[i] = min(suffix_min[i - 1], prices[i - 1])
max_profit = [0] * (len(prices) + 1)
for i in range(len(prices)):
max_profit[i + 1] = max(
max_profit[i],
prices[i] - suffix_min[i + 1],
)
return max_profit[-1]class Solution:
def maxSubArray(self, nums: list[int]) -> int:
prefix_sum = [0] * (len(nums) + 1)
for i in range(1, len(nums) + 1):
prefix_sum[i] = prefix_sum[i - 1] + nums[i - 1]
max_sum = max(nums)
min_prefix = float("inf")
for i in range(len(prefix_sum)):
if prefix_sum[i] < min_prefix:
min_prefix = prefix_sum[i]
continue
max_sum = max(max_sum, prefix_sum[i] - min_prefix)
return max_sumMy wife visited the KPMG office in Santa Clara. She also had an interview with Deloitte. It sounds like the interview didn’t go well tho…
class Solution:
def majorityElement(self, nums: list[int]) -> int:
count = 1
candidate = float("inf")
for num in nums:
if num == candidate:
count += 1
continue
count -= 1
if count == 0:
candidate = num
count = 1
return candidateclass Solution:
def addDigits(self, num: int) -> int:
assert 0 <= num
if num < 10:
return num
return self.addDigits(sum(int(c) for c in str(num)))My wife surprised me with a birthday gift! (Fitbit Air)
class Solution:
def majorityElement(self, nums: list[int]) -> int:
sorted_nums = sorted(nums)
return sorted_nums[len(nums) // 2]import collections
class Solution:
def majorityElement(self, nums: list[int]) -> int:
num_to_count = collections.Counter(nums)
for num, count in num_to_count.items():
if len(nums) // 2 < count:
return num
raise ValueError("no majority element found.")class Solution:
def addBinary(self, a: str, b: str) -> str:
len_longer = max(len(a), len(b))
a = a.rjust(len_longer, "0")
b = b.rjust(len_longer, "0")
carry = 0
result = []
for i in range(len_longer):
total = int(a[-(i + 1)]) + int(b[-(i + 1)]) + carry
result.append(str(total % 2))
carry = total // 2
if 0 < carry:
result.append("1")
return "".join(result[::-1])# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def middleNode(self, head: ListNode | None) -> ListNode | None:
if head is None:
return None
slow = head
fast = head
while fast is not None and fast.next is not None:
slow = slow.next
fast = fast.next.next
return slow# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def middleNode(self, head: ListNode | None) -> ListNode | None:
if head is None:
return None
node = head
count = 0
while node is not None:
count += 1
node = node.next
node = head
for i in range(count // 2):
node = node.next
return node# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: ListNode | None) -> ListNode | None:
reversing = head
reversed_head = None
while reversing is not None:
next_reversing = reversing.next
reversing.next = reversed_head
reversed_head = reversing
reversing = next_reversing
return reversed_headclass Solution:
def reverseList(self, head: ListNode | None) -> ListNode | None:
def reverse(node: ListNode) -> tuple[ListNode, ListNode]:
if node.next is None:
return node, node
reversed_head, reversed_tail = reverse(node.next)
reversed_tail.next = node
node.next = None
return reversed_head, node
if head is None:
return None
return reverse(head)[0]import collections
class Solution:
def canConstruct(self, ransomNote: str, magazine: str) -> bool:
return collections.Counter(ransomNote) <= collections.Counter(magazine)import collections
class Solution:
def longestPalindrome(self, s: str) -> int:
char_to_count = collections.Counter(s)
max_length = 0
for count in char_to_count.values():
if 2 <= count:
if count % 2 == 0:
max_length += count
else:
max_length += count - 1
if max_length < len(s):
max_length += 1
return max_length# The isBadVersion API is already defined for you.
# def isBadVersion(version: int) -> bool:
class Solution:
def firstBadVersion(self, n: int) -> int:
def find_first_bad(start: int, end: int) -> int:
if start == end:
return start
mid = (start + end) // 2
if not isBadVersion(mid):
return find_first_bad(mid + 1, end)
else:
return find_first_bad(start, mid)
return find_first_bad(1, n)I was a about to join this week’s LeetCode contest, but I couldn’t. The start time changed because the daylight saving time ended…
I was exhausted after working all night, so I slept a lot.