LeetCode
Greedy
This section collects Hot100 greedy tutorials, focusing on which local state is enough, why it stays valid while scanning, and how that local choice proves the global answer. Recommended Reading Order LeetCode 121: Best Time to Buy and Sell Stock, derive one-transaction greedy from the historical minimum price LeetCode 55: Jump Game, use the farthest reachable range to decide reachability LeetCode 45: Jump Game II, upgrade reachability into minimum jump layers LeetCode 435: Non-overlapping Intervals, turn minimum removals into maximum kept intervals LeetCode 452: Minimum Number of Arrows, derive interval greedy from shared intersections
Union-Find
This section collects Hot100 Union-Find templates and problems, focusing on set representatives, path compression, merge conditions, connectivity checks, and connected component counting.
Trie
This section collects Hot100 Trie tutorials, focusing on node fields, child traversal, end markers, and loop invariants.
LeetCode 155: Min Stack, Keeping the Minimum in Sync With Stack State
Problem Requirement Design a stack named MinStack that supports these operations: MinStack(): initialize the stack. push(value): push value onto the stack. pop(): remove the top element. top(): return the top element. getMin(): return the minimum element in the stack. Every operation must run in O(1) time. pop, top, and getMin are called only when the stack is non-empty, so no additional empty-stack return value is needed. Example Operations: ["MinStack", "push", "push", "push", "getMin", "pop", "top", "getMin"] Arguments: [[], [-2], [0], [-3], [], [], [], []] Output: [null, null, null, null, -3, null, 0, -2] The sequence corresponds to: ...
LeetCode 20: Valid Parentheses, Why Equal Counts Are Not Enough
Problem Requirement You are given a string s containing only these six characters: ( ) { } [ ] Determine whether the string is valid. A valid string must satisfy all three conditions: Every opening bracket is closed by the same type of closing bracket. Opening brackets are closed in the correct order. Every closing bracket has a corresponding opening bracket of the same type. Return True when all conditions hold; otherwise, return False. ...
LeetCode 394: Decode String by Saving and Restoring Nested Context
Problem Requirement Given an encoded string s, return its decoded string. The encoding rule is: k[encoded_string] The encoded_string inside the brackets is repeated exactly k times, where k is a positive integer. Encodings may be nested or adjacent to ordinary lowercase letters. The problem guarantees that: The input is always valid, with matching brackets and no extra spaces. Original text contains no digits; digits only represent repeat counts. Inputs such as 3a or 2[4] do not occur. The decoded string length does not exceed 10^5. Examples Input Output "3[a]2[bc]" "aaabcbc" "3[a2[c]]" "accaccacc" "2[abc]3[cd]ef" "abcabccdcdcdef" Constraints 1 <= s.length <= 30 s contains only lowercase English letters, digits, and [] Every repeat count is in [1, 300] LeetCode provides this method signature: ...
LeetCode 503: Where Does the Right Side End in a Circular Array?
Problem Requirement You are given a circular integer array nums. Return an array answer. For every index i: answer[i] = the first value strictly greater than nums[i] when moving right If one full trip around the array finds no greater value, answer[i] = -1. “Circular” means that moving past the final position continues from index 0. An index cannot travel one full circle and use itself as its own answer. ...
LeetCode 84: Which Bar Limits a Contiguous Rectangle?
Problem Requirement You are given a non-negative integer array heights. Each heights[i] is the height of a bar with width 1, and all bars are adjacent. Return the area of the largest rectangle that can be formed in the histogram. A legal rectangle covers a contiguous interval of bars. Its width is the number of bars in that interval, and its height cannot exceed the shortest bar in the interval. ...
LeetCode 136: Single Number Without Growing Extra Storage
Problem Requirement You are given a non-empty integer array nums. Exactly one element appears once. Every other element appears exactly twice. Return the element that appears once. LeetCode provides this method contract: singleNumber(nums: List[int]) -> int The solution must also satisfy two resource requirements: O(n) time O(1) extra space Example 1 Input: nums = [2,2,1] Output: 1 2 appears twice. Only 1 appears once. Example 2 Input: nums = [4,1,2,1,2] Output: 4 Both 1 and 2 have matching copies. Only 4 remains unpaired. ...
LeetCode 191: Number of 1 Bits and How to Skip Irrelevant Zeros
Problem Requirement Given a positive integer n, return the number of 1 bits in its binary representation. This count is also called the Hamming weight. LeetCode provides this method contract: hammingWeight(n: int) -> int Example 1 Input: n = 11 Binary: 1011 Output: 3 Example 2 Input: n = 128 Binary: 10000000 Output: 1 Constraints 1 <= n <= 2^31 - 1 The input stays in the problem’s non-negative integer domain. Although the current constraints start at 1, the implementation also handles n = 0 naturally and returns 0. ...
LeetCode 338: Counting Bits by Reusing Smaller Results
Problem Requirement Given a non-negative integer n, return an array answer of length n + 1. For every index: answer[i] = the number of 1 bits in the binary representation of i The requested range includes both 0 and n. LeetCode provides this method contract: countBits(n: int) -> List[int] Example 1 Input: n = 2 Output: [0,1,1] 0 -> 0 -> 0 set bits 1 -> 1 -> 1 set bit 2 -> 10 -> 1 set bit Example 2 Input: n = 5 Output: [0,1,1,2,1,2] Constraints 0 <= n <= 10^5 Step 1: This Time We Need Every Answer From 0 to n LeetCode 191 asks for one integer’s set-bit count. For n = 5, this problem asks for all six related answers: ...
LeetCode 739: Daily Temperatures and the First Warmer Day to the Right
Problem Requirement You are given an integer array temperatures, where temperatures[i] is the temperature on day i. Return an array answer where: answer[i] = the number of days after day i until a warmer temperature If no later day is warmer, answer[i] = 0. “Warmer” means strictly greater. An equal temperature does not resolve a waiting day. LeetCode provides this method contract: dailyTemperatures(temperatures: List[int]) -> List[int] Example Input: temperatures = [73,74,75,71,69,72,76,73] Output: [1,1,4,2,1,1,0,0] Constraints 1 <= temperatures.length <= 10^5 30 <= temperatures[i] <= 100 Step 1: The Answer Is a Waiting Time, Not a Temperature Start with a smaller input: ...
LeetCode 684: Redundant Connection With Union-Find
Subtitle / Summary The key signal in this problem is not a component count. It is a failed union: if two endpoints are already connected, adding the current edge closes a cycle. Reading time: 8-10 min Tags: Union-Find, DSU, graph, tree, cycle detection SEO keywords: LeetCode 684, Redundant Connection, Union-Find, DSU, cycle detection Meta description: A pressure-first Python guide to LeetCode 684 that derives 1-indexed Union-Find, bool-returning union, ordered edge scanning, and final checks. Problem Requirement You are given an undirected graph. It started as a tree with n nodes labeled from 1 to n, then one extra edge was added. ...