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 findMin = function(nums) { | |
| let left = 0; | |
| let right = nums.length - 1; | |
| while (left < right) { | |
| const mid = Math.floor((left + right) / 2); |
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 isGood = function(nums) { | |
| const n = Math.max(...nums); | |
| // Condition 1: length must be n + 1 | |
| if (nums.length !== n + 1) 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} limit | |
| * @return {number} | |
| */ | |
| var minMoves = function(nums, limit) { | |
| const n = nums.length; | |
| const diff = new Array(2 * limit + 2).fill(0); | |
| for (let i = 0; i < n / 2; 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[][]} tasks | |
| * @return {number} | |
| */ | |
| var minimumEffort = function(tasks) { | |
| // Sort by (minimum - actual) descending | |
| tasks.sort((a, b) => (b[1] - b[0]) - (a[1] - a[0])); | |
| let totalActual = 0; // sum of actual costs of tasks done so far | |
| let answer = 0; // minimal initial energy required |
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 separateDigits = function(nums) { | |
| const res = []; | |
| for (let n of nums) { | |
| // If the number is a single digit, just append it | |
| if (n < 10) { |
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} target | |
| * @return {number} | |
| */ | |
| var maximumJumps = function(nums, target) { | |
| const n = nums.length; | |
| // dp[i] = maximum number of jumps needed to reach index i | |
| // Initialize all as unreachable (-Infinity) |
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 | |
| * @param {number} k | |
| * @return {number[][]} | |
| */ | |
| var rotateGrid = function(grid, k) { | |
| const m = grid.length, n = grid[0].length; | |
| const layers = Math.min(m, n) / 2; | |
| for (let layer = 0; layer < layers; layer++) { |
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 minJumps = function(nums) { | |
| const n = nums.length; | |
| if (n === 1) return 0; | |
| // 1. Build SPF (smallest prime factor) up to max(nums) | |
| const maxVal = Math.max(...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[]} nums | |
| * @return {number[]} | |
| */ | |
| var maxValue = function(nums) { | |
| const n = nums.length; | |
| const pref = Array(n); | |
| const suff = Array(n); | |
| // prefix max |
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 singly-linked list. | |
| * function ListNode(val, next) { | |
| * this.val = (val===undefined ? 0 : val) | |
| * this.next = (next===undefined ? null : next) | |
| * } | |
| */ | |
| /** | |
| * @param {ListNode} head | |
| * @param {number} k |
NewerOlder