[LeetCode] 132. Palindrome Partitioning II_ Hard tag: Dynamic Programming
Given 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.
Example:
Input: "aab"
Output: 1
Explanation: The palindrome partitioning ["aa","b"] could be produced using 1 cut.
这个题目利用了两个dynamic programming的方式,先预处理s,得到所有substring的Palindrome表,T: O(n^2), S: O(n^2). 因为判断一个string是否为palindrome可以根据把首尾字母去掉之后的substring是否为palindrome,如果是,在判断首尾两个字母是否相等。如果不用dynamic programming可能会到T: O(n^3) 这个时间复杂度。
所以先从length小的来遍历一遍之后再依次遍历更长的substring,所以两个for loop要先loop length,然后再loop start position。
然后再通过mem[i] 去记录前i个字符的最小cut 的数量,为了方便for loop,因为要判断后面的substring是否为palindrome,如果是的话就+1, 这个时候会碰到如果从第一个字母到substring的最后一个字母是palindrome,结果应该是0, 但是前面加1了,所以需要用长度为n + 1 的mem去记录,初始化mem[0] = -1. 这里需要注意下标,因为在Palin的表格里面,下标就是s的下标,而mem中的下标是前n个字符。
Code:
class Solution:
def minCut(self, s):
if not s or len(s) == 1:
return 0
n = len(s)
# pre deal with the Palindrome table
palin = [[False]*n for _ in range(n)]
for i in range(n):
palin[i][i] = True
for i in range(n - 1):
palin[i][i + 1] = s[i] == s[i + 1]
for l in range(2, n): # 下标是s的下标,所以是n
for start in range(0, n - l): # s + l < n
palin[start][start + l] = palin[start + 1][start + l - 1] and s[start] == s[start + l]
# deal with the mem array
mem = list(range(-1, n)) # if in python 3, range is an iterator
for i in range(2, n + 1): # need to go to mem[n] which presents the first n characters
for j in range(0, i):
if palin[j][i - 1]: # i will be n, but palin is n * n
mem[i] = min(mem[i], mem[j] + 1)
return mem[n]
[LeetCode] 132. Palindrome Partitioning II_ Hard tag: Dynamic Programming的更多相关文章
- [LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 63. Unique Paths II_ Medium tag: Dynamic Programming
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- leetcode 132. Palindrome Partitioning II ----- java
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- Java for LeetCode 132 Palindrome Partitioning II
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- Leetcode 132. Palindrome Partitioning II
求次数的问题一般用DP class Solution(object): def minCut(self, s): """ :type s: str :rtype: int ...
- [LeetCode] 643. Maximum Average Subarray I_Easy tag: Dynamic Programming(Sliding windows)
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- [LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and re ...
- [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...
- leetcode@ [131/132] Palindrome Partitioning & Palindrome Partitioning II
https://leetcode.com/problems/palindrome-partitioning/ Given a string s, partition s such that every ...
随机推荐
- fft,ntt
在被两题卡了常数之后,花了很久优化了自己的模板 现在的一般来说任意模数求逆1s跑3e5,exp跑1e5是没啥问题的(自己电脑,可能比luogu慢一倍) 当模数是$998244353,100453580 ...
- 企业级中带你ELK如何实时收集分析Mysql慢查询日志
什么是Mysql慢查询日志? 当SQL语句执行时间超过设定的阈值时,便于记录到指定的日志文件中或者表中,所有记录称之为慢查询日志 为什么要收集Mysql慢查询日志? 数据库在运行期间,可能会存在这很多 ...
- spark Transformations算子
在java中,RDD分为javaRDDs和javaPairRDDs.下面分两大类来进行. 都必须要进行的一步. SparkConf conf = new SparkConf().setMaster(& ...
- Cpp Generals 1.2
原文链接https://www.cnblogs.com/zhouzhendong/p/Cpp-Generals.html 源码暂不公开 下载链接 适用系统: Windows: 运行时请在进入彩色界面之 ...
- js处理有序列表、js处理无序列表
有序列表.无序列表 显示id为s下所有的li..隐藏同样 $("#s li").show();
- Loadrunner乱码问题解决方案(录制&&运行)
在使用Loadrunner录制和回放时有时会出现乱码,从而导致脚本运行失败,这让我们很难定位脚本问题所在. 1.乱码产生的原因 1)loadrunner工具使用的是UTF-8编码,但被测系统使用的是G ...
- K个排序链表的合并(Hard)
问题来源:选自leetCode 23:合并K个排序链表 问题描述: 题目给定信息: 不确定需要合并的链表的数目,但依然要求我们把给定的这些有序链表合并成一个链表,并且保证合并的链表依然是有序的. 问题 ...
- buffer格式的转换
---恢复内容开始--- 定义好一个buffer 例如: var buf = new Buffer(''nihaoya); 通过str转成base64的字符 var str =buf.toString ...
- js之promise讲解
1 Promise概述 Promise对象是CommonJS工作组提出的一种规范,目的是为异步操作提供统一接口. 那么,什么是Promises? 首先,它是一个对象,也就是说与其他JavaScript ...
- C_使用clock()函数获取程序执行时间
clock():捕捉从程序开始运行到clock()被调用时所耗费的时间.这个时间单位是clock tick ,即“时钟打点”. 常数CLK_TCK:机器时钟每秒所走的时钟打点数. #include & ...