LeetCode 2055. Plates Between Candles

原题链接在这里: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 }

 

posted @ 2024-06-18 09:20  Dylan_Java_NYC  阅读(11)  评论(0编辑  收藏  举报