【leetcode】1208. Get Equal Substrings Within Budget
题目如下:
You are given two strings
sandtof the same length. You want to changestot. Changing thei-th character ofstoi-th character oftcosts|s[i] - t[i]|that is, the absolute difference between the ASCII values of the characters.You are also given an integer
maxCost.Return the maximum length of a substring of
sthat can be changed to be the same as the corresponding substring oftwith a cost less than or equal tomaxCost.If there is no substring from
sthat can be changed to its corresponding substring fromt, return0.Example 1:
Input: s = "abcd", t = "bcdf", maxCost = 3
Output: 3
Explanation: "abc" of s can change to "bcd". That costs 3, so the maximum length is 3.Example 2:
Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to charactor int, so the maximum length is 1.Example 3:
Input: s = "abcd", t = "acde", maxCost = 0
Output: 1
Explanation: You can't make any change, so the maximum length is 1.Constraints:
1 <= s.length, t.length <= 10^50 <= maxCost <= 10^6sandtonly contain lower case English letters.
解题思路:本题包了一层壳,去掉外表后题目是给定一个正整数组成的数组,求出最长的一段子数组的长度,要求子数组的和不大于cost。解题方法也不难,记per_cost[i]为abs(s[i] - t[i])的值,cost[i]为sum(per_cost[0:i])的值。对于任意一个下标i,很容易通过二分查找的方法找出cost中另外一个下标j,使得cost[i:j] <= cost。
代码如下:
class Solution(object):
def equalSubstring(self, s, t, maxCost):
"""
:type s: str
:type t: str
:type maxCost: int
:rtype: int
"""
cost = []
amount = 0
per_cost = []
for cs,ct in zip(s,t):
amount += abs(ord(cs) - ord(ct))
cost.append(amount)
per_cost.append(abs(ord(cs) - ord(ct)))
#cost.sort()
#print cost
#print per_cost
import bisect
res = -float('inf')
for i in range(len(cost)):
inx = bisect.bisect_right(cost,cost[i] + maxCost - per_cost[i])
res = max(res,inx - i)
return res
【leetcode】1208. Get Equal Substrings Within Budget的更多相关文章
- 【LeetCode】1208. 尽可能使字符串相等 Get Equal Substrings Within Budget (Python)
作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:LeetCode,力扣,算法,算法题,字符串,并查集,刷题群 目录 题目描述 示例 解题思路 滑动窗口 代码 刷题心 ...
- 【LeetCode】696. Count Binary Substrings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:暴力解法(TLE) 方法二:连续子串计算 日 ...
- 【LeetCode】416. Partition Equal Subset Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 动态规划 日期 题目地址:https://l ...
- 【leetcode】927. Three Equal Parts
题目如下: Given an array A of 0s and 1s, divide the array into 3 non-empty parts such that all of these ...
- 【leetcode】1224. Maximum Equal Frequency
题目如下: Given an array nums of positive integers, return the longest possible length of an array prefi ...
- 【leetcode】416. Partition Equal Subset Sum
题目如下: 解题思路:对于这种判断是否的题目,首先看看动态规划能不能解决.本题可以看成是从nums中任选i个元素,判断其和是否为sum(nums)/2,很显然从nums中任选i个元素的和的取值范围是[ ...
- 【LeetCode】647. Palindromic Substrings 解题报告(Python)
[LeetCode]647. Palindromic Substrings 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/p ...
- 【leetcode】698. Partition to K Equal Sum Subsets
题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...
- 【leetcode】486. Predict the Winner
题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...
随机推荐
- 解密Go语言之 pprof
相信很多人都听过“雷神 3”关于性能优化的故事.在一个 3D 游戏引擎的源码里,John Carmack 将 1/sqrt(x) 这个函数的执行效率优化到了极致. 一般我们使用二分法,或者牛顿迭代法计 ...
- JS对象—数组总结(创建、属性、方法)
JS对象—数组总结(创建.属性.方法) 1.创建字符串 1.1 new Array() var arr1 = new Array(); var arr2 = new Array(6); 数组的长度为6 ...
- Hibernate——简单的增、删、改、查操作
思路: 1.导入hibernate jar包 2.导入mysql jar包 3.创建数据库 4.创建java实体类 5.编写hibernate.cfg.xml配置文件 6.编写dao类 目录: 数据表 ...
- [转帖]Spring Cloud底层原理
拜托!面试不要再问我Spring Cloud底层原理 https://mp.weixin.qq.com/s/ZH-3JK90mhnJPfdsYH2yDA 毫无疑问,Spring Cloud 是目前微服 ...
- java中的小知识点
1.数据类型的相关知识点 1.1.java内置封装类的转换 java中内置的封装类Byte.Integer.Float.Double和Long都可以转换成double类型的数值:因为这些封装好的类中都 ...
- Cause: org.xml.sax.SAXParseException: The content of elements must consist of well-formed character data or markup.
Caused by: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org ...
- linux源码下载
概要:本文主要介绍ubuntu环境下,内核源码和命令源码的获取方式. 内核源码: 1.最简洁的方式,使用命令:apt-get source linux-$(uname -r).但配置的源服务器中不一定 ...
- 模板渲染JinJa2
模板渲染JinJa2 可以从数据库中查询数据,然后去替换我html中的对应内容(专业名词叫做模板渲染,你先渲染一下,再给浏览器进行渲染),然后再发送给浏览器完成渲染. 这个过程就相当于HTML模板 ...
- ubuntu下python3虚拟环境的配置
安装相关包 sudo pip3 install virtualenv # 虚拟环境包 sudo pip3 install virtualenvwrapper # 虚拟环境管理包 创建虚拟环境目录 su ...
- vue 实践技巧合集
前言 本文纯属个人平时实践过程中的一些经验总结,算是一点点小技巧吧,不是多么高明的技术,如果对你有帮助,那么不胜荣幸. 本文不涉及罕见API使用方法等,大部分内容都是基于对vue的一些实践而已.由于涉 ...