[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 ...
随机推荐
- 阿里云学生服务器搭建网站-Ubuntu16.04安装php开发环境
阿里云学生服务器搭建网站(2)-Ubuntu16.04安装php开发环境 优秀博文:https://www.linuxidc.com/Linux/2016-10/136327.htm https:/ ...
- spring+redis的集成,使用spring-data-redis来集成
1.参考:https://www.cnblogs.com/qlqwjy/p/8562703.html 2.首先创建一个maven项目.然后加入依赖的jar包就行了.我加入的jar包很多,反正加入了也没 ...
- 使用Type.MakeGenericType,反射构造泛型类型
有时我们会有通过反射来动态构造泛型类型的需求,该如何实现呢?举个栗子,比如我们常常定义的泛型委托Func<in T, out TResult>,当T或TResult的类型需要根据程序上下文 ...
- AtCoder Grand Contest 027 (AGC017) D - Modulo Matrix 构造
原文链接https://www.cnblogs.com/zhouzhendong/p/AGC027C.html 题解 首先我们假装 max mod min = 1 然后对着这个构造. 将各自黑白染色, ...
- WIN10在安装mysql时,出现“The security settings could not be applied to the database because the connection has failed with the following error. Error Nr. 1045
解决方法:1, 首先卸载MySQL2, 再根据这个目录 C:\ProgramData,将MySQL删除.3, 重新安装MySQL 就好了(电脑不用重启)
- python 单行写法
if not any([_ in fingers for _ in finger_ids])
- 关于FastReport在winform中的使用(包含FastReport.net的安装步骤链接)
一.FastReport的简介 FastReport是功能齐全的报表控件,使开发者可以快速并高效地为·NET/VCL/COM/ActiveX应用程序添加报表支持. 二.FastReport的安装(推荐 ...
- 表单/iframe与video标签
<form action="所有表单值提交的地址" method="传值的方式默认是GET方式,还有另一种POST方式"> 表单元素</for ...
- 在利用node连接数据库遇到的问题
本文持续更新 选择数据库时: connection.query('USE ' + dbName,function(err,rows){ //注意USE后面需要加一个空格,最终才能拼成> USE ...
- uc/os iii移植到STM32F4---IAR开发环境
也许是先入为主的原因,时钟用不惯Keil环境,大多数的教程都是拿keil写的,尝试将官方的uc/os iii 移植到IAR环境. 1.首先尝试从官网上下载的官方移植的代码,编译通过,但是执行会报堆栈溢 ...