// Intervals that can be merged must be contiguous. var merge = function (intervals) { // Sort the intervals and start from the minimum value. intervals.sort((a, b) => a[0] - b[0]); let res = [];
for (let i = 0; i < intervals.length; i++) { let len = res.length; // compare with the last value of res. if (res.length === 0 || res[len - 1][1] < intervals[i][0]) { res.push([...intervals[i]]); } else { // Take the maximum value when merge. res[len - 1][1] = Math.max(res[len - 1][1], intervals[i][1]); } } return res; };
// Traverse the array with each value in the first row as starting point in the upper right corner, and finally reverse the odd traversal array. var findDiagonalOrder = function (mat) { let [h, w] = [mat.length, mat[0].length], // n is the number of traversal n = h + w - 1, res = [];
for (let k = 0; k < n; k++) { let j, i; // When hight is larger than the width, the starting point moves down. if (k >= w) { j = w - 1; i = k - (w - 1); } else { j = k; i = 0; }
let sub = []; while (j >= 0 && i < h) { sub.push(mat[i][j]); i++; j--; } if (!((i + j) % 2)) { sub = sub.reverse(); } // console.log(res, sub); res = res.concat(sub); }
// The longest common prefix is continuously updated by the first string standard. var longestCommonPrefix = function (strs) { if (strs.length === 0) return""; if (strs.length === 1) return strs[0]; let res = strs[0]; for (let i = 1; i < strs.length; i++) { for (let j = 0; j < res.length; j++) { // console.log(j, res[j], strs[i][j]); if (res[j] !== strs[i][j]) { res = res.substring(0, j); // console.log(res); } } } return res; };
// Double traversal, putting different values into different arrays, filtering according to the rules. var isValidSudoku = function (board) { let rowArr = newArray(9).fill(0).map(() =>newSet()), colArr = newArray(9).fill(0).map(() =>newSet()), boxArr = newArray(9).fill(0).map(() =>newSet());
for (let i = 0; i < 9; i++) { for (let j = 0; j < 9; j++) { // if there is not a number, skip it. if (board[i][j] === ".") continue; // according the i and j to judge the index of sub-box. let boxIndex = Math.floor(i / 3) * 3 + Math.floor(j / 3); // check row if (rowArr[i].has(board[i][j])) returnfalse; // check column if (colArr[j].has(board[i][j])) returnfalse; // check box if (boxArr[boxIndex].has(board[i][j])) returnfalse;
// add the new valid value. rowArr[i].add(board[i][j]); colArr[j].add(board[i][j]); boxArr[boxIndex].add(board[i][j]); } } // console.log(rowArr); // console.log(colArr); // console.log(boxArr); returntrue; };
// From 1 to amount, find the optimal solution for each step. var coinChange = function (coins, amount) { if (amount <= 0) return0; if (coins.length === 0) return -1;
let len = coins.length, dp = [0];
for (let i = 1; i <= amount; i++) { // Initial dp.push(Infinity); // optimal solution of currenting money for (let j = 0; j < len; j++) { if (i >= coins[j] && dp[i - coins[j]] !== Infinity) { // with the different coin update the dp[i] to minimum constantly. dp[i] = Math.min(dp[i - coins[j]] + 1, dp[i]); } } // console.log(dp); } return dp[amount] === Infinity ? -1 : dp[amount]; };
Transition equations: $F(j) = F(i) \ and \ (i+u(i)) \geq j \ \ (0\le i \lt j)$
F(j) records whether the position can be reached. u(i) is the displacement at i.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Record whether current position can be reached. var canJump = function (nums) { if (!nums || nums.length === 0) returnfalse; let dp = [true], len = nums.length;
for (let i = 1; i < len; i++) { dp.push(false); for (let j = 0; j < i; j++) { if (dp[j] && j + nums[j] >= i) { dp[i] = true; break; } } }
F(i,j) is the number of paths to record the current location.
1 2 3 4 5 6 7 8 9 10 11 12 13
// Current number of path is equal to the sum of values of upper cell and left cell. var uniquePaths = function (m, n) { let dp = newArray(m).fill(0).map(() =>newArray(n).fill(0));
for (let i = 0; i < m; i++) { for (let j = 0; j < n; j++) { if (i === 0 || j === 0) dp[i][j] = 1; else dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } } // console.log(dp); return dp[m - 1][n - 1]; };
var uniquePathsWithObstacles = function (obstacleGrid) { let [h, w] = [obstacleGrid.length, obstacleGrid[0].length]; let dp = newArray(h).fill(0).map(() =>newArray(w).fill(0));
for (let i = 0; i < h; i++) { for (let j = 0; j < w; j++) { if (obstacleGrid[i][j] === 1) dp[i][j] = 0; else { if (i === 0 && j === 0) { dp[i][j] = 1; } elseif (obstacleGrid[i][j] !== 1) { if (i >= 1) dp[i][j] += dp[i - 1][j]; if (j >= 1) dp[i][j] += dp[i][j - 1]; } } } }
/** * @param {number[]}prices * @return {number} */ var maxProfit = function (prices) { // judge the boundary conditions if (!prices || prices.length === 1) return0; let len = prices.length, dp = [0], minPrice = prices[0];
for (let i = 1; i < len; i++) { // find the minimum price minPrice = Math.min(minPrice, prices[i]); // record the maximum profit of per day dp.push(Math.max(dp[i - 1], prices[i] - minPrice)); } return dp[len - 1]; };
// Constantly update maximum distance, observe whether the maximum distance can reach the finish line. var canJump = function (nums) { if (nums.length === 1) returntrue;
let maxPos = nums[0], len = nums.length;
// Whether you can continue to expand scope within scope of accessibility. for (let i = 0; i <= maxPos; i++) { maxPos = Math.max(maxPos, i + nums[i]); // console.log(i, maxPos); if (i < len - 1 && maxPos >= len - 1) returntrue; }
const maxAreaOfIsland = (grid) => { // up down left right const dx = [0, 0, -1, 1]; const dy = [-1, 1, 0, 0]; if (!grid) return0; // width height const [w, h] = [grid[0].length, grid.length]; // curMaxLen is the number of a block let res = 0, curMaxLen = 0;
const dfs = (x, y) => { // Judge the boundary conditions if (x >= h || y >= w || x < 0 || y < 0 || !grid[x][y]) return; // Mark the point reached grid[x][y] = 0; curMaxLen++;
// Spread to surrounding points for (let i = 0; i < 4; i++) { dfs(x + dx[i], y + dy[i]); } };
for (let i = 0; i < h; i++) { for (let j = 0; j < w; j++) { // if the value is 0 if (!grid[i][j]) continue; dfs(i, j); res = Math.max(res, curMaxLen); // Need to clear the length after traversing a part curMaxLen = 0; } }
const permute = function (nums) { const res = []; const dfs = (curPath) => { // All number has been recorded if (curPath.length === nums.length) return res.push([...curPath]); // Traverse array to find the unrecorded numbers for (let i = 0; i < nums.length; i++) { if (curPath.indexOf(nums[i]) !== -1) continue; curPath.push(nums[i]); dfs(curPath); curPath.pop(); } }; // Initialize the path array dfs([]); return res; };
-------------The end of this article, thanks for reading-------------