1. Problem Statement (Simple Explanation): Yo u are given a string s containing only: '(' ')' You must return the length of the longest valid (well-formed) parentheses substring . A valid substring must be contiguous. It must be a correct parentheses sequence (every '(' has a matching ')' in proper order). 2. Examples: Example 1: Input: s = "(()" Valid substrings: "()" (from index 1 to 2) – length 2 Longest length: 2 Output: 2 Example 2: Input: s = ")()())" Valid substrings: "()()" (from index 1 to 4) – length 4 "()" (from index 1 to 2) "()" (from index 3 to 4) Longest length: 4 Output: 4 Example 3: Input: s = "" No parentheses → no valid substring. Output: 0 Constraints: 0 ≤ ∣s∣ ≤ 3* 10 4 s[i] is '(' or ')'. 3. Approaches Overview: There are three classic approaches: Stack-based – O(n) time, O(n) space. Two-pass counters (left-to-right and right-t...
1. Problem Statement (Simple Explanation): You’re given an array of integers nums representing a permutation. You must rearrange nums in-place to form the next lexicographically greater permutation . If such a permutation exists: transform nums into it. If not (i.e., nums is the highest permutation), rearrange nums into the lowest possible order (sorted ascending). Constraints: In-place (only constant extra memory). 1 <= nums.length <= 100 0 <= nums[i] <= 100 2. Examples: Example 1: Input: nums = [1,2,3] Permutations in order: [1,2,3] [1,3,2] <-- next [2,1,3] [2,3,1] [3,1,2] [3,2,1] Output: [1,3,2] Example 2: Input: nums = [3,2,1] This is the largest permutation. The next permutation wraps to the smallest : Output: [1,2,3] Example 3: Input: nums = [1,1,5] Next permutations: [1,1,5] [1,5,1] <-- next [5,1,1] Output: [1,5,1] 3. Intuition – Classic Next Permu...