classSolution { public: /** * @param A: A list of integers * @return: A boolean */ boolcanJump(vector<int> &a){ int n = a.size(); if (n == 1) returntrue; bool f[n]; f[0] = 1; for (int i = 1; i < n; i++){ f[i] = false; for (int j = i - 1; j >= 0; j--){ if ((j + a[j] >= i) && f[j]){ f[i] = true; break; } } } return f[n - 1]; } };
AC代码(贪心)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { public:
boolcanJump(vector<int> &a){ int n = a.size(); if (n == 1) returntrue; int maxn = 0; for (int i = 0; i < n; i++){ if (i > maxn) returnfalse; elseif (maxn >= n - 1) returntrue; else maxn = max(maxn, i + a[i]); } returntrue; } };
classSolution { public: /** * @param obstacleGrid: A list of lists of integers * @return: An integer */ intuniquePathsWithObstacles(vector<vector<int>> &a){ // write your code here int m = a.size(); int n = a[0].size(); int f[101][101]; f[0][0] = !a[0][0]; for (int i = 1; i < m; i++){ if (!a[i][0]) f[i][0] = f[i - 1][0]; else f[i][0] = 0; } for (int i = 1; i < n; i++){ if (!a[0][i]) f[0][i] = f[0][i - 1]; else f[0][i] = 0; } for (int i = 1; i < m; i++){ for (int j = 1; j < n; j++){ if (!a[i][j]) f[i][j] = f[i - 1][j] + f[i][j - 1]; else f[i][j] = 0; } } return f[m - 1][n - 1]; } };
classSolution { public: /** * @param grid: a list of lists of integers * @return: An integer, minimizes the sum of all numbers along its path */ intminPathSum(vector<vector<int>> &a){ // write your code here int m = a.size(); int n = a[0].size(); int f[m][n]; for (int i = 0; i < m; i++){ for (int j = 0; j < n; j++){ if (i == 0 && j == 0){ f[i][j] = a[0][0]; continue; } if (i == 0){ f[i][j] = a[i][j] + f[i][j - 1]; continue; } if (j == 0){ f[i][j] = a[i][j] + f[i - 1][j]; continue; } f[i][j] = min(f[i - 1][j], f[i][j - 1]) + a[i][j]; } } return f[m - 1][n - 1]; } };
classSolution { public: intmaxKilledEnemies(vector <vector<char>> &a){ int m = a.size(); if (m == 0) return0; int n = a[0].size(); int res[m][n], f[m][n]; memset(res, 0, sizeof res); // up for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (i == 0) { if (a[i][j] == 'E') f[i][j] = 1; else f[i][j] = 0; res[i][j] += f[i][j]; continue; } if (a[i][j] == 'E') f[i][j] = f[i - 1][j] + 1; elseif (a[i][j] == '0') f[i][j] = f[i - 1][j]; else f[i][j] = 0; res[i][j] += f[i][j]; } } // down for (int i = m - 1; i > 0; i--) { for (int j = 0; j < n; j++) { if (i == m - 1) { if (a[i][j] == 'E') f[i][j] = 1; else f[i][j] = 0; res[i][j] += f[i][j]; continue; } if (a[i][j] == 'E') f[i][j] = f[i + 1][j] + 1; elseif (a[i][j] == '0') f[i][j] = f[i + 1][j]; else f[i][j] = 0;
res[i][j] += f[i][j]; } } //left for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (j == 0) { if (a[i][j] == 'E') f[i][j] = 1; else f[i][j] = 0; res[i][j] += f[i][j]; continue; } if (a[i][j] == 'E') f[i][j] = f[i][j - 1] + 1; elseif (a[i][j] == '0') f[i][j] = f[i][j - 1]; else f[i][j] = 0; res[i][j] += f[i][j]; } } //right for (int i = 0; i < m; i++) { for (int j = n - 1; j >= 0; j--) { if (j == n - 1) { if (a[i][j] == 'E') f[i][j] = 1; else f[i][j] = 0; res[i][j] += f[i][j]; continue; } if (a[i][j] == 'E') f[i][j] = f[i][j + 1] + 1; elseif (a[i][j] == '0') f[i][j] = f[i][j + 1]; else f[i][j] = 0;
res[i][j] += f[i][j]; } } int ans = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (a[i][j] == '0') ans = max(ans, res[i][j]); } } return ans;
} };
664. Counting Bits
题目链接 题目描述 给出一个 非负 整数 num,对所有满足 0 ≤ i ≤ num 条件的数字 i 均需要计算其二进制表示中数字 1 的个数并以数组的形式返回。
classSolution { public: /** * @param num: a non negative integer number * @return: an array represent the number of 1's in their binary */ vector<int> countBits(int a){ // write your code here vector<int> f(a + 1, 0); for (int i = 0; i <= a; i++){ if (i & 1) f[i] = f[i / 2] + 1; else f[i] = f[i / 2]; } return f; } };
classSolution { public: /** * @param costs: n x k cost matrix * @return: an integer, the minimum cost to paint all houses */ intminCostII(vector<vector<int>> &a){ int m = a.size(); if (m == 0) return0; int n = a[0].size(); int f[m + 1][n]; memset(f[0], 0, sizeof f[0]); constint INF = 0x3f3f3f3f; for (int i = 1; i <= m; i++){ for (int j = 0; j < n; j++){ f[i][j] = INF; for (int k = 0; k < n; k++){ if (k == j) continue; f[i][j] = min(f[i][j], f[i - 1][k] + a[i - 1][j]); } } } int ans = INF; for (int i = 0; i < n; i++) ans = min(ans, f[m][i]); return ans; } };
classSolution { public: /** * @param A: An array of non-negative integers * @return: The maximum amount of money you can rob tonight */ longlonghouseRobber(vector<int> &a){ // write your code here typedeflonglong ll; int n = a.size(); if (n == 0) return0; ll f[n + 1]; f[0] = 0; for (int i = 1; i <= n; i++){ if (i == 1) f[i] = a[i - 1]; if (i > 1) f[i] = max(f[i - 1], f[i - 2] + a[i - 1]); } return f[n]; } };
classSolution { public: /** * @param nums: An array of non-negative integers. * @return: The maximum amount of money you can rob tonight */ intsolve(int st, int en, vector<int> &a){ int f[en + 1]; f[st] = 0; f[st + 1] = a[st]; for (int i = st + 2; i <= en; i++){ f[i] = max(f[i - 1], f[i - 2] + a[i - 1]); } return f[en]; } inthouseRobber2(vector<int> &a){ int n = a.size(); if (n == 0) return0; if (n == 1) return a[0]; return max(solve(0, n - 1, a), solve(1, n, a)); } };
classSolution { public: /** * @param prices: Given an integer array * @return: Maximum profit */ intmaxProfit(vector<int> &a){ // write your code here if (a.empty() || !a.size()) return0; int n = a.size(); int f[n + 1]; f[0] = a[0]; for (int i = 1; i < n; i++){ if (a[i] < f[i - 1]) f[i] = a[i]; else f[i] = f[i - 1]; } int m = 0; for (int i = n - 1; i >= 1; i--){ m = max(m, a[i] - f[i - 1]); } return m; } };
AC代码(贪心)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { public: /** * @param prices: Given an integer array * @return: Maximum profit */ intmaxProfit(vector<int> &a){ int n = a.size(); if(n == 0 || n == 1) return0; int ans = 0, min1 = a[0]; for (int i = 1; i < n; i++){ ans = max(ans, a[i] - min1); min1 = min(min1, a[i]); } return ans; } };
classSolution { public: /** * @param pages: an array of integers * @param k: An integer * @return: an integer */
boolcheck(int n, int m, vector<int> a, int k){ int res = 1, sum = 0; for (int i = 0; i < n; i++){ if (sum + a[i] <= m) sum += a[i]; else sum = a[i], res++; } return res <= k; } intcopyBooks(vector<int> &a, int K){ if(a.size() == 0) return0; int n = a.size(); int ans = 0, sum = 0; for (int i = 0; i < n; i++){ sum += a[i]; ans = max(ans, a[i]); } int low = ans, high = sum; int m; while(low < high){ m = low + (high - low) / 2; if (check(n, m, a, K)) high = m; else low = m + 1; } return low; } };
394. Coins in a Line
题目大意 题目链接 有 n 个硬币排成一条线。两个参赛者轮流从右边依次拿走 1 或 2 个硬币,直到没有硬币为止。拿到最后一枚硬币的人获胜。
classSolution { public: /** * @param n: An integer * @return: A boolean which equals to true if the first player will win */ boolfirstWillWin(int n){ if(n == 0) returnfalse; int f[n + 1]; f[0] = false; f[1] = f[2] = true; for (int i = 3; i <= n; i++){ if (f[i - 1] && f[i - 2]) f[i] = false; else f[i] = true; } return f[n]; } };
找到规律后更简单的写法
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { public: /** * @param n: An integer * @return: A boolean which equals to true if the first player will win */ boolfirstWillWin(int n){ // write your code here if (n % 3 != 0) returntrue; returnfalse; } };
classSolution { public: /** * @param m: An integer m denotes the size of a backpack * @param A: Given n items with size A[i] * @return: The maximum size */ intbackPack(int m, vector<int> &a){ int n = a.size(); if (!n || !m) return0; int f[n + 1][m + 1]; memset(f, false, sizeof f); f[0][0] = true; for (int i = 1; i <= n; i++){ for (int j = 0; j <= m; j++){ f[i][j] = f[i - 1][j]; if (j >= a[i - 1]) // j 能不能由 a[i - 1] 和 j - a[i - 1] 组成 f[i][j] = f[i][j] | f[i - 1][j - a[i - 1]]; } } for (int i = m; i >= 0; i--) if (f[n][i]) return i; } };
classSolution { public: /** * @param m: An integer m denotes the size of a backpack * @param A: Given n items with size A[i] * @return: The maximum size */ intbackPack(int m, vector<int> &a){ int n = a.size(); if (!n || !m) return0; int f[m + 1]; memset(f, false, sizeof f); f[0] = true; for (int i = 1; i <= n; i++){ for (int j = m; j >= a[i - 1]; j--){ f[j] = f[j] | f[j - a[i - 1]]; } } for (int i = m; i >= 0; i--) if (f[i]) return i; } };
classSolution { public: /** * @param m: An integer m denotes the size of a backpack * @param A: Given n items with size A[i] * @param V: Given n items with value V[i] * @return: The maximum value */ intbackPackII(int W, vector<int> &w, vector<int> &v){ int n = w.size(); if (!n) return0; int f[n + 1][W + 1]; memset(f[0], 0 , sizeof f[0]); for (int i = 1; i <= n; i++){ for (int j = 0; j <= W; ++j) { f[i][j] = f[i - 1][j]; if (j >= w[i - 1]) f[i][j] = max(f[i][j], f[i - 1][j - w[i - 1]] + v[i - 1]); } } return f[n][W]; } };
classSolution { public: /** * @param m: An integer m denotes the size of a backpack * @param A: Given n items with size A[i] * @param V: Given n items with value V[i] * @return: The maximum value */ intbackPackII(int W, vector<int> &w, vector<int> &v){ int n = w.size(); if (!n) return0; int f[W + 1]; memset(f, 0, sizeof f); for (int i = 1; i <= n; i++){ for (int j = W; j >= w[i - 1]; --j) { f[j] = max(f[j], f[j - w[i - 1]] + v[i - 1]); } } return f[W]; } };
667. Longest Palindromic Subsequence
题目描述 题目链接 给一字符串 s, 找出在 s 中的最长回文子序列的长度. 你可以假设 s 的最大长度为 1000.
classSolution { public: /** * @param values: a vector of integers * @return: a boolean which equals to true if the first player will win */ boolfirstWillWin(vector<int> &a){ // write your code here if (a.empty()) returntrue; int n = a.size(); int f[n][n]; int i, j; for (i = 0; i < n; i++){ f[i][i] = a[i]; } for (int len = 1; len < n; len++){ i = 0, j = len; while(j < n){ f[i][j] = max(a[j] - f[i][j - 1], a[i] - f[i + 1][j]); i++, j++; } } return f[0][n - 1] >= 0; } };
classSolution { public: /** * @param s1: A string * @param s2: Another string * @return: whether s2 is a scrambled string of s1 */ boolisScramble(string &s1, string &s2){ // write your code here int n = s1.length(); bool f[n][n][n + 1];
for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) f[i][j][1] = (s1[i] == s2[j]); for (int k = 2; k <= n; ++k){ for (int i = 0; i <= n - k; ++i){ for (int j = 0; j <= n - k; ++j){ f[i][j][k] = false; for (int w = 1; w < k; ++w){ if (f[i][j][w] && f[i + w][j + w][k - w]){ f[i][j][k] = true; break; } if (f[i][j + k - w][w] && f[i + w][j][k - w]){ f[i][j][k] = true; break; } } } } } return f[0][0][n]; } };
classSolution { public: /** * @param nums: A list of integer * @return: An integer, maximum coins */ intmaxCoins(vector<int> &aa){ // write your code here int n = aa.size(); vector<int>a; a.push_back(1); for (int i = 0; i < n; ++i){ a.push_back(aa[i]); } a.push_back(1); int f[n + 2][n + 2]; memset(f, 0, sizeof f); int i, j; for (int l = 2; l <= n + 1; ++l){ for (i = 0; i <= n; ++i){ j = i + l; if (j > n + 1) break; for (int k = i + 1; k < j; k++) f[i][j] = max(f[i][j], f[i][k] + f[k][j] + a[i] * a[j] * a[k]); } } return f[0][n + 1]; } };
classSolution { public: /** * @param A: A string * @param B: A string * @return: The length of longest common subsequence of A and B */ intlongestCommonSubsequence(string &a, string &b){ // write your code here int n = a.length(); int m = b.length(); int f[n + 1][m + 1];
for (int i = 0; i <= n; i++){ for (int j = 0; j <= m; j++){ if (i == 0 || j == 0){ f[i][j] = 0; continue; } f[i][j] = max(f[i - 1][j], f[i][j - 1]); if (a[i - 1] == b[j - 1]) f[i][j] = max(f[i][j], f[i - 1][j - 1] + 1); } } return f[n][m];