【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 ...
随机推荐
- 重新网格化(Remesh)
原文链接 Remesh并没有一个严格的定义,简单的讲,Remesh就是从一个输入网格生成另一个网格,并且满足一定的要求.根据网格改动大小,可以分为这么几类: 保持顶点拓扑和几何信息,优化网格连接关系 ...
- 《React+Redux前端开发实战》笔记3:基于Webpack构建的Hello World案例(下)
2.使用React编码 下面正式开始使用React来编写前端代码. (1)npm安装react和react-dom: npm install react react-dom -S (2)用下面代码替换 ...
- 奉献pytorch 搭建 CNN 卷积神经网络训练图像识别的模型,配合numpy 和matplotlib 一起使用调用 cuda GPU进行加速训练
1.Torch构建简单的模型 # coding:utf-8 import torch class Net(torch.nn.Module): def __init__(self,img_rgb=3,i ...
- Docker&Java&Mysql&Python3&Supervisor&Elasticsearch安装
目录 docker 安装java 安装mysql 安装Mysql8 安装python3 安装supervisor 安装ElasticSearch 打包images docker yum install ...
- Go语言中byte类型和rune类型(五)
本篇内容本来准备在上一篇写的,想了想还是拆开写. go语言中字符串需要使用用双引号,而单引号用来表示单个的字符,字符也是组成字符串的元素.go语言的字符有两种: uint8类型,或者叫 byte 型, ...
- Who will be punished
Who will be punished Problem Description This time,suddenly,teacher Li wants to find out who have mi ...
- jmeter---linux安装运行
安装jdk 安装jmeter 配置环境变量: export JAVA_HOME=/usr/java/1.8.0_181 export CLASSPATH=.:JAVA_HOME/lib/dt.jar: ...
- Appium+Python之获取toast
思考:手机APP上的必填或错误文本提示一般1-2s就会消失,比较难定位,所以一般的固定元素定位方式是不可用的,那我们如何定位toast呢? 前提:1.Appium1.6.3以上(包括1.6.3) 2. ...
- 在Vue项目中加载krpano全景图
在Vue-cli项目中做krpano全景图编辑器的时候,由于js插件的路径是动态的,做的过程中遇到了加载不到资源的难题,在网上搜索了好久也没找到合适的办法,最后想到了可能是JS加载的问题,于是解决了问 ...
- Jquery复习(五)之append()、appendTo()、prepend()、prependTo()、after()、before()易忘点
添加元素的方法 append().appendTo().prepend().prependTo().after().before() 通过 append() .appendTo().prepend() ...