[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 ...
随机推荐
- Java Spring Boot VS .NetCore (八) Java 注解 vs .NetCore Attribute
Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...
- Gradle 使用笔记
Springboot2.0 多模块打包问题 打包命令由gradle build 变成 gradle bootJar 或 gradle bootWar buildscript { repositorie ...
- Dapper结合Repository模式的应用
Dapper结合Repository模式的应用,包括如何在数据访问层(DAL)使用Dapper组件. Dapper在真实项目中使用,扩展IDbConnection的功能,支持Oracle.MS SQL ...
- Linux查看设备命令
系统 # uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # ho ...
- 图书管理系统 (c语言实现) (全部代码)
直接上代码不解释 #include <stdio.h> #include <stdlib.h> #include <string.h> #define AVAILA ...
- 此处为当前页,设置此处的href点后没有效果
<%--此处当前页不能点,设置href为没有动作Javascript:void(0); --%> 如果javaScript:void(0);写错了,那就很尴尬(某些浏览器忽略该错误如:谷歌 ...
- 微信小程序--家庭记账本开发--07
最终展示以及相关代码 1.最终效果展示 最终效果展示已经根据最初要求使用视频拍摄在抖音上,下面是相关页面展示图片: 2.相关代码 本次开发主要页面则是首页界面以及记账界面以及实现页面跳转,以及记账内容 ...
- xls 编码 utf-8
直接用 Excel 打开 UTF-8 编码的 CSV 文件会导致汉字部分出现乱码.原因是 Excel 以 ANSI 格式打开,不会做编码识别. ==打开 UTF-8 编码的 CSV 文件的方法:1) ...
- arcgis中转换netCDF为栅格数据
最近有个同学询问我一个问题,使用arcpy把netcdf转化成栅格文件,忙活了两个小时才搞定,其实主要代码非常简单,只不过要对arcgis 的功能比较熟悉(其实多思考和查考它的帮助文章,无聊) # - ...
- Linux 定时任务调度(crontab命令)
crond 是Linux下用周期性的执行某种任务或者等待处理某些事件的一个守护进程,crond 进程会每分钟定期检查是否有要执行的任务,如果有要执行的任务则自动执行该任务 Linux 下的任务调度 系 ...