LeetCode 2055. Plates Between Candles

news/2024/9/22 6:52:17

原题链接在这里:https://leetcode.com/problems/plates-between-candles/description/

题目:

There is a long table with a line of plates and candles arranged on top of it. You are given a 0-indexed string s consisting of characters '*' and '|' only, where a '*' represents a plate and a '|' represents a candle.

You are also given a 0-indexed 2D integer array queries where queries[i] = [lefti, righti] denotes the substring s[lefti...righti] (inclusive). For each query, you need to find the number of plates between candles that are in the substring. A plate is considered between candles if there is at least one candle to its left and at least one candle to its right in the substring.

  • For example, s = "||**||**|*", and a query [3, 8] denotes the substring "*||**|". The number of plates between candles in this substring is 2, as each of the two plates has at least one candle in the substring to its left and right.

Return an integer array answer where answer[i] is the answer to the ith query.

Example 1:

ex-1

Input: s = "**|**|***|", queries = [[2,5],[5,9]]
Output: [2,3]
Explanation:
- queries[0] has two plates between candles.
- queries[1] has three plates between candles.

Example 2:

ex-2

Input: s = "***|**|*****|**||**|*", queries = [[1,17],[4,5],[14,17],[5,11],[15,16]]
Output: [9,0,0,0,0]
Explanation:
- queries[0] has nine plates between candles.
- The other queries have zero plates between candles.

Constraints:

  • 3 <= s.length <= 105
  • s consists of '*' and '|' characters.
  • 1 <= queries.length <= 105
  • queries[i].length == 2
  • 0 <= lefti <= righti < s.length

题解:

The request is give a query, we need to get to know plates count between candles within the query indices.

To save time, we need to precompute on s.
For a query [l, r], if we want to quickly find out the first candle after l, and the last candle before r. We can have two arrays to maintain the candle indexes.

When we have the candle indices, we also need a prefix sum of plates to calculate the plates between these two candles.

Ex: s = "* * | * * | * * * |" , queries : [[2,5],[5,9]]-> First we preprocess and calculate index of prevCandle, nextCandle, -> and count of '*' till current index in prefixSum vectorindex     :   0  1  2  3  4  5  6  7  8  9s       :  '*  *  |  *  *  |  *  *  *  |'prev      :  -1 -1  2  2  2  5  5  5  5  9next      :   2  2  2  5  5  5  9  9  9  9prefixSum :   1  2  2  3  4  4  5  6  7  70. query[0] = [2,5] => start = nextCandle[2] = 2=> end = prevCandle[5] =  5Thus, answer[0] = prefixSum[end] - prefixSum[start] = 4 - 2 = 21. query[1] = [5,9] => start = nextCandle[5] = 5=> end = prevCandle[9] =  9Thus, answer[1] = prefixSum[end] - prefixSum[start] = 7 - 4 = 3

And update plates count for each query.

Time Complexity: O(n). n = s.length().

Space: O(n).

AC Java:

 1 class Solution {
 2     public int[] platesBetweenCandles(String s, int[][] queries) {
 3         int n = s.length();
 4         int[] next = new int[n];
 5         int[] prev = new int[n];
 6         int[] prefix = new int[n];
 7         int canPrev = -1;
 8         int canNext = -1;
 9         int count = 0;
10         for(int i = 0; i < n; i++){
11             if(s.charAt(i) == '|'){
12                 canPrev = i; 
13             }
14 
15             prev[i] = canPrev;
16 
17             if(s.charAt(n - 1 - i) == '|'){
18                 canNext = n - 1 - i;
19             }
20 
21             next[n - 1 - i] = canNext;
22 
23             if(s.charAt(i) == '*'){
24                 count++;
25             }
26 
27             prefix[i] = count;
28         }
29 
30         int[] res = new int[queries.length];
31         for(int i = 0; i < queries.length; i++){
32             int left = queries[i][0];
33             int right = queries[i][1];
34 
35             int start = next[left];
36             int end = prev[right];
37             if(start == -1 || end == -1){
38                 res[i] = 0;
39             }else{
40                 int d = end - start;
41                 if(d <= 0){
42                     res[i] = 0;
43                 }else{
44                     res[i] = prefix[end] - prefix[start];
45                 }
46             }
47         }
48 
49         return res;
50     }
51 }

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hjln.cn/news/46777.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

Mac常用sh文件

压缩多个文件夹到一个ZIP #!/bin/bash# 定义目标目录 target_dir="/Users/yuqiu/****/" output_dir="$target_dir/ZIPDIR"# 获取当前日期 current_date=$(date +"%Y-%m-%d")# 定义压缩文件名 zip_filename="XX_${current_date}.zip"# …

探索Semantic Kernel内置插件:深入了解ConversationSummaryPlugin的应用

前言 经过前几章的学习我们已经熟悉了Semantic Kernel 插件的概念,以及基于Prompts构造的Semantic Plugins和基于本地方法构建的Native Plugins。本章我们来讲解一下在Semantic Kernel 中内置的一些插件,让我们避免重复造轮子。 内置插件 Semantic Kernel 有非常多的预定义插…

攀登不止,华为数据库论文入选SIGMOD 2024,技术创新再谱新篇

继ICDE 17篇论文入选后,华为多篇论文再次入选SIGMOD 2024, 顶会入选论文已超过100篇。本文分享自华为云社区《攀登不止,华为数据库论文入选SIGMOD 2024,技术创新再谱新篇》,作者:GaussDB 数据库。 6月9日-14日,2024年数据管理国际会议SIGMOD(ACM SIGMOD/PODS Internati…

Rust性能分析之测试及火焰图,附(lru,lfu,arc)测试

好的测试用例及性能测试是对一个库的稳定及优秀的重要标准,尽量的覆盖全的单元测试,能及早的发现bug,使程序更稳定。性能测试,在编写代码后,单元测试及性能测试是重要的验收点,好的性能测试可以让我们提前发现程序中存在的问题。 测试用例 在Rust中,测试通常有两部分,一…

FreeRTOS简单内核实现7 阻塞链表

增加阻塞链表和溢出阻塞链表,完善 RTOS 内核调度流程0、思考与回答 0.1、思考一 如何处理进入阻塞状态的任务? 为了让 RTOS 支持多优先级,我们创建了多个就绪链表(数组形式),用每一个就绪链表表示一个优先级,对于阻塞状态的任务显然要从就绪链表中移除,但是阻塞状态的任…

危急值上报及闭环管理全解析

什么是危急值制度? 危急值制度是指对提示患者处于生命危急状态的检查、检验结果建立复核、报告、记录等管理机制,以保障患者安全的制度。 管理体系(组织体系+制度建设+管理要素+宣教培训) 针对管理体系中的组织体系、制度建设、管理要素、宣教培训多为线下的制度、流程建立。…

手术分级管理制度

01手术分级管理体系 ● 医院手术分级管理实行院、科两级负责制 ● 医院医疗技术临床应用管理委员会总体负责全院手术分级管理工作,日常工作由医务办公室负责组织、协调,主要职责包括: (一)制定手术分级管理制度规范,定期检查提出改进要求 (二)审定手术分级管理目录,定…