{"id":8657,"date":"2021-11-03T19:21:13","date_gmt":"2021-11-04T02:21:13","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8657"},"modified":"2021-11-03T19:21:21","modified_gmt":"2021-11-04T02:21:21","slug":"leetcode-2055-plates-between-candles","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-2055-plates-between-candles\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2055. Plates Between Candles"},"content":{"rendered":"\n<p>There is a long table with a line of plates and candles arranged on top of it. You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;string&nbsp;<code>s<\/code>&nbsp;consisting of characters&nbsp;<code>'*'<\/code>&nbsp;and&nbsp;<code>'|'<\/code>&nbsp;only, where a&nbsp;<code>'*'<\/code>&nbsp;represents a&nbsp;<strong>plate<\/strong>&nbsp;and a&nbsp;<code>'|'<\/code>&nbsp;represents a&nbsp;<strong>candle<\/strong>.<\/p>\n\n\n\n<p>You are also given a&nbsp;<strong>0-indexed<\/strong>&nbsp;2D integer array&nbsp;<code>queries<\/code>&nbsp;where&nbsp;<code>queries[i] = [left<sub>i<\/sub>, right<sub>i<\/sub>]<\/code>&nbsp;denotes the&nbsp;<strong>substring<\/strong>&nbsp;<code>s[left<sub>i<\/sub>...right<sub>i<\/sub>]<\/code>&nbsp;(<strong>inclusive<\/strong>). For each query, you need to find the&nbsp;<strong>number<\/strong>&nbsp;of plates&nbsp;<strong>between candles<\/strong>&nbsp;that are&nbsp;<strong>in the substring<\/strong>. A plate is considered&nbsp;<strong>between candles<\/strong>&nbsp;if there is at least one candle to its left&nbsp;<strong>and<\/strong>&nbsp;at least one candle to its right&nbsp;<strong>in the substring<\/strong>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example,&nbsp;<code>s = \"||**||**|*\"<\/code>, and a query&nbsp;<code>[3, 8]<\/code>&nbsp;denotes the substring&nbsp;<code>\"*||<strong><u>**<\/u><\/strong>|\"<\/code>. The number of plates between candles in this substring is&nbsp;<code>2<\/code>, as each of the two plates has at least one candle&nbsp;<strong>in the substring<\/strong>&nbsp;to its left&nbsp;<strong>and<\/strong>&nbsp;right.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>an integer array<\/em>&nbsp;<code>answer<\/code>&nbsp;<em>where<\/em>&nbsp;<code>answer[i]<\/code>&nbsp;<em>is the answer to the<\/em>&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;<em>query<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/10\/04\/ex-1.png\" alt=\"ex-1\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"**|**|***|\", queries = [[2,5],[5,9]]\n<strong>Output:<\/strong> [2,3]\n<strong>Explanation:<\/strong>\n- queries[0] has two plates between candles.\n- queries[1] has three plates between candles.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/10\/04\/ex-2.png\" alt=\"ex-2\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"***|**|*****|**||**|*\", queries = [[1,17],[4,5],[14,17],[5,11],[15,16]]\n<strong>Output:<\/strong> [9,0,0,0,0]\n<strong>Explanation:<\/strong>\n- queries[0] has nine plates between candles.\n- The other queries have zero plates between candles.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>3 &lt;= s.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>s<\/code>&nbsp;consists of&nbsp;<code>'*'<\/code>&nbsp;and&nbsp;<code>'|'<\/code>&nbsp;characters.<\/li><li><code>1 &lt;= queries.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>queries[i].length == 2<\/code><\/li><li><code>0 &lt;= left<sub>i<\/sub>&nbsp;&lt;= right<sub>i<\/sub>&nbsp;&lt; s.length<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Binary Search<\/strong><\/h2>\n\n\n\n<p>Store the indices of all candles into an array idx.<\/p>\n\n\n\n<p>For each query q: <br>1. Find the left most candle whose index is greater or equal to left as l.<br>2. Find the left most candle whose index is greater than right, choose the previous candle as r.<\/p>\n\n\n\n<p>[idx[l], idx[r]] is the range that are elements between two candles , there are (idx[r] &#8211; idx[l] + 1) elements in total and there are (r &#8211; l + 1) candles in the range. So the number of plates is (idx[r] &#8211; idx[l] + 1) &#8211;  (r &#8211; l + 1) or (idx[r] &#8211; idx[l]) &#8211; (r &#8211; l)<\/p>\n\n\n\n<p>Time complexity: O(qlogn)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: huahua\nclass Solution {\npublic:\n  vector<int> platesBetweenCandles(string s, vector<vector<int>>& queries) {\n    vector<int> idx;\n    for (size_t i = 0; i < s.size(); ++i)\n      if (s[i] == '|') idx.push_back(i);\n    vector<int> ans;\n    for (const auto& q : queries) {\n      const int l = lower_bound(begin(idx), end(idx), q[0]) - begin(idx);\n      const int r = upper_bound(begin(idx), end(idx), q[1]) - begin(idx) - 1;      \n      ans.push_back(l >= r ? 0 : (idx[r] - idx[l]) - (r - l));\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There is a long table with a line of plates and candles arranged on top of it. You are given a&nbsp;0-indexed&nbsp;string&nbsp;s&nbsp;consisting of characters&nbsp;&#8216;*&#8217;&nbsp;and&nbsp;&#8216;|&#8217;&nbsp;only, where a&nbsp;&#8216;*&#8217;&nbsp;represents&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149],"tags":[52,177,98],"class_list":["post-8657","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-binary-search","tag-medium","tag-range-query","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8657","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8657"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8657\/revisions"}],"predecessor-version":[{"id":8659,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8657\/revisions\/8659"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8657"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}