Problem Input and Output Input: a string s and a list of strings wordDict Every element in wordDict is an available word, and the same word may be reused Output: return True if s can be split into one or more dictionary words; otherwise, return False The split must cover the entire string without skipping or reordering characters Examples Input: s = "leetcode", wordDict = ["leet", "code"] Output: True Explanation: "leetcode" can be split into "leet" + "code" Input: s = "applepenapple", wordDict = ["apple", "pen"] Output: True Explanation: "apple" + "pen" + "apple"; the same word may be reused Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] Output: False Explanation: some prefixes can be formed, but the split cannot cover the entire string Constraints 1 <= s.length <= 300 1 <= wordDict.length <= 1000 1 <= wordDict[i].length <= 20 s and wordDict[i] contain only lowercase English letters All strings in wordDict are unique Step 1: Make the Completed Prefix Visible Start with one concrete question. For s = "leetcode", before deciding whether the whole string can be split, can we record how much of it has already been completed? ...
LeetCode 213: House Robber II, Splitting the Circle into Two Linear Problems
Problem Input and Output Input: an integer array nums, where nums[i] is the money in the i-th house. The houses form a circle. Adjacent houses cannot both be robbed, so houses 0 and n - 1 cannot both be robbed either. Output: the maximum amount of money that can be robbed without triggering the alarm. Constraints: 1 <= nums.length <= 100, 0 <= nums[i] <= 1000. Examples Input: nums = [2,3,2] Output: 3 Explanation: Houses 0 and 2 are adjacent around the circle, so they cannot both be robbed; rob the house containing 3. Input: nums = [1,2,3,1] Output: 4 Explanation: We can rob indices 0 and 2, or indices 1 and 3. Input: nums = [1,2,3] Output: 3 This tutorial uses Python only and starts with the smallest first-last conflict. ...
LeetCode 322: Coin Change, Deriving Unbounded Knapsack DP from Smaller Amounts
Problem Requirements You are given an integer array coins, where coins[i] is a coin denomination, and a non-negative integer amount. The goal is to make a total exactly equal to amount. You may use each denomination any number of times. Return the minimum number of coins needed to make amount. If the amount cannot be made, return -1. If amount = 0, no coins are needed, so the answer is 0. The constraints are 1 <= coins.length <= 12, 1 <= coins[i] <= 2^31 - 1, and 0 <= amount <= 10^4. All coin denominations are distinct. ...
LeetCode 300: Longest Increasing Subsequence, Deriving Sequence DP from Ending at i
Problem Input and Output Input: an integer array nums Output: return the length of the longest strictly increasing subsequence in nums A subsequence preserves the relative order of the original array, but it does not need to be contiguous, so elements may be skipped If the selected indices are i_1 < i_2 < ... < i_k, they must also satisfy nums[i_1] < nums[i_2] < ... < nums[i_k] The subsequence is non-empty, so at least one element must be selected Constraints: 1 <= nums.length <= 2500, -10^4 <= nums[i] <= 10^4 Examples Input: nums = [10,9,2,5,3,7,101,18] Output: 4 Explanation: [2,5,7,101] is a strictly increasing subsequence of length 4; its elements do not need to be contiguous in the original array. Input: nums = [0,1,0,3,2,3] Output: 4 Explanation: [0,1,2,3] has length 4; [0,1,3] is also valid, but shorter. This article uses Python only. We start with the local question “how long can a subsequence ending at each position be?” and gradually turn it into a runnable solution. ...
LeetCode 416: Partition Equal Subset Sum, from the Half-Sum Target to Backward 0/1 Knapsack
Problem Requirements Given an array nums containing only positive integers, determine whether all its elements can be split into two subsets whose sums are equal. Input and Output Input: an integer array nums Output: return True if the array can be split equally; otherwise, return False Every element must belong to exactly one of the two subsets. No element may be discarded or used twice Examples Input: nums = [1,5,11,5] Output: True Explanation: [1,5,5] and [11] both have a sum of 11 Input: nums = [1,2,3,5] Output: False Explanation: the total is 11, and an odd total cannot be split equally Input: nums = [1,1] Output: True Explanation: put one 1 in each subset Constraints According to the LeetCode 416 statement: ...
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 74: Search a 2D Matrix
You are given an m x n integer matrix matrix and an integer target. Return True if target is in the matrix; otherwise, return False. The matrix satisfies two conditions: Every row is sorted in non-decreasing order. The first integer of each row is strictly greater than the last integer of the previous row. The required time complexity is O(log(m * n)). For example, consider this matrix: 1 3 5 7 10 11 16 20 23 30 34 60 When target = 3, return True. When target = 13, return False. The constraints are: ...
LeetCode 153: Find Minimum in Rotated Sorted Array
Problem Requirement The input is a non-empty integer array nums. Every element is unique, and before rotation the array was sorted in strictly increasing order. The array is rotated between 1 and nums.length times. One rotation moves the last element to the front; therefore, nums.length rotations restore the original increasing order. Return the minimum value in the rotated array. The problem requires an algorithm with O(log n) runtime. LeetCode uses this method contract: ...