LeetCode-2055 蜡烛之间的盘子 及库函数 lower_bound 和 upper_bound学习使用
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/plates-between-candles
题目描述
给你一个长桌子,桌子上盘子和蜡烛排成一列。给你一个下标从 0 开始的字符串 s ,它只包含字符 '*' 和 '|' ,其中 '*' 表示一个 盘子 ,'|' 表示一支 蜡烛 。
同时给你一个下标从 0 开始的二维整数数组 queries ,其中 queries[i] = [lefti, righti] 表示 子字符串 s[lefti...righti] (包含左右端点的字符)。对于每个查询,你需要找到 子字符串中 在 两支蜡烛之间 的盘子的 数目 。如果一个盘子在 子字符串中 左边和右边 都 至少有一支蜡烛,那么这个盘子满足在 两支蜡烛之间 。
比方说,s = "||**||**|*" ,查询 [3, 8] ,表示的是子字符串 "*||**|" 。子字符串中在两支蜡烛之间的盘子数目为 2 ,子字符串中右边两个盘子在它们左边和右边 都 至少有一支蜡烛。
请你返回一个整数数组 answer ,其中 answer[i] 是第 i 个查询的答案。
示例 1:

输入:s = "**|**|***|", queries = [[2,5],[5,9]]
输出:[2,3]
解释:
- queries[0] 有两个盘子在蜡烛之间。
- queries[1] 有三个盘子在蜡烛之间。
示例 2:

输入:s = "***|**|*****|**||**|*", queries = [[1,17],[4,5],[14,17],[5,11],[15,16]]
输出:[9,0,0,0,0]
解释:
- queries[0] 有 9 个盘子在蜡烛之间。
- 另一个查询没有盘子在蜡烛之间。
提示:
3 <= s.length <= 105
s 只包含字符 '*' 和 '|' 。
1 <= queries.length <= 105
queries[i].length == 2
0 <= lefti <= righti < s.length
解题思路
最初的想法是将蜡烛位置全部记录下来,然后将区间[a,b]转换成其最大蜡烛子区间[c,d],其中盘子数就是d - c - id-ic 其中,id、ic是蜡烛d c在蜡烛中的排序,时间复杂度为O(n + n * m)时间超时了
之后使用前缀和的思想,将盘子的前缀和左边右边第一个蜡烛分别记录下来,区间[a,b]的盘子数就是区间[c,d]的盘子数,也就是sumd - sumc,时间复杂度为O(n + m)
第一种解法可以进一步优化,使用二分法查找c和d时间复杂度会降为O(n + m * log(n)).
这里使用了两个库函数lower_bound 和 upper_bound,这两个库函数是属于<algorithm>头文件中,底层实现基于二分查找,lower_bound是查找数组中大于等于x的第一个数,而upper_bound是查找大于x的第一个数。
新增不使用库函数的二分写法。
代码展示
前缀和
class Solution {
public:
vector<int> platesBetweenCandles(string s, vector<vector<int>>& queries) {
vector<int> viRet;
int n = s.size();
vector<int> viSum(n);
int sum = 0;
for(int i =0; i < n; i++)
{
if(s[i] == '*')
{
sum++;
}
viSum[i] = sum;
}
vector<int> viLeft(n);
for(int i =0, l = -1; i < n; i++)
{
if(s[i] == '|')
{
l = i;
}
viLeft[i] = l;
}
vector<int> viRight(n);
for(int i = n - 1, l = -1; i >= 0; i--)
{
if(s[i] == '|')
{
l = i;
}
viRight[i] = l;
}
for(auto iter: queries)
{
int iLeft = viRight[iter[0]], iRight = viLeft[iter[1]];
if(iLeft == -1 || iRight == -1 || iLeft >= iRight)
{
viRet.push_back(0);
}
else
{
viRet.push_back(viSum[iRight] - viSum[iLeft]);
}
}
return viRet;
}
};
预处理+库函数二分:
class Solution {
public:
vector<int> platesBetweenCandles(string s, vector<vector<int>>& queries) {
vector<int> viRet;
vector<int> viIndex;
for(int i =0; i < s.size(); i ++)
{
if(s[i] == '|')
{
viIndex.push_back(i);
}
}
for(auto iter: queries)
{
int iLeft = -1, iLeftIndex = -1, iRight = -1, iRightIndex = -1;
iLeftIndex = lower_bound(viIndex.begin(), viIndex.end(), iter[0]) - viIndex.begin();
iRightIndex = upper_bound(viIndex.begin(), viIndex.end(), iter[1]) - viIndex.begin();
iRightIndex--;
if(iLeftIndex == -1 || iRightIndex == -1 || iLeftIndex >= iRightIndex)
{
viRet.push_back(0);
}
else
{
iLeft = viIndex[iLeftIndex];
iRight = viIndex[iRightIndex];
viRet.push_back(iRight - iLeft - iRightIndex + iLeftIndex);
}
}
return viRet;
}
};
预处理+二分
class Solution {
public:
vector<int> platesBetweenCandles(string s, vector<vector<int>>& queries) {
vector<int> viRet(queries.size(), 0);
vector<int> viIndex;
for (int i = 0; i < s.size(); i++)
{
if (s[i] == '|')
{
viIndex.push_back(i);
}
}
if(!viIndex.size()) return viRet;
for (int i = 0; i < queries.size(); i++)
{
int iLeft = -1, iLeftIndex = -1, iRight = -1, iRightIndex = -1;
int l = 0, r = viIndex.size() - 1;
while (l < r)
{
int mid = (l + r) / 2;
if (viIndex[mid] >= queries[i][0])
{
r = mid;
}
else
{
l = mid + 1;
}
}
if (viIndex[r] >= queries[i][0])
{
iLeft = viIndex[r];
iLeftIndex = r;
}
else
continue;
l = 0, r = viIndex.size() - 1;
while (l < r)
{
int mid = (l + r + 1) / 2;
if (viIndex[mid] <= queries[i][1])
{
l = mid;
}
else
{
r = mid - 1;
}
}
if (viIndex[r] <= queries[i][1])
{
iRight = viIndex[r];
iRightIndex = r;
}
else
continue;
if (iLeft == -1 || iRight == -1 || iLeft >= iRight)
{
}
else
{
viRet[i] = iRight - iLeft - iRightIndex + iLeftIndex;
}
}
return viRet;
}
};
运行结果

LeetCode-2055 蜡烛之间的盘子 及库函数 lower_bound 和 upper_bound学习使用的更多相关文章
- [动态规划] LeetCode 2055. 蜡烛之间的盘子
LeetCode 2055 蜡烛之间的盘子 前言: 这个题做的时间略长了,开始的时候打算先定位两个端点的蜡烛,之后在遍历其中的盘子,结果不言而喻,必time limit了,之后就预处理了前x的蜡烛间盘 ...
- LeetCode 34 - 在排序数组中查找元素的第一个和最后一个位置 - [二分][lower_bound和upper_bound]
给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在目标值,返回 [ ...
- [LeetCode] Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- leetcode:Roman to Integer(罗马数字转化为罗马数字)
Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...
- [LeetCode] Falling Squares 下落的方块
On an infinite number line (x-axis), we drop given squares in the order they are given. The i-th squ ...
- C++引用C程序库函数
C与C++混合编程 C++里面如何声明const void f(void)函数为C程序中的库函数. void f(void)用c++ compiler来编译,在产生的obj文件中的名字变成了 $f@@ ...
- [LeetCode] 327. Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
随机推荐
- DTMF2num拨号音识别
说明 很多出题人可能会把手机或者其他设备打电话的拨号音作为一个题目技能中的考察点. 什么是DTMF? 双音多频的拨号键盘是4×4的矩阵,每一行代表一个低频,每一列代表一个高频.每按一个键就发送一个高频 ...
- [seaborn] seaborn学习笔记9-绘图实例(1) Drawing example(1)
文章目录 9 绘图实例(1) Drawing example(1) 1. Anscombe's quartet(lmplot) 2. Color palette choices(barplot) 3. ...
- [常用工具] git基础学习笔记
git基础学习笔记,参考视频:1小时玩转 Git/Github 添加推送信息,-m= message git commit -m "添加注释" 查看状态 git status 显示 ...
- vulnhub靶场之HACKATHONCTF: 2
准备: 攻击机:虚拟机kali.本机win10. 靶机:HackathonCTF: 2,下载地址:https://download.vulnhub.com/hackathonctf/Hackathon ...
- 使用SQL4Automation让CodeSYS连接数据库
摘要:本文旨在说明面向CodeSYS的数据库连接方案SQL4Automation的使用方法. 1.SQL4Automation简介 1.1.什么是SQL4Automation SQL4Auto ...
- (Java)设计模式:结构型
前言 这篇博文续接的是 UML建模.设计原则.创建型设计模式.行为型设计模式,有兴趣的可以看一下 3.3.结构型 这些设计模式关注类和对象的组合.将类和对象组合在一起,从而形成更大的结构 * 3.3. ...
- 克拉玛依初赛-wp
MISC 签到 16进制转字符串 base64 再来一次base64 flag 论禅论道 7z解压得到jar 使用decom打开 解密 得到flag WEB pingme 抓包,修改POST提交的参数 ...
- cordova第三方插件的创建,修改以及调试指南---真机调试,浏览器调试
cordova使用以及真机调试,浏览器调试 创建插件 点击参考此文-- 超详细手把手教你cordova开发使用指南+自定义插件 插件修改注意事项--很重要 每次对自己代码目录里面任何内容进行修改后 都 ...
- TCP通信的客户端代码实现-TCP通信的服务器端代码实现
TCP通信的客户端代码实现 两端通信时步骤:1.服务端程序,需要事先启动,等待客户端的连接.⒉.客户端主动连接服务器端,连接成功才能通信.服务端不可以主动连接客户端.在Java中,提供了两个类用于实现 ...
- spring注入静态变量有几种方法?不看的都掉坑里了!
springboot中经常会用到properties文件中的配置,一般使用@Value注入,但是针对Utils工具类,需要注入一个静态变量有几种方法?为什么有的同学注入的值为null? 代码示例 如果 ...