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
| /** | |
| * Adds two binary strings and returns their binary sum. | |
| * @param {string} a | |
| * @param {string} b | |
| * @return {string} | |
| */ | |
| var addBinary = function(a, b) { | |
| let result = ""; // final binary string (built from right to left) | |
| let carry = 0; // carry bit (0 or 1) |
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} poured - Total cups of champagne poured into the top glass | |
| * @param {number} query_row - Row of the target glass (0-indexed) | |
| * @param {number} query_glass - Column of the target glass (0-indexed) | |
| * @return {number} - Fullness of the target glass (0 to 1) | |
| */ | |
| var champagneTower = function(poured, query_row, query_glass) { | |
| // We only need up to 100 rows, so create a 2D array of zeros. | |
| // dp[r][c] represents the *amount of champagne that arrives* at glass (r, c), | |
| // not capped at 1. |
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 ans = 0; | |
| // 1) Case: only one distinct character (runs like "aaaa") | |
| ans = Math.max(ans, longestSingleCharRun(s)); |
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 |
NewerOlder