[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 ...
 
随机推荐
- Zabbix(一)
			
安装一台CentOS: CentOS-7.4-x86_64-DVD-1708 https://www.cnblogs.com/xiewenming/p/7732144.html https://blo ...
 - Invalid tld file: "/WEB-INF/tags/xxxt.tld", see JSP 2.2 specification section 7.3.1 for more details
			
错误描述 在jsp页面引入了自定义的TLD文件的时候,碰到了一个错误 Invalid tld file: "/WEB-INF/tags/xxxt.tld", see JSP 2.2 ...
 - git本地项目上传至码云gitee
			
如果你的本机是安装成功第一次使用,先配置一下一些基本的信息 $ git config--global user.name "Your Name" $ git config --gl ...
 - Git一些简单但非常重要并常用的操作命令
			
1.将本地与github进行关联配置 生成公钥 ssh-keygen -t rsa -C "jiasheng.mei@hpe.com" 将公钥拷贝到github中 在公钥同文件夹( ...
 - Linux中jdk安装及配置
			
第一步:准备好jdk安装包:
 - Spring中RequestContextHolder以及HandlerInterceptorAdapter的使用
			
1 . RequestContextHolder 的使用 想要使用RequestContextHolder,要在web.xml中配置RequestContextListener的监听才能使用. //全 ...
 - Servlet(九):web.xml文件和server.xml文件
			
Web.xml 文件使用总结:作用: 存储项目相关的配置信息,保护 Servlet.解耦一些数据对程序的依赖.使用位置: 每个 Web 项目中Tomcat 服务器中(在服务器目录 conf 目录中)区 ...
 - Linux中Buffer和Cache的区别
			
1. Cache:缓存区,是高速缓存,是位于CPU和主内存之间的容量较小但速度很快的存储器,因为CPU的速度远远高于主内存的速度,CPU从内存中读取数据需等待很长的时间,而 Cache保存着CPU刚 ...
 - maven与eclipse集成
			
https://www.cnblogs.com/teach/p/5906425.html
 - vue中Prop父子传值方法
			
在用vue做项目的过程中感觉很好玩,特做下笔记... 父组件中: <template> <div> <fpdx-modal :zbArr="polygonArr ...