1
头图

Zero title: Algorithm (leetcode, with mind map + all solutions) 300 questions (sword refers to Offer II 091) paint the house

a topic description

题目描述
题目描述

Two solutions overview (mind map)

思维导图

All three solutions

1 Scenario 1

1) Code:

 // 方案1 “自己。动态规划法”。
// 用时:10:47 - 10:57。
// 想法:
// 1)“题干有最字眼,优先考虑动态规划”。
// 2)状态定义:dp[i][j] —— 以房子i结尾 且 其刷成颜色j 的最低价格。
// 3)状态转移:dp[i][j] = Math.min(dp[i][j], dp[i - 1][k] + costs[i][j]);
// i的范围:[1, l - 1],j的范围:[0, 2],j与k的关系 j !== k 。

// 思路:
// 1)状态初始化:l = costs.length;
// dp = new Array(l).fill(0).map(v => new Array(3).fill(Number.POSITIVE_INFINITY));
// dp[0] = costs[0]; 。
// 2)核心:状态转移:dp[i][j] = Math.min(dp[i][j], dp[i - 1][k] + costs[i][j]);
// i的范围:[1, l - 1],j的范围:[0, 2],j与k的关系 j !== k 。
// 3)返回结果:Math.min(...dp[l - 1]); 。
var minCost = function(costs) {
    // 1)状态初始化:l = costs.length;
    // dp = new Array(l).fill(0).map(v => new Array(3).fill(Number.POSITIVE_INFINITY));
    // dp[0] = costs[0]; 。
    const l = costs.length;
    let dp = new Array(l).fill(0).map(v => new Array(3).fill(Number.POSITIVE_INFINITY));
    dp[0] = costs[0];
    
    // 2)核心:状态转移:dp[i][j] = Math.min(dp[i][j], dp[i - 1][k] + costs[i][j]);
    // i的范围:[1, l - 1],j的范围:[0, 2],j与k的关系 j !== k 。
    for (let i = 1; i < l; i++) {
        // 注:如下6行代码等价于如下
        // for (let j = 0; j <= 2; j++) {
        //     for (let k = 0; k <= 2; k++) {
        //         if (j !== k) {
        //             dp[i][j] = Math.min(dp[i][j], dp[i - 1][k] + costs[i][j]);
        //         }
        //     }
        // }
        dp[i][0] = Math.min(dp[i][0], dp[i - 1][1] + costs[i][0]);
        dp[i][0] = Math.min(dp[i][0], dp[i - 1][2] + costs[i][0]);
        dp[i][1] = Math.min(dp[i][1], dp[i - 1][0] + costs[i][1]);
        dp[i][1] = Math.min(dp[i][1], dp[i - 1][2] + costs[i][1]);
        dp[i][2] = Math.min(dp[i][2], dp[i - 1][0] + costs[i][2]);
        dp[i][2] = Math.min(dp[i][2], dp[i - 1][1] + costs[i][2]);
    }
    
    // 3)返回结果:Math.min(...dp[l - 1]); 。
    return Math.min(...dp[l - 1]);
};

2 Option 2

1) Code:

 // 方案2 “官方。动态规划 - 滚动数组法”。
// 参考:
// 1)https://leetcode.cn/problems/JEj789/solution/fen-shua-fang-zi-by-leetcode-solution-q0kh/

// 想法:
// 0)“题干有最字眼,优先考虑动态规划”。
// 1)状态定义:dp[i][j] —— 以房子i结尾 且 其刷成颜色j 的最低价格。
// 2)状态转移方程:dp[i][j] = min(dp[i − 1][(j + 1) % 3],dp[i − 1][(j + 2) % 3]) + costs[i][j],
// 1 ≤ i < n 和 0 ≤ j < 3。

// 思路:
// 1)状态初始化:l =costs.length; dp = costs[0]; 。
// 2)核心:状态转移 —— dp[i][j] = min(dp[i − 1][(j + 1) % 3],dp[i − 1][(j + 2) % 3]) + costs[i][j],
// 1 ≤ i < n 和 0 ≤ j < 3。
// 3)返回结果:Math.min(...dp); 。
var minCost = function(costs) {
    // 1)状态初始化:l =costs.length; dp = costs[0]; 。
    const l =costs.length;
    let dp = costs[0];

    // 2)核心:状态转移 —— dp[i][j] = min(dp[i − 1][(j + 1) % 3],dp[i − 1][(j + 2) % 3]) + costs[i][j],
    // 1 ≤ i < n 和 0 ≤ j < 3。
    for (let i = 1; i < l; i++) {
        dpNew = new Array(3).fill(0);
        for (let j = 0; j < 3; j++) {
            dpNew[j] = Math.min(dp[(j + 1) % 3], dp[(j + 2) % 3]) + costs[i][j];
        }
        dp = dpNew;
    }

    // 3)返回结果:Math.min(...dp); 。
    return Math.min(...dp);
};

Four resource sharing & more

1 Historical Articles - Overview

Article name solution reading volume
1. Two Sum 3 types in total 2.7k+
2. Add Two Numbers 4 types in total 2.7k+
3. Longest Substring Without Repeating Characters 3 types in total 2.6k+
4. Find the Median of Two Sorted Arrays 3 types in total 2.8k+
5. Longest Palindromic Substring 4 types in total 2.8k+
6. ZigZag Conversion 2 types in total 1.9k+
7. Reverse Integer 2 types in total 2.4k+
8. String to Integer (atoi) 3 types in total 4.2k+
9. Palindrome Number 3 types in total 4.3k+
11. Container With Most Water 5 in total 4.0k+
12. Integer to Roman 3 types in total 3.2k+
13. Roman to Integer 3 types in total 3.8k+
14. Longest Common Prefix 4 types in total 3.0k+
15. The Sum of Three Numbers (3Sum) 3 types in total 60.7k+
16. 3Sum Closest 3 types in total 4.7k+
17. Letter Combinations of a Phone Number 3 types in total 3.1k+
18. The sum of four numbers (4Sum) 4 types in total 11.5k+
19. Remove Nth Node From End of List 4 types in total 1.2k+
20. Valid Parentheses 2 types in total 1.8k+
21. Merge Two Sorted Lists 3 types in total 1.2k+
22. Generate Parentheses 4 types in total 1.1k+
23. Merge k Sorted Lists 4 types in total 0.9k+
24. Swap Nodes in Pairs 3 types in total 0.5k+
25. Reverse Nodes in k-Group 5 in total 1.3k+
26. Remove Duplicates from Sorted Array 4 types in total 1.3k+
27. Remove Element 4 types in total 0.4k+
28. Implement strStr() (Implement strStr()) 5 in total 0.8k+
29. Divide Two Integers 4 types in total 0.6k+
30. Substring with Concatenation of All Words 3 types in total 0.6k+
31. Next Permutation 2 types in total 0.8k+
32. Longest Valid Parentheses 2 types in total 1.4k+
33. Search in Rotated Sorted Array 3 types in total 1.0k+
34. Find First and Last Position of Element in Sorted Array 3 types in total 0.5k+
35. Search Insert Position 3 types in total 0.3k+
36. Valid Sudoku 1 in total 0.6k+
38. Count and Say 5 in total 1.1k+
39. Combination Sum 3 types in total 1.4k+
40. Combination Sum II 2 types in total 1.6k+
41. First Missing Positive 3 types in total 1.2k+
53. Maximum Subarray Sum (Maximum Subarray) 3 types in total 0.3k+
88. Merge Sorted Array 3 types in total 0.4k+
102. Binary Tree Level Order Traversal 3 types in total 0.4k+
146. LRU Cache (LRU Cache) 2 types in total 0.5k+
160. Intersection of Two Linked Lists 2 types in total 0.1k+
200. Number of Islands 4 types in total 0.1k+
206. Reverse Linked List 3 types in total 1.0k+
215. Kth Largest Element in an Array 3 types in total 0.5k+
236. Lowest Common Ancestor of a Binary Tree 3 types in total 0.1k+
2119. A Number After a Double Reversal 2 types in total 0.3k+
2120. Execution of All Suffix Instructions Staying in a Grid 1 in total 0.4k+
2124. Check if All A's Appears Before All B's 4 types in total 0.4k+
2125. Number of Laser Beams in a Bank 3 types in total 0.3k+
2126. Destroying Asteroids 2 types in total 1.6k+
2129. Capitalize the Title 2 types in total 0.6k+
2130. Maximum Twin Sum of a Linked List 2 types in total 0.6k+
2133. Check if Every Row and Column Contains All Numbers 1 in total 0.6k+

刷题进度 - LeetCode:545 / 2678 、《剑指offer》:66 / 66

2 Introduction to bloggers

Code Farmer Sanshao, a blogger dedicated to writing minimalist but complete problem solutions (algorithms ).
Focus on one question, multiple solutions, structured thinking , welcome to brush through LeetCode ~


码农三少
54 声望8 粉丝