LeetCode
Notes on LeetCode solutions.
Translated from Chinese with AI · Read the original
2. Add Two Numbers: Easy; use carrying in addition. 1028. Convert to Base -2: Medium; carrying. 1030. Next Greater Node in Linked List: Easy; use a stack. 1020. Number of Enclaves: Medium; graph algorithms work, but traversal is simpler. 83. Remove Duplicates from Sorted List: Easy; linked lists. 82. Remove Duplicates from Sorted List II: Easy; introduce a dummy predecessor node. 559. Maximum Depth of N-ary Tree: Easy; dynamic programming. 198. House Robber: Easy; propagate optimal values through an iterative dynamic program. 213. House Robber II: Easy; similarly propagate optimal values iteratively. [509. Fibonacci Number]: Easy; the Fibonacci sequence. [307. Range Sum Query - Mutable]:Easy。 [1025. Divisor Game]: Medium; dynamic programming. [1026. Maximum Difference Between Node and Ancestor]: Easy; dynamic programming. [1027. Longest Arithmetic Sequence]: Medium; dynamic programming. [1028. Recover a Tree From Preorder Traversal]: Medium; use depth and ArrayBuffer[TreeNode] to store intermediate results. 234. Palindrome Linked List: Easy. 203. Remove Linked List Elements: Easy. 160. Intersection of Two Linked Lists: Easy. 700. Search in a Binary Search Tree: Easy. 617. Merge Two Binary Trees: Medium; recursion. 350. Intersection of Two Arrays II: Easy. 704. Binary Search: Easy; binary search. 744. Find Smallest Letter Greater Than Target: Easy; binary search. 367. Valid Perfect Square: Easy; use long integers. 877. Stone Game: Medium; a matrix-based dynamic program is a useful idea. 931. Minimum Falling Path Sum: Medium; straightforward after learning the previous matrix approach. 983. Minimum Cost for Tickets: Easy; dynamic programming from the end backward. 1030. Matrix Cells in Distance Order: Easy; breadth-first traversal. I somehow forgot how to write it… BFS uses a queue, DFS a stack. 1029. Two City Scheduling: Easy; requires a little mathematical reasoning. 1031. Maximum Sum of Two Non-Overlapping Subarrays: Easy, though implementation takes more code. 1032. Stream of Characters: Easy; the approach came readily. 374. Guess Number Higher or Lower: Easy; binary search. 441. Arranging Coins: Easy; binary-search for the nearest valid value. 475. Heaters: Easy; iterate and binary-search. A rather tedious problem. 69. Sqrt(x): Easy; binary-search for the nearest value. 278. First Bad Version: Easy; same approach. 981. Time Based Key-Value Store: Easy; took a shortcut with a skip list. 647. Palindromic Substrings: Hard; Manacher’s algorithm. 413. Arithmetic Slices: Easy; a simple optimization. [1033. Moving Stones Until Consecutive]: Easy. 714. Best Time to Buy and Sell Stock with Transaction Fee: Hard; the dynamic-programming insight is difficult to find. 413. Arithmetic Slices: Easy; a simple optimization. 1034. Coloring A Border: Easy,BFS。 1035. Uncrossed Lines: Hard; matrix-based dynamic programming again, for the second time. 771. Jewels and Stones: Easy。 1103. Distribute Candies to People: Easy。 1104. Path in Zigzag Labelled Binary Tree: An interesting problem. 1093. Statistics from a Large Sample: Easy. 1094. Car Pooling: Easy. 1124. Longest Well-Performing Interval: Medium; simple dynamic programming. 1122. Relative Sort Array: Easy。 1137. N-th Tribonacci Number: Easy。 1138. Alphabet Board Path: Easy。 1139. Largest 1-Bordered Square: Medium; a tedious sequence of operations. 1144. Decrease Elements To Make Array Zigzag: Medium。 1145. Binary Tree Coloring Game: Medium; accepted on the first try. 491. Increasing Subsequences: Medium. 11. Container With Most Water: Easy. 15. 3Sum: Easy. 136. Single Number: Too easy. 796. Rotate String: Easy 50. Pow(x, n): Easy 337. House Robber III: Easy. 825. Friends Of Appropriate Ages: Easy. 701. Insert into a Binary Search Tree: Easy. 933. Number of Recent Calls: Easy. 1047. Remove All Adjacent Duplicates In String: Easy. 844. Backspace String Compare: Easy. 451. Sort Characters By Frequency: Easy. 43. Multiply Strings: Medium. 869. Reordered Power of 2: Medium. [870. Advantage Shuffle](870. Advantage Shuffle): Medium. 121. Best Time to Buy and Sell Stock: Easy. 122. Best Time to Buy and Sell Stock II: Easy. 503. Next Greater Element II: Medium. 168. Excel Sheet Column Title: Easy. 830. Positions of Large Groups: Easy. 101. Symmetric Tree: Easy. 78. Subsets: Easy. 207. Course Schedule: Medium; almost used connected components, but a stack-based DFS was needed. 257. Binary Tree Paths: Easy. 16. 3Sum Closest: Easy. 129. Sum Root to Leaf Numbers: Easy. 238. Product of Array Except Self: Easy. 686. Repeated String Match: Easy. 888. Fair Candy Swap: Easy. 1154. Day of the Year: Easy. 1155. Number of Dice Rolls with Target Sum: Medium; add caching. 1156. Swap For Longest Repeated Character Substring: Medium. 1002. Find Common Characters: Easy. 576. Out of Boundary Paths: Easy. 169. Majority Element: Easy. 229. Majority Element II: Easy. 1080. Insufficient Nodes in Root to Leaf Paths: Medium. 1146. Snapshot Array: Medium. 976. Largest Perimeter Triangle: Medium; did not initially think of sorting first. 693. Binary Number with Alternating Bits: Easy. 779. K-th Symbol in Grammar: Medium; my initial approach was wrong. 905. Sort Array By Parity: Easy. 482. License Key Formatting: Easy. 3. Longest Substring Without Repeating Characters: Not too medium. 5. Longest Palindromic Substring: Easy. 17. Letter Combinations of a Phone Number: Easy. 19. Remove Nth Node From End of List: Easy. 22. Generate Parentheses: Medium. 98. Validate Binary Search Tree: Medium. 152. Maximum Product Subarray: Easy. 31. Next Permutation: Medium. 322. Coin Change: Medium. 142. Linked List Cycle II: Medium. 33. Search in Rotated Sorted Array: Easy. 102. Binary Tree Level Order Traversal: Easy. 34. Find First and Last Position of Element in Sorted Array: Easy. 139. Word Break: Medium. 208. Implement Trie (Prefix Tree): Medium. This trie implementation performs poorly; representing each character separately is particularly inefficient. 114. Flatten Binary Tree to Linked List: Medium. 96. Unique Binary Search Trees: Medium. 94. Binary Tree Inorder Traversal: Easy. 1162. As Far from Land as Possible: Easy. Use dp. 1160. Find Words That Can Be Formed by Characters: Easy. 1161. Maximum Level Sum of a Binary Tree: Easy. 1175. Prime Arrangements: Easy. 1332. Remove Palindromic Subsequences: Easy. 1333. Filter Restaurants by Vegan-Friendly, Price and Distance: Easy. 1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance: Floyd-Warshall Algorithm to find shortest path between cities. 1341. The K Weakest Rows in a Matrix: Easy. 1342. Reduce Array Size to The Half: Easy. 1343. Maximum Product of Splitted Binary Tree: Easy.