【leetcode】1208. Get Equal Substrings Within Budget
题目如下:
You are given two strings
s
andt
of the same length. You want to changes
tot
. Changing thei
-th character ofs
toi
-th character oft
costs|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
s
that can be changed to be the same as the corresponding substring oft
with a cost less than or equal tomaxCost
.If there is no substring from
s
that 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^5
0 <= maxCost <= 10^6
s
andt
only 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 ...
随机推荐
- Opencv之LBP特征(算法)
LBP(Local Binary Pattern),即局部二进制模式,对一个像素点以半径r画一个圈,在圈上取K个点(一般为8),这K个点的值(像素值大于中心点为1,否则为0)组成K位二进制数.此即局部 ...
- k-交叉验证KFold
交叉验证的原理放在后面,先看函数. 设X是一个9*3的矩阵,即9个样本,3个特征,y是一个9维列向量,即9个标签.现在我要进行3折交叉验证. 执行kFold = KFold(n_splits=3) : ...
- Kubernetes网络隔离NetworkPolicy
Kubernetes要求集群中所有pod,无论是节点内还是跨节点,都可以直接通信,或者说所有pod工作在同一跨节点网络,此网络一般是二层虚拟网络,称为pod网络.在安装引导kubernetes时,由选 ...
- reactstrap,scrollbar组件
react-script 编译,部署,sass,less,test,helmet等 https://github.com/facebookincubator/create-react-app/blob ...
- Python新手练手项目
1.新手练手项目集中推荐 https://zhuanlan.zhihu.com/p/22164270 2.Python学习网站 https://www.shiyanlou.com 3.数据结构可视化学 ...
- 阿里云安装filezilla
1.连接服务器 ssh 或者 远程连接 到服务器: 2.安装相应软件 安装EPEL,EPEL是yum的一个软件源,里面包含了许多基本源里没有的软件: yum -y install epel-relea ...
- POJ 2253 Frogger(dijkstra 最短路
POJ 2253 Frogger Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fion ...
- POJ 2528 ——Mayor's posters(线段树+区间操作)
Time limit 1000 ms Memory limit 65536 kB Description The citizens of Bytetown, AB, could not stand t ...
- Go基础学习
Go基础学习 go的基础语法 fmt.Println("hello world!") //go采用行分隔符 关键字 下面列举了 Go 代码中会使用到的 25 个关键字或保留字: b ...
- Git命令之:git push
保护版权:转自,http://www.yiibai.com/git/git_push.html