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 repeatedNTimes = function(nums) { | |
| // We'll use a Set to track which numbers we've seen. | |
| const seen = new Set(); | |
| // Walk through each number in the array | |
| for (let num of nums) { |
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[]} digits | |
| * @return {number[]} | |
| */ | |
| var plusOne = function(digits) { | |
| // Start from the last digit and move left | |
| for (let i = digits.length - 1; i >= 0; i--) { | |
| // If the current digit is less than 9, we can safely increment it | |
| // and return immediately because no carry is needed. |
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} row | |
| * @param {number} col | |
| * @param {number[][]} cells | |
| * @return {number} | |
| */ | |
| var latestDayToCross = function(row, col, cells) { | |
| // Helper: check if it's possible to cross on a given day | |
| function canCross(day) { | |
| // Build a grid where 1 = water, 0 = land |
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} bottom | |
| * @param {string[]} allowed | |
| * @return {boolean} | |
| */ | |
| var pyramidTransition = function(bottom, allowed) { | |
| // STEP 1: Build a mapping from (left,right) -> list of possible tops | |
| // Example: "ABC" means A+B -> C | |
| const map = new Map(); | |
| for (let pattern of allowed) { |
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[][]} grid - A 2D matrix sorted in non-increasing order | |
| * @return {number} - Total count of negative numbers in the matrix | |
| */ | |
| var countNegatives = function (grid) { | |
| let count = 0; | |
| // Loop through each row | |
| for (let row = 0; row < grid.length; row++) { |
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
| /** | |
| * Given a string of 'Y' (customer arrives) and 'N' (no customer), | |
| * find the hour to close the shop that minimizes penalty. | |
| * | |
| * Penalty rules: | |
| * - Staying open while no customers come → +1 penalty for each 'N' | |
| * - Closing early while customers would have come → +1 penalty for each 'Y' | |
| * | |
| * @param {string} customers | |
| * @return {number} |
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[]} apple | |
| * @param {number[]} capacity | |
| * @return {number} | |
| */ | |
| var minimumBoxes = function(apple, capacity) { | |
| // 1. Compute the total number of apples across all packs. | |
| // Since aplles can be split across boxes, only the total matters. | |
| let totalApples = 0; | |
| for (let a of apple) { |
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[]} strs | |
| * @return {number} | |
| */ | |
| var minDeletionSize = function(strs) { | |
| const n = strs.length; // number of rows | |
| const m = strs[0].length; // number of columns (all strings same length) | |
| // dp[i] = length of the longest valid chain ending at column i | |
| const dp = Array(m).fill(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[]} strs | |
| * @return {number} | |
| */ | |
| var minDeletionSize = function(strs) { | |
| const n = strs.length; | |
| const m = strs[0].length; | |
| // sorted[i] = true means strs[i] < strs[i+1] is already determined | |
| const sorted = Array(n - 1).fill(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 {string[]} strs | |
| * @return {number} | |
| */ | |
| var minDeletionSize = function(strs) { | |
| // Number of rows (strings) | |
| const n = strs.length; | |
| // Number of columns (length of each string) | |
| const m = strs[0].length; |