首页 > 其他分享 >LeetCode 2055. Plates Between Candles

LeetCode 2055. Plates Between Candles

时间:2024-06-18 09:21:31浏览次数:14  
标签:plates candles 2055 int Candles between query queries Plates

原题链接在这里: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 vector
   
	index     :   0  1  2  3  4  5  6  7  8  9
	  s       :  '*  *  |  *  *  |  *  *  *  |'

	prev      :  -1 -1  2  2  2  5  5  5  5  9
	next      :   2  2  2  5  5  5  9  9  9  9

	prefixSum :   1  2  2  3  4  4  5  6  7  7
	
	0. query[0] = [2,5] 
			=> start = nextCandle[2] = 2
			=> end = prevCandle[5] =  5
			Thus, answer[0] = prefixSum[end] - prefixSum[start] = 4 - 2 = 2
			
	1. query[1] = [5,9] 
			=> start = nextCandle[5] = 5
			=> end = prevCandle[9] =  9
			Thus, 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 }

 

标签:plates,candles,2055,int,Candles,between,query,queries,Plates
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18253685

相关文章

  • WPF DataTemplate DataTemplateSelector
    //xaml<Windowx:Class="WpfApp78.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • Veriadic templates
    Veriadictemplates数量不定的模板参数声明方式:#pragmaonce#ifndef__VARIADICT__#define__VARIADICT__​/*为什么需要定义这个空函数,因为传参到最后传入最后一位参数时后面的一组参数已经没有了.就是0所以这个版本就是没有参数的.当最后一个变成0个的时候调用的是空方法*/......
  • IDEA中Live Templates和Postfix Completion的用法
     前言 IDEA中代码生成的方式有两种LiveTemplatesPostfixCompletion这两种方式中,第一种基本每一个IDE都支持,但是支持第二中的很少。LiveTemplates输出模板1、sout,最基本的输出语句,快速生成System.out.println();2、soutp,快速生成参数输出语句。3、soutm,快速生成......
  • C++ templates: (1)、类模板
    1、类模板定义(主模板)template<typenameT,typenameC=list<T>,intMAX=10>classStack{public:usingvalue_type=T;public:Stack(constT&a):m_oContainer{move(a)}{cout<<"Stack<T,list<T>>()"<<......
  • C++ Function Templates (函数模板)
    C++FunctionTemplates[函数模板]1.TemplatesandGenericProgramming(模板与泛型编程)2.DefiningaFunctionTemplates(定义函数模板)2.1.InstantiatingaFunctionTemplate(实例化函数模板)2.2.TemplateTypeParameters(模板类型参数)2.3.Non......
  • cpp templates :auto推导
    目录常见推导函数的返回值常见推导1auto:产生拷贝,可以修改2auto&:左值引用,接受左值,可以修改3constauto&:const引用,可以接受左右值,不可修改4auto&&:万能引用,可以接受左右值,const引用时不能修改inta=100;constintb=100;autoa1=3;//a1为intautoa2......
  • 2024-03-09:用go语言,我们把无限数量的栈排成一行,按从左到右的次序从 0 开始编号, 每个栈
    2024-03-09:用go语言,我们把无限数量的栈排成一行,按从左到右的次序从0开始编号,每个栈的的最大容量capacity都相同。实现一个叫「餐盘」的类DinnerPlates,DinnerPlates(intcapacity)-给出栈的最大容量capacity,voidpush(intval)将给出的正整数val推入从左往右第一个......
  • P2055 [ZJOI2009] 假期的宿舍
    原题链接题解这种让来让去让我想到了二分图!!注意细节!!剩余的就是模拟了code#include<bits/stdc++.h>usingnamespacestd;intstu[55],gohome[55],know[55][55];intn;intbelong[55]={0};intvis[55]={0};intsettle(intnow){if(vis[now])return0;vis[now]......
  • ABC219H Candles
    [ABC219H]Candles很像关路灯这玩意儿啊,容易想到区间\(DP\)仿照那道题,可以考虑设出状态,即当前已经熄掉的区间\([L,R]\),与当前所在端点\(0/1\)我们有如下转移\[f_{l,r,0}=\max(f_{l+1,r,0}-(N-r+l)*Dis(l,l+1),f_{l+1,r,1}-(N-r+l)*......
  • Templates
    超级IO#defineOPENIOBUFnamespaceFASTIO{classFastIOBase{protected: #ifdefOPENIOBUFstaticconstintBUFSIZE=1<<16;charbuf[BUFSIZE+1];intbuf_p=0; #endifFILE*target;FastIOBase(FILE*f):targe......