[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 ...
随机推荐
- [原创]创芯电子实验室iFPGA-Cable JTAG工具实物图
创芯电子实验室iFPGA-Cable JTAG工具实物图 对于Xilinx平台 基于FTDI 芯片的Xilinx JTAG 同时支持UART 电平1.8~5V 支持ISE和VIVADO 速度从10M. ...
- IO多路复用注解
#!/usr/bin/env python# -*- coding:utf-8 -*- # 客户端import socket obj = socket.socket()obj.connect((&qu ...
- php操作数据库获取到的结果集mysql_result
判断取出的结果集是否为空集: $sql="select adminPwd from adminaccount"; //判断查询是否有数据 if(mysqli_num_rows($r ...
- 前端技术之--HTML
1.一套规则,浏览器认识的规则. 2.开发者: 学习Html规则 开发后台程序: - 写Html文件(充当模板的作用) ****** - 数据库获取数据,然后替换到html文件的指定位置(Web框架) ...
- Pytorch学习(一)基础语法篇
how to use pytorch 1.Tensor we can create a tensor just like creating a matrix the default type of a ...
- ISP PIPLINE (三) BPC
what is the Bad Pixel? 坏点为死点,也就是基本不随照度变化呈现光电线性转换的关系.表现为暗态常亮,亮态常暗. 坏点分类:静态坏点:亮坏点,暗坏点. ...
- Android中的WeakReference 弱引用
WeakReference 弱引用 定义:弱引用,与强引用(我们常见的引用方式)相对:特点是:GC在回收时会忽略掉弱引用对象(忽略掉这种引用关系),即:就算弱引用指向了某个对象,但只要该对象没有被强引 ...
- ZAB协议与Paxos算法
ZooKeeper并没有直接采用Paxos算法,而是采用一种被称为ZAB(ZooKeeper Atomic Broadcast)的一致性协议 ZooKeeper是一个典型的分布式数据一致性的解决方案, ...
- 移除K位数字
1.题目来源:选自LeetCode 402: 2.问题描述: 3.问题分析 通过分析我们可以得出这样的结论:如果后一个数字比前面的数字小的话,那么我们就要把前面的一个数字删除掉,并且每次把字符串中拆出 ...
- Could not find artifact cn.e3mall:e3mall-parent:pom:0.0.1-SNAPSHOT
[ERROR] [ERROR] Some problems were encountered while processing the POMs:[FATAL] Non-resolvable pare ...