LeetCode每日一题(2055. Plates Between Candles)

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:

LeetCode每日一题(2055. Plates Between Candles)_第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:

LeetCode每日一题(2055. Plates Between Candles)_第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

query[i] = [left, right], 实际我们要查询的是 s[left]右侧第一个 candle 和 s[right]左侧第一个 candle 之间的 plate 的数量, 我们只要找到 s[left]右侧第一个 candle 的左侧有多少个 plate, s[right]左侧第一个 candle 右侧有多少个 plate, 然后用所有的 plate 的数量减去这两个数量就能拿到答案。 具体每个 char 的以上两值的计算就要用到 prefix sum 了。 要注意如果 s[left], s[right]处于同一||内, 那实际计算出的答案是负数, 因为重复减了它俩同处的||内的 plate 的数量, 这种情况检查置 0 即可



impl Solution {
    pub fn plates_between_candles(s: String, queries: Vec<Vec<i32>>) -> Vec<i32> {
        let mut left: Vec<i32> = s
            .chars()
            .scan(0, |s, c| {
                if c == '*' {
                    *s += 1;
                }
                Some(*s)
            })
            .collect();
        let mut right: Vec<i32> = s
            .chars()
            .rev()
            .scan(0, |s, c| {
                if c == '*' {
                    *s += 1;
                }
                Some(*s)
            })
            .collect();
        right.reverse();
        let chars: Vec<char> = s.chars().collect();
        let mut n = *left.last().unwrap();
        for i in (0..chars.len()).rev() {
            if chars[i] == '|' {
                n = left[i];
            }
            left[i] = n;
        }
        n = right[0];
        for i in 0..chars.len() {
            if chars[i] == '|' {
                n = right[i];
            }
            right[i] = n;
        }
        let total = s.chars().filter(|c| c == &'*').count() as i32;
        queries
            .into_iter()
            .map(|q| (total - left[q[0] as usize] - right[q[1] as usize]).clamp(0, i32::MAX))
            .collect()
    }
}

你可能感兴趣的:(算法,数据结构,leetcode,算法,职场和发展)