【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
原文地址:https://www.cnblogs.com/seyjs/p/11616703.html
- apache反向代理一、泛解析域名二、APACHE配置
- Python学习 - 可视化变量赋值、循环、程序运行过程
- jdk源码分析红黑树——插入篇1.插入root2.父黑3.父红4.父红,叔红5.1父红,叔黑,外侧子孙5.2父红,叔黑,内侧子孙
- WIFI环境下Android手机和电脑通信
- 破解之美:利用ECB加密缺陷突破cookie加密
- 让Python猜猜你是否能约会成功
- python学习笔记之初识Python
- 从APK解密到批量获取他人信息
- React native和原生之间的通信
- 移动云存储服务平台Parse下的iOS安全分析
- 经典算法学习之回溯法
- 渗透测试系统BackBox Linux 4.1发布
- CMS漏洞检测工具 - CMSmap
- 基因组分析中多物种同源基因的鉴定和筛选
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 迭代器怎么就节省内存了?
- Go 多版本管理
- 空谈分布式系统设计之幂等性
- 空谈发件箱模式(outbox pattern)
- 《一起学sentinel》六、Slot的子类及实现之FlowSlot和DegradeSlot
- Hive UDF/UDAF 总结
- 3分钟短文:用Laravel发一封“漂洋过海”的电子邮件
- leetcode之单词替换
- KMP算法分析
- Spring Boot 系列:最新版优雅停机详解
- 前端学数据结构与算法(八): 单词前缀匹配神器-Trie树的实现及其应用
- 突击并发编程JUC系列-Locksupport 与 Condition
- 01.视频播放器框架介绍
- C#扫描器编写各种问题荟萃
- JavaScript中的浅拷贝与深拷贝