LeetCode 【数据结构与算法专栏】【动态规划】

刷题笔记

动态规划leetcode专栏

dp入门问题

leetcode 509 斐波那契数

class Solution {
public:
    int fib(int n) {
        if(n <= 1) {
            return n;
        }
        return fib(n-1) + fib(n-2);
    }
};
class Solution {
public:
    vector<int> memory = vector<int>(40, -1);
    int fib(int n) {
        if(n <= 1) {
            return n;
        }
        if (memory[n] != -1) return memory[n];
        int a = fib(n-1);
        memory[n-1] = a;
        int b = fib(n-2);
        memory[n-2] = b;
        return a + b;
    }
};
class Solution {
public:

//1、确定dp数组,以及下标的含义
//2、确定dp数组的递推公式
//3、dp数组如何进行初始化
//4、dp数组的遍历顺序
//5、举例推导dp数组

    int fib(int n) {
        if (n <= 1) return n;
        vector<int> dp(n+1, 0);
        dp[0] = 0;
        dp[1] = 1;
        for (int i = 2; i <= n; i++) {
            dp[i] = dp[i-1] + dp[i-2];
        }
        return dp[n];
    }
};
class Solution {
public:
    int fib(int n) {
        if(n <= 1) {
            return n;
        }
        int state1 = 0;
        int state2 = 1;
        for(int i = 2; i <= n; i++) {
            int nextState = state1 + state2;
            state1 = state2;
            state2 = nextState;
        }
        return state2;
    }
};

leetcode 70 爬楼梯

//1、确定dp数组以及下标的含义
//2、确定递推公式
//3、dp数组如何初始化
//4、确定遍历顺序
//5、举例推导dp数组
class Solution {
public:
    int climbStairs(int n) {
        if(n <= 2) return n;
        vector<int> dp(n+1);
        dp[1] = 1;
        dp[2] = 2;
        for(int i = 3; i <= n; i++) {
            dp[i] = dp[i-1] + dp[i-2];
        }
        return dp[n];
    }
};
class Solution {
public:
    int climbStairs(int n) {
        if(n <= 2) return n;
        int state1 = 1;
        int state2 = 2;
        for(int i = 3; i <= n; i++) {
            int nextState = state2 + state1;
            state1 = state2;
            state2 = nextState;
        }
        return state2;
    }
};

leetcode 746 使用最小花费爬楼梯

//dp[i]的定义:第i个台阶所花费的最少体力为dp[i]。
//递推公式:dp[i] = min(dp[i-1], dp[i-2]) + cost[i];
//dp数组的初始化
class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost) {
        int sumCost = 0;
        int N = cost.size();
        vector<int> dp(N);
        dp[0] = cost[0];
        dp[1] = cost[1];
        for(int i = 2; i < N; i++) {
            dp[i] = min(dp[i-1], dp[i-2]) + cost[i];
        }
        return min(dp[N-1], dp[N-2]);
    }
};
class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost) {
        int n = cost.size();
        vector<int> dp(n+1, 0);
        dp[0] = 0;
        dp[1] = 0;
        for (int i = 2; i <= n; i++) {
            dp[i] = min(dp[i-1] + cost[i-1], dp[i-2] + cost[i-2]);
        }
        return dp[n];
    }
};

leetcode 62 不同路径

//机器人从(0,0)位置出发,到(m-1,n-1)终点。
//dp数组以及下标的定义:表示从(0,0)出发,到(i,j)有dp[i][j]条不同的路径。
//状态转移公式:dp[i][j] = dp[i-1][j] + dp[i][j-1]; 
//dp数组初始化:dp[0][j]=1 dp[i][0]=1
class Solution {
public:
    int uniquePaths(int m, int n) {
        vector<vector<int>> dp(m, vector<int>(n, 0));
        for(int i = 0; i < m; i++) dp[i][0] = 1;
        for(int j = 0; j < n; j++) dp[0][j] = 1;
        for(int i = 1; i < m; i++) {
            for(int j = 1; j < n; j++) {
                dp[i][j] = dp[i-1][j] + dp[i][j-1];
            }
        }
        return dp[m-1][n-1];
    }
};
//超时的代码,递归实现
class Solution {
public:
    int uniquePaths(int m, int n) {
        return dfs(1, 1, m, n);
    }

    int dfs(int i, int j, int m, int n) {
        if(i > m || j > n) return 0;
        if(i == m && j == n) return 1;
        return dfs(i+1, j, m, n) + dfs(i, j+1, m, n);
    }
};
//回溯
//超时的代码,将机器人行走路径抽象为树模型,采用深度优先搜索的方式搜索到达符合要求的叶子节点数目
class Solution {
public:
    int uniquePaths(int m, int n) {
        int res = 0;
        BackTracking(1, m, 1, n, res);
        return res;
    }
    void BackTracking(int i, int m, int j, int n, int& res) {
        if (i == m && j == n) {
            res++;
        }      
        if (i > m || j > n) {
            return;
        }  
        BackTracking(i+1, m, j, n, res);
        BackTracking(i, m, j+1, n, res);
    }
};

leetcode 63 不同路径 II

机器人从(0,0)位置出发,到(m-1,n-1)终点。
dp数组以及下标的定义:表示从(0,0)出发,到(i,j)有dp[i][j]条不同的路径。
状态转移公式:dp[i][j] = dp[i-1][j] + dp[i][j-1];
dp数组初始化:dp[0][j]=1 dp[i][0]=1
对于障碍的处理,就是标记对应的dp数组保持初始值0就可以了,需要注意初始化时对障碍物的处理

class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
        int m = obstacleGrid.size();
        int n = obstacleGrid[0].size();
        vector<vector<int>> dp(m, vector<int>(n, 0));
        for(int i = 0; i < m && obstacleGrid[i][0] == 0; i++) dp[i][0] = 1;
        for(int j = 0; j < n && obstacleGrid[0][j] == 0; j++) dp[0][j] = 1;
        for(int i = 1; i < m; i++) {
            for(int j = 1; j < n; j++) {
                if(obstacleGrid[i][j] == 0) {
                    dp[i][j] = dp[i-1][j] + dp[i][j-1];
                }
            }
        }
        return dp[m-1][n-1];
    }
};

leetcode 64 最小路径和

class Solution {
    public int minPathSum(int[][] grid) {
        // 定义dfs(i,j) 表示从左上角到第i行第j列这个格子(记作(i,j))的最小价值和。
        return dfs(grid.length - 1, grid[0].length - 1, grid);
    }
    private int dfs(int i, int j, int[][] grid) {
        if (i < 0 || j < 0) {
            return Integer.MAX_VALUE;
        }
        if (i == 0 && j == 0) {
            return grid[i][j];
        }
        return Math.min(dfs(i, j - 1, grid), dfs(i - 1, j, grid)) + grid[i][j];
    }
}
class Solution {
    public int minPathSum(int[][] grid) {
        // 定义dfs(i,j) 表示从左上角到第i行第j列这个格子(记作(i,j))的最小价值和。
        int m = grid.length;
        int n = grid[0].length;
        int[][] memo = new int[m][n];
        for (int i = 0; i < m; i++) {
            Arrays.fill(memo[i], -1);    // -1表示没有计算过
        }
        return dfs(m - 1, n - 1, grid, memo);
    }
    private int dfs(int i, int j, int[][] grid, int[][] memo) {
        if (i < 0 || j < 0) {
            return Integer.MAX_VALUE;
        }
        if (i == 0 && j == 0) {
            return grid[i][j];
        }
        if (memo[i][j] != -1) {
            return memo[i][j];
        }
        memo[i][j] = Math.min(dfs(i, j - 1, grid, memo), dfs(i - 1, j, grid, memo)) + grid[i][j];
        return memo[i][j];
    }
}
class Solution {
    public int minPathSum(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        // 从左上角(0,0)的位置到当前(m,n)位置的路径和是最小的,等于dp[m][n]
        int[][] dp = new int[m][n];
        // dp数组初始化
        dp[0][0] = grid[0][0];
        for (int i = 1; i < m; i++) {
            dp[i][0] = dp[i-1][0] + grid[i][0];
        }
        for (int j = 1; j < n; j++) {
            dp[0][j] = dp[0][j-1] + grid[0][j];
        }
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1]) + grid[i][j];
            }
        }
        return dp[m-1][n-1];
    }
}

leetcode 343 整数拆分

dp定义:dp[i]表示给定整数i拆分后乘积的最大值。(至少拆分成两个整数的乘积和)
状态转移: 让j从1开始遍历,dp[i]可通过j * dp[i - j]得到,同时注意dp[i]对应的值是经过拆分了的,所以还应判断两个数拆分的情况,即j*(i-j)的值,取最大即可。
在遍历过程还要比较当前这两者的较大值更大,还是上一步遍历得到的dp[i]更大,因此得到递推公式:dp[i] = max(dp[i], max((i - j) * j, dp[i - j] * j)).为什么递推公式不是 dp[i]=max(i*(i-j),i*dp[i-j]),是因为,dp[i]在最里层循环被计算了很多次
初始化:严格来说,初始化dp[0]和dp[1]无意义,从2开始拆分才有意义,因此初始化dp[2] = 1,为了计算推导还是初始化dp[1] = 1
确定遍历顺序
举例推导dp数组

class Solution {
public:
    int integerBreak(int n) {
        vector<int> dp(n+1);
        dp[1] = 1;
        for(int i = 2; i <= n; i++) {
            for(int j = 1; j < i; j++) {
                 dp[i] = max(dp[i], max((i - j) * j, dp[i - j] * j));
            }
        }
        return dp[n];
    }
};

leetcode 96 不同的二叉搜索树

//dp[i]定义:1到i为节点组成的二叉搜索树的个数为dp[i]。
//递推关系:dp[i] += dp[以j为头结点左子树节点数量] * dp[以j为头结点右子树节点数量]
//dp[3] = dp[2] * dp[0] + dp[1] * dp[1] + dp[0] * dp[2]这三部分分别是以1、2、3为头结点
//dp[i] += dp[j-1]*dp[i-j]
//首先一定是遍历节点数,从递归公式:dp[i] += dp[j - 1] * dp[i - j]可以看出
//节点数为i的状态是依靠 i之前节点数的状态。那么遍历i里面每一个数作为头结点的状态,用j来遍历。
class Solution {
public:
    int numTrees(int n) {
        vector<int> dp(n + 1);
        dp[0] = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                dp[i] += dp[j - 1] * dp[i - j];
            }
        }
        return dp[n];
    }
};
//超时代码
//二叉搜索树的中序遍历的结果一定是从1到n
//int dfs(int start, int end) 返回从start到end之间的结点可以构成的子树的数目
class Solution {
public:
    int numTrees(int n) {
        return dfs(1, n);
    }
    int dfs(int start, int end) {
        if (start >= end) {
            return 1;
        }
        int res = 0;
        for (int i = start ; i <= end; i++) {            //以i为根节点
            res += dfs(start, i - 1) * dfs(i + 1, end);
        }
        return res;
    }
};

0-1背包问题

0-1背包——暴力搜索、回溯法和剪枝限界

0-1背包——动态规划,二维和一维dp数组

纯01背包问题:给我们一个容器,问我们装满该容器的最大价值是多少?
暴力解法求背包中的最佳组合,返回最大价值
i是物品编号,j是背包可以承受的最大重量

//return the bag max value
int dfsVer2(int i, int j, vector<int>& weight, vector<int>& value) {
	if (i == 0 || j == 0) {
		return 0;
	}
	if (weight[i] > j) {
		return dfsVer2(i - 1, j, weight, value);
	}
	int value1 = dfsVer2(i - 1, j, weight, value);
	int value2 = dfsVer2(i - 1, j - weight[i], weight, value) + value[i];
	return max(value1, value2);
}
//using dfs search total result about the value of the bag
void dfsVer1(vector<int>& result, int totval, int i, int j, vector<int>& weight, vector<int>& value) {
	if (i < 0 || j < 0) {
		return;
	}
	if (i == 0 || j == 0) {
		result.push_back(totval);
		return;
	}
	if (weight[i] > j) {
		dfsVer1(result, totval, i - 1, j, weight, value);
	}
	dfsVer1(result, totval, i - 1, j, weight, value);
	dfsVer1(result, totval+value[i], i - 1, j - weight[i], weight, value);
}

leetcode 416 分割等和子集

采用回溯法,但是会超时

//本题要求集合里能否出现总和为 sum / 2 的子集
class Solution {
public:
    bool canPartition(vector<int>& nums) {
        int res = 0;
        for (int i = 0; i < nums.size(); i++) {
            res += nums[i];
        }
        if (res % 2 == 1) return false;
        int target = res / 2; 
        return BackTracking(0, target, nums);
    }

    bool BackTracking(int i, int target, vector<int>& nums) {
        if (i >= nums.size() || target < 0) {
            return false;
        }
        if (target == 0) {
            return true;
        }
        if(BackTracking(i + 1, target - nums[i], nums)) {
            return true;
        }
        else {
            if (BackTracking(i + 1, target, nums)) {
                return true;
            }
        }
        return false;
    }
};
class Solution {
public:
    bool canPartition(vector<int>& nums) {
        int res = 0;
        for (int i = 0; i < nums.size(); i++) {
            res += nums[i];
        }
        if (res % 2 == 1) return false;
        int target = res / 2; 
        bool flag = false;
        BackTracking(0, target, nums, flag);
        return flag;
    }

    void BackTracking(int i, int target, vector<int>& nums, bool& flag) {
        if (i >= nums.size()) {
            return;
        }
        if (target == 0) {
            flag = true;
            return;
        }
        else if (target < 0) {
            return;
        }
        BackTracking(i + 1, target - nums[i], nums, flag);
        BackTracking(i + 1, target, nums, flag);
    }
};

采用动态规划二维dp数组

//01背包问题,可以把问题抽象为给定一个数组和一个容量为target的背包,能否找到一种组合使背包装满。
//数组的下标相当于物品编号,数组值相等于物品value
//dp[i][j]: 在nums[0, i]集合中选取元素其和等于j,若能找到dp[i][j] = true;(找到物品填满容量j的背包)
//dp[i][j] = dp[i-1][j] || dp[i-1][j-nums[i]]
class Solution {
public:
    bool canPartition(vector<int>& nums) {
        int sum = 0;
        for (int i = 0; i < nums.size(); i++) {
			sum += nums[i];
		}
		if ((sum & 1) == 1)
			return false;
		int target = sum / 2;
        vector<vector<bool>> dp(nums.size(), vector<bool>(target+1, false));

        for(int j = 0; j <= target; j++) {
            dp[0][j] = (nums[0] == j ? true : false);
        }

        for(int i = 1; i < nums.size(); i++) {
            for(int j = 0; j <= target; j++) {
                if(j >= nums[i]) {
                    dp[i][j] = dp[i-1][j] || dp[i-1][j-nums[i]];
                }
                else {
                    dp[i][j] = dp[i-1][j];
                }
            }
        }
        return dp[nums.size()-1][target];
    }
};

采用滚动数组将二维降到一维

//dp[i][j]: 在nums[0, i]集合中选取元素其和等于j,若能找到dp[i][j] = true;(找到物品填满容量j的背包)
//dp[j]表示当前能否填满容量为j的背包,若能填满dp[j] == true,否则为false
//状态转移方程为 dp[j] = dp[j] || dp[j-nums[i]]
class Solution {
public:
    bool canPartition(vector<int>& nums) {
        int sum = 0;
        for (int i = 0; i < nums.size(); i++) {
			sum += nums[i];
		}
		if ((sum & 1) == 1)
			return false;
		int target = sum / 2;
        vector<bool> dp(target+1, false);

        for(int i = 0; i < nums.size(); i++) {
            for(int j = target; j >= nums[i]; j--) {
                if(i == 0) {
                    dp[j] = (nums[0] == j ? true : false);
                }
                dp[j] = dp[j] || dp[j-nums[i]];
            }
        }
        return dp[target];
    }
};

对于dp数组的定义不同,状态转移方程也不同,按照01背包的定义
dp[j]定义为装满 j 容量的背包所获得的最大价值,将数组中的元素值看成value,则如果dp[target] == target,则返回true。
递推公式:dp[j] = max(dp[j], dp[j-nums[i]] + nums[i]);

class Solution {
public:
    bool canPartition(vector<int>& nums) {
        int sum = 0;
        for (int i = 0; i < nums.size(); i++) {
			sum += nums[i];
		}
		if ((sum & 1) == 1)
			return false;
		int target = sum / 2;
        vector<int> dp(target+1, 0);

        for(int i = 0; i < nums.size(); i++) {
            for(int j = target; j >= nums[i]; j--) {
                dp[j] = max(dp[j], dp[j-nums[i]] + nums[i]);
            }
        }
        return dp[target] == target;
    }
};

leetcode 1046 最后一块石头的重量

class Solution {
public:
    int lastStoneWeight(vector<int>& stones) {
        multiset<int, greater<int>> st(stones.begin(), stones.end());
        while (st.size() > 1) {
            int ele1 = *(st.begin());
            st.erase(st.begin());
            int ele2 = *(st.begin());
            st.erase(st.begin());    
            int tmp = ele1 - ele2;
            st.insert(tmp);
        } 
        return *(st.begin());
    }
};
class Solution {
public:
    int lastStoneWeight(vector<int>& stones) {
        priority_queue<int> pq(stones.begin(), stones.end());
        while (pq.size() > 1) {
            int ele1 = pq.top();
            pq.pop();
            int ele2 = pq.top();
            pq.pop();
            int tmp = ele1 - ele2;
            if (tmp != 0) pq.push(tmp);
        }
        return pq.empty() ? 0 : pq.top();
    }
};

leetcode 1049 最后一块石头的重量 II

01背包问题,将石头分成两堆,求两堆石头的最小差值,两堆石头的总和为sum,则其中肯定有一堆石头的重量<=sum/2。这一堆石头的重量为dp[j],dp[j] <= sum/2,本质上我们需要求dp[j]的最大值,即最接近甚至等于sum/2的值,这样才能保证两堆石头的差值最小,两堆石头的差值等于2*(sum/2 - dp[sum/2])。
二维dp:
dp[i][j] 定义为从下标为[0-i]的石头中任意取,装满背包承重量j所产生的最大价值,这里石头的重量==石头的价值,所以dp[i][j] <= j。dp[i][j] = max(dp[i-1][j], dp[i-1][j-stones[i]] + stones[i])
一维dp:
dp[j] 定义为装满背包容量 j 所产生的最大价值,这里的价值就是石头的重量,value = weight。
dp[j] = max(dp[j], dp[j-stones[i]]+stones[i])

class Solution {
public:
    //dp[i][j]从编号0到i的石头中选择,装满背包j的容量,所能够产生的最大价值
    //weight和value数组相同
    //dp[i][j] = max(dp[i-1][j], dp[i][j - stones[i]] + stones[j] )
    int lastStoneWeightII(vector<int>& stones) {    
        int sum = 0;
        int N = stones.size();
        for (int i = 0; i < N; i++) {
            sum += stones[i];
        }
        int target = sum / 2;
        vector<vector<int>> dp(N, vector<int>(target + 1, 0));
        for (int j = target; j >= stones[0]; j--) {
            dp[0][j] = stones[0];
        }
        for (int i = 1; i < N; i++) {              //注意这里是从1开始
            for (int j = 0; j <= target; j++) {
                if (j < stones[i]) {
                    dp[i][j] = dp[i-1][j];
                }
                else {
                    dp[i][j] = max(dp[i-1][j], dp[i-1][j - stones[i]] + stones[i]);
                }
            }
        }
        int res = (sum - dp[N-1][target]) - dp[N-1][target];
        return res;
    }
};
class Solution {
public:
    int lastStoneWeightII(vector<int>& stones) {
        int sum = accumulate(stones.begin(), stones.end(), 0);
        vector<int> dp(sum/2+1, 0);

        for(int i = 0; i < stones.size(); i++) {      //注意这里一定是从0开始不然就错了
            for(int j = sum/2; j >= stones[i]; j--) {
                dp[j] = max(dp[j], dp[j-stones[i]]+stones[i]);
            }
        }
        return sum - 2 * dp[sum/2];
    }
};

leetcode 494 目标和

回溯法暴力搜索

class Solution {
public:
    int findTargetSumWays(vector<int>& nums, int target) {
        int result = 0;
        backtracking(0, result, 0, target, nums);
        return result;
    }

    void backtracking(int k, int& result, int curSum, int target, vector<int>& nums) {
        if(k >= nums.size()) {
            if(curSum == target) {
                result++;
            }
            return;
        }
        backtracking(k+1, result, curSum + nums[k], target, nums);
        backtracking(k+1, result, curSum - nums[k], target, nums);
    }
};

问题简化:给我们一个固定容量的背包,用物品装满这个背包有多少种方法,每个物品最多只选1次?

在一个集合当中我们需要分出两个子集:
加法集合和减法集合
left + right = sum; 加法集合加减法集合
left - right = target;
right = sum - left;
left - (sum - left) = target;
left = (target + sum) / 2 加法集合中全部元素的和
求最终得到的不同表达式的数目就可以转换成求nums中的元素组合成left的所有情况。
dp[i][j]:使用下标为[0, i]的nums[i]能够凑满j(包括j)这么大容量的包,有dp[i][j]种方法。
dp[i][j] = dp[i-1][j](不需要nums[i]物品就可以填满容量j的包的方法) + dp[i-1][j-nums[i]](需要nums[i]才可以填满容量j的包的方法)。第i个物品可以选择用它来填j空间,也可以不选择它填j空间,两种决策的方法进行相加。
dp数组初始化,dp[0][0]为1,装满容量为0的背包,有一种方法,就是装0件物品。dp[0][j],看第一个元素的大小情况,进行赋值。

class Solution {
public:
    int findTargetSumWays(vector<int>& nums, int target) {
        int sum = accumulate(nums.begin(), nums.end(), 0);
        if ((sum+target) % 2 == 1) return 0;
        if (abs(target) > sum) return 0;
        int left = (sum + target) / 2;
        vector<vector<int>> dp(nums.size(), vector<int>(left + 1, 0));
        dp[0][0] = 1;
        for(int j = 0; j <= left; j++) {
            if(nums[0] == j) {
                dp[0][j] = dp[0][j] + 1;
            }
        }
        for(int i = 1; i < nums.size(); i++) {
            for(int j = 0; j <= left; j++) {
                if(j >= nums[i]) {
                    dp[i][j] = dp[i-1][j] + dp[i-1][j-nums[i]];
                }
                else {
                    dp[i][j] = dp[i-1][j];
                }
            }
        }
        return dp[nums.size()-1][left];
    }
};

dp[j] 定义为凑满容量为j大小的背包,有dp[j]种方法
根据二维转一维度,状态转移公式,dp[j] = dp[j] + dp[j-nums[i]]
初始化dp[0]=1,装满容量0的背包,装0个物品就行,一种方法

class Solution {
public:
    int findTargetSumWays(vector<int>& nums, int target) {
        int sum = accumulate(nums.begin(), nums.end(), 0);
        if ((sum+target) % 2 == 1) return 0;
        if (abs(target) > sum) return 0;
        int left = (sum+target) / 2;
        vector<int> dp(left+1, 0);
        dp[0] = 1;

        for(int i = 0; i < nums.size(); i++) {
            for(int j = left; j >= nums[i]; j--) {
                dp[j] = dp[j] + dp[j-nums[i]];
            }
        }
        return dp[left];
    }
};

二维费用的01背包问题

P05: 二维费用的背包问题
问题:
二维费用的背包问题是指:对于每件物品,具有两种不同的费用,选择这件物品必须同时付出这两种代价,对于每种代价都有一个可付出的最大值(背包容量),问怎样选择物品可以得到最大的价值。设这两种代价分别为代价1和代价2,第i件物品所需的两种代价分别为a[i]和b[i],两种代价可付出的最大值(两种背包的容量)分别为V和U。物品的价值为w[i]。
算法:
费用加了一维,只需状态也加一维即可。设f[i][v][u]表示前i件物品付出两种代价分别为v和u时可获得的最大价值。状态转移方程就是:f[i][v][u] = max(f[i-1][v][u], f[i-1][v-a[i]][u-b[i]]+w[i])。如前述方法,可以只使用二维的数组:当每件物品可以取一次时变量v和u采用顺序的循环,当物品有如完全背包问题时采用逆序的循环,当物品有如多重背包问题时拆分物品。
物品总个数的限制
有时,“二维费用”的条件是以一种隐含的方式给出的:最多只能取M件物品。这事实上相当于每件物品多了一种“件数”的费用,每个物品的件数费用均为1,可以付出的最大件数费用为M。换句话说,设f[v][m]表示付出费用v,最多选m件时可得到的最大价值,则根据物品的类型(01、完全、多重)用不同的方法循环更新,最后在f[0……V][0……M]范围内寻找答案。另外,如果要求“恰取M件物品”,则在f[0……V][M]范围内寻找答案。
小结
事实上,当发现由熟悉的动态规划题目变形得来的题目时,在原来的状态中加一维以满足新的限制是一种比较通用的方法。希望你能从本讲中初步体会到这种方法。

leetcode 474 一和零

问题简化:装满背包最多需要多少件物品,每件物品含有两个代价值cost1和cost2,背包对应两个代价值也有两个上限M和N。

该问题可以看成二维费用的背包问题,在01背包的基础上增加了一个费用维度,而这道题中的每个字符串相当于一个物品,而该物品的价值可以看成是单位1。对比于求满足费用维度的条件下,物品的最大价值组合,实际上得到的就是最大的子集。

本题strs数组中的元素就是物品,每个物品都是一个,而m和n相当于是一个背包,两个维度的背包。
1、确定dp数组以及下标的含义:dp[i][j],最多有i个0和j个1的strs的最大子集的大小为dp[i][j]
2、确定递推公式:dp[i][j]可以由前一个strs里的字符串推导出来,设前一个字符串有zeroNum个0,oneNum个1。
dp[i][j]就可以是dp[i-zeroNum][j-oneNum]+1。然后在遍历过程中取dp[i][j]的最大值。
dp[i-zeroNum][j-oneNum]的含义就是,最多有i-zeroNum个0和j-oneNum个1的最大子集的大小。
dp[i][j] = max(dp[i][j], dp[i-zeroNum][j-oneNum]+1)这个方程实际上是三维度降低维到了二维,如果把物品的维度加上,则dp[k][i][j] = max(dp[k-1][i][j], dp[k-1][i-zeroNum][j-oneNum]+1),所以这里对两个费用维度进行遍历的时候需要采用逆序的方式进行遍历。

在动态规划中,如果第i个状态只与第i-1个状态有关,而不与其他的例如第i - k(0 < k < i)个状态有关,那么意味着此时在空间上有优化的空间,我们可以采用滚动数组或者从后往前的方式填表来代替开辟更高维度的数组。

滚动数组可以理解,但另一种方式是从后往前的方式填表,这是为什么呢?

我们可以举个例子,假设一个状态方程为 dp[i][j] = dp[i-1][j-1] + 1。如果采用从后向前填表,那么我们的dp[i-1][j-1]应该是上一轮计算的结果,因为这一轮我们还没有更新过这个值。但如果采用从前往后填表,那么我们的dp[i-1][j-1]应该是这一轮计算的结果,因为这一轮我们已经更新过这个值。但是我们这个二维dp数组是最初的三维dp数组的一个优化,因此,在状态迁移时,我们需要的是上一轮计算的dp[i-1][j-1]。这就是为什么我们要从后往前填表了,主要是保留上一轮计算的结果不被覆盖。

最原始的版本代码

class Solution {
public:
    int findMaxForm(vector<string>& strs, int m, int n) {
        vector<vector<vector<int>>> dp;
        for (int i = 0; i <= strs.size(); i++) {
            dp.push_back(vector<vector<int>>(m+1, vector<int>(n+1, 0)));
        } 
        for (int k = 1; k <= strs.size(); k++) {
            int zeroNum = 0;
            int oneNum = 0;
            string s = strs[k-1];
            for (int x = 0; x < s.size(); x++) {
                if (s[x] == '0') {
                    zeroNum++;
                }
                else {
                    oneNum++;
                }
            }
            for (int i = 0; i <= m; i++) {
                for (int j = 0; j <= n; j++) {
                    if (i < zeroNum || j < oneNum) {
                        dp[k][i][j] = dp[k-1][i][j];
                    }
                    else {
                        dp[k][i][j] = max(dp[k-1][i][j], dp[k-1][i-zeroNum][j-oneNum] + 1);
                    }
                }
            }
        }
        return dp[strs.size()][m][n];
    }
};

采用滚动数组降维后的版本

class Solution {
public:
    int findMaxForm(vector<string>& strs, int m, int n) {
        vector<vector<int>> dp(m+1, vector<int>(n+1, 0));
        for(int i = 0; i < strs.size(); i++) {
            int oneNum = 0, zeroNum = 0;
            for(char c : strs[i]) {
                if(c == '0') zeroNum++;
                else oneNum++;
            }
            for(int i = m; i >= zeroNum; i--) {
                for(int j = n; j >= oneNum; j--) {
                    dp[i][j] = max(dp[i][j], dp[i-zeroNum][j-oneNum]+1);
                }
            }
        }
        return dp[m][n];
    }
};

完全背包问题

/*
完全背包和01背包的区别:
完全背包中,同一件物品可以使用无数次,01背包中一件物品只能使用一次。
01背包
for (int i = 0; i < size; i++) {     先遍历物品,再遍历背包,背包需要倒序遍历
	for (int j = bagWeight; j >= weight[i]; j--) {
		dp[j] = max(dp[j], dp[j-weight[i]] + value[i]);
	}
}
完全背包
for (int i = 0; i < size; i++) {     先遍历物品,再遍历背包,背包需要倒序遍历
	for (int j = weight[i]; j <= bagWeight; j++) {
		dp[j] = max(dp[j], dp[j-weight[i]] + value[i]);
	}
}
对于完全背包,先遍历物品还是先遍历背包得到的结果都是相同的
*/

leetcode 518. 零钱兑换 II

//dp[i][j]:使用下标为[0, i]的nums[i]能够凑满j(包括j)这么大容量的包,有dp[i][j]种方法。
//dp[i][j] = dp[i-1][j](不需要nums[i]物品就可以填满容量j的包的方法) + dp[i-1][j-nums[i]](需要nums[i]才可以填满容量j的包的方法)。
//第i个物品可以选择用它来填j空间,也可以不选择它填j空间,两种决策的方法进行相加。
//dp数组初始化,dp[0][0]为1,装满容量为0的背包,有一种方法,就是装0件物品。dp[0][j],看第一个元素的大小情况,进行赋值。

//dp[j] 定义为凑满容量为j大小的背包,有dp[j]种方法
//根据二维转一维度,状态转移公式,dp[j] = dp[j] + dp[j-nums[i]]
//初始化dp[0]=1,装满容量0的背包,装0个物品就行,一种方法
//dp[i][j]:使用下标为[0, i]的nums[i]能够凑满j(包括j)这么大容量的包,有dp[i][j]种方法。
//dp[i][j] = dp[i-1][j](不需要nums[i]物品就可以填满容量j的包的方法) + dp[i-1][j-nums[i]](需要nums[i]才可以填满容量j的包的方法)。
//第i个物品可以选择用它来填j空间,也可以不选择它填j空间,两种决策的方法进行相加。
//dp数组初始化,dp[0][0]为1,装满容量为0的背包,有一种方法,就是装0件物品。dp[0][j],看第一个元素的大小情况,进行赋值。

//dp[j] 定义为凑满容量为j大小的背包,有dp[j]种方法
//根据二维转一维度,状态转移公式,dp[j] = dp[j] + dp[j-nums[i]]
//初始化dp[0]=1,装满容量0的背包,装0个物品就行,一种方法

class Solution {
public:
    int change(int amount, vector<int>& coins) {
        vector<int> dp(amount + 1);
        dp[0] = 1;
        for (int i = 0; i < coins.size(); i++) {
            for (int j = coins[i]; j <= amount; j++) {
                dp[j] = dp[j] + dp[j - coins[i]];
            }
        }
        return dp[amount];
    }
};

leetcode 377. 组合总和 Ⅳ

//(1, 1, 2)和(1, 2, 1)是两个不同的排列,对于组合来说,两者是相同的情况
//我们先遍历物品,后遍历背包得到的是组合数
//我们先遍历背包,后遍历物品得到的才是排列数
//此题需要我们求的是排列数
/*
    for (int i = 0; i < nums.size(); i++)  物品 nums[1,2,5]
        for (int j = 0; j <= target; j++)  背包
    先遍历物品,则先1后2,先将1放入,在背包里面进行遍历,然后才把2放进来,在背包里面遍历一遍
    我们只会出现1,2这种情况,不会出现2,1这种情况

    for (int j = 0; j <= target; j++)      背包
        for (int i = 0; i < nums.size(); i++) 物品
    我们的背包,每一个容量下,都是在1,2的情况下进行遍历的,每个容量下都放了1,2
    所以,我们既有1,2,也有2,1 物品排序和其重量排序不一致
*/

//eg nums = [1,2], target = 3
class Solution {
public:
    int combinationSum4(vector<int>& nums, int target) {
        vector<uint64_t> dp(target + 1);
        dp[0] = 1;
        for (int j = 0; j <= target; j++) {
            for (int i = 0; i < nums.size(); i++) {
                if(j >= nums[i]) dp[j] = dp[j] + dp[j - nums[i]];
            }
        }
        return dp[target];
    }
};

leetcode 322. 零钱兑换

//dp[j]:从coins中凑成大小为j的容量,所需要的最少硬币的个数

//凑足总额为j - coins[i]的最少个数为dp[j - coins[i]],那么只需要加上一个钱币coins[i]即dp[j - coins[i]] + 1就是dp[j](考虑coins[i])
//所以dp[j] 要取所有 dp[j - coins[i]] + 1 中最小的。
//递推公式:dp[j] = min(dp[j - coins[i]] + 1, dp[j]);

//首先凑足总金额为0所需钱币的个数一定是0,那么dp[0] = 0;
//考虑到递推公式的特性,dp[j]必须初始化为一个最大的数,否则就会在min(dp[j - coins[i]] + 1, dp[j])比较的过程中被初始值dp[0] = 0覆盖。
//所以下标非0的元素都是应该是最大值。

//本题并不强调集合是组合还是排列。
//如果求组合数就是外层for循环遍历物品,内层for遍历背包。
//如果求排列数就是外层for遍历背包,内层for循环遍历物品。
//本题钱币数量可以无限使用,那么是完全背包。所以遍历的内循环是正序。

class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        vector<uint64_t> dp(amount + 1, INT_MAX);
        dp[0] = 0;
        for (int i = 0; i < coins.size(); i++) {
            for (int j = coins[i]; j <= amount; j++) {
                dp[j] = min(dp[j], dp[j - coins[i]] + 1);
            }
        }
        return  dp[amount] == INT_MAX ? -1 : dp[amount];
    }
};

leetcode 279. 完全平方数

//dp[j]
//dp[j] = min(dp[j], dp[j - nums[i]] + 1);
class Solution {
public:
    int numSquares(int n) {
        vector<uint64_t> dp(n + 1, INT_MAX);
        dp[0] = 0;
        vector<int> nums;
        for (int i = 1; i <= pow(n, 0.5); i++) {
            nums.push_back(i * i);
        }
        for (int i = 0; i < nums.size(); i++) {
            for (int j = nums[i]; j <= n; j++) {
                dp[j] = min(dp[j], dp[j - nums[i]] + 1);
            }
        }
        return dp[n];
    }
};

leetcode 139. 单词拆分

//dp[j]表示当前能否用字典中的字符串拼接成字符串s[0-j-1],若能拼接dp[j] == true,否则为false,这里的j代表的是长度
//判断dp[s.size()]是否为true

//if 从j到i切割下来的子串在我们的wordDoct中,同时dp[j]为true ----> dp[i] = true

//dp[0]需要初始化为true
//dp其他状态默认都是false

//遍历顺序:求排列数,而不是组合数,对物品的顺序是有要求的,所以我们应该先遍历背包,再遍历物品

//for(int i = 1; i <= s.size(); i++) 
//for(int j = 0; j < i; j++) 这里需要做一个截取的操作,j一定要小于i,i是背包的容量,我们要截取的[j,i]这段才是我们需要判断的物品
//物品,我们需要判断是否出现在字典里

class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        unordered_set<string> wordSet(wordDict.begin(), wordDict.end());
        vector<bool> dp(s.size() + 1, false);
        dp[0] = true;
        for (int i = 1; i <= s.size(); i++) {    // 遍历背包
            for (int j = 0; j < i; j++) {        // 遍历物品
                string word = s.substr(j, i - j);  //substr(起始位置,截取的长度) word是我们截取的物品
                if (wordSet.find(word) != wordSet.end() && dp[j]) {      //物品在我们的字典里,并且dp[j] == true
                    dp[i] = true;
                }
            }
        }
        return dp[s.size()];
    }
};

采用回溯处理,超时代码

class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        unordered_set<string> wordSet(wordDict.begin(), wordDict.end());
        bool res = false;
        backTracking(0, s, wordSet, res);
        return res;
    }
    void backTracking(int startIndex, string s, unordered_set<string>& wordSet, bool& res) {
        if (startIndex >= s.size()) {
            res = true;
            return;
        }
        for (int i = startIndex; i < s.size(); i++) {
            string st = s.substr(startIndex, i - startIndex + 1);
            if (wordSet.find(st) != wordSet.end()) {
                backTracking(i + 1, s, wordSet, res);
            }
            else {
                continue;
            }
        }
    }
};

递归的过程中有很多重复计算,可以使用数组保存一下递归过程中计算的结果。

这个叫做记忆化递归。

使用memory数组保存每次计算的以startIndex起始的计算结果,如果memory[startIndex]里已经被赋值了,直接用memory[startIndex]的结果。

class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        unordered_set<string> wordSet(wordDict.begin(), wordDict.end());
        vector<bool> memory(s.size(), true); 
        return backTracking(0, s, wordSet, memory);
    }
    bool backTracking(int startIndex, string s, unordered_set<string>& wordSet, vector<bool>& memory) {
        if (startIndex >= s.size()) {
            return true;
        }
        // 如果memory[startIndex]不是初始值了,直接使用memory[startIndex]的结果
        if (!memory[startIndex]) return memory[startIndex];

        for (int i = startIndex; i < s.size(); i++) {
            string st = s.substr(startIndex, i - startIndex + 1);
            if (wordSet.find(st) != wordSet.end()) {
                if(backTracking(i + 1, s, wordSet, memory)) return true;
            }
            else {
                continue;
            }
        }
        memory[startIndex] = false;      //记录以startIndex开始的子串是不可以被拆分的
        return false;
    }
};

打家劫舍问题

leetcode 198. 打家劫舍

//dp[i] 考虑下标i,i以及之前的,能偷的最大的金额是dp[i]
//两种状态:偷i房间,nums[i],因为决定偷i房间,所以我们不能偷i-1房间,则考虑dp[i-2] + nums[i]
//不偷i房间,dp[i-1]
//初始化dp[0]、dp[1]
class Solution {
public:
    int rob(vector<int>& nums) {
        if (nums.size() == 0){
            return 0;
        }
        if (nums.size() == 1){
            return nums[0];
        }
        vector<int> dp(nums.size(), 0);  
        dp[0] = nums[0];
        dp[1] = max(nums[0], nums[1]);
        for (int i = 2; i < nums.size(); i++){
            dp[i] = max(dp[i-1], dp[i-2]+nums[i]);
        }
        return dp[nums.size()-1];
    }
};

leetcode 213. 打家劫舍 II

//dp[i] 考虑下标i,i以及之前的,能偷的最大的金额是dp[i]
//两种状态:偷i房间,nums[i],因为决定偷i房间,所以我们不能偷i-1房间,则考虑dp[i-2] + nums[i]
//不偷i房间,dp[i-1]
//初始化dp[0]、dp[1]
class Solution {
public:
    int robOne(vector<int>& nums) {
        if (nums.size() == 0){
            return 0;
        }
        if (nums.size() == 1){
            return nums[0];
        }
        vector<int> dp(nums.size(), 0);  
        dp[0] = nums[0];
        dp[1] = max(nums[0], nums[1]);
        for (int i = 2; i < nums.size(); i++){
            dp[i] = max(dp[i-1], dp[i-2]+nums[i]);
        }
        return dp[nums.size()-1];
    }
    int rob(vector<int>& nums) {
        //情况1,不考虑首元素和尾元素
        //情况2,不考虑尾元素
        //情况3,不考虑首元素
        //情况2和3中,都包含了情况1的情况
        if (nums.size() == 1) return nums[0];
        vector<int> nums1(nums.begin(), nums.end() - 1);
        vector<int> nums2(nums.begin() + 1, nums.end());
        int res1 = robOne(nums1);
        int res2 = robOne(nums2);
        return max(res1, res2);
    }
};

leetcode 337. 打家劫舍 III (树形dp)

//每个结点只有两个状态,偷这个结点,不偷这个结点
//dp[0]不偷该结点,不偷当前结点所获得的最大金额
//dp[1]偷该结点,偷当前结点所获得的最大金额
//从底部向上去遍历,采用后序遍历的方式,最终是根节点的偷还是不偷两个状态
class Solution {
public:
    int rob(TreeNode* root) {
        vector<int> result = robTree(root);    //result就是我们的dp数组
        return max(result[0], result[1]);
    }
    vector<int> robTree(TreeNode* cur) {   //返回当前结点,偷还是不偷,最大的金额数量
        if (cur == NULL) {
            return vector<int>(2, 0);
        }
        vector<int> left_dp = robTree(cur->left);
        vector<int> right_dp = robTree(cur->right);

        //偷当前结点,如果偷当前结点,那么它的左右孩子就一定不能偷了
        int value1 = cur->val + left_dp[0] + right_dp[0];           //回溯的时候进行dp,树形dp
        //不偷当前结点,考虑是否要偷左右孩子,偷不偷取决于左孩子在偷的情况下和不偷的情况下,最大的钱币数量是多少
        int value2 = max(left_dp[0], left_dp[1]) + max(right_dp[0], right_dp[1]);
        vector<int> tmp = {value2, value1};
        return tmp;
    }
};

买卖股票问题

leetcode 121. 买卖股票的最佳时机

//这道题目最直观的想法,就是暴力,找最优间距了。
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int result = 0;
        for (int i = 0; i < prices.size(); i++) {
            for (int j = i + 1; j < prices.size(); j++){
                result = max(result, prices[j] - prices[i]);
            }
        }
        return result;
    }
};
//因为股票就买卖一次,那么贪心的想法很自然就是取最左最小值,取最右最大值,那么得到的差值就是最大利润。
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int low = INT_MAX;
        int result = 0;
        for (int i = 0; i < prices.size(); i++) {
            low = min(low, prices[i]);             // 取最左最小价格
            result = max(result, prices[i] - low); // 直接取最大区间利润
        }
        return result;
    }
};
//二维dp数组:在第i天,持有这支股票和不持有这支股票。
//dp[i][0] 第i天持有这支股票获取的最大利润 dp[i][1] 第i天不持有这支股票获取的最大利润
//最终要求的结果是max(dp[n-1][0], dp[n-1][1])
//dp[i][0] = dp[i-1][0] (第i-1天也是持有这个股票,这支股票在前几天就购买了) 保持持有的状态
//dp[i][0] = -prices[i] (在第i天的时候,买入这支股票,第i天就持有这个股票了) 状态改变了,第i天买入这支股票,股票只能买卖一次
//dp[i][0] = max(dp[i-1][0], -prices[i]);
//dp[i][1] = max(dp[i-1][1], dp[i-1][0]+prices[i]);   //第i天的时候把这支股票给卖了,dp[i-1][0]+prices[i]
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        vector<vector<int>> dp(prices.size(), vector<int>(2, 0));
        dp[0][0] = 0 - prices[0];    //dp数组初始化
        dp[0][1] = 0;
        for (int i = 1; i < prices.size(); i++) {    //dp数组遍历顺序
            dp[i][0] = max(dp[i-1][0], -prices[i]);
            dp[i][1] = max(dp[i-1][1], dp[i-1][0]+prices[i]);
        }
        //其实,我们不持有股票的最大利润,一定比持有股票的最大利润,要多。 return dp[prices.size() - 1][1]就行
        return max(dp[prices.size() -1][0], dp[prices.size() - 1][1]);
    }
};

leetcode 122 买卖股票的最佳时机 II

//贪心解法
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int result = 0;
        for(int i = 1; i < prices.size(); i++)
        {
            result += max(prices[i] - prices[i-1], 0);
        }
        return result;
    }
};
//二维dp数组:在第i天,持有这支股票和不持有这支股票。
//dp[i][0] 第i天持有这支股票获取的最大利润 dp[i][1] 第i天不持有这支股票获取的最大利润
//最终要求的结果是max(dp[n-1][0], dp[n-1][1])
//dp[i][0] = dp[i-1][0] (第i-1天也是持有这个股票,这支股票在前几天就购买了) 保持持有的状态
//dp[i][0] = -prices[i] (在第i天的时候,买入这支股票,第i天就持有这个股票了) 状态改变了,第i天买入这支股票
//dp[i][0] = max(dp[i-1][0], dp[i-1][1]-prices[i]);
//dp[i][1] = max(dp[i-1][1], dp[i-1][0]+prices[i]);   //第i天的时候把这支股票给卖了,dp[i-1][0]+prices[i]
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        vector<vector<int>> dp(prices.size(), vector<int>(2, 0));
        dp[0][0] = 0 - prices[0];    //dp数组初始化
        dp[0][1] = 0;
        for (int i = 1; i < prices.size(); i++) {    //dp数组遍历顺序
            dp[i][0] = max(dp[i-1][0], dp[i-1][1]-prices[i]);
            dp[i][1] = max(dp[i-1][1], dp[i-1][0]+prices[i]);
        }
        //其实,我们不持有股票的最大利润,一定比持有股票的最大利润,要多。 return dp[prices.size() - 1][1]就行
        return max(dp[prices.size() -1][0], dp[prices.size() - 1][1]);
    }
};

leetcode 714. 买卖股票的最佳时机含手续费

class Solution {
public:
    int maxProfit(vector<int>& prices, int fee) {
        int n = prices.size();
        vector<vector<int>> dp(n, vector<int>(2, 0));
        dp[0][0] -= prices[0];          //持股票
        for (int i = 1; i < n; i++) {
            dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
            dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i] - fee);
        }
        return max(dp[n - 1][0], dp[n - 1][1]);
    }
};

leetcode 309. 最佳买卖股票时机含冷冻期

//dp[i][0] 第i天持有这支股票,获取的最大利润
//dp[i][1] 第i天不持有这支股票,获取的最大利润
//dp[i][2] 第i天不持有这支股票处于冷冻期,,获取的最大利润
class Solution {
public:
    int maxProfit(vector<int>& prices) {
        vector<vector<int>> dp(prices.size(), vector<int>(3, 0));
        dp[0][0] = 0 - prices[0];
        for (int i = 1; i < prices.size(); i++) {
            // 保持之前/开启买入状态
            dp[i][0] = max(dp[i - 1][0], dp[i - 1][2] - prices[i]); // 如果是开启买入,不能从前一个卖出状态买入,因为有冷冻期,只能从经历了冷冻期的状态买入
            // 保持之前/开启卖出状态
            dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
            // 保持之前/开启冷冻期状态
            dp[i][2] = max(dp[i - 1][2], dp[i - 1][1]); // 从前一天的卖出状态进入冷冻期,或继续保持冷冻期
        }
        // 不需要使用max,因为如果最后一天卖出,那么是卖出位最大
        // 如果最后一天是冷冻期,冷冻期位的值来源于前一天的卖出位,当天卖出位也来源于前一天的卖出位
        // 所以无论如何都是卖出位是最大值
        return dp[prices.size() - 1][1];
    }
};

leetcode 123. 买卖股票的最佳时机 III

//dp[i][0] 第i天,不操作
//dp[i][1] 第i天第一次持有这支股票获取的最大利润
//dp[i][2] 第i天第一次不持有这支股票获取的最大利润
//dp[i][3] 第i天第二次持有这支股票获取的最大利润
//dp[i][4] 第i天第二次不持有这支股票获取的最大利润

//dp[i][0] = dp[i-1][0]
//dp[i][1] = max(dp[i-1][1], dp[i-1][0] - prices[i]);
//dp[i][2] = max(dp[i-1][2], dp[i-1][1] + prices[i]);
//dp[i][3] = max(dp[i-1][3], dp[i-1][2] - prices[i]);
//dp[i][4] = max(dp[i-1][4], dp[i-1][3] + prices[i]);

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if (prices.size() == 0) return 0;
        vector<vector<int>> dp(prices.size(), vector<int>(5, 0));
        dp[0][0] = 0;
        dp[0][1] = -prices[0];
        dp[0][2] = 0;
        dp[0][3] = -prices[0];
        dp[0][4] = 0;
        for (int i = 1; i < prices.size(); i++) {
            dp[i][0] = dp[i - 1][0];
            dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
            dp[i][2] = max(dp[i - 1][2], dp[i - 1][1] + prices[i]);
            dp[i][3] = max(dp[i - 1][3], dp[i - 1][2] - prices[i]);
            dp[i][4] = max(dp[i - 1][4], dp[i - 1][3] + prices[i]);
        }
        return dp[prices.size() - 1][4];
    }
};

leetcode 188. 买卖股票的最佳时机 IV

代码随想录

/*
1、确定dp数组以及下标的含义
使用二维数组 dp[i][j] :第i天的状态为j,所剩下的最大现金是dp[i][j]
j的状态表示为:
0 表示不操作
1 第一次买入
2 第一次卖出
3 第二次买入
4 第二次卖出
除了0以外,偶数就是卖出,奇数就是买入。
题目要求是至多有K笔交易,那么j的范围就定义为 2 * k + 1 就可以了。
所以二维dp数组的定义为:
vector<vector<int>> dp(prices.size(), vector<int>(2 * k + 1, 0));
*/

/*
2、确定递推公式
dp[i][1],表示的是第i天,持有股票的状态,并不是说一定要第i天买入股票
达到dp[i][1]状态,有两个具体操作:
操作一:第i天买入股票了,那么dp[i][1] = dp[i - 1][0] - prices[i]
操作二:第i天没有操作,而是沿用前一天买入的状态,即:dp[i][1] = dp[i - 1][1]
选最大的,所以 dp[i][1] = max(dp[i - 1][0] - prices[i], dp[i - 1][1]);
同理dp[i][2]也有两个操作:
操作一:第i天卖出股票了,那么dp[i][2] = dp[i - 1][1] + prices[i]
操作二:第i天没有操作,沿用前一天卖出股票的状态,即:dp[i][2] = dp[i - 1][2]
所以dp[i][2] = max(dp[i - 1][1] + prices[i], dp[i - 1][2])
同理可以类比剩下的状态,代码如下:
for (int j = 0; j < 2 * k - 1; j += 2) {
    dp[i][j + 1] = max(dp[i - 1][j + 1], dp[i - 1][j] - prices[i]);
    dp[i][j + 2] = max(dp[i - 1][j + 2], dp[i - 1][j + 1] + prices[i]);
}
*/

/*
3、dp数组初始化
第0天没有操作,这个最容易想到,就是0,即:dp[0][0] = 0;
第0天做第一次买入的操作,dp[0][1] = -prices[0];
第0天做第一次卖出的操作,这个初始值应该是多少呢?
此时还没有买入,怎么就卖出呢? 其实大家可以理解当天买入,当天卖出,所以dp[0][2] = 0;
第0天第二次买入操作,初始值应该是多少呢?
第二次买入依赖于第一次卖出的状态,其实相当于第0天第一次买入了,第一次卖出了,然后在买入一次(第二次买入),那么现在手头上没有现金,只要买入,现金就做相应的减少。
所以第二次买入操作,初始化为:dp[0][3] = -prices[0];
第二次卖出初始化dp[0][4] = 0;
所以同理可以推出dp[0][j]当j为奇数的时候都初始化为 -prices[0]
代码如下:
for (int j = 1; j < 2 * k; j += 2) {
    dp[0][j] = -prices[0];
}
在初始化的地方同样要类比j为偶数是卖、奇数是买的状态。
*/

/*
4、dp数组遍历顺序
从递归公式其实已经可以看出,一定是从前向后遍历,因为dp[i],依靠dp[i - 1]的数值。
*/

/*
5、举例推导dp数组
最后一次卖出,一定是利润最大的,dp[prices.size() - 1][2 * k]即就是最后求解。
*/

class Solution {
public:
    int maxProfit(int k, vector<int>& prices) {
        if (prices.size() == 0) return 0;
        vector<vector<int>> dp(prices.size(), vector<int>(2 * k + 1, 0));
        for (int j = 1; j < 2 * k; j += 2) {
            dp[0][j] = -prices[0];
        }
        for (int i = 1;i < prices.size(); i++) {
            for (int j = 0; j < 2 * k - 1; j += 2) {
                dp[i][j + 1] = max(dp[i - 1][j + 1], dp[i - 1][j] - prices[i]);
                dp[i][j + 2] = max(dp[i - 1][j + 2], dp[i - 1][j + 1] + prices[i]);
            }
        }
        return dp[prices.size() - 1][2 * k];
    }
};

子序列问题

leetcode 674. 最长连续递增序列

暴力解法

class Solution {
public:
    int findLengthOfLCIS(vector<int>& nums) {
        int res = 1;
        for (int i = 0; i < nums.size(); i++) {
            int len = 1;
            for (int j = i; j < nums.size() - 1; j++) {
                if (nums[j] < nums[j+1]) {
                    len++;
                }
                else {
                    break;
                }
            }
            res = max(res, len);
        }
        return res;
    }
};

采用并查集,将不同的连续递增序列放入不同的集合,求出集合元素数目最多的就是最长连续递增序列。

class Solution {
    class UnionFind 
    {
        vector<int> s;
    public:
        UnionFind(int n) {
            s = vector<int>(n, -1);
        }
        int Find(int idx) {
            int i = idx;
            while (s[i] >= 0) {
                i = s[i];
            }
            return i;
        }
        void Union(int idx1, int idx2) {
            int p1 = Find(idx1);
            int p2 = Find(idx2);
            if (p1 == p2) return;
            if (s[p1] <= s[p2]) {
                s[p1] += s[p2];
                s[p2] = p1;
            }
            else {
                s[p2] += s[p1];
                s[p1] = p2;
            }
        }
        int GetMaxUnionELeNum() {
            int res = INT_MAX;
            for (int i = 0; i < s.size(); i++) {
                res = min(res, s[i]);
            }
            return abs(res);
        }
    };
public:
    int findLengthOfLCIS(vector<int>& nums) {
        if (nums.size() == 0) return 0;
        UnionFind uf(nums.size());
        for (int i = 0; i < nums.size() - 1; i++) {
            if (nums[i] < nums[i+1]) {
                uf.Union(i, i + 1);
            }
        }
        return uf.GetMaxUnionELeNum();
    }
};
//dp[i]表示以nums[i]元素结尾的最长连续递增子序列的长度
//dp[i-1]则表示以nums[i-1]元素结尾的最长连续递增子序列的长度
//子序列的起始位置并没有要求
//if (nums[i] > nums[i-1]) dp[i] = dp[i-1] + 1;
//因为这里要求是连续的递增子序列,所以没有必要遍历i之前的每一个元素比较dp[j] + 1了
//dp数组全部初始化为1,每个元素的最长连续递增子序列的长度最少为1
class Solution {
public:
    int findLengthOfLCIS(vector<int>& nums) {
        vector<int> dp(nums.size(), 1);
        int result = 1;
        for (int i = 1; i < nums.size(); i++) {
            if (nums[i] > nums[i-1]) {
                dp[i] = dp[i-1] + 1;
            }
            // if nums[i] <= nums[i-1] dp[i]不发生改变
            result = max(result, dp[i]);
        }
        return result;
    }
};

leetcode 300. 最长递增子序列

//dp[i]表示所有以nums[i]元素为结尾的递增子序列中最长的递增子序列长度
//dp[i] = max(dp[i], dp[j] + 1)  j-->[0, i - 1] dp[i]的值是dp[j]+1中的最大值
//dp初始化:dp[i] = 1,子序列最少是包含nums[i]的,长度至少都是1
//遍历顺序 i 从小到大遍历,需要依赖前面已经计算过的结果,求解更新的dp[i]的值 for(int i = 1; i < nums.size(); i++)
//for (int j = 0; j < i; j++)对于每个确定位置的i,遍历所有的dp[j] + 1的情况,选择出最大的作为dp[i]
//最终的result,则需要在dp[i]中选出最大的递增子序列长度
class Solution {
public:
    int lengthOfLIS(vector<int>& nums) {
        vector<int> dp(nums.size(), 1);
        int result = 0;
        for (int i = 0; i < nums.size(); i++) {
            for (int j = 0; j < i; j++) {
                if (nums[i] > nums[j]) {
                    dp[i] = max(dp[i], dp[j] + 1);   //if dp[j] + 1 > dp[i] --> update dp[i]
                }
            }
            result = max(result, dp[i]);
        }
        return result;
    }
};

leetcode 718. 最长重复子数组

暴力解法

//超时代码
class Solution {
public:
    int findLength(vector<int>& nums1, vector<int>& nums2) {
        int res = 0;    
        for (int i = 0; i < nums1.size(); i++) {       //两层for循环,分别遍历nums1和nums2子数组的起始位置
            for (int j = 0; j < nums2.size(); j++) {
                int len = 0;
                int tmp1 = i;
                int tmp2 = j;
                while (tmp1 < nums1.size() && tmp2 < nums2.size()) {
                    if (nums1[tmp1] == nums2[tmp2]) {
                        len++;
                        tmp1++;
                        tmp2++;
                    }
                    else {
                        break;
                    }
                }
                res = max(res, len);
            }
        }
        return res;
    }
};
//dp[i][j] 以i-1为结尾的nums1和j-1为结尾的nums2,这两个数组的最长重复子数组长度
//if (nums1[i-1] == nums2[j-1]) dp[i][j] = dp[i-1][j-1] + 1;
//dp[i][0] dp[0][j] 无意义的状态 初始化为0
//两层for循环,遍历nums1和nums2
//为什么没有定义以i和j为结尾的最长重复子数组?
//因为根据此dp数组的定义对于dp[i][0]进行初始化的时候,需要遍历nums1中的所有元素,和nums2[0]进行对比,如果相等则赋值为1
class Solution {
public:
    int findLength(vector<int>& nums1, vector<int>& nums2) {
        vector<vector<int>> dp(nums1.size() + 1, vector<int>(nums2.size() + 1, 0));
        int res = 0;
        for (int i = 1; i <= nums1.size(); i++) {
            for (int j = 1; j <= nums2.size(); j++) {
                if (nums1[i-1] == nums2[j-1]) {
                    dp[i][j] = dp[i-1][j-1] + 1;
                }
                res = max(res, dp[i][j]);
            }
        }
        return res;
    }
};
class Solution {
    // dp[i][j] 表示以下标i结尾的数组1和以下标j结尾的数组2,对应的最长公共子序列的长度。
    // if t[i] == t[j] dp[i][j] = dp[i-1][j-1] + 1
    // dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1])
    public int longestCommonSubsequence(String text1, String text2) {
        char[] arr1 = text1.toCharArray();
        char[] arr2 = text2.toCharArray();
        int n1 = arr1.length;
        int n2 = arr2.length;
        int[][] dp = new int[n1][n2];

        int res = 0;

        for (int i = 0; i < n1; i++) {
            if (arr1[i] == arr2[0]) {
                while (i < n1) {
                    dp[i][0] = 1;
                    res = Math.max(res, dp[i][0]);
                    i++;
                }
                break;
            }
        }

        for (int j = 0; j < n2; j++) {
            if (arr1[0] == arr2[j]) {
                while (j < n2) {
                    dp[0][j] = 1;
                    res = Math.max(res, dp[0][j]);
                    j++;
                }
                break;
            }
        }

        if (arr1[0] == arr2[0]) dp[0][0] = 1;
        else dp[0][0] = 0;

        for (int i = 1; i < n1; i++) {
            for (int j = 1; j < n2; j++) {
                if (arr1[i] == arr2[j]) {
                    dp[i][j] = dp[i-1][j-1] + 1;
                } else {
                    dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
                }
                res = Math.max(res, dp[i][j]);
            }
        }
        return res;
    }
}

leetcode 1143. 最长公共子序列

//表示两个数组之间元素的状态,采用二维dp数组
//dp[i][j] 以[0,i-1]nums1的数组长度,和[0,j-1]nums2的数组长度,的最长公共子序列长度
//if (nums[i-1] == nums[j-1]) dp[i][j] = dp[i-1][j-1] + 1];
//eg: abce ace 的最长公共子序列长度就是 abc ac 的最长公共子序列长度加1
//dp[i][j] = dp[i][j-1] eg: abc ace 最长公共子序列都为ac
//dp[i][j] = dp[i-1][j] eg: acb aec 
//综合以上两种情况,故if (nums[i-1] != nums[j-1]) dp[i][j] =  max(dp[i][j-1], dp[i-1][j]);
//dp的初始化 dp[i][0]和dp[0][j]初始化为0
class Solution {
public:
    int longestCommonSubsequence(string text1, string text2) {
        vector<vector<int>> dp(text1.size() + 1, vector<int>(text2.size() + 1, 0));
        int res = 0;
        for (int i = 1; i <= text1.size(); i++) {
            for (int j = 1; j <= text2.size(); j++) {
                if (text1[i-1] == text2[j-1]) {
                    dp[i][j] = dp[i-1][j-1] + 1;
                }
                else {
                    dp[i][j] =  max(dp[i][j-1], dp[i-1][j]);
                }
                res = max(res, dp[i][j]);
            }
        }
        return res;        
    }
};

leetcode 1035. 不相交的线(同最长公共子序列)

//同leetcode 1143. 最长公共子序列
class Solution {
public:
    int maxUncrossedLines(vector<int>& nums1, vector<int>& nums2) {
        vector<vector<int>> dp(nums1.size() + 1, vector<int>(nums2.size() + 1, 0));
        int res = 0;
        for (int i = 1; i <= nums1.size(); i++) {
            for (int j = 1; j <= nums2.size(); j++) {
                if (nums1[i-1] == nums2[j-1]) {
                    dp[i][j] = dp[i-1][j-1] + 1;
                }
                else {
                    dp[i][j] =  max(dp[i][j-1], dp[i-1][j]);
                }
                res = max(res, dp[i][j]);
            }
        }
        return res;   
    }
};

leetcode 53. 最大子数组和

//用分治法来做
class Solution {
public:
int CrossSubArray(vector<int>& A, int low, int mid, int high) {
	int leftMaxVal = INT_MIN;
	int Sum = 0;
	for (int i = mid; i >= low; i--) {
		Sum += A[i];
		if (Sum > leftMaxVal) {
			leftMaxVal = Sum;
		}
	}
	int rightMaxVal = INT_MIN;
	Sum = 0;
	for (int i = mid + 1; i <= high; i++) {
		Sum += A[i];
		if (Sum > rightMaxVal) {
			rightMaxVal = Sum;
		}
	}
	return leftMaxVal + rightMaxVal;
}
int MaxSubArray(vector<int>& A, int low, int high) {
	if (low == high) {     //递归到解决到剩下最后一个元素时,最大子数组的值就是该元素的值
		return A[low];
	}
	int mid = (low + high) / 2; 
	int leftSum = MaxSubArray(A, low, mid);
	int rightSum = MaxSubArray(A, mid + 1, high);
	int crossSum = CrossSubArray(A, low, mid, high);
	if (leftSum >= rightSum && leftSum >= crossSum) return leftSum;
	else if (rightSum >= leftSum && rightSum >= crossSum) return rightSum;
	else return crossSum;
}
    int maxSubArray(vector<int>& nums) {
        return MaxSubArray(nums, 0, nums.size()-1);
    }
};
//dp[i]定义为以nums[i]结尾的子数组的最大连续子序列的和
//dp[i] = dp[i-1] + nums[i];  延续前面已经计算过的子序列和
//eg: 0 4 -3 ----> 0 4 -3 2 
//dp[i] = nums[i];    不延续前面的子序列和,从当前位置从头计算
//eg: 0 3 -4 -----> 0 3 -4 2
//故:dp[i] = max(dp[i-1] + nums[i], nums[i]);
class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        vector<int> dp(nums.size(), 0);
        dp[0] = nums[0];
        int res = INT_MIN;
        for (int i = 1; i < nums.size(); i++) {
            dp[i] = max(dp[i-1] + nums[i], nums[i]);
            res = max(res, dp[i]);    //这里没有处理比较dp[0]  
        }
        res = max(res, dp[0]);
        return res;
    } 
};

leetcode 392. 判断子序列

//给定字符串 s 和 t ,判断 s 是否为 t 的子序列。
//字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。
//例如,"ace"是"abcde"的一个子序列,而"aec"不是
//dp[i][j]以i-1为尾的字符串s和以j-1为尾字符串t的相同子序列的长度
//if (s[i-1] == t[j-1]) dp[i][j] = dp[i-1][j-1] + 1
//如果这两个字符串不相同,我们只能在t这个字符串里面删除元素,如果s[i-1] != t[j-1],我们就相当于将该元素删除掉了
//删除掉我们就考虑j-1前面的字符串和你这个i-1完整这个字符串的相同子序列长度。
//else dp[i][j] = dp[i][j-1]
//dp数组初始化,推导方向:从(i-1,j-1)或者(i,j-1)递推到dp[i][j]
//dp[i][0]和dp[0][j]初始化,无意义初始化为0
//dp数组的遍历顺序,从左到右,从上到下
class Solution {
public:
    bool isSubsequence(string s, string t) {
        vector<vector<int>> dp(s.size()+1, vector<int>(t.size()+1, 0));
        for (int i = 1; i <= s.size(); i++) {
            for (int j = 1; j <= t.size(); j++) {
                if (s[i-1] == t[j-1]) {
                    dp[i][j] = dp[i-1][j-1] + 1;
                }
                else {
                    dp[i][j] = dp[i][j-1];
                }
            }
        }
        if (dp[s.size()][t.size()] == s.size()) {
            return true;
        }
        else {
            return false;
        }
    }
};

leetcode 115. 不同的子序列

//求s里面有多少个像t这样的子序列,就是s字符串中有多少种删除元素的方式,使s可以变成t,那么我们要输出这种方式
//dp[i][j]以i-1为尾的字符串s中有多少个以j-1为尾的字符串t的个数
//if (s[i-1] == t[j-1]) dp[i][j] = dp[i-1][j-1] + dp[i-1][j]
//使用s[i-1]和不使用s[i-1]的情况
//s: bagg t : bag 此时s[3] == t[2],也就是说使用了我们的s3,但是我们的s3也可以不考虑,也就是说模拟把这个s3删除掉,s[2] == t[2]
//else dp[i][j] = dp[i-1][j] (不考虑使用s[i-1]的情况)

//dp[i][0]和dp[0][j]初始化
//dp[i][0]是在字符串s中寻找空字符串的方法,也就是将s中的所有元素全部删除就变成空了,只有一种方法,初始化为1
//dp[0][j]初始化为0
//dp[0][0] = 1

//dp数组的遍历顺序


class Solution {
public:
    int numDistinct(string s, string t) {
        vector<vector<uint64_t>> dp(s.size()+1, vector<uint64_t>(t.size()+1, 0)); 
        for (int i = 0; i <= s.size(); i++) {
            dp[i][0] = 1;
        }
        for (int j = 0; j <= t.size(); j++) {
            dp[0][j] = 0;
        }
        dp[0][0] = 1;
        for (int i = 1; i <= s.size(); i++) {
            for (int j = 1; j <= t.size(); j++) {
                if (s[i-1] == t[j-1]) {
                    dp[i][j] = dp[i-1][j-1] + dp[i-1][j];
                }
                else {
                    dp[i][j] = dp[i-1][j];
                }
            }
        }
        return dp[s.size()][t.size()];
    }
};

leetcode 516. 最长回文子序列

给定一个字符串 s,找到其中最长的回文子序列的长度。

  1. 定义状态

    • dp[i][j] 表示字符串 s 的第 i 到第 j 个字符之间的最长回文子序列的长度。
  2. 状态转移方程

    • 如果 s[i] == s[j],则 dp[i][j] = dp[i+1][j-1] + 2
    • 否则,dp[i][j] = max(dp[i+1][j], dp[i][j-1])
  3. 初始化

    • dp[i][i] = 1,因为单个字符是一个长度为 1 的回文子序列。
  4. 结果

    • dp[0][n-1],其中 n 是字符串 s 的长度。
class Solution {
    // 定义状态:dp[i][j]表示字符串s的第i个到第j个字符之间的最长回文子序列的长度
    // 状态转移方程:if s[i] == s[j] dp[i][j] = dp[i+1][j-1] + 2
    //              else dp[i][j] = Math.max(dp[i+1][j], dp[i][j-1])
    // 初始化 dp[i][i] = 1
    // 结果 dp[0][n-1]
    public int longestPalindromeSubseq(String s) {
        int n = s.length();
        int[][] dp = new int[n][n];
        for (int i = 0; i < n; i++) {
            dp[i][i] = 1;
        }
        // 从i+1 --> i 则i逆序  从j-1 --> j 则j正序
        for (int i = n - 1; i >= 0; i--) {
            for (int j = i + 1; j < n; j++) {
                if (s.charAt(i) == s.charAt(j)) {
                    dp[i][j] = dp[i+1][j-1] + 2;
                } else {
                    dp[i][j] = Math.max(dp[i+1][j], dp[i][j-1]);
                }
            }
        }
        return dp[0][n-1];
    }
}
class Solution {
    public int longestPalindromeSubseq(String s) {
        char[] chs = s.toCharArray();
        int n = chs.length;
        return dfs(0, n - 1, chs); 
    }

    private int dfs(int i, int j, char[] s) {
        if (i > j) {
            return 0;   // 空串
        }
        if (i == j) {
            return 1;   // 只有一个字母
        }
        if (s[i] == s[j]) {
            return dfs(i + 1, j - 1, s) + 2;  // 都选择
        }
        return Math.max(dfs(i + 1, j, s), dfs(i, j - 1, s)); // 枚举哪个不选
    }
}
class Solution {
    public int longestPalindromeSubseq(String s) {
        char[] chs = s.toCharArray();
        int n = chs.length;
        int[][] memo = new int[n][n];
        for (int i = 0; i < n; i++) {
            Arrays.fill(memo[i], -1);
        }
        return dfs(0, n - 1, chs, memo); 
    }

    private int dfs(int i, int j, char[] s, int[][] memo) {
        if (i > j) {
            return 0;   // 空串
        }
        if (i == j) {
            return 1;   // 只有一个字母
        }
        if (memo[i][j] != -1) {   // 之前计算过
            return memo[i][j];
        }
        if (s[i] == s[j]) {
            memo[i][j] = dfs(i + 1, j - 1, s, memo) + 2;  // 都选择
            return memo[i][j];
        }
        return memo[i][j] = Math.max(dfs(i + 1, j, s, memo), dfs(i, j - 1, s, memo)); // 枚举哪个不选
    }
}

leetcode 5. 最长回文子串

class Solution {
    public String longestPalindrome(String s) {
        String res = "";
        for (int i = 0; i < s.length(); i++) {
            // 以 s[i] 为中心的最长回文子串
            String s1 = findPalindrome(s, i, i);
            // 以 s[i] 和 s[i+1] 为中心的最长回文子串
            String s2 = findPalindrome(s, i, i + 1);
            // res = longest(res, s1, s2)
            res = res.length() > s1.length() ? res : s1;
            res = res.length() > s2.length() ? res : s2;
        }
        return res;
    }
    // 在 s 中寻找以 s[l] 和 s[r] 为中心的最长回文串
    // 区分l和r为中心主要处理,abba和aba这种问题。
    public String findPalindrome(String s, int l, int r) {
        // 防止索引越界
        while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
            l--;
            r++;
        }
        // 返回以 s[l] 和 s[r] 为中心的最长回文串
        return s.substring(l + 1, r);
    }
}

leetcode 72. 编辑距离

class Solution {
    // 定义状态:`dp[i][j]` 表示将 `word1` 的前 `i` 个字符转换成 `word2` 的前 `j` 个字符所需的最少操作次数。
    // 状态转移方程:如果 `word1[i-1] == word2[j-1]`,则 `dp[i][j] = dp[i-1][j-1]`。
    //              否则,`dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1`。
    // 初始化:`dp[0][j] = j`,表示将空字符串转换为 `word2` 的前 `j` 个字符需要 `j` 次插入操作。
    //         `dp[i][0] = i`,表示将 `word1` 的前 `i` 个字符转换为空字符串需要 `i` 次删除操作。
    // 结果:`dp[m][n]`,其中 `m` 和 `n` 分别是 `word1` 和 `word2` 的长度。
    public int minDistance(String word1, String word2) {
        char[] w1 = word1.toCharArray();
        char[] w2 = word2.toCharArray();
        int n1 = w1.length;
        int n2 = w2.length;
    
        int[][] dp = new int[n1 + 1][n2 + 1];

        for (int i = 0; i <= n1; i++) {
            dp[i][0] = i;
        }

        for (int j = 0; j <= n2; j++) {
            dp[0][j] = j;
        }

        for (int i = 1; i <= n1; i++) {
            for (int j = 1; j <= n2; j++) {
                if (w1[i-1] == w2[j-1]) {
                    dp[i][j] = dp[i-1][j-1];
                } else {
                    // dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1; 
                    int minValue = Math.min(dp[i-1][j], dp[i][j-1]);
                    minValue = Math.min(minValue, dp[i-1][j-1]);
                    dp[i][j] = minValue + 1;  // 插入or替换
                }
            }
        }

        return dp[n1][n2];
    }
}
# [LeetCode](https://leetcode.com/problemset/algorithms/) ![Language](https://img.shields.io/badge/language-Python%20%2F%20C++%2011-orange.svg) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE.md) ![Progress](https://img.shields.io/badge/progress-468%20%2F%20468-ff69b4.svg) Up to date (2016-12-18), there are `447` Algorithms / `13` Database / `4` Shell / `4` Draft questions on [LeetCode Online Judge](https://leetcode.com/). The number of questions is increasing recently. Here is the classification of all `468` questions. For more questions and solutions, you can see my [LintCode](https://github.com/kamyu104/LintCode) repository. I'll keep updating for full summary and better solutions. Stay tuned for updates. (Notes: "馃摉" means you need to subscribe to [LeetCode premium membership](https://leetcode.com/subscribe/) for the access to premium questions.) ## Algorithms * [Bit Manipulation](https://github.com/kamyu104/LeetCode#bit-manipulation) * [Array](https://github.com/kamyu104/LeetCode#array) * [String](https://github.com/kamyu104/LeetCode#string) * [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap](https://github.com/kamyu104/LeetCode#heap) * [Tree](https://github.com/kamyu104/LeetCode#tree) * [Hash Table](https://github.com/kamyu104/LeetCode#hash-table) * [Data Structure](https://github.com/kamyu104/LeetCode#data-structure) * [Math](https://github.com/kamyu104/LeetCode#math) * [Two Pointers](https://github.com/kamyu104/LeetCode#two-pointers) * [Sort](https://github.com/kamyu104/LeetCode#sort) * [Recursion](https://github.com/kamyu104/LeetCode#recursion) * [Binary Search](https://github.com/kamyu104/LeetCode#binary-search) * [Binary Search Tree](https://github.com/kamyu104/LeetCode#binary-search-tree) * [Breadth-First Search](https://github.com/kamyu104/LeetCode#breadth-first-search) * [Depth-First Search](https://github.com/kamyu104/LeetCode#depth-first-search) * [Backtracking](https://github.com/kamyu104/LeetCode#backtracking) * [Dynamic Programming](https://github.com/kamyu104/LeetCode#dynamic-programming) * [Greedy](https://github.com/kamyu104/LeetCode#greedy) * [Design](https://github.com/kamyu104/LeetCode#design) ## Database * [SQL](https://github.com/kamyu104/LeetCode#sql) ## Shell * [Shell Script](https://github.com/kamyu104/LeetCode#shell-script) ## Bit Manipulation | # | Title | Solution | Time | Space | Difficulty | Tag | Note| |-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----| 136 | [Single Number](https://leetcode.com/problems/single-number/) | [C++](./C++/single-number.cpp) [Python](./Python/single-number.py) | _O(n)_ | _O(1)_ | Easy ||| 137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [C++](./C++/single-number-ii.cpp) [Python](./Python/single-number-ii.py) | _O(n)_ | _O(1)_ | Medium ||| 190 | [Reverse Bits](https://leetcode.com/problems/reverse-bits/) | [C++](./C++/reverse-bits.cpp) [Python](./Python/reverse-bits.py) | _O(1)_ | _O(1)_ | Easy ||| 191 |[Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) | [C++](./C++/number-of-1-bits.cpp) [Python](./Python/number-of-1-bits.py) | _O(1)_ | _O(1)_ | Easy ||| 201 | [Bitwise AND of Numbers Range](https://leetcode.com/problems/bitwise-and-of-numbers-range/) | [C++](./C++/bitwise-and-of-numbers-range.cpp) [Python](./Python/bitwise-and-of-numbers-range.py) | _O(1)_ | _O(1)_ | Medium || 231 | [Power of Two](https://leetcode.com/problems/power-of-two/) | [C++](./C++/power-of-two.cpp) [Python](./Python/power-of-two.py) | _O(1)_ | _O(1)_ | Easy | LintCode | 260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [C++](./C++/single-number-iii.cpp) [Python](./Python/single-number-iii.py) | _O(n)_ | _O(1)_ | Medium || 268| [Missing Number](https://leetcode.com/problems/missing-number/) | [C++](./C++/missing-number.cpp) [Python](./Python/missing-number.py) | _O(n)_ | _O(1)_ | Medium | LintCode || 318| [Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) | [C++](./C++/maximum-product-of-word-lengths.cpp) [Python](./Python/maximum-product-of-word-lengths.py) | _O(n)_ ~ _O(n^2)_ | _O(n)_ | Medium || Bit Manipulation, Counting Sort, Pruning| 342 | [Power of Four](https://leetcode.com/problems/power-of-four/) | [C++](./C++/power-of-four.cpp) [Python](./Python/power-of-four.py) | _O(1)_ | _O(1)_ | Easy | | 371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [C++](./C++/sum-of-two-integers.cpp) [Python](./Python/sum-of-two-integers.py) | _O(1)_ | _O(1)_ | Easy | LintCode | 389 | [Find the Difference](https://leetcode.com/problems/find-the-difference/) | [C++](./C++/find-the-difference.cpp) [Python](./Python/find-the-difference.py) | _O(n)_ | _O(1)_ | Easy | | 393 | [UTF-8 Validation](https://leetcode.com/problems/utf-8-validation/) | [C++](./C++/utf-8-validation.cpp) [Python](./Python/utf-8-validation.py) | _O(n)_ | _O(1)_ | Medium | | 401 | [Binary Watch](https://leetcode.com/problems/binary-watch/) | [C++](./C++/binary-watch.cpp) [Python](./Python/binary-watch.py) | _O(1)_ | _O(1)_ | Easy | | 411 | [Minimum Unique Word Abbreviation](https://leetcode.com/problems/minimum-unique-word-abbreviation/) | [C++](./C++/minimum-unique-word-abbreviation.cpp) [Python](./Python/minimum-unique-word-abbreviation.py) | _O(2^n)_ | _O(n)_ | Hard | 馃摉 | 421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [C++](./C++/maximum-xor-of-two-numbers-in-an-array.cpp) [Python](./Python/maximum-xor-of-two-numbers-in-an-array.py) | _O(n)_ | _O(1)_ | Medium || 461 | [Hamming Distance](https://leetcode.com/problems/hamming-distance/) | [C++](./C++/hamming-distance.cpp) [Python](./Python/hamming-distance.py) | _O(1)_ | _O(1)_ | Easy || 462 | [Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/) | [C++](./C++/minimum-moves-to-equal-array-elements-ii.cpp) [Python](./Python/minimum-moves-to-equal-array-elements-ii.py) | _O(n)_ on average | _O(1)_ | Medium || 477 | [Total Hamming Distance](https://leetcode.com/problems/total-hamming-distance/) | [C++](./C++/total-hamming-distance.cpp) [Python](./Python/total-hamming-distance.py) | _O(n)_ | _O(1)_ | Medium || ## Array | # | Title | Solution | Time | Space | Difficulty | Tag | Note| |-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----| 15 | [3 Sum](https://leetcode.com/problems/3sum/) | [C++](./C++/3sum.cpp) [Python](./Python/3sum.py) | _O(n^2)_ | _O(1)_ | Medium || Two Pointers 16 | [3 Sum Closest](https://leetcode.com/problems/3sum-closest/) | [C++](./C++/3sum-closest.cpp) [Python](./Python/3sum-closest.py) | _O(n^2)_ | _O(1)_ | Medium || Two Pointers 18| [4 Sum](https://leetcode.com/problems/4sum/) | [C++](./C++/4sum.cpp) [Python](./Python/4sum.py) | _O(n^3)_ | _O(1)_ | Medium || Two Pointers 26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| [C++](./C++/remove-duplicates-from-sorted-array.cpp) [Python](./Python/remove-duplicates-from-sorted-array.py) | _O(n)_ | _O(1)_ | Easy || Two Pointers 27 | [Remove Element](https://leetcode.com/problems/remove-element/) | [C++](./C++/remove-element.cpp) [Python](./Python/remove-element.py) | _O(n)_ | _O(1)_ | Easy || 31 | [Next Permutation](https://leetcode.com/problems/next-permutation/)| [C++](./C++/next-permutation.cpp) [Python](./Python/next-permutation.py) | _O(n)_ | _O(1)_ | Medium || Tricky 41 | [First Missing Positive](https://leetcode.com/problems/first-missing-positive/)| [C++](./C++/first-missing-positive.cpp) [Python](./Python/first-missing-positive.py) | _O(n)_ | _O(1)_ | Hard || Tricky 48 | [Rotate Image](https://leetcode.com/problems/rotate-image/) | [C++](./C++/rotate-image.cpp) [Python](./Python/rotate-image.py) | _O(n^2)_ | _O(1)_ | Medium || 54 | [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [C++](./C++/spiral-matrix.cpp) [Python](./Python/spiral-matrix.py) | _O(m * n)_ | _O(1)_ | Medium || 59 | [Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii/) | [C++](./C++/spiral-matrix-ii.cpp) [Python](./Python/spiral-matrix-ii.py) | _O(n^2)_ | _O(1)_ | Medium || 66 | [Plus One](https://leetcode.com/problems/plus-one/) | [C++](./C++/plus-one.cpp) [Python](./Python/plus-one.py) | _O(n)_ | _O(1)_ | Easy || 73 | [Set Matrix Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) | [C++](./C++/set-matrix-zeroes.cpp) [Python](./Python/set-matrix-zeroes.py) | _O(m * n)_ | _O(1)_ | Medium || 80 | [Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/)| [C++](./C++/remove-duplicates-from-sorted-array-ii.cpp) [Python](./Python/remove-duplicates-from-sorted-array-ii.py) | _O(n)_ | _O(1)_ | Medium || Two Pointers 118 | [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/)| [C++](./C++/pascals-triangle.cpp) [Python](./Python/pascals-triangle.py) | _O(n^2)_ | _O(1)_ | Easy || 119 | [Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/)| [C++](./C++/pascals-triangle-ii.cpp) [Python](./Python/pascals-triangle-ii.py) | _O(n^2)_ | _O(1)_ | Easy || 121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/)| [C++](./C++/best-time-to-buy-and-sell-stock.cpp) [Python](./Python/best-time-to-buy-and-sell-stock.py) | _O(n)_ | _O(1)_ | Easy || 128 | [Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/)| [C++](./C++/longest-consecutive-sequence.cpp) [Python](./Python/longest-consecutive-sequence.py) | _O(n)_ | _O(n)_ | Hard || Tricky 157 | [Read N Characters Given Read4](https://leetcode.com/problems/read-n-characters-given-read4/) | [C++](./C++/read-n-characters-given-read4.cpp) [Python](./Python/read-n-characters-given-read4.py) | _O(n)_ | _O(1)_ | Easy |馃摉| 158 | [Read N Characters Given Read4 II - Call multiple times](https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/) | [C++](./C++/read-n-characters-given-read4-ii-call-multiple-times.cpp) [Python](./Python/read-n-characters-given-read4-ii-call-multiple-times.py) | _O(n)_ | _O(1)_ | Hard |馃摉| 163 | [Missing Ranges](https://leetcode.com/problems/missing-ranges/)| [C++](./C++/missing-ranges.cpp) [Python](./Python/missing-ranges.py) | _O(n)_ | _O(1)_ | Medium | 馃摉 | 169 | [Majority Element](https://leetcode.com/problems/majority-element/) | [C++](./C++/majority-element.cpp) [Python](./Python/majority-element.py) | _O(n)_ | _O(1)_ | Easy | 189 | [Rotate Array](https://leetcode.com/problems/rotate-array/) | [C++](./C++/rotate-array.cpp) [Python](./Python/rotate-array.py) | _O(n)_ | _O(1)_ | Easy || 209 | [Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/) | [C++](./C++/minimum-size-subarray-sum.cpp) [Python](./Python/minimum-size-subarray-sum.py) | _O(n)_ | _O(1)_ | Medium | | Binary Search 215 | [Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/) | [C++](./C++/kth-largest-element-in-an-array.cpp) [Python](./Python/kth-largest-element-in-an-array.py)| _O(n)_ ~ _O(n^2)_ | _O(1)_ | Medium | EPI| 228 | [Summary Ranges](https://leetcode.com/problems/summary-ranges/) | [C++](./C++/summary-ranges.cpp) [Python](./Python/summary-ranges.py)| _O(n)_ | _O(1)_ | Medium | | 229 | [Majority Element II](https://leetcode.com/problems/majority-element-ii/) | [C++](./C++/majority-element-ii.cpp) [Python](./Python/majority-element-ii.py) | _O(n)_ | _O(1)_ | Medium | | 238 | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) | [C++](./C++/product-of-array-except-self.cpp) [Python](./Python/product-of-array-except-self.py) | _O(n)_ | _O(1)_ | Medium | LintCode | 240 | [Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/) | [C++](./C++/search-a-2d-matrix-ii.cpp) [Python](./Python/search-a-2d-matrix-ii.py) | _O(m + n)_ | _O(1)_ | Medium | EPI, LintCode | 243| [Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance/) | [C++](./C++/shortest-word-distance.cpp) [Python](./Python/shortest-word-distance.py) | _O(n)_ | _O(1)_ | Easy |馃摉|| 245| [Shortest Word Distance III](https://leetcode.com/problems/shortest-word-distance-iii/) | [C++](./C++/shortest-word-distance-iii.cpp) [Python](./Python/shortest-word-distance-iii.py) | _O(n)_ | _O(1)_ | Medium |馃摉|| 251| [Flatten 2D Vector](https://leetcode.com/problems/flatten-2d-vector/) | [C++](./C++/flatten-2d-vector.cpp) [Python](./Python/flatten-2d-vector.py) | _O(1)_ | _O(1)_ | Medium |馃摉|| 277| [Find the Celebrity](https://leetcode.com/problems/find-the-celebrity/) | [C++](./C++/find-the-celebrity.cpp) [Python](./Python/find-the-celebrity.py) | _O(n)_ | _O(1)_ | Medium |馃摉, EPI || 289| [Game of Life](https://leetcode.com/problems/game-of-life/) | [C++](./C++/game-of-life.cpp) [Python](./Python/game-of-life.py) | _O(m * n)_ | _O(1)_ | Medium ||| 293| [Flip Game](https://leetcode.com/problems/flip-game/) | [C++](./C++/flip-game.cpp) [Python](./Python/flip-game.py) | _O(n * (c+1))_ | _O(1)_ | Easy |馃摉|| 296| [Best Meeting Point](https://leetcode.com/problems/best-meeting-point/) | [C++](./C++/best-meeting-point.cpp) [Python](./Python/best-meeting-point.py) | _O(m * n)_ | _O(m + n)_ | Hard |馃摉|| 311| [Sparse Matrix Multiplication](https://leetcode.com/problems/sparse-matrix-multiplication/) | [C++](./C++/sparse-matrix-multiplication.cpp) [Python](./Python/sparse-matrix-multiplication.py) | _O(m * n * l)_ | _O(m * l)_ | Medium |馃摉|| 334| [Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/) | [C++](./C++/increasing-triplet-subsequence.cpp) [Python](./Python/increasing-triplet-subsequence.py) | _O(n)_ | _O(1)_ | Medium ||| 370| [Range Addition](https://leetcode.com/problems/range-addition/) | [C++](./C++/range-addition.cpp) [Python](./Python/range-addition.py) | _O(k + n)_ | _O(1)_ | Medium |馃摉|| 384| [Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/) | [C++](./C++/shuffle-an-array.cpp) [Python](./Python/shuffle-an-array.py) | _O(n)_ | _O(n)_ | Medium | EPI || 396| [Rotate Function](https://leetcode.com/problems/rotate-function/) | [C++](./C++/rotate-function.cpp) [Python](./Python/rotate-function.py) | _O(n)_ | _O(1)_ | Easy ||| 412| [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [C++](./C++/fizz-buzz.cpp) [Python](./Python/fizz-buzz.py) | _O(n)_ | _O(1)_ | Easy ||| 414| [Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | [C++](./C++/third-maximum-number.cpp) [Python](./Python/third-maximum-number.py) | _O(n)_ | _O(1)_ | Easy ||| 419| [Battleships in a Board](https://leetcode.com/problems/battleships-in-a-board/) | [C++](./C++/battleships-in-a-board.cpp) [Python](./Python/battleships-in-a-board.py) | _O(m * n)_ | _O(1)_ | Medium ||| 422| [Valid Word Square](https://leetcode.com/problems/valid-word-square/) | [C++](./C++/valid-word-square.cpp) [Python](./Python/valid-word-square.py) | _O(m * n)_ | _O(1)_ | Easy |馃摉|| 442| [Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/) | [C++](./C++/find-all-duplicates-in-an-array.cpp) [Python](./Python/find-all-duplicates-in-an-array.py) | _O(n)_ | _O(1)_ | Medium ||| 448| [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | [C++](./C++/find-all-numbers-disappeared-in-an-array.cpp) [Python](./Python/find-all-numbers-disappeared-in-an-array.py) | _O(n)_ | _O(1)_ | Easy ||| ## String | # | Title | Solution | Time | Space | Difficulty | Tag | Note| |-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----| 5| [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring/) | [C++](./C++/longest-palindromic-substring.cpp) [Python](./Python/longest-palindromic-substring.py) | _O(n)_ | _O(n)_ | Medium || `Manacher's Algorithm` 6| [ZigZag Conversion](https://leetcode.com/problems/zigzag-conversion/) | [C++](./C++/zigzag-conversion.cpp) [Python](./Python/zigzag-conversion.py) | _O(n)_ | _O(1)_ | Easy || 8| [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/) | [C++](./C++/string-to-integer-atoi.cpp) [Python](./Python/string-to-integer-atoi.py) | _O(n)_ | _O(1)_ | Easy || 14| [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/) | [C++](./C++/longest-common-prefix.cpp) [Python](./Python/longest-common-prefix.py) | _O(n * k)_ | _O(1)_ | Easy || 28| [Implement strStr()](https://leetcode.com/problems/implement-strstr/) | [C++](./C++/implement-strstr.cpp) [Python](./Python/implement-strstr.py) | _O(n + k)_ | _O(k)_ | Easy || `KMP Algorithm` 38| [Count and Say](https://leetcode.com/problems/count-and-say/) | [C++](./C++/count-and-say.cpp) [Python](./Python/count-and-say.py)| _O(n * 2^n)_ | _O(2^n)_ | Easy || 43| [Multiply Strings](https://leetcode.com/problems/multiply-strings/) | [C++](./C++/multiply-strings.cpp) [Python](./Python/multiply-strings.py) | _O(m * n)_ | _O(m + n)_ | Medium || 58| [Length of Last Word](https://leetcode.com/problems/length-of-last-word/) | [C++](./C++/length-of-last-word.cpp) [Python](./Python/length-of-last-word.py) | _O(n)_ | _O(1)_ | Easy || 67| [Add Binary](https://leetcode.com/problems/add-binary/) | [C++](./C++/add-binary.cpp) [Python](./Python/add-binary.py) | _O(n)_ | _O(1)_ | Easy || 68| [Text Justification](https://leetcode.com/problems/text-justification/) | [C++](./C++/text-justification.cpp) [Python](./Python/text-justification.py) | _O(n)_ | _O(1)_ | Hard || 125| [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [C++](./C++/valid-palindrome.cpp) [Python](./Python/valid-palindrome.py) | _O(n)_ | _O(1)_ | Easy || 151| [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [C++](./C++/reverse-words-in-a-string.cpp) [Python](./Python/reverse-words-in-a-string.py) | _O(n)_ | _O(1)_ | Medium || 161| [One Edit Distance](https://leetcode.com/problems/one-edit-distance/) | [C++](./C++/one-edit-distance.cpp) [Python](./Python/one-edit-distance.py) | _O(m + n)_ | _O(1)_ | Medium |馃摉 | 165| [Compare Version Numbers](https://leetcode.com/problems/compare-version-numbers/) | [C++](./C++/compare-version-numbers.cpp) [Python](./Python/compare-version-numbers.py) | _O(n)_ | _O(1)_ | Easy || 186| [Reverse Words in a String II](https://leetcode.com/problems/reverse-words-in-a-string-ii/) |[C++](./C++/reverse-words-in-a-string-ii.cpp) [Python](./Python/reverse-words-in-a-string-ii.py) | _O(n)_ | _O(1)_ | Medium | 馃摉 | 214| [Shortest Palindrome](https://leetcode.com/problems/shortest-palindrome/) | [C++](./C++/shortest-palindrome.cpp) [Python](./Python/shortest-palindrome.py) | _O(n)_ | _O(n)_ | Hard || `KMP Algorithm` `Manacher's Algorithm` 242| [Valid Anagram](https://leetcode.com/problems/valid-anagram/)| [C++](./C++/valid-anagram.cpp) [Python](./Python/valid-anagram.py) | _O(n)_ | _O(1)_ | Easy | LintCode | 271| [Encode and Decode Strings](https://leetcode.com/problems/encode-and-decode-strings/) | [C++](./C++/encode-and-decode-strings.cpp) [Python](./Python/encode-and-decode-strings.py) | _O(n)_ | _O(1)_ | Medium | 馃摉 | 273| [Integer to English Words](https://leetcode.com/problems/integer-to-english-words/) | [C++](./C++/integer-to-english-words.cpp) [Python](./Python/integer-to-english-words.py) | _O(logn)_ | _O(1)_ | Hard | | 306| [Addictive Number](https://leetcode.com/problems/additive-number/) | [C++](./C++/additive-number.cpp) [Python](./Python/additive-number.py) | _O(n^3)_ | _O(n)_ | Medium | | 383| [Ransom Note](https://leetcode.com/problems/ransom-note/) | [C++](./C++/ransom-note.cpp) [Python](./Python/ransom-note.py) | _O(n)_ | _O(1)_ | Easy | EPI | 405| [Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal/) | [C++](./C++/convert-a-number-to-hexadecimal.cpp) [Python](./Python/convert-a-number-to-hexadecimal.py) | _O(n)_ | _O(1)_ | Easy | | 408| [Valid Word Abbreviation](https://leetcode.com/problems/valid-word-abbreviation/) | [C++](./C++/valid-word-abbreviation.cpp) [Python](./Python/valid-word-abbreviation.py) | _O(n)_ | _O(1)_ | Easy | 馃摉 | 415| [Add Strings](https://leetcode.com/problems/add-strings/) | [C++](./C++/add-strings.cpp) [Python](./Python/add-strings.py) | _O(n)_ | _O(1)_ | Easy | | 420| [Strong Password Checker](https://leetcode.com/problems/strong-password-checker/) | [C++](./C++/strong-password-checker.cpp) [Python](./Python/strong-password-checker.py) | _O(n)_ | _O(1)_ | Hard | | 434| [Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/) | [C++](./C++/number-of-segments-in-a-string.cpp) [Python](./Python/number-of-segments-in-a-string.py) | _O(n)_ | _O(1)_ | Easy | | 459| [Repeated Substring Pattern](https://leetcode.com/problems/repeated-substring-pattern/) | [C++](./C++/repeated-substring-pattern.cpp) [Python](./Python/repeated-substring-pattern.py) | _O(n)_ | _O(n)_ | Easy || `KMP Algorithm` | 468| [Validate IP Address](https://leetcode.com/problems/validate-ip-address/) | [C++](./C++/validate-ip-address.cpp) [Python](./Python/validate-ip-address.py) | _O(1)_ | _O(1)_ | Medium | | ## Linked List | # | Title | Solution | Time | Space | Difficulty | Tag | Note| |-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----| 2| [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | [C++](./C++/add-two-numbers.cpp) [Python](./Python/add-two-numbers.py) | _O(n)_ | _O(1)_ | Medium || 21| [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/)| [C++](./C++/merge-two-sorted-lists.cpp) [Python](./Python/merge-two-sorted-lists.py) | _O(n)_ | _O(1)_ | Easy || 23| [Merge k Sorted Lists](https://leetcode.com/problems/merge-k-sorted-lists/) | [C++](./C++/merge-k-sorted-lists.cpp) [Python](./Python/merge-k-sorted-lists.py) | _O(nlogk)_| _O(1)_| Hard | | Heap, Divide and Conquer 24| [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/)| [C++](./C++/swap-nodes-in-pairs.cpp) [Python](./Python/swap-nodes-in-pairs.py) | _O(n)_ | _O(1)_ | Easy || 25| [Reverse Nodes in k-Group](https://leetcode.com/problems/reverse-nodes-in-k-group/)| [C++](./C++/reverse-nodes-in-k-group.cpp) [Python](./Python/reverse-nodes-in-k-group.py) | _O(n)_ | _O(1)_ | Hard || 61| [Rotate List](https://leetcode.com/problems/rotate-list/)| [C++](./C++/rotate-list.cpp) [Python](./Python/rotate-list.py) | _O(n)_ | _O(1)_ | Medium || 82| [Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/)| [C++](./C++/remove-duplicates-from-sorted-list-ii.cpp) [Python](./Python/remove-duplicates-from-sorted-list-ii.py) | _O(n)_ | _O(1)_ | Medium || 83| [Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/)| [C++](./C++/remove-duplicates-from-sorted-list.cpp) [Python](./Python/remove-duplicates-from-sorted-list.py) | _O(n)_ | _O(1)_ | Easy || 92| [Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/)| [C++](./C++/reverse-linked-list-ii.cpp) [Python](./Python/reverse-linked-list-ii.py) | _O(n)_ | _O(1)_ | Medium || 138| [Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/) | [C++](./C++/copy-list-with-random-pointer.cpp) [Python](./Python/copy-list-with-random-pointer.py) | _O(n)_ | _O(1)_ | Hard || 160| [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/)| [C++](./C++/intersection-of-two-linked-lists.cpp) [Python](./Python/intersection-of-two-linked-lists.py) | _O(m + n)_ | _O(1)_ | Easy || 203| [Remove Linked List Elements](https://leetcode.com/problems/remove-linked-list-elements/)| [C++](./C++/remove-linked-list-elements.cpp) [Python](./Python/remove-linked-list-elements.py) | _O(n)_ | _O(1)_ | Easy || 206| [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/)| [C++](./C++/reverse-linked-list.cpp) [Python](./Python/reverse-linked-list.py) | _O(n)_ | _O(1)_ | Easy || 234| [Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list/)| [C++](./C++/palindrome-linked-list.cpp) [Python](./Python/palindrome-linked-list.py) | _O(n)_ | _O(1)_ | Easy || 237| [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list/)| [C++](./C++/delete-node-in-a-linked-list.cpp) [Python](./Python/delete-node-in-a-linked-list.py) | _O(1)_ | _O(1)_ | Easy | LintCode | 328| [Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list/)| [C++](./C++/odd-even-linked-list.cpp) [Python](./Python/odd-even-linked-list.py) | _O(n)_ | _O(1)_ | Medium | | 369| [Plus One Linked List](https://leetcode.com/problems/plus-one-linked-list/)| [C++](./C++/plus-one-linked-list.cpp) [Python](./Python/plus-one-linked-list.py) | _O(n)_ | _O(1)_ | Medium | 馃摉 | Two Pointers | 445| [Add Two Numbers II](https://leetcode.com/problems/add-two-numbers-ii/)| [C++](./C++/add-two-numbers-ii.cpp) [Python](./Python/add-two-numbers-ii.py) | _O(m + n)_ | _O(m + n)_ | Medium ||| ## Stack | # | Title | Solution | Time | Space | Difficulty | Tag | Note| |-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----| 20| [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/)| [C++](./C++/valid-parentheses.cpp) [Python](./Python/valid-parentheses.py) | _O(n)_ | _O(n)_ | Easy || 32| [Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/)| [C++](./C++/longest-valid-parentheses.cpp) [Python](./Python/longest-valid-parentheses.py) | _O(n)_ | _O(1)_ | Hard || 71| [Simplify Path](https://leetcode.com/problems/simplify-path/)| [C++](./C++/simplify-path.cpp) [Python](./Python/simplify-path.py) | _O(n)_ | _O(n)_ | Medium || 84| [Largest Rectangle in Histogram](https://leetcode.com/problems/largest-rectangle-in-histogram/) | [C++](./C++/largest-rectangle-in-histogram.cpp) [Python](./Python/largest-rectangle-in-histogram.py) | _O(n)_ | _O(n)_ | Hard || Ascending Stack, DP 85| [Maximal Rectangle](https://leetcode.com/problems/maximal-rectangle/)| [C++](./C++/maximal-rectangle.cpp) [Python](./Python/maximal-rectangle.py)| _O(m * n)_ | _O(n)_ | Hard | EPI | Ascending Stack 101| [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/)| [C++](./C++/symmetric-tree.cpp) [Python](./Python/symmetric-tree.py) | _O(n)_ | _O(h)_ | Easy || 150| [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/)| [C++](./C++/evaluate-reverse-polish-notation.cpp) [Python](./Python/evaluate-reverse-polish-notation.py)| _O(n)_| _O(n)_| Medium || 155| [Min Stack](https://leetcode.com/problems/min-stack/) | [C++](./C++/min-stack.cpp) [Python](./Python/min-stack.py) | _O(n)_ | _O(1)_ | Easy || 173| [Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator/) | [C++](./C++/binary-search-tree-iterator.cpp) [Python](./Python/binary-search-tree-iterator.py) | _O(1)_| _O(h)_| Medium || 224| [Basic Calculator](https://leetcode.com/problems/basic-calculator/) | [C++](./C++/basic-calculator.cpp) [Python](./Python/basic-calculator.py) | _O(n)_| _O(n)_| Hard || 227| [Basic Calculator II](https://leetcode.com/problems/basic-calculator-ii/) | [C++](./C++/basic-calculator-ii.cpp) [Python](./Python/basic-calculator-ii.py) | _O(n)_| _O(n)_| Medium || 232| [Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/) | [C++](./C++/implement-queue-using-stacks.cpp) [Python](./Python/implement-queue-using-stacks.py) | _O(1), amortized_| _O(n)_| Easy | EPI, LintCode | 255| [Verify Preorder Sequence in Binary Search Tree](https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/) | [C++](./C++/verify-preorder-sequence-in-binary-search-tree.cpp) [Python](./Python/verify-preorder-sequence-in-binary-search-tree.py) | _O(n)_| _O(1)_| Medium |馃摉|| 272| [Closest Binary Search Tree Value II](https://leetcode.com/problems/closest-binary-search-tree-value-ii/) | [C++](./C++/closest-binary-search-tree-value-ii.cpp) [Python](./Python/closest-binary-search-tree-value-ii.py) | _O(h + k)_| _O(h)_| Hard |馃摉|| 331| [Verify Preorder Serialization of a Binary Tree](https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/) | [C++](./C++/verify-preorder-serialization-of-a-binary-tree.cpp) [Python](./Python/verify-preorder-serialization-of-a-binary-tree.py) | _O(n)_| _O(1)_| Medium ||| 341| [Flatten Nested List Iterator](https://leetcode.com/problems/flatten-nested-list-iterator/)| [C++](./C++/flatten-nested-list-iterator.cpp) [Python](./Python/flatten-nested-list-iterator.py) | _O(n)_ | _O(h)_ | Medium |馃摉| Iterator | 385| [Mini Parser](https://leetcode.com/problems/mini-parser/)| [C++](./C++/mini-parser.cpp) [Python](./Python/mini-parser.py) | _O(n)_ | _O(h)_ | Medium ||| 394| [Decode String](https://leetcode.com/problems/decode-string/)| [C++](./C++/decode-string.cpp) [Python](./Python/decode-string.py) | _O(n)_ | _O(h)_ | Medium ||| 439| [Ternary Expression Parser](https://leetcode.com/problems/ternary-expression-parser/) | [C++](./C++/ternary-expression-parser.cpp) [Python](./Python/ternary-expression-parser.py) | _O(n)_ | _O(1)_ | Medium |馃摉| 456| [132 Pattern](https://leetcode.com/problems/132-pattern/) | [C++](./C++/132-pattern.cpp) [Python](./Python/132-pattern.py) | _O(n)_ | _O(n)_ | Medium || ## Queue | # | Title | Solution | Time | Space | Difficulty | Tag | Note| |-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----| 239| [Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum/)| [C++](./C++/sliding-window-maximum.cpp) [Python](./Python/sliding-window-maximum.py) | _O(n)_ | _O(k)_ | Hard | EPI, LintCode | 281| [Zigzag Iterator](https://leetcode.com/problems/zigzag-iterator/)| [C++](./C++/zigzag-iterator.cpp) [Python](./Python/zigzag-iterator.py) | _O(n)_ | _O(k)_ | Medium |馃摉|| 346| [Moving Average from Data Stream](https://leetcode.com/problems/moving-average-from-data-stream/)| [C++](./C++/moving-average-from-data-stream.cpp) [Python](./Python/moving-average-from-data-stream.py) | _O(1)_ | _O(w)_ | Easy |馃摉|| ## Heap | # | Title | Solution | Time | Space | Difficulty | Tag | Note| |-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----| 264| [Ugly Number II](https://leetcode.com/problems/ugly-number-ii/) | [C++](./C++/ugly-number-ii.cpp) [Python](./Python/ugly-number-ii.py) | _O(n)_ | _O(1)_ | Medium | CTCI, LintCode | BST, Heap | 295| [Find Median from Data Stream](https://leetcode.com/problems/find-median-from-data-stream/) | [C++](./C++/find-median-from-data-stream.cpp) [Python](./Python/find-median-from-data-stream.py) | _O(nlogn)_ | _O(n)_ | Hard | EPI, LintCode | BST, Heap | 313| [Super Ugly Number](https://leetcode.com/problems/super-ugly-number/) | [C++](./C++/super-ugly-number.cpp) [Python](./Python/super-ugly-number.py) | _O(n * k)_ | _O(n + k)_ | Medium || BST, Heap | 358| [Rearrange String k Distance Apart](https://leetcode.com/problems/rearrange-string-k-distance-apart/)| [C++](./C++/rearrange-string-k-distance-apart.cpp) [Python](./Python/rearrange-string-k-distance-apart.py) | _O(n)_ | _O(n)_ | Hard |馃摉| Greedy, Heap | 373 | [Find K Pairs with Smallest Sums](https://leetcode.com/problems/find-k-pairs-with-smallest-sums/) | [C++](./C++/find-k-pairs-with-smallest-sums.cpp) [Python](./Python/find-k-pairs-with-smallest-sums.py) | _O(k * log(min(n, m, k)))_ | _O(min(n, m, k))_ | Medium ||| 378 | [Kth Smallest Element in a Sorted Matrix](https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/) | [C++](./C++/kth-smallest-element-in-a-sorted-matrix.cpp) [Python](./Python/kth-smallest-element-in-a-sorted-matrix.py) | _O(k * log(min(n, m, k)))_ | _O(min(n, m, k))_ | Medium | LintCode || 407 | [Trapping Rain Water II](https://leetcode.com/problems/trapping-rain-water-ii/) | [C++](./C++/trapping-rain-water-ii.cpp) [Python](./Python/trapping-rain-water-ii.py) | _O(m * n * (logm + logn))_ | _O(m * n)_ | Hard | LintCode || ## Tree | # | Title | Solution | Time | Space | Difficulty | Tag | Note| |-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [C++](./C++/binary-tree-inorder-traversal.cpp) [Python](./Python/binary-tree-inorder-traversal.py) | _O(n)_| _O(1)_| Medium || `Morris Traversal` | 99 | [Recover Binary Search Tree](https://leetcode.com/problems/recover-binary-search-tree/) | [C++](./C++/recover-binary-search-tree.cpp) [Python](./Python/recover-binary-search-tree.py) | _O(n)_| _O(1)_| Hard || `Morris Traversal` 144 | [Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) | [C++](./C++/binary-tree-preorder-traversal.cpp) [Python](./Python/binary-tree-preorder-traversal.py) | _O(n)_| _O(1)_| Medium || `Morris Traversal` 145 | [Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/) | [C++](./C++/binary-tree-postorder-traversal.cpp) [Python](./Python/binary-tree-postorder-traversal.py) | _O(n)_| _O(1)_| Hard || `Morris Traversal` 208 | [Implement Trie (Prefix Tree)](https://leetcode.com/problems/implement-trie-prefix-tree/) | [C++](./C++/implement-trie-prefix-tree.cpp) [Python](./Python/implement-trie-prefix-tree.py) | _O(n)_ | _O(1)_ | Medium || Trie 211 | [Add and Search Word - Data structure design](https://leetcode.com/problems/add-and-search-word-data-structure-design/) | [C++](./C++/add-and-search-word-data-structure-design.cpp) [Python](./Python/add-and-search-word-data-structure-design.py) | _O(min(n, h))_ | _O(min(n, h))_ | Medium || Trie, DFS 226| [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/) | [C++](./C++/invert-binary-tree.cpp) [Python](./Python/invert-binary-tree.py) | _O(n)_ | _O(h)_, _O(w)_ | Easy || 297 | [Serialize and Deserialize Binary Tree](https://leetcode.com/problems/serialize-and-deserialize-binary-tree/) | [C++](./C++/serialize-and-deserialize-binary-tree.cpp) [Python](./Python/serialize-and-deserialize-binary-tree.py) | _O(n)_ | _O(h)_ | Hard | LintCode | DFS 307 | [Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/) | [C++](./C++/range-sum-query-mutable.cpp) [Python](./Python/range-sum-query-mutable.py) | ctor: _O(n)_, update: _O(logn)_, query: _O(logn)_ | _O(n)_ | Medium | LintCode | DFS, Segment Tree, BIT 308 | [Range Sum Query 2D - Mutable](https://leetcode.com/problems/range-sum-query-2d-mutable/) | [C++](./C++/range-sum-query-2d-mutable.cpp) [Python](./Python/range-sum-query-2d-mutable.py) | ctor: _O(m * n)_, update: _O(logm + logn)_, query: _O(logm + logn)_ | _O(m * n)_ | Hard | 馃摉 | DFS, Segment Tree, BIT 315|[Count of Smaller Numbers After Self](https://leetcode.com/problems/count-of-smaller-numbers-after-self/)| [C++](./C++/count-of-smaller-numbers-after-self.cpp) [Python](./Python/count-of-smaller-numbers-after-self.py)| _O(nlogn)_ | _O(n)_ | Hard | LintCode | BST, BIT, Divide and Conquer | ## Hash Table | # | Title | Solution | Time | Space | Difficulty | Tag | Note| |-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----| 1| [Two Sum](https://leetcode.com/problems/two-sum/) | [C++](./C++/two-sum.cpp) [Python](./Python/two-sum.py) | _O(n)_ | _O(n)_ | Easy || 3| [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [C++](./C++/longest-substring-without-repeating-characters.cpp) [Python](./Python/longest-substring-without-repeating-characters.py) | _O(n)_ | _O(1)_ | Medium || 30| [Substring with Concatenation of All Words](https://leetcode.com/problems/substring-with-concatenation-of-all-words/) | [C++](./C++/substring-with-concatenation-of-all-words.cpp) [Python](./Python/substring-with-concatenation-of-all-words.py) | _O(m * n * k)_ | _O(n * k)_ | Hard || 36| [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) | [C++](./C++/valid-sudoku.cpp) [Python](./Python/valid-sudoku.py) | _O(9^2)_ | _O(9)_ | Easy || 49| [Group Anagrams](https://leetcode.com/problems/anagrams/) | [C++](./C++/anagrams.cpp) [Python](./Python/anagrams.py) | _O(n * glogg)_ | _O(n)_ | Medium || 76| [Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/) | [C++](./C++/minimum-window-substring.cpp) [Python](./Python/minimum-window-substring.py) | _O(n)_ | _O(k)_ | Hard || 149| [Max Points on a Line](https://leetcode.com/problems/max-points-on-a-line/) | [C++](./C++/max-points-on-a-line.cpp) [Python](./Python/max-points-on-a-line.py) | _O(n^2)_ | _O(n)_ | Hard || 159| [Longest Substring with At Most Two Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/)| [C++](./C++/longest-substring-with-at-most-two-distinct-characters.cpp) [Python](./Python/longest-substring-with-at-most-two-distinct-characters.py) | _O(n)_ | _O(1)_ | Hard |馃摉| 170| [Two Sum III - Data structure design](https://leetcode.com/problems/two-sum-iii-data-structure-design/) | [C++](./C++/two-sum-iii-data-structure-design.cpp) [Python](./Python/two-sum-iii-data-structure-design.py) | _O(n)_ | _O(n)_ | Easy | 馃摉 | 187| [Repeated DNA Sequences](https://leetcode.com/problems/repeated-dna-sequences/) | [Python](./Python/repeated-dna-sequences.py) | _O(n)_ | _O(n)_ | Medium || 202| [Happy Number](https://leetcode.com/problems/happy-number/) | [C++](./C++/happy-number.cpp) [Python](./Python/happy-number.py) | _O(k)_ | _O(k)_ | Easy || 204| [Count Primes](https://leetcode.com/problems/count-primes/) | [C++](./C++/count-primes.cpp) [Python](./Python/count-primes.py) | _O(n)_ | _O(n)_ | Easy || 205| [Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/) | [C++](./C++/isomorphic-strings.cpp) [Python](./Python/isomorphic-strings.py) | _O(n)_ | _O(1)_ | Easy || 217| [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | [C++](./C++/contains-duplicate.cpp) [Python](./Python/contains-duplicate.py) | _O(n)_ | _O(n)_ | Easy || 219| [Contains Duplicate II](https://leetcode.com/problems/contains-duplicate-ii/) | [C++](./C++/contains-duplicate-ii.cpp) [Python](./Python/contains-duplicate-ii.py) | _O(n)_ | _O(n)_ | Easy || 244| [Shortest Word Distance II](https://leetcode.com/problems/shortest-word-distance-ii/) | [C++](./C++/shortest-word-distance-ii.cpp) [Python](./Python/shortest-word-distance-ii.py) | ctor: _O(n)_, lookup: _O(a + b)_ | _O(n)_ | Medium |馃摉|| 246| [Strobogrammatic Number](https://leetcode.com/problems/strobogrammatic-number/) | [C++](./C++/strobogrammatic-number.cpp) [Python](./Python/strobogrammatic-number.py) | _O(n)_ | _O(1)_ | Easy |馃摉|| 249| [Group Shifted Strings](https://leetcode.com/problems/group-shifted-strings/) | [C++](./C++/group-shifted-strings.cpp) [Python](./Python/group-shifted-strings.py) | _O(nlogn)_ | _O(n)_ | Easy |馃摉|| 266| [Palindrome Permutation](https://leetcode.com/problems/palindrome-permutation/) | [C++](./C++/palindrome-permutation.cpp) [Python](./Python/palindrome-permutation.py) | _O(n)_ | _O(1)_ | Easy |馃摉|| 288| [Unique Word Abbreviation](https://leetcode.com/problems/unique-word-abbreviation/) | [C++](./C++/unique-word-abbreviation.cpp) [Python](./Python/unique-word-abbreviation.py) | ctor: _O(n)_, lookup: _O(1)_ | _O(k)_ | Easy |馃摉|| 290| [Word Pattern](https://leetcode.com/problems/word-pattern/) | [C++](./C++/word-pattern.cpp) [Python](./Python/word-pattern.py) | _O(n)_ | _O(c)_ | Easy | variant of [Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/) || 299| [Bulls and Cows](https://leetcode.com/problems/bulls-and-cows/) | [C++](./C++/bulls-and-cows.cpp) [Python](./Python/bulls-and-cows.py) | _O(n)_ | _O(1)_ | Easy ||| 305| [Number of Islands II](https://leetcode.com/problems/number-of-islands-ii/) | [C++](./C++/number-of-islands-ii.cpp) [Python](./Python/number-of-islands-ii.py) | _O(k)_ | _O(k)_| Hard | LintCode, 馃摉 | Union Find 314| [Binary Tree Vertical Order Traversal](https://leetcode.com/problems/binary-tree-vertical-order-traversal/) | [C++](./C++/binary-tree-vertical-order-traversal.cpp) [Python](./Python/binary-tree-vertical-order-traversal.py) | _O(n)_ | _O(n)_| Medium | 馃摉 | BFS 323| [Number of Connected Components in an Undirected Graph](https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/) | [C++](./C++/number-of-connected-components-in-an-undirected-graph.cpp) [Python](./Python/number-of-connected-components-in-an-undirected-graph.py) | _O(n)_ | _O(n)_| Medium | 馃摉 | Union Find 325| [Maximum Size Subarray Sum Equals k](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/) | [C++](./C++/maximum-size-subarray-sum-equals-k.cpp) [Python](./Python/maximum-size-subarray-sum-equals-k.py) | _O(n)_ | _O(n)_| Medium | 馃摉 | 336| [Palindrome Pairs](https://leetcode.com/problems/palindrome-pairs/) | [C++](./C++/palindrome-pairs.cpp) [Python](./Python/palindrome-pairs.py) | _O(n * k^2)_ | _O(n * k)_ | Hard | | | 340| [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/)| [C++](./C++/longest-substring-with-at-most-k-distinct-characters.cpp) [Python](./Python/longest-substring-with-at-most-k-distinct-characters.py) | _O(n)_ | _O(1)_ | Hard |馃摉| 356| [Line Reflection](https://leetcode.com/problems/line-reflection/) | [C++](./C++/line-reflection.cpp) [Python](./Python/line-reflection.py) | _O(n)_| _O(n)_| Medium |馃摉| Hash, Two Pointers | 387| [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [C++](./C++/first-unique-character-in-a-string.cpp) [Python](./Python/first-unique-character-in-a-string.py) | _O(n)_| _O(n)_| Easy ||| 388| [Longest Absolute File Path](https://leetcode.com/problems/longest-absolute-file-path/) | [C++](./C++/longest-absolute-file-path.cpp) [Python](./Python/longest-absolute-file-path.py) | _O(n)_| _O(d)_| Medium || Stack | 409| [Longest Palindrome](https://leetcode.com/problems/longest-palindrome/) | [C++](./C++/longest-palindrome.cpp) [Python](./Python/longest-palindrome.py) | _O(n)_| _O(1)_| Easy ||| 424| [Longest Repeating Character Replacement](https://leetcode.com/problems/longest-repeating-character-replacement/) | [C++](./C++/longest-repeating-character-replacement.cpp) [Python](./Python/longest-repeating-character-replacement.py) | _O(n)_| _O(1)_| Medium ||| 438| [Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/) | [C++](./C++/find-all-anagrams-in-a-string.cpp) [Python](./Python/find-all-anagrams-in-a-string.py) | _O(n)_ | _O(1)_ | Easy || 447| [Number of Boomerangs](https://leetcode.com/problems/number-of-boomerangs/) | [C++](./C++/number-of-boomerangs.cpp) [Python](./Python/number-of-boomerangs.py) | _O(n^2)_ | _O(n)_ | Easy || 454| [4Sum II](https://leetcode.com/problems/4sum-ii/) | [C++](./C++/4sum-ii.cpp) [Python](./Python/4sum-ii.py) | _O(n^2)_ | _O(n^2)_ | Medium || 473| [Matchsticks to Square](https://leetcode.com/problems/matchsticks-to-square/) | [C++](./C++/matchsticks-to-square.cpp) [Python](./Python/matchsticks-to-square.py) | _O(n * s * 2^n)_ | _O(n * (2^n + s))_ | Medium || ## Data Structure | # | Title | Solution | Time | Space | Difficulty | Tag | Note| |-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----| 146| [LRU Cache](https://leetcode.com/problems/lru-cache/) | [C++](./C++/lru-cache.cpp) [Python](./Python/lru-cache.py) | _O(1)_ | _O(k)_ | Hard || 225| [Implement Stack using Queues](https://leetcode.com/problems/implement-stack-using-queues/) | [C++](./C++/implement-stack-using-queues.cpp) [Python](./Python/implement-stack-using-queues.py) | push: _O(n)_, pop: _O(1)_, top: _O(1)_ | _O(n)_ | Easy || ## Math | # | Title | Solution | Time | Space | Difficulty | Tag | Note| |-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----| 7| [Reverse Integer](https://leetcode.com/problems/reverse-integer/) | [C++](./C++/reverse-integer.cpp) [Python](./Python/reverse-integer.py) | _O(1)_ | _O(1)_ | Easy || 9| [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [C++](./C++/palindrome-number.cpp) [Python](./Python/palindrome-number.py) | _O(1)_ | _O(1)_ | Easy || 12| [Integer to Roman](https://leetcode.com/problems/integer-to-roman/) | [C++](./C++/integer-to-roman.cpp) [Python](./Python/integer-to-roman.py) | _O(n)_ | _O(1)_ | Medium || 13| [Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [C++](./C++/roman-to-integer.cpp) [Python](./Python/roman-to-integer.py) | _O(n)_ | _O(1)_ | Easy || 29| [Divide Two Integers](https://leetcode.com/problems/divide-two-integers/) | [C++](./C++/divide-two-integers.cpp) [Python](./Python/divide-two-integers.py) | _O(1)_ | _O(1)_ | Medium || 50| [Pow(x, n)](https://leetcode.com/problems/powx-n/) | [C++](./C++/powx-n.cpp) [Python](./Python/powx-n.py) | _O(1)_ | _O(1)_ | Medium || 60| [Permutation Sequence](https://leetcode.com/problems/permutation-sequence/) | [C++](./C++/permutation-sequence.cpp) [Python](./Python/permutation-sequence.py) | _O(n^2)_ | _O(n)_ | Medium || `Cantor Ordering` 65| [Valid Number](https://leetcode.com/problems/valid-number/) | [C++](./C++/valid-number.cpp) [Python](./Python/valid-number.py) | _O(n)_ | _O(1)_ | Hard || `Automata` 89| [Gray Code](https://leetcode.com/problems/gray-code/) | [C++](./C++/gray-code.cpp) [Python](./Python/gray-code.py) | _O(2^n)_ | _O(1)_ | Medium || 166| [Fraction to Recurring Decimal](https://leetcode.com/problems/fraction-to-recurring-decimal/) | [C++](./C++/fraction-to-recurring-decimal.cpp) [Python](./Python/fraction-to-recurring-decimal.py) | _O(logn)_ | _O(1)_ | Medium || 168| [Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title/) | [C++](./C++/excel-sheet-column-title.cpp) [Python](./Python/excel-sheet-column-title.py) | _O(logn)_ | _O(1)_ | Easy || 171| [Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/) | [C++](./C++/excel-sheet-column-number.cpp) [Python](./Python/excel-sheet-column-number.py) | _O(n)_ | _O(1)_ | Easy || 172| [Factorial Trailing Zeroes](https://leetcode.com/problems/factorial-trailing-zeroes/) | [C++](./C++/factorial-trailing-zeroes.cpp) [Python](./Python/factorial-trailing-zeroes.py) | _O(1)_ | _O(1)_ | Easy || 223| [Rectangle Area](https://leetcode.com/problems/rectangle-area/) | [C++](./C++/rectangle-area.cpp) [Python](./Python/rectangle-area.py) | _O(1)_ | _O(1)_ | Easy || 233| [Number of Digit One](https://leetcode.com/problems/number-of-digit-one/) | [C++](./C++/number-of-digit-one.cpp) [Python](./Python/number-of-digit-one.py) | _O(1)_ | _O(1)_ | Hard | CTCI, LintCode| 248| [Strobogrammatic Number III](https://leetcode.com/problems/strobogrammatic-number-iii/) | [C++](./C++/strobogrammatic-number-iii.cpp) [Python](./Python/strobogrammatic-number-iii.py) | _O(5^(n/2))_ | _O(n)_ | Hard |馃摉|| 258| [Add Digits](https://leetcode.com/problems/add-digits/) | [C++](./C++/add-digits.cpp) [Python](./Python/add-digits.py) | _O(1)_ | _O(1)_ | Easy ||| 263| [Ugly Number](https://leetcode.com/problems/ugly-number/) | [C++](./C++/ugly-number.cpp) [Python](./Python/ugly-number.py) | _O(1)_ | _O(1)_ | Easy ||| 292| [Nim Game](https://leetcode.com/problems/nim-game/) | [C++](./C++/nim-game.cpp) [Python](./Python/nim-game.py) | _O(1)_ | _O(1)_ | Easy | LintCode || 319 | [Bulb Switcher](https://leetcode.com/problems/bulb-switcher/) | [C++](./C++/bulb-switcher.cpp) [Python](./Python/bulb-switcher.py) | _O(1)_ | _O(1)_ | Medium ||| 326 | [Power of Three](https://leetcode.com/problems/power-of-three/) | [C++](./C++/power-of-three.cpp) [Python](./Python/power-of-three.py) | _O(1)_ | _O(1)_ | Easy ||| 335 | [Self Crossing](https://leetcode.com/problems/self-crossing/) | [C++](./C++/self-crossing.cpp) [Python](./Python/self-crossing.py) | _O(n)_ | _O(1)_ | Hard ||| 338 | [Counting Bits](https://leetcode.com/problems/counting-bits/) | [C++](./C++/counting-bits.cpp) [Python](./Python/counting-bits.py) | _O(n)_ | _O(n)_ | Medium ||| 343 | [Integer Break](https://leetcode.com/problems/integer-break/) | [C++](./C++/integer-break.cpp) [Python](./Python/integer-break.py) | _O(logn)_ | _O(1)_ | Medium || Tricky, DP | 365 | [Water and Jug Problem](https://leetcode.com/problems/water-and-jug-problem/) | [C++](./C++/water-and-jug-problem.cpp) [Python](./Python/water-and-jug-problem.py) | _O(logn)_ | _O(1)_ | Medium || Euclidean Algorithm | 372 | [Super Pow](https://leetcode.com/problems/super-pow/) | [C++](./C++/super-pow.cpp) [Python](./Python/super-pow.py) | _O(n)_ | _O(1)_ | Medium ||| 382 | [Linked List Random Node](https://leetcode.com/problems/linked-list-random-node/) | [C++](./C++/linked-list-random-node.cpp) [Python](./Python/linked-list-random-node.py) | _O(n)_ | _O(1)_ | Medium || `Reservoir Sampling` | 386 | [Lexicographical Numbers](https://leetcode.com/problems/lexicographical-numbers/) | [C++](./C++/lexicographical-numbers.cpp) [Python](./Python/lexicographical-numbers.py) | _O(n)_ | _O(1)_ | Medium ||| 390 | [Elimination Game](https://leetcode.com/problems/elimination-game/) | [C++](./C++/elimination-game.cpp) [Python](./Python/elimination-game.py) | _O(logn)_ | _O(1)_ | Medium || 391 | [Perfect Rectangle](https://leetcode.com/problems/perfect-rectangle/) | [C++](./C++/perfect-rectangle.cpp) [Python](./Python/perfect-rectangle.py) | _O(n)_ | _O(n)_ | Hard | | 398 | [Random Pick Index](https://leetcode.com/problems/random-pick-index/) | [C++](./C++/random-pick-index.cpp) [Python](./Python/random-pick-index.py) | _O(n)_ | _O(1)_ | Medium || `Reservoir Sampling` | 400 | [Nth Digit](https://leetcode.com/problems/nth-digit/) | [C++](./C++/nth-digit.cpp) [Python](./Python/nth-digit.py) | _O(logn)_ | _O(1)_ | Easy ||| 413 | [Arithmetic Slices](https://leetcode.com/problems/arithmetic-slices/) | [C++](./C++/arithmetic-slices.cpp) [Python](./Python/arithmetic-slices.py) | _O(n)_ | _O(1)_ | Medium ||| 423 | [Reconstruct Original Digits from English](https://leetcode.com/problems/reconstruct-original-digits-from-english/) | [C++](./C++/reconstruct-original-digits-from-english.cpp) [Python](./Python/reconstruct-original-digits-from-english.py) | _O(n)_ | _O(1)_ | Medium | [GCJ2016 - Round 1B](https://code.google.com/codejam/contest/11254486/dashboard#s=p0)|| 441 | [Arranging Coins](https://leetcode.com/problems/arranging-coins/) | [C++](./C++/arranging-coins.cpp) [Python](./Python/arranging-coins.py) | _O(nlogn)_ | _O(1)_ | Easy || Binary Search| 453 | [Minimum Moves to Equal Array Elements](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/) | [C++](./C++/minimum-number-of-arrows-to-burst-balloons.cpp) [Python](./Python/minimum-number-of-arrows-to-burst-balloons.py) | _O(n)_ | _O(1)_ | Easy ||| 458 | [Poor Pigs](https://leetcode.com/problems/poor-pigs/) | [C++](./C++/poor-pigs.cpp) [Python](./Python/poor-pigs.py) | _O(n)_ | _O(1)_ | Easy ||| 469 | [Convex Polygon](https://leetcode.com/problems/convex-polygon/) | [C++](./C++/convex-polygon.cpp) [Python](./Python/convex-polygon.py) | _O(n)_ | _O(1)_ | Medium |馃摉|| ## Sort | # | Title | Solution | Time | Space | Difficulty | Tag | Note| |-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----| 56| [Merge Intervals](https://leetcode.com/problems/merge-intervals/)| [C++](./C++/merge-intervals.cpp) [Python](./Python/merge-intervals.py) | _O(nlogn)_ | _O(1)_ | Hard || 57| [Insert Interval](https://leetcode.com/problems/insert-interval/)| [C++](./C++/insert-interval.cpp) [Python](./Python/insert-interval.py) | _O(n)_ | _O(1)_ | Hard || 75| [Sort Colors](https://leetcode.com/problems/sort-colors/) | [C++](./C++/sort-colors.cpp) [Python](./Python/sort-colors.py) | _O(n)_ | _O(1)_ | Medium || Tri Partition 88| [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/)| [C++](./C++/merge-sorted-array.cpp) [Python](./Python/merge-sorted-array.py) | _O(n)_ | _O(1)_ | Easy || 147| [Insertion Sort List](https://leetcode.com/problems/insertion-sort-list/)|[C++](./C++/insertion-sort-list.cpp) [Python](./Python/insertion-sort-list.py) | _O(n^2)_ | _O(1)_ | Medium || 148| [Sort List](https://leetcode.com/problems/sort-list/) | [C++](./C++/sort-list.cpp) [Python](./Python/sort-list.py) | _O(nlogn)_ | _O(logn)_ | Medium || 164| [Maximum Gap](https://leetcode.com/problems/maximum-gap/) | [C++](./C++/maximum-gap.cpp) [Python](./Python/maximum-gap.py)| _O(n)_ | _O(n)_ | Hard || Tricky 179| [Largest Number](https://leetcode.com/problems/largest-number/) | [C++](./C++/largest-number.cpp) [Python](./Python/largest-number.py) | _O(nlogn)_ | _O(1)_ | Medium || 218| [The Skyline Problem](https://leetcode.com/problems/the-skyline-problem/) | [C++](./C++/the-skyline-problem.cpp) [Python](./Python/the-skyline-problem.py) | _O(nlogn)_ | _O(n)_ | Hard || Sort, BST| 252| [Meeting Rooms](https://leetcode.com/problems/meeting-rooms/) | [C++](./C++/meeting-rooms.cpp) [Python](./Python/meeting-rooms.py) | _O(nlogn)_ | _O(n)_ | Easy |馃摉| | 253| [Meeting Rooms II](https://leetcode.com/problems/meeting-rooms-ii/) | [C++](./C++/meeting-rooms-ii.cpp) [Python](./Python/meeting-rooms-ii.py) | _O(nlogn)_ | _O(n)_ | Medium |馃摉| | 274| [H-Index](https://leetcode.com/problems/h-index/) | [C++](./C++/h-index.cpp) [Python](./Python/h-index.py) | _O(n)_ | _O(n)_ | Medium || Counting Sort | 280| [Wiggle Sort](https://leetcode.com/problems/wiggle-sort/) | [C++](./C++/wiggle-sort.cpp) [Python](./Python/wiggle-sort.py) | _O(n)_ | _O(1)_ | Medium |馃摉| | 324| [Wiggle Sort II](https://leetcode.com/problems/wiggle-sort-ii/) | [C++](./C++/wiggle-sort-ii.cpp) [Python](./Python/wiggle-sort-ii.py) | _O(n)_ on average | _O(1)_ | Medium | variant of [Sort Colors](https://leetcode.com/problems/sort-colors/) | Tri Partition | 347| [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | [C++](./C++/top-k-frequent-elements.cpp) [Python](./Python/top-k-frequent-elements.py) | _O(n)_ on average | _O(n)_ | Medium | | Quick Select, Heap | 406| [Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height/) | [C++](./C++/queue-reconstruction-by-height.cpp) [Python](./Python/queue-reconstruction-by-height.py) | _O(n * sqrt(n))_ | _O(n)_ | Medium | | | 451| [Sort Characters By Frequency](https://leetcode.com/problems/sort-characters-by-frequency/) | [C++](./C++/sort-characters-by-frequency.cpp) [Python](./Python/sort-characters-by-frequency.py) | _O(n)_ | _O(n)_ | Medium | | | ## Two Pointers | # | Title | Solution | Time | Space | Difficulty | Tag | Note| |-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----| 19| [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/)| [C++](./C++/remove-nth-node-from-end-of-list.cpp) [Python](./Python/remove-nth-node-from-end-of-list.py) | _O(n)_ | _O(1)_ | Easy || 86| [Partition List](https://leetcode.com/problems/partition-list/)| [C++](./C++/partition-list.cpp) [Python](./Python/partition-list.py) | _O(n)_ | _O(1)_ | Medium || 141| [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/)| [C++](./C++/linked-list-cycle.cpp) [Python](./Python/linked-list-cycle.py) | _O(n)_ | _O(1)_ | Easy || 142| [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/)| [C++](./C++/linked-list-cycle-ii.cpp) [Python](./Python/linked-list-cycle-ii.py) | _O(n)_ | _O(1)_ | Medium || 143| [Reorder List](https://leetcode.com/problems/reorder-list/)| [C++](./C++/reorder-list.cpp) [Python](./Python/reorder-list.py) | _O(n)_ | _O(1)_ | Medium || 167| [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | [C++](./C++/two-sum-ii-input-array-is-sorted.cpp) [Python](./Python/two-sum-ii-input-array-is-sorted.py) | _O(n)_ | _O(1)_ | Medium | | 259 | [3Sum Smaller](https://leetcode.com/problems/3sum-smaller/) | [C++](./C++/3sum-smaller.cpp) [Python](./Python/3sum-smaller.py) | _O(n^2)_ | _O(1)_ | Medium | 馃摉, LintCode | 283 | [Move Zeroes](https://leetcode.com/problems/move-zeroes/) | [C++](./C++/move-zeroes.cpp) [Python](./Python/move-zeroes.py) | _O(n)_ | _O(1)_ | Easy | | 287| [Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/)| [C++](./C++/find-the-duplicate-number.cpp) [Python](./Python/find-the-duplicate-number.py) | _O(n)_ | _O(1)_ | Hard | | Binary Search, Two Pointers | 344| [Reverse String](https://leetcode.com/problems/reverse-string/) | [C++](./C++/reverse-string.cpp) [Python](./Python/reverse-string.py) | _O(n)_ | _O(1)_ | Easy | | 345| [Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/) | [C++](./C++/reverse-vowels-of-a-string.cpp) [Python](./Python/reverse-vowels-of-a-string.py) | _O(n)_ | _O(1)_ | Easy | | 349| [Intersection of Two Arrays](https://leetcode.com/problems/intersection-of-two-arrays/) | [C++](./C++/intersection-of-two-arrays.cpp) [Python](./Python/intersection-of-two-arrays.py) | _O(m + n)_ | _O(min(m, n))_ | Easy | EPI | Hash, Binary Search 350| [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [C++](./C++/intersection-of-two-arrays-ii.cpp) [Python](./Python/intersection-of-two-arrays-ii.py) | _O(m + n)_ | _O(1)_ | Easy | EPI | Hash, Binary Search 360| [Sort Transformed Array](https://leetcode.com/problems/sort-transformed-array/) | [C++](./C++/sort-transformed-array.cpp) [Python](./Python/sort-transformed-array.py) | _O(n)_ | _O(1)_ | Medium |馃摉| 457| [Circular Array Loop](https://leetcode.com/problems/circular-array-loop/) | [C++](./C++/circular-array-loop.cpp) [Python](./Python/circular-array-loop.py) | _O(n)_ | _O(1)_ | Medium || ## Recursion | # | Title | Solution | Time | Space | Difficulty | Tag | Note| |-----|---------------- | --------------- | --------------- | --------------- | ------------- |--------------|-----| 95| [Unique Binary Search Trees II](https://leetcode.com/problems/unique-binary-search-trees-ii/) | [C++](./C++/unique-binary-search-trees-ii.cpp) [Python](./Python/unique-binary-search-trees-ii.py) | _O(4^n / n^(3/2)_ | _O(4^n / n^(3/2)_ | Medium || 98| [Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/)|[C++](./C++/validate-binary-search-tree.cpp) [Python](./Python/validate-binary-search-tree.py)| _O(n)_ | _O(1)_ | Medium || 100| [Same Tree](https://leetcode.com/problems/same-tree/) |[C+](./C++/same-tree.cpp) [Python](./Python/same-tree.py) | _O(n)_ | _O(h)_ | Easy || 104| [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/)| [C++](./C++/maximum-depth-of-binary-tree.cpp) [Python](./Python/maximum-depth-of-binary-tree.py)| _O(n)_ | _O(h)_ | Easy || 105| [Construct Binary Tree from Preorder and Inorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/) | [C++](./C++/construct-binary-tree-from-preorder-and-inorder-traversal.cpp) [Python](./Python/construct-binary-tree-from-preorder-and-inorder-traversal.py) | _O(n)_ | _O(n)_ | Medium || 106| [Construct Binary Tree from Inorder and Postorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/) | [C++](./C++/construct-binary-tree-from-inorder-and-postorder-traversal.cpp) [Python](./Python/construct-binary-tree-from-inorder-and-postorder-traversal.py) | _O(n)_ | _O(n)_ | Medium || 108| [Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) | [C++](./C++/convert-sorted-array-to-binary-search-tree.cpp) [Python](./Python/convert-sorted-array-to-binary-search-tree.py) | _O(n)_ | _O(logn)_ | Medium || 109| [Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/) | [C++](./C++/convert-sorted-list-to-binary-search-tree.cpp) [Python](./Python/convert-sorted-list-to-binary-search-tree.py) | _O(n)_ | _O(logn)_ | Medium || 110| [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) | [Python](./Python/balanced-binary-tree.py) | _O(n)_| _O(h)_ | Easy || 111| [Minimum Depth of Binary Tr
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值