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 minimumDistance = function(nums) { | |
| const lastTwo = new Map(); // value -> [prevPrev, prev] | |
| let ans = Infinity; | |
| for (let i = 0; i < nums.length; i++) { | |
| const v = 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} | |
| */ | |
| var minimumDistance = function(nums) { | |
| const pos = new Map(); | |
| // Collect positions for each value | |
| for (let i = 0; i < nums.length; i++) { | |
| if (!pos.has(nums[i])) pos.set(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 | |
| * @param {number[][]} queries | |
| * @return {number} | |
| * | |
| * Hybrid solution: | |
| * - For small k (k ≤ B): use lane-based multiplicative difference arrays | |
| * - For large k (k > B): brute-force each arithmetic progression | |
| * | |
| * This avoids O(n * sqrt(n)) BigInt multiplications and stays fast in JS. |
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[][]} queries | |
| * @return {number} | |
| */ | |
| var xorAfterQueries = function(nums, queries) { | |
| const MOD = 1_000_000_007; | |
| for (const [l, r, k, v] of queries) { | |
| let idx = l; |
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} width | |
| * @param {number} height | |
| */ | |
| var Robot = function(width, height) { | |
| this.w = width; | |
| this.h = height; | |
| // Perimeter length | |
| this.P = 2 * (width + height) - 4; |
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} moves | |
| * @return {boolean} | |
| */ | |
| var judgeCircle = function(moves) { | |
| // Track horizontal (x) and vertical (y) displacement from origin (0, 0) | |
| let x = 0; | |
| let y = 0; | |
| // Process each move one by one |
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} encodedText | |
| * @param {number} rows | |
| * @return {string} | |
| */ | |
| var decodeCiphertext = function(encodedText, rows) { | |
| const n = encodedText.length; | |
| if (n === 0 || rows === 1) return encodedText; | |
| const cols = n / rows; |
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[]} robots | |
| * @param {number[]} distance | |
| * @param {number[]} walls | |
| * @return {number} | |
| */ | |
| var maxWalls = function(robots, distance, walls) { | |
| // ----------------------------- | |
| // 1. Sort robots and pair with distances | |
| // ----------------------------- |
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[][]} coins | |
| * @return {number} | |
| */ | |
| var maximumAmount = function(coins) { | |
| const m = coins.length; | |
| const n = coins[0].length; | |
| // dp[i][j][k] = max coins at (i,j) using k neutralizations | |
| const NEG_INF = -1e15; |
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} str1 // pattern of 'T' and 'F' | |
| * @param {string} str2 // substring to match or avoid | |
| * @return {string} | |
| */ | |
| var generateString = function (str1, str2) { | |
| const n = str1.length; | |
| const m = str2.length; | |
| // Final string length is n + m - 1 |
NewerOlder