区间 dp
求解一段区间上的最优解。主要是通过合并小区间的 最优解进而得出整个大区间上最优解的算法。
石子合并...
wdt_
即使上线也匆忙,新版本,愁断肠,orz...
展开
-
【区间 dp】A031_LC_石子游戏 V(注意只剩下两堆石头的特判)
给你一个数组 arr ,该数组表示一个从 1 到 n 的数字排列。有一个长度为 n 的二进制字符串,该字符串上的所有位最初都设置为 0 。在从 1 到 n 的每个步骤 i 中(假设二进制字符串和 arr 都是从 1 开始索引的情况下),二进制字符串上位于位置 arr[i] 的位将会设为 1 。给你一个整数 m ,请你找出二进制字符串上存在长度为 m 的一组 1 的最后步骤。一组 1 是一个连续的、由 1 组成的子串,且左右两边不再有可以延伸的 1 。返回存在长度 恰好 为 m 的 一组 1 的最后步原创 2020-08-24 08:19:10 · 211 阅读 · 0 评论 -
【区间 dp】B029_LC_布尔运算(记忆化+枚举两个数且作为子问题的target)
给定一个布尔表达式和一个期望的布尔结果 result,布尔表达式由 0 (false)、1 (true)、& (AND)、 | (OR) 和 ^ (XOR) 符号组成。实现一个函数,算出有几种可使该表达式得出 result 值的括号方法示例 1:输入: s = "1^0|0|1", result = 0输出: 2解释: 两种可能的括号方法是1^(0|(0|1))1^((0|0)|1)示例 2:输入: s = "0&0&0&1^1|0", result = 1原创 2020-08-12 17:36:38 · 221 阅读 · 0 评论 -
【区间 dp】A028_LC_切棍子的最小成本(首尾添加元素)
一、Problem给你两个正整数 n 和 k,二进制字符串 Sn 的形成规则如下:S1 = “0”当 i > 1 时,Si = Si-1 + “1” + reverse(invert(Si-1))其中 + 表示串联操作,reverse(x) 返回反转 x 后得到的字符串,而 invert(x) 则会翻转 x 中的每一位(0 变为 1,而 1 变为 0)例如,符合上述描述的序列的前 4 个字符串依次是:S1 = “0”S2 = “011”S3 = “0111001”S4 = “011原创 2020-08-09 18:47:14 · 370 阅读 · 0 评论 -
【区间 dp】A027_LC_统计不同回文子序列(两边逼近(记忆化 / dp))
一、Problem给定一个字符串 S,找出 S 中不同的非空回文子序列个数,并返回该数字与 10^9 + 7 的模。通过从 S 中删除 0 个或多个字符来获得子序列。如果一个字符序列与它反转后的字符序列一致,那么它是回文字符序列。如果对于某个 i,A_i != B_i,那么 A_1, A_2, … 和 B_1, B_2, … 这两个字符序列是不同的。示例 1:输入:S = 'bccb'输出:6解释:6 个不同的非空回文子字符序列分别为:'b', 'c', 'bb', 'cc', 'bc原创 2020-08-01 18:54:33 · 270 阅读 · 2 评论 -
【区间 dp】A027_LC_移除盒子 & 奇怪的打印机(移除连续的盒子+先移除中间的不相同再合并两端)
一、Problem给出一些不同颜色的盒子,盒子的颜色由数字表示,即不同的数字表示不同的颜色。你将经过若干轮操作去去掉盒子,直到所有的盒子都去掉为止。每一轮你可以移除具有相同颜色的连续 k 个盒子(k >= 1),这样一轮之后你将得到 k*k 个积分。当你将所有盒子都去掉之后,求你能获得的最大积分和。输入:[1, 3, 2, 2, 2, 3, 4, 3, 1]输出:23解释:[1, 3, 2, 2, 2, 3, 4, 3, 1] ----> [1, 3, 3, 4, 3,原创 2020-07-30 15:35:54 · 180 阅读 · 0 评论 -
【区间 dp】B026_LC_最大平均值和的分组(分成 k-1 份 + 分成 1 份)
一、Problem我们将给定的数组 A 分成 K 个相邻的非空子数组 ,我们的分数由每个子数组内的平均值的总和构成。计算我们所能得到的最大分数是多少。注意我们必须使用 A 数组中的每一个数进行分组,并且分数不一定需要是整数。输入: A = [9,1,2,3,9]K = 3输出: 20解释: A 的最优分组是[9], [1, 2, 3], [9]. 得到的分数是 9 + (1 + 2 + 3) / 3 + 9 = 20.我们也可以把 A 分成[9, 1], [2], [3, 9].这样的分原创 2020-07-24 23:42:38 · 185 阅读 · 0 评论 -
【区间 dp】A025_LC_戳气球(枚举长度、左右端点、中点)
一、Problem有 n 个气球,编号为0 到 n-1,每个气球上都标有一个数字,这些数字存在数组 nums 中。现在要求你戳破所有的气球。如果你戳破气球 i ,就可以获得 nums[left] * nums[i] * nums[right] 个硬币。 这里的 left 和 right 代表和 i 相邻的两个气球的序号。注意当你戳破了气球 i 后,气球 left 和气球 right 就变成了相邻的气球。求所能获得硬币的最大数量。输入: [3,1,5,8]输出: 167 解释: nums = [3原创 2020-07-19 21:55:33 · 182 阅读 · 0 评论 -
【区间 dp】A023_LC_合并石头的最低成本(穷举分割点)
一、Problem有 N 堆石头排成一排,第 i 堆中有 stones[i] 块石头。每次移动(move)需要将连续的 K 堆石头合并为一堆,而这个移动的成本为这 K 堆石头的总数。找出把所有石头合并成一堆的最低成本。如果不可能,返回 -1 。Input: stones = [3,2,4,1], K = 2Output: 20Explanation: We start with [3, 2, 4, 1].We merge [3, 2] for a cost of 5, and we are原创 2020-07-01 23:19:48 · 231 阅读 · 0 评论 -
【区间 dp】A022_LC_最短公共超序列(LMS 加强 / 优化)
一、ProblemGiven two strings str1 and str2, return the shortest string that has both str1 and str2 as subsequences. If multiple answers exist, you may return any of them.(A string S is a subsequence of string T if deleting some number of characters from T原创 2020-07-01 10:44:43 · 390 阅读 · 0 评论 -
【区间 dp】B021_LC_分隔数组以得到最大和(预处理区间最大值 / 优化)
一、Problem给出整数数组 A,将该数组分隔为长度最多为 K 的几个(连续)子数组。分隔完成后,每个子数组的中的值都会变为该子数组中的最大值。返回给定数组完成分隔后的最大和。输入:A = [1,15,7,9,2,5,10], K = 3输出:84解释:A 变为 [15,15,15,9,10,10,10]提示:1 <= K <= A.length <= 5000 <= A[i] <= 10^6二、Solution方法一:预处理区间最大值这题我一看两个条原创 2020-06-27 10:23:37 · 293 阅读 · 0 评论 -
【区间 dp】B020_LC_多边形三角剖分的最低得分(画图)
一、Problem给定 N,想象一个凸 N 边多边形,其顶点按顺时针顺序依次标记为 A[0], A[i], …, A[N-1]。假设您将多边形剖分为 N-2 个三角形。对于每个三角形,该三角形的值是顶点标记的乘积,三角剖分的分数是进行三角剖分后所有 N-2 个三角形的值之和。返回多边形进行三角剖分后可以得到的最低分。输入:[3,7,4,5]输出:144解释:有两种三角剖分,可能得分分别为:3*7*5 + 4*5*7 = 245,或 3*4*5 + 3*4*7 = 144。最低分数为 144。原创 2020-06-26 20:29:14 · 247 阅读 · 0 评论 -
【区间 dp】B019_LC_单词拆分 I(向前看 / 优化边界)
一、Problem给定一个非空字符串 s 和一个包含非空单词列表的字典 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。说明:拆分时可以重复使用字典中的单词。你可以假设字典中没有重复的单词。输入: s = "applepenapple", wordDict = ["apple", "pen"]输出: true解释: 返回 true 因为 "applepenapple" 可以被拆分成 "apple pen apple"。注意你可以重复使用字典中的单词。输入原创 2020-06-25 21:58:43 · 203 阅读 · 0 评论 -
【区间 dp】B018_LC_叶值的最小代价生成树(枚举长度 / 单调栈)
一、Problem给你一个正整数数组 arr,考虑所有满足以下条件的二叉树:每个节点都有 0 个或是 2 个子节点数组 arr 中的值与树的中序遍历中每个叶节点的值每个非叶节点的值等于其左子树和右子树中叶节点的最大值的乘积在所有这样的二叉树中,返回每个非叶节点的值的最小可能总和。这个和的值是一个 32 位整数。输入:arr = [6,2,4]输出:32解释:有两种可能的树,第一种的非叶节点的总和为 36,第二种非叶节点的总和为 32。 24 24原创 2020-06-25 12:03:27 · 191 阅读 · 0 评论 -
【区间 dp】A017_LC_填充书架(暴搜 / 记忆化搜索 / dp(疑惑))
一、Problem附近的家居城促销,你买回了一直心仪的可调节书架,打算把自己的书都整理到新的书架上。你把要摆放的书 books 都整理好,叠成一摞:从上往下,第 i 本书的厚度为 books[i][0],高度为 books[i][1]。按顺序 将这些书摆放到总宽度为 shelf_width 的书架上。先选几本书放在书架上(它们的厚度之和小于等于书架的宽度 shelf_width),然后再建一层书架。重复这个过程,直到把所有的书都放在书架上。需要注意的是,在上述过程的每个步骤中,摆放书的顺序与你整理原创 2020-06-23 22:56:52 · 222 阅读 · 0 评论 -
【区间 dp】A016_NK_善变的同伴(最大的m段字段和问题)
一、Problem又到了吃午饭的时间,你和你的同伴刚刚研发出了最新的GSS-483型自动打饭机器人,现在你们正在对机器人进行功能测试。为了简化问题,我们假设午饭一共有N个菜,对于第i个菜,你和你的同伴对其定义了一个好吃程度(或难吃程度,如果是负数的话……)A[i],由于一些技(经)术(费)限制,机器人一次只能接受一个指令:两个数L, R——表示机器人将会去打第L~R一共R-L+1个菜。本着不浪费的原则,你们决定机器人打上来的菜,含着泪也要都吃完,于是你们希望机器人打的菜的好吃程度之和最大然而,你善原创 2020-06-20 11:48:26 · 210 阅读 · 0 评论 -
【区间 dp】A015_LC_段式回文(两边紧逼法)
一、Problem段式回文 其实与 一般回文 类似,只不过是最小的单位是 一段字符 而不是 单个字母。举个例子,对于一般回文 “abcba” 是回文,而 “volvo” 不是,但如果我们把 “volvo” 分为 “vo”、“l”、“vo” 三段,则可以认为 “(vo)(l)(vo)” 是段式回文(分为 3 段)。给你一个字符串 text,在确保它满足段式回文的前提下,请你返回 段 的 最大数量 k。如果段的最大数量为 k,那么存在满足以下条件的 a_1, a_2, …, a_k:每个 a_i 都是原创 2020-06-19 16:39:33 · 380 阅读 · 0 评论 -
【区间 dp】A014_LC_安排邮筒(麻烦的边界)
一、ProblemGiven the array houses and an integer k. where houses[i] is the location of the ith house along a street, your task is to allocate k mailboxes in the street.Return the minimum total distance between each house and its nearest mailbox.The answer原创 2020-06-14 18:01:43 · 251 阅读 · 0 评论 -
【区间 dp】B013_LC_最长递增子序列的个数(累加)
一、ProblemGiven an unsorted array of integers, find the number of longest increasing subsequence.Input: [1,3,5,4,7]Output: 2Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7].Note: Length of the given array will be n原创 2020-06-08 17:35:44 · 199 阅读 · 0 评论 -
【区间 dp】A012_LC_两个子序列的最大点积(分类讨论)
一、ProblemGiven two arrays nums1 and nums2.Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length.A subsequence of a array is a new array which is formed from the original array by deleting some (can be none原创 2020-05-24 22:44:07 · 206 阅读 · 0 评论 -
【区间 dp】A011_LC_恢复数组(剪枝)
一、ProblemA program was supposed to print an array of integers. The program forgot to print whitespaces and the array is printed as a string of digits and all we know is that all integers in the array were in the range [1, k] and there are no leading zeros原创 2020-05-22 23:22:21 · 169 阅读 · 0 评论 -
【区间 dp】B010_LC_最长回文子串(暴力 / dp / 中心扩展(代办))
一、Problem二、Solution方法一:暴力不用先取子串,可以利用 s 的字符串数组根据索引进行全局比较取子串的话会超时的。class Solution { char[] cs; public String longestPalindrome(String s) { if (s.length() == 1) return s; String res = ""; cs = s.toCharArray原创 2020-05-21 21:36:17 · 191 阅读 · 0 评论 -
【区间 dp】B009_JM_同桌的你(分组后 LIS / 二分优化)
一、Problem输入第一行输入一个整数 N,表示有 N 对同桌。第二行输入2N2N个整数,表示从左到右每一张桌子上同学的成绩 aia_iai输出输出一个整数表示最少需要几个同学出列调整。316 2 1 7 5 102二、Solution方法一:dp难点在于转化问题:因为两个人在同一张桌子上,故两个人的桌子序号是一样的,比如成绩16,2,1,7,5,10,对应的排名如下1,5,6,3,4,2,对应的桌子号如下1,2,1,3,3,2 需求是将拥有同桌子号的同学变成紧原创 2020-05-18 10:31:40 · 133 阅读 · 0 评论 -
【区间 dp】A008_LC_灌溉花园的最少水龙头数目(枚举区间 / 贪心)
一、ProblemThere is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).There are n + 1 taps located at points [0, 1, …, n] in the garden.Given an integer n and an integer a原创 2020-05-17 17:10:42 · 379 阅读 · 2 评论 -
【区间 dp】B007_LQ_带限制的摆动序列(暴搜 / dp)
一、Problem如果一个序列的奇数项都比前一项大,偶数项都比前一项小,则称为一个摆动序列。即a2i<a2i−1,a2i+1>a2ia_{2i} < a_{2i-1}, a_{2i+1} > a_{2i}a2i<a2i−1,a2i+1>a2i小明想知道,长度为 m,每个数都是 1 到n 之间的正整数的摆动序列一共有多少个。输入输入一行包含两个整数 m,n。输出输出一个整数,表示答案。答案可能很大,请输出答案除以10000的余数。输入3 4输出原创 2020-05-16 20:41:49 · 131 阅读 · 0 评论 -
【区间 dp】A006_LC_带限制的子序列和(dp / 单调队列优化)
一、ProblemGiven an integer array nums and an integer k, return the maximum sum of a non-empty subsequence of that array such that for every two consecutive integers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i <= k is sat原创 2020-05-15 15:59:53 · 142 阅读 · 0 评论 -
【区间 dp】A005_LC_工作计划的最低难度(暴搜 / dp)
一、ProblemYou want to schedule a list of jobs in d days. Jobs are dependent (i.e To work on the i-th job, you have to finish all the jobs j where 0 <= j < i).You have to finish at least one task every day. The difficulty of a job schedule is the sum原创 2020-05-11 10:03:26 · 191 阅读 · 0 评论 -
【区间 dp】B003_LC_分割回文串 II(回溯 / dp / 判回文子串优化)
一、ProblemGiven a string s, partition s such that every substring of the partition is a palindrome.Return the minimum cuts needed for a palindrome partitioning of s.Input: "aab"Output: 1Explanation: The palindrome partitioning ["aa","b"] could be produ原创 2020-05-09 22:55:26 · 160 阅读 · 0 评论 -
【区间 dp】B020_最长回文子序列(递归 | 记忆化搜索 | 二维 dp | 一维 dp)
一、题目描述Given a string s, find the longest palindromic subsequence's length in s. You may assume that the maximum length of s is 1000.Input:"bbbab"Output:4二、题解方法一:递归(超时)结束条件:两个指针背道而驰时,证明没有...原创 2020-03-07 20:51:14 · 266 阅读 · 0 评论 -
【区间 dp】B002_让字符串成为回文串的最少插入次数(LPSs 解法)
一、题目描述Given a string s. In one step you can insert any character at any index of the string.Return the minimum number of steps to make s palindrome.A Palindrome String is one that reads the same ba...原创 2020-04-09 11:31:56 · 198 阅读 · 0 评论 -
【区间 dp】B000_两个字符串的删除操作(dp)
一、题目描述Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string.Input: "sea", "eat...原创 2020-04-06 10:31:36 · 178 阅读 · 0 评论