This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {string} s | |
| * @return {number} | |
| */ | |
| var longestBalanced = function(s) { | |
| const n = s.length; | |
| let maxLen = 0; | |
| // Try every possible starting index i | |
| for (let i = 0; i < n; i++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Longest Balanced Subarray: | |
| * A subarray is balanced if the number of DISTINCT even values | |
| * equals the number of DISTINCT odd values. | |
| * | |
| * This solution uses: | |
| * - value compression | |
| * - tracking first occurrences of each value | |
| * - a segment tree with lazy propagation | |
| * - sliding left boundary |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} nums | |
| * @return {number} | |
| */ | |
| var longestBalanced = function(nums) { | |
| const n = nums.length; | |
| let maxLen = 0; | |
| // Fix a starting index i | |
| for (let i = 0; i < n; i++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Definition for a binary tree node. | |
| * function TreeNode(val, left, right) { | |
| * this.val = (val===undefined ? 0 : val) | |
| * this.left = (left===undefined ? null : left) | |
| * this.right = (right===undefined ? null : right) | |
| * } | |
| */ | |
| /** | |
| * @param {TreeNode} root |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} nums | |
| * @param {number} k | |
| * @return {number} | |
| */ | |
| var minRemoval = function(nums, k) { | |
| // Step 1: Sort the array so window are contiguous and ordered | |
| nums.sort((a, b) => a - b); | |
| let n = nums.length; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} nums | |
| * @return {number[]} | |
| */ | |
| var constructTransformedArray = function(nums) { | |
| const n = nums.length; | |
| const result = new Array(n); | |
| for (let i = 0; i < n; i++) { | |
| const steps = nums[i]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} nums | |
| * @return {number} | |
| * | |
| * This solution uses top‑down DP with memoization. | |
| * We track 4 phases (k = 0..3) of a trionic subarray: | |
| * | |
| * 0 → before starting the first increasing phase | |
| * 1 → strictly increasing | |
| * 2 → strictly decreasing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} nums | |
| * @return {boolean} | |
| */ | |
| var isTrionic = function(nums) { | |
| const n = nums.length; | |
| // Need at least 4 elements to form inc → dec → inc | |
| if (n < 4) return false; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} nums | |
| * @param {number} k | |
| * @param {number} dist | |
| * @return {number} | |
| */ | |
| var minimumCost = function(nums, k, dist) { | |
| const n = nums.length; | |
| const INF = Number.MAX_SAFE_INTEGER; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[]} nums | |
| * @return {number} | |
| */ | |
| var minimumCost = function(nums) { | |
| const n = nums.length; | |
| // We must split into 3 subarrays, so n must be at least 3. | |
| // If n == 3, there's no choice: each element starts a subarray. | |
| if (n === 3) { |
NewerOlder