随笔分类 -  动态规划

摘要:方法一:O(n3) #include <bits/stdc++.h> using namespace std; const int N = 3030; int a[N], b[N], f[N][N]; int n; int main() { scanf("%d",&n); for(int i = 1 阅读全文
posted @ 2020-09-21 10:25 Sexyomaru 阅读(567) 评论(0) 推荐(0)
摘要:解法一: #include <bits/stdc++.h> using namespace std; const int N = 30030; int a[N], f[N], b[N]; int n; int main() { while(cin >> a[n]) n++; int res = 0, 阅读全文
posted @ 2020-09-10 15:03 Sexyomaru 阅读(242) 评论(0) 推荐(0)
摘要:方法一:动态规划 class Solution { public: int minCost(string s, vector<int>& cost) { int n = s.size(),INF = 1e9; vector<vector<int>> f(n,vector<int>(27,INF)); 阅读全文
posted @ 2020-09-07 11:24 Sexyomaru 阅读(228) 评论(0) 推荐(0)
摘要:class Solution { public int countRoutes(int[] pos, int start, int end, int f) { int n = pos.length; int mod = (int)(1e9 + 7); long[][] dp = new long[f 阅读全文
posted @ 2020-09-06 17:15 Sexyomaru 阅读(269) 评论(0) 推荐(0)
摘要:分析:本题可以采用方格取数的代码,证明如下 #include <bits/stdc++.h> using namespace std; const int N = 55; int a[N][N], f[2*N][N][N]; int n, m; int main() { scanf("%d%d",& 阅读全文
posted @ 2020-09-04 16:30 Sexyomaru 阅读(206) 评论(0) 推荐(0)
摘要:#include <bits/stdc++.h> using namespace std; const int N = 15; int w[N][N], f[2*N][N][N]; int n, a, b, c; int main() { scanf("%d",&n); while(cin >> a 阅读全文
posted @ 2020-09-04 11:00 Sexyomaru 阅读(181) 评论(0) 推荐(0)
摘要:class Solution { public int getLengthOfOptimalCompression(String s, int k) { int n = s.length(); int[][] dp = new int[n+1][k+1]; // dp[i][j]:考虑前i个字符最多 阅读全文
posted @ 2020-08-20 15:41 Sexyomaru 阅读(410) 评论(0) 推荐(0)
摘要:class Solution { public int findNumberOfLIS(int[] nums) { int n = nums.length; int[] dp = new int[n]; Arrays.fill(dp,1); // dp[i] 以i结尾的最大长度 int[] coun 阅读全文
posted @ 2020-08-19 19:09 Sexyomaru 阅读(86) 评论(0) 推荐(0)
摘要:class Solution { public List<Integer> largestDivisibleSubset(int[] nums) { List<Integer> res = new ArrayList<>(); int n = nums.length; if(n == 0) retu 阅读全文
posted @ 2020-08-19 17:10 Sexyomaru 阅读(117) 评论(0) 推荐(0)
摘要:方法一: 背包模型 #include <bits/stdc++.h> using namespace std; const int N = 10010, mod = 1e9 + 7; int dp[N][N]; int n; int main() { scanf("%d",&n); for(int 阅读全文
posted @ 2020-08-18 20:00 Sexyomaru 阅读(185) 评论(0) 推荐(0)
摘要:class Solution { Map<Integer, Integer> map = new HashMap(); public int minDays(int n) { if(n == 0) return 0; if(!map.containsKey(n)){ int ans = n; // 阅读全文
posted @ 2020-08-16 15:49 Sexyomaru 阅读(134) 评论(0) 推荐(0)
摘要:class Solution { public int removeBoxes(int[] boxes) { int n = boxes.length; int[][][] dp = new int[n][n][n]; // dp[i][j][k] 从i到j的盒子j右面右连续的等于第j个盒子颜色有k 阅读全文
posted @ 2020-08-15 15:51 Sexyomaru 阅读(151) 评论(0) 推荐(0)
摘要:分析:将木棍的两个端点(0和n)加到cost数组中,dp[i][j]表示把这段木棍的第i到j个点完成切割的最小成本,i和j都是cost数组的下标。 状态计算: 最后的合并位置是k dp[i][j] = min(dp[i, k] + dp[k, j] + cost) class Solution { 阅读全文
posted @ 2020-08-10 19:48 Sexyomaru 阅读(507) 评论(0) 推荐(0)
摘要:class Solution { public int maxNonOverlapping(int[] nums, int target) { int n = nums.length; int[] dp = new int[n+1]; // nums前i个数的最大满足条件子数组数目 Map<Inte 阅读全文
posted @ 2020-08-10 19:32 Sexyomaru 阅读(182) 评论(0) 推荐(0)
摘要:分析: 这是道二维费用的背包问题,可以像01背包一样优化掉第一维的决策,选或不选这个袋子,然后需要像01背包一样倒序循环体积,和01背包的优化一样。 其次,这道题的难点是可以买多,只要买够就行,我们只需要对负数的体积取0就行,表示我们即使当前需要的饮料小于袋子中的饮料数量,我们依然可以购买,只要从0 阅读全文
posted @ 2020-07-31 15:07 Sexyomaru 阅读(141) 评论(0) 推荐(0)
摘要:分析: 首先我们要思考如果让这个NP完全题目复杂度降低,那么可以优先考虑到使用位运算,状态压缩等解决思路。 然后接着思考,我们可以发现,我们所需要的不是整个方案,而只是方案最优解,所以我们只需要记录当前这个方案的最优解即可,那么我们考虑的状态,不久只有,在当前方案i中,目前抵达的点是j。 现在既然装 阅读全文
posted @ 2020-07-31 14:17 Sexyomaru 阅读(182) 评论(0) 推荐(0)
摘要:分析: 1. 所谓的状态压缩DP,就是用二进制数保存状态。为什么不直接用数组记录呢?因为用一个二进制数记录方便作位运算。 2. 本题等价于找到所有横放 1 X 2 小方格的方案数,因为所有横放确定了,那么竖放方案是唯一的。 3. 用f[i][j]记录第i-1列已经充满且第i列第j个状态。j状态位等于 阅读全文
posted @ 2020-07-31 11:05 Sexyomaru 阅读(214) 评论(0) 推荐(0)
摘要:class Solution { public int maxProduct(int[] nums) { int n = nums.length; int preMin = nums[0]; // i 之前最小值 int preMax = nums[0]; // i 之前最大值 int res = 阅读全文
posted @ 2020-07-30 16:04 Sexyomaru 阅读(61) 评论(0) 推荐(0)
摘要:class Solution { public int maximumSum(int[] arr) { int n = arr.length; int[][] dp = new int[n][2]; // dp[i][0]: 以i结尾未删 dp[i][1]: 以i结尾未删过 dp[0][0] = a 阅读全文
posted @ 2020-07-30 10:58 Sexyomaru 阅读(147) 评论(0) 推荐(0)
摘要:class Solution { public int countCornerRectangles(int[][] grid) { if(grid.length < 2 || grid[0].length < 2) return 0; int m = grid.length, n = grid[0] 阅读全文
posted @ 2020-07-21 16:37 Sexyomaru 阅读(143) 评论(0) 推荐(0)