【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 ...
随机推荐
- THUWC2019(?)历险记
Day \(-?\) 搞文化. Day \(-4\) 突然发现自己复活了,然后就来机房了( Day \(-3\) 返 璞 归 真, 开 始 骆 氪 上午考试,被吊打了/kk Day \(-2\) 上午 ...
- c++ 引用 日期&时间
日期时间[点击进入看吧,没啥可后期拓展的] 引用 引用变量是一个别名,也就是说,它是某个已存在变量的另一个名字.一旦把引用初始化为某个变量,就可以使用该引用名称或变量名称来指向变量. 一.引用和指针的 ...
- tarjan缩点相关知识及代码
emmm原谅我确实是找不到不用缩点的tarjan题才会想到自学一下缩点这个东西的.. 题目没有,只能自己出数据并手动模拟... 首先看一张图(懒得画,还是看输入数据吧,劳烦自行画图..) 7 9(n个 ...
- 【Deep Learning Nanodegree Foundation笔记】第 0 课:课程计划
第一周 机器学习的类型,以及何时使用机器学习 我们将首先简单介绍线性回归和机器学习.这将让你熟悉这些领域的常用术语,你需要了解的技术进展,并了解深度学习在更大的机器学习背景中的位置. 直播:线性回归 ...
- Pandas中关于 loc \ iloc 用法的理解
转载至:https://blog.csdn.net/w_weiying/article/details/81411257 loc函数:通过行索引 "Index" 中的具体值来取行数 ...
- 应用安全 - 免杀 - 工具 - the-backdoor-factory - 使用|命令 - 汇总
安装 Kali下方式一: git clone https://github.com/secretsquirrel/the-backdoor-factory方式二: apt-get install ba ...
- 【神经网络与深度学习】CIFAR-10数据集介绍
CIFAR-10数据集含有6万个32*32的彩色图像,共分为10种类型,由 Alex Krizhevsky, Vinod Nair和 Geoffrey Hinton收集而来.包含50000张训练图片, ...
- Dialupass v3.20 汉化绿色版 显示查看拨号上网密码
Dialupass 显示查看拨号上网密码 拨号上网的密码不小心丢了怎么办?这个工具可以帮你!在紧要关头,它会让你体验到它的奇效!有备无患,快收藏这个小东东吧. 这是一款拯救忘记了拨号网络密码的使用者的 ...
- (转).net中的session与cookies区别及使用方法
cookie数据存放在客户的浏览器上,session数据放在服务器上,cookie不是很安全,别人可以分析存放在本地的COOKIE并进行COOKIE欺骗,考虑到安全应当使用session 先介绍一 ...
- IDEA 如何批量修改变量名
修改前的变量 System.out.println("bbbbb"); System.out.println("bbbbb"); System.out.prin ...