Solved/ Hinted/ Peeked
Jan-31
- 0238.Product of Array Except Self Solved w bug
Jan-25
- 0881.Boats to SavePeople Peeked
Jan-24
- 0189.Rotate Array hinted
Jan-23
- 0005.Longest Palindromic Substring solved w bug
- 0417.Pacific Atlantic Water Flow
- 1004.Max Consecutive Ones III
- 0049.Group Anagrams solved
- 0014.Longest Common Prefix hinted
- only need
min_length - don’t know how to cal the
min_length min_len = min(len(word) for word in strs)
- only need
- 1636.Sort Array by Increasing Frequency
Jan-22
- Solved w bug not familiar the interval
0402.Remove K Digits
Jan-22-2026 Solved w bug, took too long time
digital stringcan compare- use
whileto deal with stack str.join()works with any iterable of stringslstrip()only work with strings, to eliminate'0'
0146.LRU Cache Hinted w bug
class Node, add key, val, prev, nextputupdate key val as well. if exist, remove then add- In function
_add, forget to add node.prev
0031.Next Permutation Peeked
- do not know the solution
array.reverse()is in place,revered(arr)need assign to a variable
Redo
Jan-21
- 3528.Unit Conversion I Peeked
- 1863.Sum of All Subset XOR Totals Peeked
- 0093.Restore IP Addresses Peeked
- 0055.Jump Game Solved w bug
Review:
Jan-20
- 0049.Group Anagrams Solved w bug
''.join(string)better to usetuple(counts)from collections import defaultdict
- 0545.Boundary of Binary Tree Peeked
- 0735.Asteroid Collision Peeked, need
aliveflag, redo in the future
Jan-19
Done:
- 0173.Binary Search Tree Iterator Solved DFS method, Hinted stack method
- 0005.Longest Palindromic Substring Solved w bug, forget to restore pointer
- 1507.Reformat Date hinted, don’t know
zfill()is a string method that pads zeros on the left
- 0014.Longest Common Prefix Solved, need clear explanation
- 0236.Lowest Common Ancestor of BT Solved w bug, bad explanation
- 0543.Diameter of Binary Tree Peeked, no idea, use global variable
Redo:
- 0173.Binary Search Tree Iterator redo use stack
- 0543.Diameter of Binary Tree redo
Jan-18
Plan:
- 1169.Invalid Transactions: Peeked, with bug, need redo
- Mistake: compare each transaction under the same name,
- 0207.Course Schedule: Solved w/o bug, need improve explanation and edge case
- 0078.Subsets: Solved w bug, 1) boundary wrong 2)wrong time complexity
Required redo:
Jan-09
Review:
- 0001.Two Sum - Solved, Do in 10mins
- 1636.Sort Array by Increasing Frequency - Solved
LC1636. Extension Problem
Question:
- You are given an array logs, where each element represents an activity (or log) code. The same code may appear multiple times.
- The system processes the logs in order. At each time step i:
- Consider the prefix logs[0..i]
- Count how many distinct activity codes have appeared so far. The total cost is the sum of these unique counts over all time steps.
Example:
- Input:
logs = [2, 2, 3, 1, 1] (n = 5) - A good permutation:
arr = [2, 2, 1, 1, 3] - Result:
arr = [1, 1, 2, 2, 3]cost is 1 + 1 + 2 + 2 + 3 = 9
| [] | distinct | result |
|---|---|---|
| [1] | 1 | 1 |
| [1, 1] | 1 | 2 |
| [1, 1, 2] | 2 | 4 |
| [1, 1, 2, 2] | 2 | 6 |
| [1, 1, 2, 2, 3] | 3 | 9 |
- Restate the problem: Given an int array
logs, we can permute(reorder) the array. For any reorder array, define cost: for eachi,cost_iis the number of unique values in prefixarray[0...i], Total cost issum(cost_i). Return the minimum cost. - The key idea: Sort array from higher frequency to lower, keep unique count low as long as possible. Use
setto track seen values and sumlen(seen)each step. - Time complexity: O() Space complexity: O(n)
- Edge case:
Solution:
def minimum_time(logs):
counter_logs = Counter(logs)
sorted_logs = sorted(logs, key=lambda x: (-counter_logs[x], x)) # most frequent first
seen = set()
cost = 0
for num in sorted_logs:
seen.add(num)
cost += len(seen)
return cost- leetcode3
- Solve Type: Solution, need redo
- Mistake: forget sliding window concept (Variable size window)
- Restate the problem: Given a string of lowercase letters, return the length of the longest substring that contains no repeated characters.
- Key point: Use sliding window with two pointers and a frequency hashmap.
Expand window by moving right pointer, if the window become invalid.
Shrink window by moving left pointer. After window is valid, we update the answer using the current window length. - Time Complexity: O(n) Space Complexity: O(n)
- Edge case:
while right < len(string):
add right
while window invalid:
remove left
right += 1
update answer (when valid)
Jan-10
Leetcode
Redo:
LC3 Longest Substring Without Repeating Characters
- Invariant: the current window
s[left:right+1]contains no duplicate characters.
LC242. Valid Anagram - easy
242. Valid Anagram
Solve Type: Answer
- Let me restate the problem, Given two strings
tands, consisting only of lowercase English letters. Anagram means same characters, same frequency but in a different order. return True iftis an anagram ofsotherwise return False. - The key point is to first compare length of
sandt. If they differ, returnFalse. Then, use a hashmap to count character frequenciess{char: frequency}. Next, looptto decrementcount[char]. if we finish the loop without failing, return True. - Time complexity: O(n) for hashmap Space complexity: O(n).
- If hashmap only store 26 lowercase english letter, the space complexity is O(1)
LC424. Longest Repeating Character Replacement
424. Longest Repeating Character Replacement
Solve Type: Answer, Explain not clear
- Restate problem:
Given a string, consists only of uppercase English letters and an intk. We can replace at mostktimes in any substring so that all characters in that substring become the same. Return the maximum length of substring. - Key point:
Invariant (window validity rule): window_size ⇐ most_frequency + k
We don’s decreasemost_frequencyduring shrinking, it may become stale, but it stays an upper bound and the algorithm still finds the correct maximum length.
Expand window: update counts andmost_freq
Shrink window: from left until it is valid - Time Complexity: O(n) Space Complexity: O(n)
Space O(1) for uppercase letters only
LC567. Permutation in String
567. Permutation in String
Solve Type: Answer, passed the test case but better add if right_char in need_dic:, more clear way is using 26-count arrays
- Restate the problem: Given two strings
s1ands2of lowercase English letters, returnTrueifs2contains a substring that is a permutation ofs1, meaning it has the same length and the same character counts. Otherwise returnFalse. - Key Point: sliding window + hashmap
- First, compare the length between
s1ands2 - Count the frequency of characters in
s1asneed. - As the window move right, update the window count
- Shrink the window when size larger than
len(s1) - Track the frequency of characters in the window, when all match, return True
- First, compare the length between
- Time Complexity: O(n) Space Complexity: O(1)
- Edge case:
s1is longer thans2
LC347. Top K Frequent Elements - Medium
347. Top K Frequent Elements
Solve Type: Hint
Mistake:
heapifyis in place and returnNone, run in O(n) timeheapq.heappop(min_heap), run in O()
- Restate the problem: Given an integer array and an integer
k, Return the most frequent elements (order does not matter) - Key point: Heap + Hashmap
- Use hashmap to count the frequency of each number
- Build a min-heap of pair
(-frequency, num), so higher frequency will pop at first - Pop heap up to
ktimes (or until the heap is empty) and collect the numbers into the result.
- Complexity:
- Time Complexity: O()
- Counter is O(n)
- heapify is O(n)
heappopis O(), pop k times
- Space: O(m), m is unique numbers
- Time Complexity: O()
- Edge case:
kis larger thanlen(nums)
- 0005.Longest Palindromic Substring Solved: incorrectly initialized
max_lento 0
Jan-11
- 0014.Longest Common Prefix Solve Type: Answer with bug. and not optimal
Explain sliding window [left, right]: two types of sliding window, fixed-size and variable-size.
- Expend window, move right to include a new element into window
- Update window state
- Shrink window: if window violate the rules, move left to restore the rule.
- Update the result. when the window is valid
LC468. Validate IP Address - Medium
468. Validate IP Address
Solve Type: Solution
Mistake:
- forget split
parts = queryIP.split('.') - check whether char appear in the string
if "." in sting isdigit()
- Restate the problem: Give a string. Determine whether it is a valid IPv4 address, a valid IPv6 address, or Neither.
- Key idea: decide the format by checking the delimiter: Validate the IPv6 parts using a set of hexadecimal characters.
- Time Complexity: O(n) Space Complexity: O(1)
- Edge Case: Empty IP
LC20. Valid Parentheses - easy
20. Valid Parentheses
Solve Type: Answer with bug
Mistake: add all parentheses into stack, only need append left_side
- Restate the problem: Given a string, containing only parentheses. return True if it is a valid parentheses string (correct matched and properly nested). Otherwise, return False.
- Key idea: hashmap + stack
- Use a hash map to store matching pairs for closing brackets:
- Use a stack to store opening brackets.
- Scan(loop) the string
- If the character is an opening bracket, push it onto a stack.
- If it’s a closing bracket, the stack must not be empty and the stack top must match the required opening bracket;
- otherwise return
False.
- Time: O(n) Space: O(n)
- Edge case: Empty string
LC121. Best Time to Buy and Sell Stock
121. Best Time to Buy and Sell Stock
Solve Type: Answer, not clear explanation
- Restate the problem: Given an integer array(prices per day), choose one day to buy and a later day to sell stock, return the maximum profit
- key idea: Max difference with prefix minimum
Scan the prices once, keep the lowest prices, for each day, sell price - min_buy, track the maximum of profit. - Time complexity: O(n) Space: O(1)
- Edge case: if prices always go down, return 0
LC402. Remove K Digits
402. Remove K Digits
Solve Type: Strong Hint, weak explanation. better to redo
Mistake:
res = ''.join(stack).lstrip('0')
- Restate the problem: give me a integer string
numand intk, remove exactlykdigits in string, return the smallest possible int - Key idea: Monotolic Increasing Stack
- If the current digit < stack top, have to delete stack top to make the number lexicographically smaller
(remove larger digit earlier always yields a smaller number than keeping it and removing something later) - Leading zero need to remove
- If the current digit < stack top, have to delete stack top to make the number lexicographically smaller
- Time complexity: O(n) Space: O(n)
- Edge case:
len(num) = k, return 0
Jan-12
Leetcode
Redo:
- LC3. Answer
- LC424. Longest Repeating Character Replacement - Answer
LC102. Binary Tree Level Order Traversal
102. Binary Tree Level Order Traversal
Solve Type: Answer, bad explanation
- Restate the problem: Given a root of a binary tree, traversal tree level by level. return a list of lists, where each inner lists contains all values on the same depth.
- Key idea: BDS with a queue
Level-order traversal is naturally a BFS problem.Use a queue: push root, then repeatedly pop nodes and push their children. - Complexity
Time complexity: O(n) Space Complexity: O(n) - Edge case
root is None
Jan-14
Leetcode
LC1004.Max_Consecutive_Ones_III
- Answered, no bug
- Mistake: Space complexity should be O(1)
Jan-15
Leetcode
- Solved, explanation was not clear
0217.Contains Duplicate - Solved
0133.Clone Graph - peeked, with strong hint
- node is used, name cur instead
- need redo Jan-16
Jan-16
Redo:
- 0133.Clone Graph - forget edge case, should return
None
Review:
- 0695.Max Area of Island - Solved, debug
- 0042.Trapping Rain Water - Solved, need clear explanation and clean code
Jan-17
Review:
- 0912.Sort an Array
- Solved with bug, condition wrong
- 1636.Sort Array by Increasing Frequency