id
int64 1
3.58k
| problem_description
stringlengths 516
21.8k
| instruction
int64 0
3
| solution_c
dict |
---|---|---|---|
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n priority_queue< int, vector<int>, greater<int> > pq_left, pq_right;// {costs[i], i}\n int left_limit = candidates -1, right_limit = costs.size() - candidates ; \n long long min_cost= 0;\n \n // iterate over costs array and push K elements in both queues\n // loop k time to get the minimum cost. If both left and right side are equal then pop from the left side\n // After each pop change the left and right limit\n if(left_limit >= right_limit || k == costs.size()){\n // push all elements in pq\n for(int i = 0 ; i < costs.size() ; i++ ){\n pq_left.push(costs[i]);\n }\n\n int loop = k ;\n while(loop){\n auto min_cost_lab = pq_left.top(); pq_left.pop();\n min_cost += min_cost_lab;\n loop--;\n }\n return min_cost;\n }\n else{\n // fill the pq_left and pq_right\n for(int i = 0 ; i <= left_limit ; i++ ){\n pq_left.push(costs[i]);\n cout<< \"added in pq left\" << costs[i]<<endl;\n }\n for(int i = costs.size()-1 ; i >= right_limit ; i-- ){\n pq_right.push(costs[i]);\n cout<< \"added in pq right\" << \" i \"<< i << \" right_limit \"<< right_limit<< costs[i]<<endl;\n }\n\n int loop = k ;\n while(loop >0){ // extra conditions\n cout<<\" left and right limit ========\"<< left_limit << \" \"<< right_limit<<endl;\n if(!pq_left.empty() && !pq_right.empty() && pq_left.top() <= pq_right.top()){\n auto min_cost_lab = pq_left.top(); pq_left.pop();\n cout<<\" left pq top \" << min_cost_lab << \"pq_left size \" <<pq_left.size()<<endl; \n min_cost += min_cost_lab;\n // adding new cost which is not in any queue\n if(left_limit+1< right_limit){\n left_limit++;\n pq_left.push(costs[left_limit]);\n cout<<\" adding costs \" << costs[left_limit] <<endl;\n }\n }\n else if(!pq_left.empty() && !pq_right.empty() && pq_left.top() > pq_right.top()){\n auto min_cost_lab = pq_right.top(); pq_right.pop();\n cout<<\" right pq top \" << min_cost_lab <<\"pq_right size \" <<pq_right.size()<<endl;\n min_cost += min_cost_lab;\n // adding new cost which is not in any queue\n if(right_limit -1 > left_limit){\n right_limit--;\n pq_right.push(costs[right_limit]);\n cout<<\" adding costs \" << costs[right_limit] <<endl;\n }\n }\n else if (pq_left.empty() || pq_right.empty() ){\n if(pq_left.empty()){\n auto min_cost_lab = pq_right.top(); pq_right.pop();\n min_cost += min_cost_lab;\n }\n else{\n auto min_cost_lab = pq_left.top(); pq_left.pop();\n min_cost += min_cost_lab;\n }\n }\n loop--;\n }\n\n }\n return min_cost;\n\n }\n};",
"memory": "77635"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n priority_queue< int, vector<int>, greater<int> > pq_left, pq_right;// {costs[i], i}\n int left_limit = candidates -1, right_limit = costs.size() - candidates ; \n long long min_cost= 0;\n \n // iterate over costs array and push K elements in both queues\n // loop k time to get the minimum cost. If both left and right side are equal then pop from the left side\n // After each pop change the left and right limit\n if(left_limit >= right_limit || k == costs.size()){\n // push all elements in pq\n for(int i = 0 ; i < costs.size() ; i++ ){\n pq_left.push(costs[i]);\n }\n\n int loop = k ;\n while(loop){\n auto min_cost_lab = pq_left.top(); pq_left.pop();\n min_cost += min_cost_lab;\n loop--;\n }\n return min_cost;\n }\n else{\n // fill the pq_left and pq_right\n for(int i = 0 ; i <= left_limit ; i++ )pq_left.push(costs[i]);\n \n for(int i = costs.size()-1 ; i >= right_limit ; i-- )pq_right.push(costs[i]);\n\n int loop = k ;\n while(loop >0){ // extra conditions\n \n if(!pq_left.empty() && !pq_right.empty() && pq_left.top() <= pq_right.top()){\n auto min_cost_lab = pq_left.top(); pq_left.pop();\n \n min_cost += min_cost_lab;\n // adding new cost which is not in any queue\n if(left_limit+1< right_limit){\n left_limit++;\n pq_left.push(costs[left_limit]);\n }\n }\n else if(!pq_left.empty() && !pq_right.empty() && pq_left.top() > pq_right.top()){\n auto min_cost_lab = pq_right.top(); pq_right.pop();\n min_cost += min_cost_lab;\n // adding new cost which is not in any queue\n if(right_limit -1 > left_limit){\n right_limit--;\n pq_right.push(costs[right_limit]);\n }\n }\n else if (pq_left.empty() || pq_right.empty() ){\n if(pq_left.empty()){\n auto min_cost_lab = pq_right.top(); pq_right.pop();\n min_cost += min_cost_lab;\n }\n else{\n auto min_cost_lab = pq_left.top(); pq_left.pop();\n min_cost += min_cost_lab;\n }\n }\n loop--;\n }\n\n }\n return min_cost;\n\n }\n};",
"memory": "77635"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n int len = costs.size();\n priority_queue<int, vector<int>, greater<int>> q_left, q_right;\n long long total = 0;\n int left = min(candidates, len/2);\n int right = max(len-candidates-1,len/2 - 1);\n cout << left << endl;\n cout << right << endl;\n for(int i = 0; i < len;++i){\n if(i < left){\n q_left.push(costs[i]);\n }else if(i > right){\n q_right.push(costs[i]);\n }\n }\n if(len > 2*candidates){\n int l, r;\n while(k != 0 && right >= left){\n l = q_left.top();\n r = q_right.top();\n cout << \"Check!\" << endl;\n if(l <= r){\n q_left.pop();\n total += l;\n k--;\n q_left.push(costs[left]);\n left++;\n\n }else{\n q_right.pop();\n total += r;\n k--;\n q_right.push(costs[right]);\n right--;\n }\n }\n }\n\n if(k != 0){\n while(!q_right.empty()){\n q_left.push(q_right.top());\n q_right.pop();\n }\n while(k != 0){\n total += q_left.top();\n q_left.pop();\n k--;\n }\n \n } \n return total;\n }\n};",
"memory": "78145"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n long long cost = 0;\n int n = costs.size() / 2;\n if(candidates * 2 > costs.size()){\n priority_queue<int, vector<int>, greater<int>> p;\n for(int i : costs){\n p.push(i);\n }\n while(k > 0){\n cost += p.top();\n p.pop();\n k--;\n }\n return cost;\n }\n priority_queue<pair<int,int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n for(int i = 0; i < candidates; i++){\n pq.push({costs[i], i});\n }\n for(int i = costs.size()-candidates; i < costs.size(); i++){\n pq.push({costs[i], i});\n }\n while(k>0){\n if(candidates * 2 > costs.size()){\n break;\n }\n cost += pq.top().first;\n if(pq.top().second < costs.size() / 2){\n cout << pq.top().first << endl;\n costs.erase(costs.begin());\n pq.pop();\n if(candidates * 2 > costs.size()){\n k -= 1;\n break;\n }\n pq.push({costs[candidates-1], candidates-1});\n k -= 1;\n }\n else{\n cout << pq.top().first << \" \";\n costs.erase(costs.end());\n pq.pop();\n if(candidates * 2 > costs.size()){\n k -= 1;\n break;\n }\n pq.push({costs[costs.size() - candidates], costs.size() - candidates});\n cout << pq.top().first << endl;\n k -= 1;\n }\n }\n if(k != 0){\n priority_queue<int, vector<int>, greater<int>> p;\n while(!pq.empty()){\n p.push(pq.top().first);\n pq.pop();\n }\n while(k > 0){\n cost += p.top();\n p.pop();\n k--;\n }\n }\n return cost;\n }\n};",
"memory": "78145"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n priority_queue<int, vector<int>, greater<int>> left, right;\n\n int i=0, j=costs.size()-1;\n while(i < candidates){\n left.push(costs[i]);\n costs[i] = INT_MAX;\n i++;\n }\n while(costs.size()-1-j < candidates){\n right.push(costs[j]);\n costs[j] = INT_MAX;\n j--;\n }\n long long ans = 0;\n while(k--){\n if(!right.size() || (left.size() && left.top() <= right.top())){\n int num = left.top();\n left.pop();\n ans += num;\n if(i < costs.size()){\n left.push(costs[i]);\n costs[i] = INT_MAX;\n i++;\n }\n }\n else{\n int num = right.top();\n right.pop();\n ans += num;\n if(j >= 0){\n right.push(costs[j]);\n costs[j] = INT_MAX;\n j--;\n }\n }\n }\n\n return ans;\n }\n};",
"memory": "78655"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n priority_queue<int, vector<int>, greater<int>> left, right;\n\n int i=0, j=costs.size()-1;\n while(i < candidates){\n left.push(costs[i]);\n costs[i] = INT_MAX;\n i++;\n }\n while(costs.size()-1-j < candidates){\n right.push(costs[j]);\n costs[j] = INT_MAX;\n j--;\n }\n\n long long ans = 0;\n while(k--){\n if(!right.size() || (left.size() && left.top() <= right.top())){\n int num = left.top();\n left.pop();\n\n ans += num;\n\n if(i < costs.size()){\n left.push(costs[i]);\n costs[i] = INT_MAX;\n i++;\n }\n }\n else{\n int num = right.top();\n right.pop();\n\n ans += num;\n \n if(j >= 0){\n right.push(costs[j]);\n costs[j] = INT_MAX;\n j--;\n }\n }\n }\n\n return ans;\n }\n};",
"memory": "78655"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int> &costs, int k, int candidates)\n{\n int n = costs.size();\n long long ans = 0;\n\n if (((2 * candidates) + k - 1) >= n)\n {\n priority_queue<int, vector<int>, greater<int>> pq;\n for (int i = 0; i < n; i++)\n {\n pq.push(costs[i]);\n }\n\n while (k--)\n {\n ans += pq.top();\n pq.pop();\n }\n }\n else\n {\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n\n for (int i = 0; i < candidates; i++)\n {\n pq.push({costs[i], i});\n }\n\n int fr = candidates;\n\n for (int i = n - 1; i >= n - candidates; i--)\n {\n pq.push({costs[i], i});\n }\n\n int sec = n - candidates - 1;\n\n while (k--)\n {\n int x = pq.top().first;\n int index = pq.top().second;\n pq.pop();\n\n ans += x;\n if (index < fr)\n {\n pq.push({costs[fr], fr});\n fr++;\n }\n else\n {\n pq.push({costs[sec], sec});\n sec--;\n }\n }\n }\n\n return ans;\n}\n};",
"memory": "79165"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& c, int k, int ca) {\n vector<int> v(c.size(), 0);\n\n priority_queue<pair<int,int> , vector<pair<int,int>>, greater<pair<int,int>> > p1;\n priority_queue<pair<int,int> , vector<pair<int,int>>, greater<pair<int,int>> > p2;\n\n long long r=0;\n int i=0;\n int i1=0;\n int i2=c.size()-1;\n if(c.size()<2*ca) {\n sort(c.begin(), c.end());\n for(int i=0;i<c.size();i++) {\n r+=c[i];\n if(i==k-1)\n break;\n }\n return r;\n }\n\n while(i<ca) {\n v[i]=1;\n v[i2]=1;\n p1.push(make_pair(c[i], i));\n p2.push(make_pair(c[i2], i2));\n i++;\n i2--;\n if(p1.size()+p2.size()==c.size())\n break;\n \n }\n\n int l=0;\n vector<int> x;\n\n while(l<k) {\n if(p1.size() + p2.size()<2*ca) {\n while(!p1.empty()) {\n x.push_back(p1.top().first);\n p1.pop();\n } \n while(!p2.empty()) {\n x.push_back(p2.top().first);\n p2.pop();\n }\n\n sort(x.begin(), x.end());\n break;\n }\n if(p1.top().first<=p2.top().first) {\n r+=p1.top().first;\n \n p1.pop();\n while(i<c.size() && p1.size()<ca) {\n if(v[i]==0) {\n v[i]=1;\n p1.push(make_pair(c[i], i));\n \n }\n i++;\n }\n } else {\n r+=p2.top().first;\n \n p2.pop();\n while(i2>=0 && p2.size()<ca) {\n if(v[i2]==0) {\n v[i2]=1;\n p2.push(make_pair(c[i2], i2));\n \n }\n i2--;\n }\n }\n l++;\n }\n int o=0;\n while(l<k) {\n r+=x[o++];\n l++;\n }\n\n return r;\n }\n};",
"memory": "79165"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n typedef pair<int, int> ii;\n\n long long totalCost(vector<int>& costs, int k, int candidates) {\n long long result = 0;\n std::priority_queue<ii, std::vector<ii>, std::greater<ii>> pq;\n\n int left_index = candidates - 1;\n int right_index = costs.size() - candidates;\n\n if(right_index <= left_index) right_index = left_index + 1;\n\n for(int i = 0; i <= left_index; i++) pq.push(ii(costs[i], i));\n for(int i = costs.size()-1; i >= right_index; i--) pq.push(ii(costs[i], i));\n \n for(int selected = 0; selected < k; selected++){\n auto best = pq.top();\n pq.pop();\n result += best.first;\n\n if(best.second <= left_index){\n if(left_index+1 < right_index){\n ++left_index;\n pq.push(ii(costs[left_index], left_index));\n }\n } else {\n if(left_index+1 < right_index){\n --right_index;\n pq.push(ii(costs[right_index], right_index));\n }\n }\n }\n \n return result;\n }\n};",
"memory": "79675"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 2 | {
"code": "#define ll long long\n#define P pair<int, int>\nclass Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n int n = costs.size();\n if(n == 1) return costs[0];\n \n priority_queue<P, vector<P>, greater<P>> pq;\n int i = 0, j = n - 1;\n\n //initialize first set of workers\n for(; i < candidates && i < j; i++, j--){\n pq.push({costs[i], i});\n pq.push({costs[j], j});\n }\n\n //calculate the cost\n ll res = 0;\n while(k--){\n int cost = pq.top().first;\n int idx = pq.top().second;\n pq.pop();\n\n res += cost;\n if(i <= j){\n //there are still workers to be processed\n if(idx < i){\n pq.push({costs[i], i});\n i++;\n }\n else if(idx > j){\n pq.push({costs[j], j});\n j--;\n }\n }\n }\n return res;\n }\n};",
"memory": "79675"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\n\nprivate:\n\n pair<int, int>p1;\n pair<int, int>p2;\n\npublic:\n\n long long totalCost(vector<int>& costs, int k, int candidates) {\n int n = costs.size();\n long long ans = 0;\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>pq1;\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>pq2;\n for(int i = 0; i < candidates; i++) {\n if(costs[i] != -1) {\n pq1.push({costs[i], i});\n costs[i] = -1;\n }\n if(costs[n-1-i] != -1) {\n pq2.push({costs[n-i-1], n-i-1});\n costs[n-i-1] = -1;\n }\n }\n int y = candidates;\n int z = n - candidates - 1;\n for(int i = 0; i < k; i++) {\n if(!pq1.empty()) p1 = pq1.top();\n if(!pq2.empty()) p2 = pq2.top();\n\n if(pq1.size() == 0) {\n ans += p2.first;\n pq2.pop();\n continue;\n }\n else if(pq2.size() == 0) {\n ans += p1.first;\n pq1.pop();\n continue;\n }\n if(p1.first < p2.first) {\n ans += p1.first;\n pq1.pop();\n if(y < n && costs[y] != -1) {\n pq1.push({costs[y], y});\n costs[y] = -1;\n y++;\n }\n }\n else if(p1.first > p2.first) {\n ans += p2.first;\n pq2.pop();\n if(z >= 0 && costs[z] != -1) {\n pq2.push({costs[z], z});\n costs[z] = -1;\n z--;\n }\n }\n else {\n if(p1.second < p2.second) {\n ans += p1.first;\n pq1.pop();\n if(y < n && costs[y] != -1) {\n pq1.push({costs[y], y});\n costs[y] = -1;\n y++;\n }\n }\n else if(p1.second > p2.second) {\n ans += p2.first;\n pq2.pop();\n if(z >= 0 && costs[z] != -1) {\n pq2.push({costs[z], z});\n costs[z] = -1;\n z--;\n }\n }\n }\n }\n return ans;\n }\n};",
"memory": "80185"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n long long res=0;\n int n = costs.size();\n vector<int> hired(n,0);\n \n priority_queue<int,vector<int>,greater<int>> left,right;\n int l=0,r=n-1;\n for(;l<candidates;l++){\n left.push(costs[l]);\n hired[l]=1;\n }\n l--;\n \n for(;r>=n-candidates;r--){\n if(!hired[r]){\n right.push(costs[r]);\n hired[r]=1;\n }\n else\n break;\n }\n r++;\n\n cout<<endl<<left.size()<<\" \"<<right.size()<<\" \"<<endl;\n for(int i=0;i<k;i++){\n if(left.size() && (!right.size() || left.top()<=right.top())){\n res+=left.top();\n // cout<<left.top()<<\"l \";\n left.pop();\n ++l;\n if(l<n && !hired[l]){ \n left.push(costs[l]);\n hired[l]=1;\n }\n }\n else{\n res+=right.top();\n // cout<<right.top()<<\"r \"<<right.size()<<\" \";\n right.pop();\n r--;\n if(r>=0 && !hired[r]){\n right.push(costs[r]);\n hired[r]=1;\n // cout<<\"**\";\n }\n }\n }\n return res;\n }\n};",
"memory": "80185"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n\tpriority_queue<int, vector<int>, greater<int>> pqL, pqR;\n\n\tint i = 0, j = costs.size() - 1, temp = candidates;\n\tlong long ans = 0;\n\tdeque<int> dq;\n\tfor (int i = 0; i < costs.size(); i++)\n\t\tdq.push_back(costs[i]);\n\twhile (temp--)\n\t{\n\t\tif (!dq.empty())\n\t\t{\n\t\t\tpqL.push(dq.front());\n\t\t\tdq.pop_front();\n\t\t}\n\t\tif (!dq.empty())\n\t\t{\n\t\t\tpqR.push(dq.back());\n\t\t\tdq.pop_back();\n\t\t}\n\t}\n\n\twhile (k)\n\t{\n\t\tif (!pqL.empty())\n\t\t{\n\t\t\tif (!pqR.empty())\n\t\t\t{\n\t\t\t\tif (pqL.top() <= pqR.top())\n\t\t\t\t{\n\t\t\t\t\tans += pqL.top();\n\t\t\t\t\tpqL.pop();\n\t\t\t\t\tif (!dq.empty())\n\t\t\t\t\t{\n\t\t\t\t\t\tpqL.push(dq.front());\n\t\t\t\t\t\tdq.pop_front();\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tans += pqR.top();\n\t\t\t\t\tpqR.pop();\n\t\t\t\t\tif (!dq.empty())\n\t\t\t\t\t{\n\t\t\t\t\t\tpqR.push(dq.back());\n\t\t\t\t\t\tdq.pop_back();\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\telse\n\t\t\t{\n\t\t\t\tans += pqL.top();\n\t\t\t\tpqL.pop();\n\t\t\t\tif (!dq.empty())\n\t\t\t\t{\n\t\t\t\t\tpqL.push(dq.front());\n\t\t\t\t\tdq.pop_front();\n\t\t\t\t}\n\n\t\t\t}\n\t\t}\n\t\telse if (!pqR.empty()) {\n\t\t\tans += pqR.top();\n\t\t\tpqR.pop();\n\t\t\tif (!dq.empty())\n\t\t\t{\n\t\t\t\tpqR.push(dq.back());\n\t\t\t\tdq.pop_back();\n\t\t\t}\n\t\t}\n\t\tk--;\n\t}\n\n\n\treturn ans;\n}\n};",
"memory": "80695"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n if(2 * candidates >= costs.size()){\n priority_queue<long long> pq;\n for(auto x: costs){\n pq.push(-x);\n }\n\n long long ans = 0;\n for(long long i=0;i<k;i++){\n ans -= pq.top();\n pq.pop();\n }\n\n return ans;\n } else {\n priority_queue<long long> pq1;\n priority_queue<long long> pq2;\n for(long long i=0;i<candidates;i++){\n pq1.push(-costs[i]);\n pq2.push(-costs[costs.size()-1-i]);\n }\n\n long long flag = 0;\n long long ans = 0;\n long long left = candidates;\n long long right = costs.size()-1-candidates;\n for(long long i=0;i<k;i++){\n if(flag==1){\n // assume all the elements are merged to pq1;\n ans -= pq1.top();\n pq1.pop();\n } else {\n long long left_candi = -pq1.top();\n long long right_candi = -pq2.top();\n if(left_candi<=right_candi){\n ans += left_candi;\n pq1.pop();\n pq1.push(-costs[left]);\n left++;\n } else {\n ans += right_candi;\n pq2.pop();\n pq2.push(-costs[right]);\n right--;\n }\n\n if(left > right){\n flag = 1;\n while(!pq2.empty()){\n pq1.push(pq2.top());\n pq2.pop();\n }\n }\n }\n }\n\n return ans;\n }\n }\n};",
"memory": "80695"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n\n int n=costs.size();\n\n priority_queue<int,vector<int>,greater<int>>front_queue;\n priority_queue<int,vector<int>,greater<int>>back_queue;\n priority_queue<int,vector<int>,greater<int>>final_queue;\n\n int ptr1=0;\n int ptr2=n-1;\nlong long ans=0;\nint hired=0;\n while(ptr1<ptr2 &&hired<k){\n while(front_queue.size()<candidates &&ptr1<ptr2){\n front_queue.push(costs[ptr1]);\n ptr1++;\n }\n // if(ptr1>=ptr2){\n // break;\n // }\n while(back_queue.size()<candidates &&ptr2>ptr1){\n back_queue.push(costs[ptr2]);\n ptr2--;\n }\n // if(ptr1>=ptr2){\n // break;\n // }\n cout<<\" ptr1 \"<<ptr1<<\" ptr2 \"<< ptr2<< \"size\"<<back_queue.size()<<endl;\n if(!front_queue.empty() &&!back_queue.empty() &&front_queue.top()<=back_queue.top()){\n ans+=front_queue.top();\n cout<<front_queue.top()<<endl;\n front_queue.pop();\n hired+=1;\n }\n else{\n if(!front_queue.empty() &&!back_queue.empty() &&front_queue.top()>=back_queue.top()){\n ans+=back_queue.top();\n cout<<back_queue.top()<<endl;\n back_queue.pop();\n hired+=1;\n }}\n }\n while(ptr1<=ptr2){\n final_queue.push(costs[ptr1]);\n ptr1++;\n }\n while(!front_queue.empty()){\n final_queue.push(front_queue.top());\n front_queue.pop();\n }\n\n while(!back_queue.empty()){\n final_queue.push(back_queue.top());\n back_queue.pop();\n }\n while(hired<k ){\n ans+=final_queue.top();\n final_queue.pop();\n hired++;\n }\n\n \n return ans;\n\n\n \n }\n};",
"memory": "81205"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;\n int n = costs.size();\n vector<bool> isHired(n, false), inQ(n, false);\n for (int i = 0; i < candidates; i++) {\n q.push({costs[i], i});\n inQ[i] = true;\n }\n for (int i = n-1; i >= n-candidates; i--) {\n if (inQ[i]) {\n break;\n }\n q.push({costs[i], i});\n inQ[i] = true;\n }\n long long totalCost = 0;\n int left = candidates, right = n - candidates - 1;\n\n auto hireWorker = [&](int idx) {\n // cout << idx << ' ';\n isHired[idx] = true;\n totalCost += costs[idx];\n if (idx < left) {\n while (left < n && isHired[left]) {\n left++;\n }\n if (left < n) {\n q.push({costs[left], left});\n inQ[left] = true;\n }\n left++;\n }\n if (idx > right) {\n while (right >= 0 && isHired[right]) {\n right--;\n }\n if (right >= 0) {\n q.push({costs[right], right});\n inQ[right] = true;\n }\n right--;\n }\n inQ[idx] = false;\n };\n\n for (int i = 0; i < k; i++) {\n auto p = q.top();\n hireWorker(p.second);\n\n while (!q.empty() && isHired[q.top().second]) {\n q.pop();\n }\n }\n\n return totalCost;\n }\n};",
"memory": "81205"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n int n = costs.size();\n long long ans = 0;\n auto great = [&](int posa, int posb) -> bool {\n return costs[posa] > costs[posb];\n };\n priority_queue<int, vector<int>, decltype(great)> first(great);\n priority_queue<int, vector<int>, decltype(great)> second(great);\n vector<int> popped(n, 0);\n\n int s = 0, e = 0;\n for (int i = 0; i < candidates; i++) {\n first.push(i);\n }\n s = candidates;\n for (int i = n - 1; i >= n - candidates; i--) {\n second.push(i);\n }\n e = n - candidates - 1;\n\n while (k > 0) {\n while (!first.empty() && popped[first.top()]) {\n first.pop();\n }\n while (!second.empty() && popped[second.top()]) {\n second.pop();\n }\n if (!first.empty() && !second.empty()) {\n if (costs[first.top()] <= costs[second.top()]) {\n ans += costs[first.top()];\n popped[first.top()] = 1;\n first.pop();\n if (s < n)\n first.push(s++);\n } else {\n ans += costs[second.top()];\n popped[second.top()] = 1;\n second.pop();\n if (e > 0)\n second.push(e--);\n }\n } else if (!first.empty()) {\n ans += costs[first.top()];\n popped[first.top()] = 1;\n first.pop();\n } else if (!second.empty()) {\n ans += costs[second.top()];\n popped[second.top()] = 1;\n second.pop();\n }\n k--;\n }\n return ans;\n }\n};",
"memory": "81715"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n int n = costs.size();\n long long ans = 0;\n auto great = [&](int posa, int posb) -> bool {\n return costs[posa] > costs[posb];\n };\n priority_queue<int, vector<int>, decltype(great)> first(great);\n priority_queue<int, vector<int>, decltype(great)> second(great);\n vector<int> popped(n, 0);\n\n int s = 0, e = 0;\n for (int i = 0; i < candidates; i++) {\n first.push(i);\n }\n s = candidates;\n for (int i = n - 1; i >= n - candidates; i--) {\n second.push(i);\n }\n e = n - candidates - 1;\n\n while (k > 0) {\n while (!first.empty() && popped[first.top()]) {\n first.pop();\n }\n while (!second.empty() && popped[second.top()]) {\n second.pop();\n }\n if (!first.empty() && !second.empty()) {\n if (costs[first.top()] <= costs[second.top()]) {\n ans += costs[first.top()];\n popped[first.top()] = 1;\n first.pop();\n if (s < n)\n first.push(s++);\n } else {\n ans += costs[second.top()];\n popped[second.top()] = 1;\n second.pop();\n if (e > 0)\n second.push(e--);\n }\n } else if (!first.empty()) {\n ans += costs[first.top()];\n popped[first.top()] = 1;\n first.pop();\n } else if (!second.empty()) {\n ans += costs[second.top()];\n popped[second.top()] = 1;\n second.pop();\n }\n k--;\n }\n return ans;\n }\n};",
"memory": "81715"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "struct Node {\n int cost;\n int index;\n bool start;\n};\n\n\nstruct Comp {\n bool operator()(Node n1, Node n2) {\n if (n1.cost == n2.cost) {\n return n1.index > n2.index;\n }\n return n1.cost > n2.cost;\n }\n};\n\nclass Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n priority_queue<Node, vector<Node>, Comp> heap;\n int n = costs.size();\n // int first_index = 0, last_index = n-1;\n // unordered_set<int> st;\n int max_start = 0, min_end = n-1;\n for (int i=0;i<min(candidates, n);i++) {\n heap.push({costs[i], i, true});\n // st.insert(i);\n max_start = i;\n }\n\n for(int i=n-1;i>=n-candidates;i--) {\n if (i > max_start) {\n heap.push({costs[i], i, false});\n // st.insert(i);\n min_end = i;\n }\n }\n long long ans = 0;\n while(k--) {\n Node top = heap.top();\n heap.pop();\n ans += top.cost;\n if (top.start) {\n if (max_start + 1 < min_end) {\n heap.push({costs[max_start+1], max_start+1, top.start});\n // st.insert(max_start+1);\n max_start++;\n }\n } else {\n if (min_end - 1 > max_start) {\n heap.push({costs[min_end-1], min_end-1, top.start});\n // st.insert(min_end-1);\n min_end--;\n }\n }\n }\n\n return ans;\n }\n};",
"memory": "82225"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int c) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n int ll=0;\n int lh=c-1;\n int rh=costs.size()-1; \n int rl= rh-c+1;\n if(lh>=rl){\n for(int i=0;i<costs.size();i++){\n pq.push({costs[i],i});\n }\n }\n else{\n for(int i=ll;i<=lh;i++){\n pq.push({costs[i],i});\n }\n for(int i=rl;i<=rh;i++){\n pq.push({costs[i],i});\n }\n }\n long long sum=0;\n vector<int>visited(costs.size(),-1);\n while(k>0){\n if(lh>=rl) break;\n int indx = pq.top().second;\n int ele = pq.top().first;\n visited[indx]=1;\n sum+=ele;\n pq.pop();\n if(ll<=indx && indx<=lh){\n if(indx==ll){\n while(visited[ll]==1) ll++;\n }\n lh++;\n if(lh<rl) pq.push({costs[lh],lh});\n }\n else if(rl<=indx && indx<=rh){\n if(indx==rh){\n while(visited[rh]==1) rh--;\n }\n rl--;\n if(lh<rl) pq.push({costs[rl],rl});\n }\n k--;\n }\n while(k){\n sum+=pq.top().first;\n pq.pop();\n k--;\n \n }\n return sum;\n }\n};",
"memory": "82735"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int c) {\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n int ll=0;\n int lh=c-1;\n int rh=costs.size()-1; \n int rl= rh-c+1;\n if(lh>=rl){\n for(int i=0;i<costs.size();i++){\n pq.push({costs[i],i});\n }\n }\n else{\n for(int i=ll;i<=lh;i++){\n pq.push({costs[i],i});\n }\n for(int i=rl;i<=rh;i++){\n pq.push({costs[i],i});\n }\n }\n long long sum=0;\n cout<<ll<<\" \"<<lh<<\" \"<<rl<<\" \"<<rh<<endl;\n vector<int>visited(costs.size(),-1);\n while(k>0){\n if(lh>=rl) break;\n int indx = pq.top().second;\n int ele = pq.top().first;\n visited[indx]=1;\n sum+=ele;\n cout<<ele<<endl;\n\n pq.pop();\n if(ll<=indx && indx<=lh){\n if(indx==ll){\n while(visited[ll]==1) ll++;\n }\n lh++;\n if(lh<rl) pq.push({costs[lh],lh});\n }\n else if(rl<=indx && indx<=rh){\n if(indx==rh){\n while(visited[rh]==1) rh--;\n }\n rl--;\n if(lh<rl) pq.push({costs[rl],rl});\n }\n cout<<ll<<\" \"<<lh<<\" \"<<rl<<\" \"<<rh<<endl;\n k--;\n }\n while(k){\n sum+=pq.top().first;\n pq.pop();\n k--;\n \n }\n return sum;\n }\n};",
"memory": "82735"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int c) {\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n int ll=0;\n int lh=c-1;\n int rh=costs.size()-1; \n int rl= rh-c+1;\n if(lh>=rl){\n for(int i=0;i<costs.size();i++){\n pq.push({costs[i],i});\n }\n }\n else{\n for(int i=ll;i<=lh;i++){\n pq.push({costs[i],i});\n }\n for(int i=rl;i<=rh;i++){\n pq.push({costs[i],i});\n }\n }\n long long sum=0;\n vector<int>visited(costs.size(),-1);\n while(k>0){\n if(lh>=rl) break;\n int indx = pq.top().second;\n int ele = pq.top().first;\n visited[indx]=1;\n sum+=ele;\n pq.pop();\n if(ll<=indx && indx<=lh){\n if(indx==ll){\n while(visited[ll]==1) ll++;\n }\n lh++;\n if(lh<rl) pq.push({costs[lh],lh});\n }\n else if(rl<=indx && indx<=rh){\n if(indx==rh){\n while(visited[rh]==1) rh--;\n }\n rl--;\n if(lh<rl) pq.push({costs[rl],rl});\n }\n k--;\n }\n while(k){\n sum+=pq.top().first;\n pq.pop();\n k--;\n \n }\n return sum;\n }\n};",
"memory": "83245"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq1, pq2;\n long long ans = 0;\n int n = costs.size();\n candidates = min(candidates, n);\n int l = 0, r = 0;\n\n for(int i = 0; i < candidates; i++) {\n pq1.push(make_pair(costs[i], i));\n l = i;\n pq2.push(make_pair(costs[n - i - 1], n - i - 1));\n r = n - i - 1;\n }\n\n // while(!pq1.empty()) {\n // cout << pq1.top().first << \" \" << pq1.top().second << endl;\n // pq1.pop();\n // }\n // cout << endl;\n // while(!pq2.empty()) {\n // cout << pq2.top().first << \" \" << pq2.top().second << endl;\n // pq2.pop();\n // }\n // cout << endl;\n // for(int i = 0; i < candidates; i++) {\n // pq1.push(make_pair(costs[i], i));\n // l = i;\n // pq2.push(make_pair(costs[n - i - 1], n - i - 1));\n // r = n - i - 1;\n // }\n\n for(int i = 0; i < k; i++) {\n // cout << \"Looking at \" << pq1.top().first <<\",\" << pq1.top().second;\n // cout << \" and \" << pq2.top().first << \",\" << pq2.top().second << endl;\n \n\n if(pq2.size() == 0 || (pq1.size() > 0 && pq1.top().first < pq2.top().first)) {\n ans += pq1.top().first;\n // cout << pq1.top().first << \" \"<< pq1.top().second;\n pq1.pop();\n // cout << endl << \"Size \"<< pq1.size() << endl;\n\n l++;\n if(l < n && l < r)\n pq1.push(make_pair(costs[l], l));\n }\n else if(pq1.size() > 0 && pq2.size() > 0 && pq1.top().first == pq2.top().first) {\n ans += pq1.top().first;\n // cout << pq1.top().first << \" \";\n if(pq1.top().second < pq2.top().second) {\n // cout<< pq1.top().second;\n \n pq1.pop();\n l++;\n if(l < n && l < r)\n pq1.push(make_pair(costs[l], l));\n }\n else if(pq1.top().second == pq2.top().second) {\n // cout<< pq1.top().second;\n \n pq2.pop();\n pq1.pop();\n l++;\n r--;\n }\n else {\n // cout<< pq2.top().second;\n pq2.pop();\n r--;\n if(r >= 0 && r > l)\n pq2.push(make_pair(costs[r], r));\n }\n }\n else if(pq1.size() == 0 || (pq2.size() > 0 && pq1.top().first > pq2.top().first)) {\n ans += pq2.top().first;\n // cout << pq2.top().first << \" \" << pq2.top().second;\n pq2.pop();\n r--;\n if(r >= 0 && r > l)\n pq2.push(make_pair(costs[r], r));\n }\n // cout << endl;\n }\n return ans;\n }\n};",
"memory": "83245"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "typedef long long ll;\ntypedef pair<int,int> pi;\nclass Solution {\npublic:\n ll totalCost(vector<int>& costs, int k, int candidates) {\n int n = costs.size();\n \n priority_queue<pi,vector<pi>,greater<pi>> leftPQ, rightPQ;\n int leftIndex = 0;\n int rightIndex = n-1;\n vector<bool> hired(n);\n \n ll totalCost = 0;\n for(int i=0;i<k;i++) {\n while(leftPQ.size() < candidates && leftIndex < n) {\n if(!hired[leftIndex]) {\n leftPQ.push({costs[leftIndex], leftIndex});\n }\n leftIndex++;\n }\n while(rightPQ.size() < candidates && rightIndex >= 0) {\n if(!hired[rightIndex]) {\n rightPQ.push({costs[rightIndex], rightIndex});\n }\n rightIndex--;\n }\n \n pi leftCandidate = leftPQ.top();\n pi rightCandidate = rightPQ.top();\n if(leftCandidate.first != rightCandidate.first) {\n if(leftCandidate.first < rightCandidate.first) {\n totalCost += leftCandidate.first;\n hired[leftCandidate.second] = true;\n leftPQ.pop();\n } else {\n totalCost += rightCandidate.first;\n hired[rightCandidate.second] = true;\n rightPQ.pop();\n }\n } else {\n totalCost += leftCandidate.first;\n hired[leftCandidate.second] = true;\n leftPQ.pop();\n if(rightCandidate.second == leftCandidate.second) {\n rightPQ.pop();\n }\n }\n }\n \n return totalCost;\n }\n};",
"memory": "83755"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "#define P pair<int,int>\nclass Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidate) {\n priority_queue<P,vector<P>,greater<P>> pq,st;\n long long res = 0;\n int n = costs.size();\n for(int i = 0; i < candidate; i++){\n pq.push({costs[i],i});\n st.push({costs[n-i-1],n-i-1});\n }\n int a = candidate, b = n - candidate - 1;\n vector<bool> vis(n,false);\n while(k--){\n while(!pq.empty() && vis[pq.top().second]){ pq.pop(); if(a<n) pq.push({costs[a],a++});}\n while(!st.empty() && vis[st.top().second]){ st.pop(); if(b>=0) st.push({costs[b],b--});}\n if(st.top().first < pq.top().first || (st.top().first == pq.top().first && st.top().second < pq.top().second)){\n res+=st.top().first;\n cout<<st.top().first<<endl;\n vis[st.top().second] = true;\n st.pop();\n if(b>=0) st.push({costs[b],b--});\n }else{\n res+=pq.top().first;\n cout<<pq.top().first<<\"-\"<<endl;\n vis[pq.top().second] = true;\n pq.pop();\n if(a<n) pq.push({costs[a],a++});\n }\n }\n return res;\n }\n};",
"memory": "83755"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n auto pairGreater = [](const pair<int, int> &p1, const pair<int, int> &p2) {\n if (p1.first == p2.first) return p1.second > p2.second;\n return p1.first > p2.first;\n };\n priority_queue<pair<int, int>, \n vector<pair<int, int>>, \n decltype(pairGreater)> lpq, rpq;\n const int size = costs.size();\n const int last = size - 1;\n for (int i = 0; i < candidates; ++i) {\n lpq.push({costs[i], i});\n rpq.push({costs[last - i], last - i});\n }\n auto hired = make_unique<char[]>(size);\n // add new elem to left maxheap\n auto addLeft = [&](int &lpos) {\n while (lpos < size) {\n if (!hired[lpos]) {\n lpq.push({costs[lpos], lpos});\n ++lpos;\n return;\n } \n ++lpos;\n }\n };\n // add new elem to right maxheap\n auto addRight = [&](int &rpos) {\n while (rpos >= 0) {\n if (!hired[rpos]) {\n rpq.push({costs[rpos], rpos});\n --rpos;\n return;\n }\n --rpos;\n }\n };\n\n long long cost{0};\n int lpos{candidates}, rpos{last - candidates};\n while (k--) {\n auto [l, il] = lpq.top();\n auto [r, ir] = rpq.top();\n\n if (l <= r) {\n hired[il] = 1;\n cost += l;\n lpq.pop();\n addLeft(lpos);\n if (il > rpos) {\n rpq.pop();\n addRight(rpos);\n }\n } else {\n hired[ir] = 1;\n cost += r;\n rpq.pop();\n addRight(rpos);\n if (ir < lpos) {\n lpq.pop();\n addLeft(lpos);\n }\n }\n }\n return cost;\n }\n};",
"memory": "84265"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n using pp=pair<int,int>;\n long long totalCost(vector<int>& costs, int k, int candidates) {\n int i,j,n=costs.size();\n priority_queue<int,vector<int>,greater<int>> pqL;\n priority_queue<pp,vector<pp>,greater<pp>> pqR;\n\n for(i=0;i<candidates;i++) pqL.push(costs[i]);\n for(j=n-candidates;j<n;j++) pqR.push({costs[j],j});\n\n long long ans=0;\n i=candidates,j=n-candidates-1;\n\n unordered_set<int> st;\n while(k>0 && i<=j){\n if(pqL.top()<=pqR.top().first){\n ans+=pqL.top();\n pqL.pop();\n\n pqL.push(costs[i]);\n i++;\n } else{\n ans+=pqR.top().first;\n st.insert(pqR.top().second);\n pqR.pop();\n\n pqR.push({costs[j],j});\n j--;\n }\n\n k--;\n }\n\n for(;i<n;i++){\n if(!st.count(i)) pqL.push(costs[i]);\n }\n\n while(k>0){\n ans+=pqL.top();\n pqL.pop();\n k--;\n }\n\n return ans;\n }\n};",
"memory": "84265"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n using pp=pair<int,int>;\n long long totalCost(vector<int>& costs, int k, int candidates) {\n int i,j,n=costs.size();\n priority_queue<int,vector<int>,greater<int>> pqL;\n priority_queue<pp,vector<pp>,greater<pp>> pqR;\n\n for(i=0;i<candidates;i++) pqL.push(costs[i]);\n for(j=n-candidates;j<n;j++) pqR.push({costs[j],j});\n\n long long ans=0;\n i=candidates,j=n-candidates-1;\n\n unordered_set<int> st;\n while(k>0 && i<=j){\n if(pqL.top()<=pqR.top().first){\n ans+=pqL.top();\n pqL.pop();\n\n pqL.push(costs[i]);\n i++;\n } else{\n ans+=pqR.top().first;\n st.insert(pqR.top().second);\n pqR.pop();\n\n pqR.push({costs[j],j});\n j--;\n }\n\n k--;\n }\n\n for(;i<n;i++){\n if(!st.count(i)) pqL.push(costs[i]);\n }\n\n while(k>0){\n ans+=pqL.top();\n pqL.pop();\n k--;\n }\n\n return ans;\n }\n};",
"memory": "84775"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "#include <vector>\n#include <queue>\nusing namespace std;\n\ntypedef pair<long long, long long> pii;\n\nclass Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n ios_base::sync_with_stdio(false);\n priority_queue<pii, vector<pii>, greater<pii>> pq;\n int n = costs.size();\n int l = 0, r = n - 1;\n long long sum = 0;\n\n // Add the initial candidates from both ends\n for (int i = 0; i < candidates; i++) {\n if (l <= r) pq.push({costs[l], l++});\n if (l <= r) pq.push({costs[r], r--});\n }\n\n // Pick the smallest cost elements k times\n for (int i = 0; i < k; i++) {\n pii top = pq.top();\n pq.pop();\n sum += top.first;\n\n // Add the next candidate from either end\n if (l <= r) {\n if (top.second < l) pq.push({costs[l], l++});\n else pq.push({costs[r], r--});\n }\n }\n\n return sum;\n }\n};\n",
"memory": "85285"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "#include <vector>\n#include <queue>\nusing namespace std;\n\ntypedef pair<long long, long long> pii;\n\nclass Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n priority_queue<pii, vector<pii>, greater<pii>> pq;\n int n = costs.size();\n int l = 0, r = n - 1;\n long long sum = 0;\n\n // Add the initial candidates from both ends\n for (int i = 0; i < candidates; i++) {\n if (l <= r) pq.push({costs[l], l++});\n if (l <= r) pq.push({costs[r], r--});\n }\n\n // Pick the smallest cost elements k times\n for (int i = 0; i < k; i++) {\n pii top = pq.top();\n pq.pop();\n sum += top.first;\n\n // Add the next candidate from either end\n if (l <= r) {\n if (top.second < l) pq.push({costs[l], l++});\n else pq.push({costs[r], r--});\n }\n }\n\n return sum;\n }\n};\n",
"memory": "85795"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "typedef pair<long long, long long> pii;\nclass Solution {\n\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n priority_queue<pii,vector<pii>,greater<pii>>pq;\n int n=costs.size();\n int l=0;\n int r=n-1;\n for(int i=0;i<candidates;i++){\n if(l<=r){\n pq.push({costs[l],l});\n l++;\n }\n if(l<=r){\n pq.push({costs[r],r});\n r--;\n }\n }\n long long sum=0;\n for(int i=0;i<k;i++){\n pii x=pq.top();\n pq.pop();\n sum+=x.first;\n\n if(l<=r){\n if(x.second<l){\n pq.push({costs[l],l++});\n }\n else{\n pq.push({costs[r],r--});\n }\n }\n }\n return sum;\n }\n};",
"memory": "85795"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "#include <format>\n\nclass Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n \n struct Cand {\n int cost;\n int ix;\n int isLeft;\n\n bool operator<(Cand const& other) const {\n return cost > other.cost || (cost==other.cost && ix > other.ix);\n }\n };\n\n class CandIter {\n public:\n CandIter(int nbCands)\n : leftIx{0}, rightIx{nbCands - 1} { }\n bool empty() const {\n return leftIx > rightIx;\n }\n int next(bool isLeft) {\n return isLeft ? leftIx++ : rightIx--;\n }\n \n private:\n int leftIx;\n int rightIx;\n };\n\n priority_queue<Cand, vector<Cand>> cands;\n\n CandIter candIter(size(costs));\n for (bool isLeft : {true, false}) {\n for (int i = 0; i < candidates && !empty(candIter); ++i) {\n auto ix = candIter.next(isLeft);\n cands.emplace(costs[ix], ix, isLeft);\n } \n }\n \n long long totalCost = 0;\n while (k > 0 && !empty(cands)) {\n auto cand = std::move(cands.top());\n cands.pop();\n totalCost += cand.cost;\n\n cout << format(\"{} {}\\n\", cand.cost, cand.ix);\n\n if (!candIter.empty()) {\n int ix = candIter.next(cand.isLeft);\n cands.emplace(costs[ix], ix, cand.isLeft);\n }\n \n k--;\n }\n\n return totalCost;\n }\n};",
"memory": "86305"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n long long cost = 0;\n priority_queue<pair<int, int>, \n vector<pair<int, int>>, greater<pair<int, int>>> minHeapLeft, minHeapRight;\n for (int i = 0; i < candidates; i++) {\n minHeapLeft.push(make_pair(costs[i], i));\n minHeapRight.push(make_pair(costs[costs.size()-i-1], costs.size()-i-1));\n }\n vector<int> taken(costs.size(), 0);\n long long i = 0, l = candidates, r = costs.size()-candidates-1;\n while (i < k) {\n pair<long long, int> elem;\n if (minHeapLeft.top().first <= minHeapRight.top().first) {\n elem = minHeapLeft.top();\n minHeapLeft.pop();\n if (l < costs.size()) {\n minHeapLeft.push(make_pair(costs[l], l));\n l++;\n }\n } else {\n elem = minHeapRight.top();\n minHeapRight.pop();\n if (r >= 0) {\n minHeapRight.push(make_pair(costs[r], r));\n r--;\n }\n }\n if (taken[elem.second] == 0) {\n taken[elem.second] = 1;\n cost += elem.first;\n i++;\n }\n }\n return cost;\n }\n};",
"memory": "86305"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n priority_queue<pair<long long,long long>,vector<pair<long long,long long>>,greater<pair<long long,long long>>>q1,q2;\n int n = costs.size();\n int itr1=0, itr2=n-1;\n for(int i=0;i<candidates;i++){\n if(itr1<n){\n q1.push({costs[itr1],itr1});\n itr1++;\n }\n else break;\n }\n\n for(int i=0;i<candidates;i++){\n if(itr2>=itr1){\n q2.push({costs[itr2],itr2});\n itr2--;\n }\n else break;\n }\n\n long long count=0;\n while(k && q1.size()>0 && q2.size()>0){\n auto top1 = q1.top();\n q1.pop();\n auto top2 = q2.top();\n q2.pop();\n\n // cout<<top1.first<<\" \"<<top1.second<<\" \"<<top2.first<<\" \"<<top2.second<<'\\n';\n // cout<<itr1<<\" \"<<itr2<<'\\n';\n if(top2.first<top1.first){\n q1.push(top1);\n count+=top2.first;\n if(itr2>=itr1){\n q2.push({costs[itr2],itr2});\n itr2--;\n }\n }\n else{\n q2.push(top2);\n count+=top1.first;\n if(itr2>=itr1){\n // cout<<costs[itr1]<<\" \"<<itr1<<'\\n';\n q1.push({costs[itr1],itr1});\n itr1++;\n }\n }\n k--;\n }\n\n while(k && q1.size()>0){\n auto top1 = q1.top();\n q1.pop();\n\n count+=top1.first;\n if(itr2>=itr1){\n q1.push({costs[itr1],itr1});\n itr1++;\n }\n k--;\n }\n\n while(k && q2.size()>0){\n auto top1 = q2.top();\n q2.pop();\n\n count+=top1.first;\n if(itr2>=itr1){\n q2.push({costs[itr2],itr2});\n itr2--;\n }\n k--;\n }\n\n return count;\n }\n};",
"memory": "86815"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n#define ll long long\n#define p pair<ll,ll>\n long long totalCost(vector<int>& costs, int k, int candidates) {\n priority_queue<p,vector<p>,greater<p>> start,last;\n int n=costs.size();\n int i=0,j=n-1;\n ll tot=0;\n while(k--){\n while(start.size()<candidates && i<=j){\n start.push({costs[i],i});\n i++;\n }\n while(last.size()<candidates && j>=i){\n last.push({costs[j],j});\n j--;\n }\n if(start.empty() || (!last.empty() && last.top().first<start.top().first)){\n tot+=last.top().first;\n last.pop();\n }\n else{\n tot+=start.top().first;\n start.pop();\n }\n }\n return tot;\n }\n};",
"memory": "86815"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Compare{\n public:\n bool operator()(const pair<int,int> &x, const pair<int,int> &y)\n {\n return x.first > y.first;\n }\n};\n\n\nclass Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n int n = costs.size();\n int index_i = 0;\n int index_j = 0;\n priority_queue<pair<int,int>,vector<pair<int,int>>,Compare> pq;\n priority_queue<pair<int,int>,vector<pair<int,int>>,Compare> pq2;\n \n for(int i=0;i<costs.size();i++)\n {\n if(pq.size() == candidates) break;\n pq.push({costs[i],i});\n index_i = i;\n }\n for(int i=costs.size()-1;i>=0;i--)\n {\n if(pq2.size() == candidates) break;\n pq2.push({costs[i],i});\n index_j = i;\n }\n \n \n // cout << pq.size() << \" \" << pq.top();\n\n vector<bool> visited(n+10,false);\n vector<int> v;\n long long sum = 0;\n while(true)\n {\n if(v.size() == k) break;\n if(pq.size() == 0 && pq2.size() == 0) break;\n\n if(pq.size() > 0 && pq2.size() > 0)\n {\n int x = pq.top().first;\n int y = pq2.top().first;\n \n if(x <= y)\n {\n if(visited[pq.top().second] == false)\n {\n v.push_back(x);\n visited[pq.top().second] = true;\n sum += x;\n pq.pop();\n if(index_i+1 < costs.size())\n {\n index_i++;\n pq.push({costs[index_i],index_i});\n }\n\n }\n \n else{\n pq.pop();\n \n }\n }\n else{\n if(visited[pq2.top().second] == false)\n {\n v.push_back(y);\n visited[pq2.top().second] = true;\n sum += y;\n pq2.pop();\n if(index_j-1 >= 0)\n {\n index_j--;\n pq2.push({costs[index_j],index_j});\n }\n }\n \n else {\n \n pq2.pop();\n }\n\n\n }\n\n\n\n }\n else if(pq.size() > 0)\n {\n int x = pq.top().first;\n if(visited[pq.top().second] == false)\n {\n v.push_back(x);\n visited[pq.top().second] = true;\n sum += x;\n pq.pop();\n if(index_i+1 < costs.size())\n {\n index_i++;\n pq.push({costs[index_i],index_i});\n }\n\n }\n else pq.pop();\n \n }\n else if(pq2.size() > 0)\n {\n int y = pq2.top().first;\n if(visited[pq2.top().second] == false)\n {\n v.push_back(y);\n visited[pq2.top().second] = true;\n sum += y;\n pq2.pop();\n if(index_j-1 >= 0)\n {\n index_j--;\n pq2.push({costs[index_j],index_j});\n }\n }\n else pq2.pop();\n\n }\n\n\n\n }\n return sum;\n \n\n\n\n\n\n\n\n }\n};",
"memory": "87325"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n struct Tup {\n int cost;\n size_t idx;\n bool left;\n\n bool operator<(const Tup& other) const {\n if (cost == other.cost) {\n return idx > other.idx;\n }\n return cost > other.cost;\n }\n };\n\n long long totalCost(vector<int>& costs, int k, int candidates) {\n long long total_cost = 0;\n priority_queue<Tup, vector<Tup>> pq{};\n int left = 0;\n int right = costs.size() - 1;\n for (size_t i=0; i < candidates && left <= right; i++) {\n pq.push({costs[i], i, true});\n if (left != right) {\n pq.push({costs[costs.size() - 1 - i], costs.size() - 1 - i, false});\n }\n left++;\n right--;\n }\n while (k--) {\n if (pq.empty()) {\n break;\n }\n auto [cost, idx, is_left] = pq.top();\n pq.pop();\n total_cost += cost;\n if (left <= right) {\n if (is_left) {\n pq.push({costs[left], static_cast<size_t>(left), true});\n left++;\n } else {\n pq.push({costs[right], static_cast<size_t>(right), false});\n right--;\n }\n }\n }\n return total_cost;\n }\n};",
"memory": "87835"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "// long long ans = 0;\n// priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> left;\n// priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> right;\n \n// int n = costs.size();\n \n// for(int i=0;i<n;i++){\n// left.push({costs[i],i});\n// right.push({costs[n-i-1],n-i-1});\n// }\n\n// if(left.top().first>right.top().first){\n\n// }\n// else if(left.top().first<right.top().first){\n\n// }\n// else{\n \n// }\n\n// cout<<q.top().second;\n\nclass Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n\n long long ans = 0;\n priority_queue<int,vector<int>,greater<int>> left;\n priority_queue<int,vector<int>,greater<int>> right;\n unordered_map<int,bool> m;\n \n int n = costs.size();\n\n int currleft = 0;\n int currright = n-1;\n\n \n for(int i=0;i<candidates;i++){\n left.push(costs[currleft++]); \n }\n\n for(int i=0;i<candidates;i++){\n if(currleft<=currright)\n right.push(costs[currright--]); \n }\n\n \n\n\n \n \n\n for(int i=0;i<k;i++){\n if(left.empty()){\n left.push(INT_MAX);\n }\n\n if(right.empty()){\n right.push(INT_MAX);\n }\n\n\n\n if(left.top()>right.top()){\n ans += right.top();\n right.pop();\n if(currleft<=currright){\n right.push(costs[currright]);\n m[currright] = 1;\n currright -= 1;\n } \n\n cout<<\"aa\"<<endl; \n }\n else{\n ans += left.top();\n left.pop();\n if(currleft<=currright){\n left.push(costs[currleft]);\n m[currleft] = 1;\n currleft += 1;\n }\n }\n\n cout<<ans<<endl;\n\n } \n\n return ans;\n \n }\n};",
"memory": "88345"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& arr, int k, int c) {\n // create two pqs\n long long ans = 0;\n priority_queue <int, vector<int>, greater<int>> q1, q2;\n int n = arr.size(); int l=c-1; int r = n-c;\n unordered_set<int> q;\n for(int i=0;i<c;i++) {\n if(q.count(i)==0)\n { q1.push(arr[i]);\n q.insert(i);\n }\n if (q.count(n-i-1)==0) {\n q.insert(n-i-1);\n q2.push(arr[n-1-i]);\n }\n };\n int i = 0;\n cout<<\"l--\" << l << \"r--\" << r<<\"\\n\";\n while(l<r && i<k) {\n i++;\n if(q1.top()<=q2.top()) {\n cout<<q1.top()<<\"18\"<<\"\\n\";\n ans += q1.top(); q1.pop();\n if(l+1<r) \n q1.push(arr[++l]);\n else\n l++;\n }\n else {\n cout<<q2.top()<<\"26\"<<\"\\n\";\n ans += q2.top(); q2.pop();\n if(r-1>l) \n q2.push(arr[--r]);\n else\n r--;\n }\n }\n while(i<k) {\n if(q1.empty()){\n cout<<q2.top()<<\"\\n\";\n ans += q2.top(); q2.pop();}\n else if(q2.empty()){\n cout<<q1.top()<<\"\\n\";\n ans += q1.top(); q1.pop();}\n else {\n if(q1.top()<=q2.top()) \n { \n cout<<q1.top()<<\"\\n\";\n ans += q1.top(); q1.pop();}\n else {\n cout<<q2.top()<<\"\\n\";\n ans += q2.top(); q2.pop();\n }\n }\n i++;\n }\n return ans;\n\n // if costs.size() <= 2*k\n }\n};",
"memory": "88855"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n long long res = 0;\n priority_queue<int,vector<int>,greater<int> > pq1;\n priority_queue<int,vector<int>,greater<int> > pq2;\n map<int,int> m;\n int a = -1;\n for (int i=0;i<candidates;i++)\n {\n m[i];\n pq1.push(costs[i]);\n a++;\n }\n a++;\n int b = costs.size();\n cout << costs.size()-candidates << endl;\n for (int i=max(a,(int)costs.size()-candidates);i<costs.size();i++)\n {\n if (m.find(i) == m.end())\n {\n m[i];\n pq2.push(costs[i]);\n b--;\n }\n }\n b--;\n // cout <\n for (int i=0;i<k;i++)\n {\n long long t1=1e9,t2 = 1e9;\n if (pq1.size() > 0)\n {\n t1 = pq1.top();\n }\n if (pq2.size() > 0)\n {\n t2 = pq2.top();\n }\n if (t1 > t2)\n {\n res += t2;\n pq2.pop();\n if (a <= b)\n {\n pq2.push(costs[b]);\n }\n b--;\n }\n else\n {\n res += t1;\n pq1.pop();\n if (a <= b)\n {\n pq1.push(costs[a]);\n }\n a++;\n }\n }\n return res;\n }\n};",
"memory": "89365"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "struct CompareFn {\n bool operator()(pair<int, int> const& p1, pair<int, int> const& p2) {\n if (p1.first > p2.first) {\n return true;\n } else if (p1.first == p2.first) {\n return p1.second > p2.second;\n }\n return false;\n }\n};\n\nclass Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n long long ans = 0;\n int l = 0 + candidates - 1, r = costs.size() - candidates;\n // if (l > costs.size() - 1) {\n // l = costs.size() - 1;\n // }\n // if (r < 0) {\n // r = 0;\n // }\n priority_queue<pair<int, int>, vector<pair<int, int>>, CompareFn> pq;\n // put the left and right band into pq first\n unordered_set<int> seen;\n int n = min(candidates, (int)costs.size());\n for (int i = 0; i < n; i++) {\n if (seen.find(i) == seen.end()) {\n pq.push({costs[i], i});\n seen.insert(i);\n }\n }\n n = max(0, (int)costs.size() - candidates);\n for (int i = costs.size() - 1; i >= n; i--) {\n if (seen.find(i) == seen.end()) {\n pq.push({costs[i], i});\n seen.insert(i);\n }\n }\n while (k > 0) {\n if (l < r) {\n // case: left and right band have at least candi amount of workers\n // ex: [17,12,10,2], 7, [2,11,20,8]\n pair<int, int> node = pq.top();\n int cost = node.first;\n int i = node.second;\n pq.pop();\n ans += cost;\n // check which band this node belongs to\n if (i <= l) {\n // belong to left band\n l++;\n if (l < r) {\n // avoid pushing dup to pq\n pq.push({costs[l], l});\n }\n }\n if (i >= r) {\n // belong to right band\n r--;\n if (r > l) {\n pq.push({costs[r], r});\n }\n }\n } else {\n // case: fewer than candi remaining on each band\n ans += pq.top().first;\n pq.pop();\n }\n k--;\n }\n return ans;\n }\n};",
"memory": "89875"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n long long ans = 0;\n unordered_map<int, int> mp;\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n\n int left = 0, right = costs.size()-1;\n for(int j=0; j<costs.size() && j<candidates; ++j){\n if(mp[j] == 0){\n pq.push({costs[j], j});\n left++;\n mp[j]=1;\n }\n } \n int cnt = 0;\n for(int j=costs.size()-1; j>=0 && cnt < candidates; --j){ \n if(mp[j] == 0){\n pq.push({costs[j], j});\n right--;\n mp[j]=1;\n }\n cnt++;\n }\n \n for(int i=0; i<k; ++i){\n ans += pq.top().first;\n int index = pq.top().second;\n pq.pop();\n\n\n if(index < left && left <= right){\n pq.push({costs[left], left});\n left++;\n }\n else if(index > right && left <= right){\n pq.push({costs[right], right});\n right--;\n }\n\n }\n\n return ans;\n }\n};",
"memory": "90385"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n multiset<int> left,right;\n int n = costs.size();\n int lefti = candidates-1;\n int righti = n-candidates;\n long long ans = 0;\n if(righti-lefti<=1){\n sort(costs.begin(),costs.end());\n for(int i = 0;i<min(n,k);i++) ans+=costs[i];\n return ans;\n }\n for(int i = 0;i<candidates;i++){\n left.insert(costs[i]);\n right.insert(costs[n-1-i]);\n }\n while(k && righti-lefti>1){\n int a = *(left.begin());\n int b = *(right.begin());\n if(a<=b){\n ans+=a;\n lefti++;\n left.erase(left.find(a));\n left.insert(costs[lefti]);\n }\n else{\n ans+=b;\n righti--;\n right.erase(right.find(b));\n right.insert(costs[righti]);\n }\n k--;\n // cout<<ans<<\" \";\n }\n if(k==0) return ans;\n multiset<int> f;\n for(auto x:left) f.insert(x);\n for(auto x:right) f.insert(x);\n while(k && f.size()>0){\n int a = *(f.begin());\n ans+=a;\n f.erase(f.find(a));\n k--;\n cout<<ans<<\" \";\n }\n return ans;\n }\n};",
"memory": "92935"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n int n = costs.size();\n priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> pq;\n \n unordered_set<int> processedIndices;\n\n for(int i = 0; i < candidates; i++) {\n pq.push({costs[i], i});\n processedIndices.insert(i);\n }\n \n for(int i = n - 1; i >= n - candidates && i >= 0; i--) {\n if(processedIndices.count(i)) continue; \n pq.push({costs[i], i});\n processedIndices.insert(i);\n }\n\n int front = candidates;\n int back = n - candidates - 1;\n long long ans = 0;\n\n while(k--) {\n auto [cost, index] = pq.top();\n pq.pop();\n ans += cost;\n\n if (index <= front && front <= back) {\n pq.push({costs[front], front});\n front++;\n }\n else if (index >= back && back >= front) {\n pq.push({costs[back], back});\n back--;\n }\n }\n\n return ans;\n }\n};\n",
"memory": "93445"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n int n = costs.size();\n priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> pq;\n \n unordered_set<int> processedIndices;\n\n for(int i = 0; i < candidates; i++) {\n pq.push({costs[i], i});\n processedIndices.insert(i);\n }\n \n for(int i = n - 1; i >= n - candidates && i >= 0; i--) {\n if(processedIndices.count(i)) continue; \n pq.push({costs[i], i});\n processedIndices.insert(i);\n }\n\n int front = candidates;\n int back = n - candidates - 1;\n long long ans = 0;\n\n while(k--) {\n auto [cost, index] = pq.top();\n pq.pop();\n ans += cost;\n\n if (index <= front && front <= back) {\n while(front < n && processedIndices.count(front)) {\n front++;\n }\n pq.push({costs[front], front});\n front++;\n }\n else if (index >= back && back >= front) {\n while(back >= 0 && processedIndices.count(back)) {\n back--;\n }\n pq.push({costs[back], back});\n back--;\n }\n }\n\n return ans;\n }\n};\n",
"memory": "93955"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n int n = costs.size();\n priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> pq;\n \n unordered_set<int> processedIndices;\n\n for(int i = 0; i < candidates; i++) {\n pq.push({costs[i], i});\n processedIndices.insert(i);\n }\n \n for(int i = n - 1; i >= n - candidates && i >= 0; i--) {\n if(processedIndices.count(i)) continue; \n pq.push({costs[i], i});\n processedIndices.insert(i);\n }\n\n int front = candidates;\n int back = n - candidates - 1;\n long long ans = 0;\n\n while(k--) {\n auto [cost, index] = pq.top();\n pq.pop();\n ans += cost;\n\n if (index <= front && front <= back) {\n //while(front < n && processedIndices.count(front)) {\n // front++;\n //}\n pq.push({costs[front], front});\n front++;\n }\n else if (index >= back && back >= front) {\n // while(back >= 0 && processedIndices.count(back)) {\n /// back--;\n //}\n pq.push({costs[back], back});\n back--;\n }\n }\n\n return ans;\n }\n};\n",
"memory": "93955"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "#include <set>\nstruct Cost{int cost, pos;};\n\nstruct CostCmp\n{\n bool operator()(const Cost& lhs, const Cost& rhs) const\n {\n if( lhs.cost < rhs.cost) {\n return true;\n } \n if ( lhs.cost == rhs.cost ) {\n return( lhs.pos < rhs.pos);\n }\n return false;\n }\n};\n\nclass Solution {\npublic:\n\n long long totalCost(vector<int>& costs, int k, int candidates) {\n long long rval = 0;\n int nc = costs.size();\n\n if(k>=nc) {\n return accumulate(costs.begin(),costs.end(),long(0));\n }\n std::multiset<Cost,CostCmp> posbycost;\n int canlow = candidates;\n int canhigh = candidates;\n for(int i = 0; (i < nc) && (i < candidates); ++i) {\n posbycost.insert({costs[i], i});\n }\n for(int i = max(candidates, nc - candidates); i < nc; ++i) {\n posbycost.insert({costs[i], i});\n }\n\n while(k){\n cout<< posbycost.begin()->cost<<endl;\n rval += (posbycost.begin()->cost);\n int delpos = posbycost.begin()->pos;\n posbycost.erase(posbycost.begin());\n if(nc > (canlow + canhigh)) {\n if(delpos < canlow) {\n posbycost.insert({costs[canlow], canlow});\n canlow++;\n } else {\n posbycost.insert({costs[nc-canhigh-1], nc-canhigh-1});\n canhigh++;\n }\n }\n k--;\n }\n return rval;\n }\n};",
"memory": "94465"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n priority_queue<pair<long long,long long>,vector<pair<long long,long long>>,\n greater<pair<long long,long long>>> minHeap;\n long long i,maxl,maxr;\n map<long long,long long> mp;\n long long ans = 0;\n\n if(candidates*2>=costs.size()) {\n sort(costs.begin(),costs.end());\n for(i=0;i<k;i++) {\n ans=ans+costs[i];\n }\n return ans;\n }\n\n for(i=0;i<candidates;i++) {\n minHeap.push({costs[i],i});\n mp[i]=0;\n maxl=i;\n }\n\n for(i=costs.size()-1;i>=costs.size()-candidates ;i--) {\n minHeap.push({costs[i],i});\n mp[i]=1;\n maxr=i;\n }\n\n while(k--) {\n pair<int,int> p = minHeap.top();\n minHeap.pop();\n ans=ans+p.first;\n if(mp[p.second]==0 && maxl+1<maxr) {\n minHeap.push({costs[maxl+1],maxl+1});\n maxl++;\n mp[maxl]=0;\n } else if(mp[p.second]==1 && maxr-1 > maxl) {\n minHeap.push({costs[maxr-1],maxr-1});\n maxr--;\n mp[maxr]=1;\n }\n }\n return ans;\n }\n};",
"memory": "94975"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int cand) {\n long long ans=0;\n if(costs.size()<=2*cand)\n {\n sort(costs.begin(),costs.end());\n for(int i=0;i<k;i++) ans+=costs[i];\n }\n else\n {\n priority_queue<vector<int>,vector<vector<int>>,greater<vector<int>>>pq;\n int i=0,n=costs.size(),j=costs.size()-cand;\n for(i=0;i<cand;i++)\n {\n pq.push({costs[i],i});\n pq.push({costs[n-i-1],n-i-1});\n }\n i--;\n while(k && i<j)\n {\n auto v=pq.top(); pq.pop();\n long long res=v[0];\n int ind=v[1];\n ans+=res;\n k--;\n if(ind<=i) \n {\n i++;\n if(i<j)\n pq.push({costs[i],i});\n }\n else\n {\n j--;\n if(i<j)\n pq.push({costs[j],j});\n }\n }\n while(k)\n {\n ans+=pq.top()[0];\n pq.pop();\n k--;\n }\n }\n return ans;\n }\n};",
"memory": "95485"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\n\n struct cmp {\n bool operator()(pair<int, int>& a, pair<int, int>& b) {\n return a.second > b.second ||\n a.second == b.second && a.first > b.first;\n }\n };\n\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> lpq, rpq;\n long long res = 0;\n int li = 0, ri = costs.size() - 1;\n unordered_set<int> selectedCands;\n while (selectedCands.size() < k) {\n while (lpq.size() < candidates && li < costs.size()) {\n if (selectedCands.find(li) == selectedCands.end()) {\n lpq.push({li, costs[li]});\n }\n li++;\n }\n while (rpq.size() < candidates && ri >= 0) {\n if (selectedCands.find(ri) == selectedCands.end()) {\n rpq.push({ri, costs[ri]});\n }\n ri--;\n }\n\n int curSelect;\n pair<int, int> rtop = rpq.top(), ltop = lpq.top();\n\n if (ltop.first == rtop.first) {\n curSelect = ltop.first;\n res += ltop.second;\n lpq.pop();\n rpq.pop();\n } else if (ltop.second <= rtop.second) {\n curSelect = ltop.first;\n res += ltop.second;\n lpq.pop();\n } else {\n curSelect = rtop.first;\n res += rtop.second;\n rpq.pop();\n }\n selectedCands.insert(curSelect);\n }\n return res;\n }\n};",
"memory": "95995"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\n\n struct cmp {\n bool operator()(pair<int, int>& a, pair<int, int>& b) {\n return a.second > b.second ||\n a.second == b.second && a.first > b.first;\n }\n };\n\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> lpq, rpq;\n long long res = 0;\n int li = 0, ri = costs.size() - 1;\n unordered_set<int> selectedCands;\n while (selectedCands.size() < k) {\n while (lpq.size() < candidates && li < costs.size()) {\n if (selectedCands.find(li) == selectedCands.end()) {\n lpq.push({li, costs[li]});\n }\n li++;\n }\n while (rpq.size() < candidates && ri >= 0) {\n if (selectedCands.find(ri) == selectedCands.end()) {\n rpq.push({ri, costs[ri]});\n }\n ri--;\n }\n\n int curSelect;\n pair<int, int> rtop = rpq.top(), ltop = lpq.top();\n\n if (ltop.first == rtop.first) {\n curSelect = ltop.first;\n res += ltop.second;\n lpq.pop();\n rpq.pop();\n } else if (ltop.second <= rtop.second) {\n curSelect = ltop.first;\n res += ltop.second;\n lpq.pop();\n } else {\n curSelect = rtop.first;\n res += rtop.second;\n rpq.pop();\n }\n selectedCands.insert(curSelect);\n }\n return res;\n }\n};",
"memory": "95995"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nclass Solution {\npublic:\n typedef pair<int, int> candidate;\n\n long long totalCost(vector<int>& costs, int k, int candidates) {\n const auto n = costs.size();\n priority_queue<candidate, vector<candidate>, greater<candidate>> lheap;\n priority_queue<candidate, vector<candidate>, greater<candidate>> rheap;\n unordered_set<int> hired;\n int lMax = candidates - 1;\n int rMin = n - candidates;\n for (int i = 0; i < candidates && i < n; ++i) {\n lheap.push(make_pair(costs[i], i));\n rheap.push(make_pair(costs[n - 1 - i], n - 1 - i));\n }\n long long res = 0;\n for (int i = 0; i < k; ++i) {\n if (lheap.empty()) {\n const candidate& val = rheap.top();\n res += val.first;\n rheap.pop();\n continue;\n } else if (rheap.empty()) {\n const candidate& val = lheap.top();\n res += val.first;\n lheap.pop();\n continue;\n }\n const candidate& lCandidate = lheap.top();\n const candidate& rCandidate = rheap.top();\n if (lCandidate < rCandidate) {\n res += lCandidate.first;\n hired.insert(lCandidate.second);\n lheap.pop();\n while (++lMax < n && hired.count(lMax));\n if (lMax < n) {\n lheap.push(make_pair(costs[lMax], lMax));\n }\n } else if (lCandidate > rCandidate) {\n res += rCandidate.first;\n hired.insert(rCandidate.second);\n rheap.pop();\n while (--rMin >= 0 && hired.count(rMin));\n if (rMin >= 0) {\n rheap.push(make_pair(costs[rMin], rMin));\n }\n } else {\n res += lCandidate.first;\n hired.insert(lCandidate.second);\n lheap.pop();\n rheap.pop();\n }\n }\n return res;\n }\n};",
"memory": "96505"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int c) {\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>p1;\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>p2;\n unordered_set<int>s;\n int n= costs.size();\n for(int i=0;i<c;i++){\n p1.push({costs[i],i});\n p2.push({costs[n-i-1],n-i-1});\n }\n int f=c,e=n-c-1;\n long long res=0;\n while(k--){\n if(!p1.empty() && (p2.empty() ||p1.top().first<=p2.top().first)){\n if(p1.top().second==p2.top().second){\n p2.pop();\n }\n res+=p1.top().first;\n s.insert(p1.top().second);\n p1.pop();\n while(f < n && s.find(f)!=s.end()) f++;\n if(f<n ) p1.push({costs[f],f});\n f++;\n\n }\n else if(!p2.empty()) {\n \n res+=p2.top().first;\n s.insert(p2.top().second);\n p2.pop();\n while(e>=0 && s.find(e)!=s.end()) e--;\n if(e>=0) p2.push({costs[e],e});\n e--;\n }\n }\n return res;\n }\n};",
"memory": "97015"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\n\n struct cmp {\n bool operator()(pair<int, int>& a, pair<int, int>& b) {\n return a.second > b.second ||\n a.second == b.second && a.first > b.first;\n }\n };\n\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> lpq, rpq;\n long long res = 0;\n int li = 0, ri = costs.size() - 1;\n unordered_set<int> selectedCands, rRepeatCands, lRepeatCands;\n while (selectedCands.size() < k) {\n while (lpq.size() - lRepeatCands.size() < candidates &&\n li < costs.size()) {\n if (selectedCands.find(li) == selectedCands.end()) {\n lpq.push({li, costs[li]});\n }\n li++;\n }\n while (rpq.size() - rRepeatCands.size() < candidates && ri >= 0) {\n if (selectedCands.find(ri) == selectedCands.end()) {\n rpq.push({ri, costs[ri]});\n }\n ri--;\n }\n\n int curSelect;\n pair<int, int> rtop = rpq.top(), ltop = lpq.top();\n\n if (li == costs.size()) {\n curSelect = ltop.first;\n if (selectedCands.find(curSelect) == selectedCands.end()) {\n res += ltop.second;\n selectedCands.insert(curSelect);\n }\n lpq.pop();\n continue;\n }\n if (selectedCands.find(ltop.first) != selectedCands.end()){\n lpq.pop();\n continue;\n }\n if (selectedCands.find(rtop.first) != selectedCands.end()){\n rpq.pop();\n continue;\n }\n if (ltop.second <= rtop.second) {\n curSelect = ltop.first;\n if (curSelect > ri) {\n rRepeatCands.insert(curSelect);\n }\n res += ltop.second;\n lpq.pop();\n } else {\n curSelect = rtop.first;\n if (curSelect < li) {\n lRepeatCands.insert(curSelect);\n }\n res += rtop.second;\n rpq.pop();\n }\n selectedCands.insert(curSelect);\n }\n return res;\n }\n};",
"memory": "97525"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\n\n struct cmp {\n bool operator()(pair<int, int>& a, pair<int, int>& b) {\n return a.second > b.second ||\n a.second == b.second && a.first > b.first;\n }\n };\n\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> lpq, rpq;\n long long res = 0;\n int li = 0, ri = costs.size() - 1;\n unordered_set<int> selectedCands, rRepeatCands, lRepeatCands;\n while (selectedCands.size() < k) {\n while (lpq.size() - lRepeatCands.size() < candidates &&\n li < costs.size()) {\n if (selectedCands.find(li) == selectedCands.end()) {\n lpq.push({li, costs[li]});\n }\n li++;\n }\n while (rpq.size() - rRepeatCands.size() < candidates && ri >= 0) {\n if (selectedCands.find(ri) == selectedCands.end()) {\n rpq.push({ri, costs[ri]});\n }\n ri--;\n }\n\n int curSelect;\n pair<int, int> rtop = rpq.top(), ltop = lpq.top();\n\n if (li == costs.size()) {\n curSelect = ltop.first;\n if (selectedCands.find(curSelect) == selectedCands.end()) {\n res += ltop.second;\n selectedCands.insert(curSelect);\n }\n lpq.pop();\n continue;\n }\n if (selectedCands.find(ltop.first) != selectedCands.end()){\n lpq.pop();\n continue;\n }\n if (selectedCands.find(rtop.first) != selectedCands.end()){\n rpq.pop();\n continue;\n }\n if (ltop.second <= rtop.second) {\n curSelect = ltop.first;\n if (curSelect > ri) {\n rRepeatCands.insert(curSelect);\n }\n res += ltop.second;\n lpq.pop();\n } else {\n curSelect = rtop.first;\n if (curSelect < li) {\n lRepeatCands.insert(curSelect);\n }\n res += rtop.second;\n rpq.pop();\n }\n selectedCands.insert(curSelect);\n }\n return res;\n }\n};",
"memory": "98035"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\n\n struct cmp {\n bool operator()(pair<int, int>& a, pair<int, int>& b) {\n return a.second > b.second ||\n a.second == b.second && a.first > b.first;\n }\n };\n\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n priority_queue<pair<int, int>, vector<pair<int, int>>, cmp> lpq, rpq;\n long long res = 0;\n int li = 0, ri = costs.size() - 1;\n unordered_set<int> selectedCands, rRepeatCands, lRepeatCands;\n while (selectedCands.size() < k) {\n while (lpq.size() - lRepeatCands.size() < candidates &&\n li < costs.size()) {\n if (selectedCands.find(li) == selectedCands.end()) {\n lpq.push({li, costs[li]});\n }\n li++;\n }\n while (rpq.size() - rRepeatCands.size() < candidates && ri >= 0) {\n if (selectedCands.find(ri) == selectedCands.end()) {\n rpq.push({ri, costs[ri]});\n }\n ri--;\n }\n\n int curSelect;\n pair<int, int> rtop = rpq.top(), ltop = lpq.top();\n\n if (li == costs.size()) {\n curSelect = ltop.first;\n if (selectedCands.find(curSelect) == selectedCands.end()) {\n res += ltop.second;\n selectedCands.insert(curSelect);\n }\n lpq.pop();\n continue;\n }\n if (selectedCands.find(ltop.first) != selectedCands.end()){\n lpq.pop();\n continue;\n }\n if (selectedCands.find(rtop.first) != selectedCands.end()){\n rpq.pop();\n continue;\n }\n if (ltop.second <= rtop.second) {\n curSelect = ltop.first;\n if (curSelect > ri) {\n rRepeatCands.insert(curSelect);\n }\n res += ltop.second;\n lpq.pop();\n } else {\n curSelect = rtop.first;\n if (curSelect < li) {\n lRepeatCands.insert(curSelect);\n }\n res += rtop.second;\n rpq.pop();\n }\n selectedCands.insert(curSelect);\n }\n return res;\n }\n};",
"memory": "98035"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> left, right;\n int n = costs.size();\n set<pair<int,int>> m;\n int lc = -1, rc=n;\n for(int i=0;i<candidates;i++){\n lc++;\n rc--;\n left.push({costs[lc],lc});\n right.push({costs[rc], rc});\n }\n long long ans = 0;\n for(int i=0;i<k;i++){\n pair<int,int> p;\n bool l = true;\n do{\n if(left.empty()){\n p = right.top();\n right.pop();\n rc--;\n l = false;\n }else if(right.empty()){\n p = left.top();\n left.pop();\n lc++;\n }else{\n if(right.top().first<left.top().first){\n p = right.top();\n right.pop();\n rc--;\n l = false;\n }else{\n p = left.top();\n left.pop();\n lc++;\n }\n }\n if(lc<rc){\n if(l){\n left.push({costs[lc],lc});\n }else right.push({costs[rc],rc});\n }\n }while(m.find(p)!=m.end());\n m.insert(p);\n ans+=p.first;\n }\n return ans;\n }\n};",
"memory": "98545"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n priority_queue<pair<int,int> , vector<pair<int,int>>, greater<pair<int,int>>> pq1, pq2;\n int n = costs.size();\n\n for(int i=0;i<candidates;i++){\n pq1.push({costs[i], i});\n }\n\n for(int i=n-1;i>=n-candidates;i--){\n pq2.push({costs[i], i});\n }\n\n // while(pq1.size()){\n // cout<<pq1.top().first<<\"--->\"<<pq1.top().second<<endl;\n // pq1.pop();\n // }\n\n // while(pq2.size()){\n // cout<<pq2.top().first<<\"--->\"<<pq2.top().second<<endl;\n // pq2.pop();\n // }\n\n int idx1 = candidates, idx2 = n - candidates - 1;\n\n long long ans = 0;\n set<int> st;\n\n while(k){\n while(pq1.size() && st.find(pq1.top().second) != st.end()){\n pq1.pop();\n\n if(idx1 < n){\n pq1.push({costs[idx1], idx1});\n idx1++;\n }\n }\n\n while(pq2.size() && st.find(pq2.top().second) != st.end()){\n pq2.pop();\n\n if(idx2 >= 0){\n pq2.push({costs[idx2], idx2});\n idx2--;\n }\n }\n\n // while(pq1.size()){\n // cout<<pq1.top().first<<\"--->\"<<pq1.top().second<<endl;\n // pq1.pop();\n // }\n\n // while(pq2.size()){\n // cout<<pq2.top().first<<\"--->\"<<pq2.top().second<<endl;\n // pq2.pop();\n // }\n\n int temp1 = pq1.top().first, temp2 = pq2.top().first;\n\n //cout<<temp1<<\" \"<<temp2<<endl;\n\n if(temp1 <= temp2){\n st.insert(pq1.top().second);\n pq1.pop();\n \n ans += temp1;\n\n if(idx1< n){\n pq1.push({costs[idx1], idx1});\n idx1++;\n }\n }\n\n else{\n st.insert(pq2.top().second);\n pq2.pop();\n\n ans += temp2;\n\n if(idx2 >= 0){\n pq2.push({costs[idx2], idx2});\n idx2--;\n }\n }\n\n //cout<<\"ans-->\"<<ans<<\"idx--->\"<<idx1<<\"idx2-->\"<<idx2<<endl;\n k--;\n }\n\n return ans;\n\n return 0;\n }\n};",
"memory": "99055"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> left, right;\n int n = costs.size();\n set<pair<int,int>> m;\n int lc = -1, rc=n;\n for(int i=0;i<candidates;i++){\n lc++;\n rc--;\n left.push({costs[lc],lc});\n right.push({costs[rc], rc});\n }\n long long ans = 0;\n for(int i=0;i<k;i++){\n pair<int,int> p;\n do{\n if(left.empty()){\n p = right.top();\n right.pop();\n rc--;\n if(lc<rc){\n right.push({costs[rc],rc});\n }\n }else if(right.empty()){\n p = left.top();\n left.pop();\n lc++;\n if(lc<rc){\n left.push({costs[lc],lc});\n }\n }else{\n if(right.top().first<left.top().first){\n p = right.top();\n right.pop();\n rc--;\n if(lc<rc){\n right.push({costs[rc],rc});\n }\n }else{\n p = left.top();\n left.pop();\n lc++;\n if(lc<rc){\n left.push({costs[lc],lc});\n }\n }\n }\n }while(m.find(p)!=m.end());\n m.insert(p);\n ans+=p.first;\n }\n return ans;\n }\n};",
"memory": "99565"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n int n = costs.size();\n priority_queue<\n tuple<int, int, bool>,\n vector<tuple<int, int, bool>>,\n greater<>\n > pq;\n\n int left = 0;\n for (int i = 0; i < candidates; ++i) {\n bool isBack = false;\n pq.push({ costs[left], left, isBack });\n if (++left >= n) {\n break;\n }\n }\n\n int right = n - 1;\n for (int i = 0; i < candidates; ++i) {\n bool isBack = true;\n pq.push({ costs[right], right, isBack });\n if (--right < 0) {\n break;\n }\n }\n\n int count = 0;\n long long sum = 0;\n unordered_set<int> seen;\n while (!pq.empty() && count < k) {\n auto [cost, idx, isBack] = pq.top(); pq.pop();\n if (!seen.count(idx)) {\n seen.insert(idx);\n sum += cost;\n ++count;\n }\n\n if (isBack && right >= 0) {\n pq.push({ costs[right], right, isBack });\n --right;\n }\n\n if (!isBack && left < n) {\n pq.push({ costs[left], left, isBack });\n ++left;\n }\n }\n\n return sum;\n\n }\n};",
"memory": "100075"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "#define ll long long\nclass Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n int n=costs.size();\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n int cnt=0;\n ll ans=0;\n int prev_i=-1,prev_j=n;\n int flag=-1;\n unordered_map<int,int>mp;\n while(cnt<k){\n int temp=0;\n \n if(flag==-1){\n int i=prev_i+1;\n while(i<prev_j && temp<candidates){\n pq.push({costs[i],i});\n mp[i]=1;\n cout<<costs[i]<<endl;\n temp++;\n i++;\n }\n i--;\n prev_i=i;\n }\n \n \n temp=0;\n \n cout<<endl;\n if(flag==-1){\n int j=prev_j-1;\n while(j>prev_i && temp<candidates){\n cout<<costs[j]<<endl;\n pq.push({costs[j],j});\n temp++;\n j--;\n }\n j++;\n prev_j=j;\n }\n \n cout<<endl;\n if(flag==0){\n int i=prev_i+1;\n if(i<prev_j){\n pq.push({costs[i],i});\n mp[i]=1;\n prev_i=i;\n }\n }else if(flag==1){\n int j=prev_j-1;\n if(j>prev_i){\n pq.push({costs[j],j});\n prev_j=j;\n }\n }\n // cout<<pq.top().first<<\" \"<<pq.top().second<<endl;\n ans+=pq.top().first;\n if(mp[pq.top().second])flag=0;\n else flag=1;\n pq.pop();\n cnt++;\n }\n return ans;\n }\n};",
"memory": "100585"
} |
2,553 | <p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
<ul>
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
<ul>
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
</ul>
</li>
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
<li>A worker can only be chosen once.</li>
</ul>
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
<strong>Output:</strong> 11
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
The total hiring cost is 11.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
The total hiring cost is 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
<li><code>1 <= k, candidates <= costs.length</code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n long long totalCost(vector<int>& costs, int k, int candidates) {\n int n = costs.size();\n set<pair<int, int>> st;\n // long long t = 0;\n // for(auto it: costs) t+= it;\n // cout<<t<<endl;\n int i = 0, j = n - 1;\n while (i < candidates) {\n st.insert({costs[i], i});\n i++;\n }\n\n int l = candidates;\n while (j >= n - candidates && l--) {\n st.insert({costs[j], j});\n j--;\n }\n\n long long ans = 0;\n\n while (j >= i && !st.empty() && k--) {\n auto it = st.begin();\n int val = it->first;\n int ind = it->second;\n //cout<<val<<\" \"<<ind<<endl;\n st.erase(it);\n cout<<k<<\" \";\n\n ans += val;\n\n if (ind > j) {\n if (j >= i) st.insert({costs[j], j});\n j--;\n } else if (ind < i) {\n if (i <= j) st.insert({costs[i], i});\n i++;\n }\n }\n \n if(k<=0) return ans;\n while (k-- && !st.empty()) {\n\n auto it = st.begin();\n ans += it->first;\n int val = it->first;\n int ind = it->second;\n // cout<<k<<\" \";\n // cout<<val<<\" \"<<ind<<\" \"<<endl;\n st.erase(it);\n }\n\n return ans;\n }\n};\n",
"memory": "101095"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countGoodStrings(int low, int high, int zero, int one) {\n int dp[high+1];\n memset(dp, 0, sizeof(dp));\n dp[0] = 1;\n for(int i=1; i<=high; i++) {\n if(i-zero>=0) {\n dp[i] += dp[i-zero];\n if(dp[i]>=1000000007) dp[i]%=1000000007;\n };\n if(i-one>=0) {\n dp[i] += dp[i-one];\n if(dp[i]>=1000000007) dp[i]%=1000000007;\n }\n }\n \n\n int ans = 0;\n for(int i=low; i<=high; i++) {\n ans += dp[i];\n if(ans>=1000000007) ans%=1000000007;\n }\n return ans;\n }\n};",
"memory": "7815"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 0 | {
"code": "const int mod=1e9+7;\nclass Solution {\npublic:\n int countGoodStrings(int low, int high, int zero, int one) {\n int dp[high+1];\n memset(dp,0,sizeof(dp));\n dp[0]=1;\n for(int i=1;i<=high;i++){\n if(i>=one) dp[i]=(dp[i] + dp[i-one])%mod;\n if(i>=zero) dp[i]=(dp[i] + dp[i-zero])%mod;\n }\n int ans=0;\n for(int i=low;i<=high;i++){\n ans=(ans+dp[i])%mod;\n }\n return ans;\n }\n};",
"memory": "7815"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n const long long int mod=1e9+7;\n int countGoodStrings(int low, int high, int zero, int one) {\n long long int dp[high+1];\n memset(dp,0,sizeof(dp));\n dp[0]=1;\n for(int i=min(one,zero);i<=high;i++){\n if(i>=zero){\n dp[i]=(dp[i]+dp[i-zero])%mod;\n }\n if(i>=one){\n dp[i]=(dp[i]+dp[i-one])%mod;\n }\n }\n long long int sum=0;\n for(int i=low;i<=high;i++){\n sum=(sum+dp[i])%mod;\n }\n return sum;\n }\n};",
"memory": "8046"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countGoodStrings(int minLength, int maxLength, int zeroGroup, int oneGroup) {\n int dp[maxLength + max(oneGroup, zeroGroup)];\n memset(dp, 0, sizeof(dp));\n int mod = 1e9 + 7;\n dp[maxLength] = 1;\n for(int i = maxLength - 1; i >= 0; --i){\n dp[i] = (dp[i + zeroGroup] % mod + dp[i + oneGroup] % mod + (i >= minLength)) % mod;\n }\n return dp[0];\n }\n};",
"memory": "8278"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countGoodStrings(int low, int high, int zero, int one) {\n int mini = min(zero, one);\n int maxi = max(zero, one);\n int dp[high+1];\n int ans[high+1];\n int mod = 1000000007;\n\n for(int i = 0 ; i <= high; i++) {\n dp[i] = 0;\n ans[i] = 0;\n }\n\n for (int i = mini; i <= high; i++) {\n dp[i] = dp[i] + (i == mini ? 1:0) + dp[i-mini];\n if (i >= maxi)\n dp[i] = dp[i] + (i == maxi ? 1:0) + dp[i-maxi];\n dp[i] %= mod;\n ans[i] = (ans[i-1] + dp[i])%mod;\n }\n\n return (ans[high] - ans[low-1] + mod)%mod;\n }\n};",
"memory": "8278"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countGoodStrings(int low, int high, int zero, int one) \n {\n uint64_t res{};\n vector<int> dp(high + 1);\n dp[0] = 1;\n\n for (int len = 1; len <= high; ++len)\n {\n if (len >= zero)\n dp[len] += dp[len - zero];\n\n if (len >= one)\n dp[len] += dp[len - one];\n \n dp[len] %= 1000000007;\n if (len >= low)\n res = (res + dp[len]) % 1000000007;\n }\n\n return res;\n }\n};",
"memory": "8509"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countGoodStrings(int low, int high, int zero, int one) {\n vector<int> dp(high+1);\n long long ans=0;\n dp[0]=1;\n int i=min(zero,one);\n int mod=1e9+7;\n for(;i<=high;i++)\n {\n if(i-zero>=0)\n {\n dp[i]+=dp[i-zero]%mod;\n }\n if(i-one>=0)\n {\n dp[i]+=dp[i-one]%mod;\n }\n if(i>=low)\n {\n ans+=dp[i];\n ans%=mod;\n }\n }\n return ans%mod;\n }\n};",
"memory": "8740"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countGoodStrings(int low, int high, int zero, int one) {\n int ans = 0;\n int MOD = 1e9 + 7;\n vector<int> dp(high + 1, 0);\n dp[0] = 1;\n for (int i = (zero > one ? one : zero); i <= high; i++) {\n if (i >= zero && dp[i - zero]) {\n dp[i] = (dp[i] + dp[i - zero]) % MOD;\n }\n if (i >= one && dp[i - one]) {\n dp[i] = (dp[i] + dp[i - one]) % MOD;\n }\n if (i >= low) ans = (ans + dp[i]) % MOD;\n }\n return ans;\n }\n};",
"memory": "8971"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countGoodStrings(int low, int high, int zero, int one) {\n // Define the modulo value\n const int MOD = 1e9 + 7;\n\n // dp[i] will store the number of ways to create a string of length 'i'\n std::vector<int> dp(high + 1, 0);\n\n // There is exactly one way to form an empty string (base case)\n dp[0] = 1;\n\n // Fill the dp array for all lengths from 1 to high\n for (int i = 1; i <= high; ++i) {\n // If we can add 'zero' number of '0's to form length 'i'\n if (i >= zero) {\n dp[i] = (dp[i] + dp[i - zero]) % MOD;\n }\n // If we can add 'one' number of '1's to form length 'i'\n if (i >= one) {\n dp[i] = (dp[i] + dp[i - one]) % MOD;\n }\n }\n\n // Sum up the count of good strings for lengths between 'low' and 'high'\n int result = 0;\n for (int i = low; i <= high; ++i) {\n result = (result + dp[i]) % MOD;\n }\n\n return result;\n }\n};",
"memory": "9203"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int countGoodStrings(int low, int high, int zero, int one) {\n vector<int> v(high+1,0);\n v[0] = 1;\n int mod = 1e9+7;\n int sum = 0;\n for(int i = 1; i <= high; i++){\n if(i>=zero){\n v[i] = (v[i]*1LL+v[i-zero])%mod;\n }if(i>=one){\n v[i] = (v[i]*1LL+v[i-one])%mod;\n }\n // cout<<v[i]<<\" \";\n if(i>=low && i<=high){\n sum = (sum*1LL+v[i])%mod;\n }\n }\n return sum;\n }\n};",
"memory": "9203"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 0 | {
"code": "\nclass Solution {\npublic:\n // int score(int target, vector<int> dp, int zero, int one){\n // if(target == 0) return 1;\n\n // if(target < 0) return 0;\n\n // if(dp[target] != -1) return dp[target];\n // long long sum;\n\n // sum = score(target-one, dp, zero, one) + score(target-zero, dp, zero, one);\n // return dp[target] = (sum % mod);\n // }\n\n int countGoodStrings(int low, int high, int zero, int one) {\n \n vector <int> dp(high+1);\n dp[0]=1; \n int res=0, mod = 1e9+7;\n\n for(int i=1 ; i<=high; i++){\n if(i>=zero) dp[i] = (dp[i] + dp[i-zero]) % mod;\n\n if(i>=one) dp[i] = (dp[i]+dp[i-one]) % mod;\n\n if(i>=low) res =(res+dp[i])% mod;\n }\n\n return res;\n\n }\n};",
"memory": "9434"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int dp[100005];\n const int MOD = 1e9 + 7;\n\n int f(int len, int low, int high, int zero, int one) {\n if (len > high) return 0; \n if (dp[len] != -1) return dp[len];\n int ans = 0;\n if (len >= low) {\n ans = 1; \n }\n int z = f(len + zero, low, high, zero, one);\n int o = f(len + one, low, high, zero, one);\n return dp[len] = (ans + z + o) % MOD;\n }\n\n int countGoodStrings(int low, int high, int zero, int one) {\n vector<int> dp(high + 1);\n dp[0] = 1;\n int md = 1e9 + 7;\n for (int i = min(zero, one); i <= high; i++) {\n if(i>=zero){\n dp[i] = (dp[i] + dp[i - zero]) % md;\n }\n if(i>=one){\n dp[i] = (dp[i] + dp[i - one]) % md;\n }\n \n }\n int sum = 0;\n for (int i = low; i <= high; i++) {\n sum = (sum + dp[i]) % md;\n }\n return sum;\n }\n};\n",
"memory": "9665"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int countGoodStrings(int low, int high, int zero, int one) \n {\n uint64_t res{};\n deque<int> dp(high + 1);\n dp[0] = 1;\n\n for (int len = 1; len <= high; ++len)\n {\n if (len >= zero)\n dp[len] += dp[len - zero];\n\n if (len >= one)\n dp[len] += dp[len - one];\n \n dp[len] %= 1000000007;\n if (len >= low)\n res = (res + dp[len]) % 1000000007;\n }\n\n return res;\n }\n};",
"memory": "9896"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n long long dp[100001];\n int z;\n int o;\n const long long MOD = 1000000007;\n \n long long recurse(int i) {\n if (i < 0) return 0;\n if (dp[i] != -1) return dp[i];\n dp[i] = recurse(i-o) + recurse(i-z);\n return dp[i] %= MOD;\n }\n \n int countGoodStrings(int low, int high, int zero, int one) {\n long long ans = 0;\n z = zero;\n o = one;\n fill(dp, dp+high+1, -1);\n dp[0] = 1;\n for (int i = low; i<=high; i++) {\n ans += recurse(i);\n ans %= MOD;\n }\n \n return ans;\n }\n};",
"memory": "10128"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\n int dp[(int)10e5 + 1];\npublic:\n int countGoodStrings(int low, int high, int zero, int one) {\n dp[0] = 1;\n const int MOD = (int)1e9 + 7;\n for (int i = 1; i <= high; ++i) {\n if (i - zero >= 0) dp[i] += dp[i-zero];\n if (i - one >= 0) dp[i] += dp[i-one];\n dp[i] %= MOD;\n }\n int total = 0;\n for (int i = low; i <= high; ++i) {\n total += dp[i];\n total %= MOD;\n }\n return total;\n }\n};",
"memory": "10359"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\n int dp[(int)10e5 + 1];\npublic:\n int countGoodStrings(int low, int high, int zero, int one) {\n dp[0] = 1;\n const int MOD = (int)1e9 + 7;\n for (int i = 1; i <= high; ++i) {\n if (i - zero >= 0) dp[i] += dp[i-zero];\n if (i - one >= 0) dp[i] += dp[i-one];\n dp[i] %= MOD;\n }\n int total = 0;\n for (int i = low; i <= high; ++i) {\n total += dp[i];\n total %= MOD;\n }\n return total;\n }\n};",
"memory": "10590"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\n long long lim = 1e9+7;\n int dp[100001];\n int backtrack(int zero, int one, int len) {\n if (len == 0) return 1;\n if (dp[len] != -1) return dp[len];\n\n long long acc = 0;\n if (len-zero >= 0) acc = (acc + backtrack(zero, one, len-zero)) % lim;\n if (len-one >= 0) acc = (acc + backtrack(zero, one, len-one)) % lim;\n return dp[len] = acc;\n }\n\npublic:\n int countGoodStrings(int low, int high, int zero, int one) {\n memset(dp, -1, sizeof(dp));\n long long ans = 0;\n for (int i = low; i <= high; i++) {\n ans = (ans + backtrack(zero, one, i)) % lim;\n }\n return ans;\n }\n};\n",
"memory": "10821"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int countGoodStrings(int low, int high, int zero, int one) {\n long ans = 0;\n long MOD = 1e9 + 7;\n vector<long> dp(high + 1, 0);\n dp[0] = 1;\n for (int i = 1; i <= high; i++) {\n if (i >= zero) {\n dp[i] = (dp[i] + dp[i - zero]) % MOD;\n }\n if (i >= one) {\n dp[i] = (dp[i] + dp[i - one]) % MOD;\n }\n if (i >= low) ans = (ans + dp[i]) % MOD;\n }\n return ans;\n }\n};",
"memory": "11978"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int countGoodStrings(int low, int high, int zero, int one) {\n long ans = 0;\n long MOD = 1e9 + 7;\n vector<long> dp(high + 1, 0);\n dp[0] = 1;\n for (int i = 1; i <= high; i++) {\n if (i >= zero && dp[i - zero]) {\n dp[i] = (dp[i] + dp[i - zero]) % MOD;\n }\n if (i >= one && dp[i - one]) {\n dp[i] = (dp[i] + dp[i - one]) % MOD;\n }\n if (i >= low) ans = (ans + dp[i]) % MOD;\n }\n return ans;\n }\n};",
"memory": "12209"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int countGoodStrings(int low, int high, int zero, int one) {\n long ans = 0;\n long MOD = 1e9 + 7;\n vector<long> dp(high + 1, 0);\n dp[0] = 1;\n for (int i = min(zero, one); i <= high; i++) {\n if (i >= zero && dp[i - zero]) {\n dp[i] = (dp[i] + dp[i - zero]) % MOD;\n }\n if (i >= one && dp[i - one]) {\n dp[i] = (dp[i] + dp[i - one]) % MOD;\n }\n if (i >= low) ans = (ans + dp[i]) % MOD;\n }\n return ans;\n }\n};",
"memory": "12209"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\n static constexpr auto MOD = int(1e9) + 7;\n using DP = vector<long long>;\npublic:\n int countGoodStrings(int low, int high, int zero, int one) {\n DP dp(high + 1, 0);\n dp.front() = 1;\n long long res = 0;\n for (int end = 1; end <= high; end++)\n {\n if (end >= zero)\n {\n dp[end] += dp[end - zero];\n dp[end] %= MOD;\n }\n if (end >= one)\n {\n dp[end] += dp[end - one];\n dp[end] %= MOD;\n }\n if (end >= low)\n {\n res += dp[end];\n res %= MOD;\n }\n }\n return res;\n }\n};",
"memory": "12440"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int countGoodStrings(int low, int high, int zero, int one) {\n long ans = 0;\n long MOD = 1e9 + 7;\n vector<long> dp(high + 1, 0);\n dp[0] = 1;\n for (int i = (zero > one ? one : zero); i <= high; i++) {\n if (i >= zero && dp[i - zero]) {\n dp[i] = (dp[i] + dp[i - zero]) % MOD;\n }\n if (i >= one && dp[i - one]) {\n dp[i] = (dp[i] + dp[i - one]) % MOD;\n }\n if (i >= low) ans = (ans + dp[i]) % MOD;\n }\n return ans;\n }\n};",
"memory": "12440"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int countGoodStrings(int low, int high, int zero, int one) {\n long ans = 0;\n long MOD = 1e9 + 7;\n vector<long> dp(high + 1, 0);\n dp[0] = 1;\n for (int i = (zero > one ? one : zero); i <= high; i++) {\n if (i >= zero && dp[i - zero]) {\n dp[i] = (dp[i] + dp[i - zero]) % MOD;\n }\n if (i >= one && dp[i - one]) {\n dp[i] = (dp[i] + dp[i - one]) % MOD;\n }\n if (i >= low) ans = (ans + dp[i]) % MOD;\n }\n return ans;\n }\n};",
"memory": "12671"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int countGoodStrings(int low, int high, int zero, int one) {\n int ans = 0;\n int MOD = 1e9 + 7;\n vector<long> dp(high + 1, 0);\n dp[0] = 1;\n for (int i = (zero > one ? one : zero); i <= high; i++) {\n if (i >= zero && dp[i - zero]) {\n dp[i] = (dp[i] + dp[i - zero]) % MOD;\n }\n if (i >= one && dp[i - one]) {\n dp[i] = (dp[i] + dp[i - one]) % MOD;\n }\n if (i >= low) ans = (ans + dp[i]) % MOD;\n }\n return ans;\n }\n};",
"memory": "12903"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int L, H, Z, O;\n int mod=1e9+7;\n int dp[100001];\n int solve(int l)\n {\n if(l>H)\n return 0;\n \n if(dp[l]!=-1)\n return dp[l];\n\n bool flag=0;\n\n if(l>=L && l<=H)\n {\n flag=1;\n }\n long long take_0 = solve(l+Z);\n long long take_1 = solve(l+O);\n\n return dp[l] = (flag + take_0 + take_1)%mod;\n }\n int countGoodStrings(int low, int high, int zero, int one) \n {\n L=low, H=high, Z=zero, O=one;\n memset(dp, -1, sizeof(dp));\n return solve(0);\n }\n};",
"memory": "13134"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int L, H, Z, O;\n int mod=1e9+7;\n int dp[100001];\n int solve(int l)\n {\n if(l>H)\n return 0;\n \n if(dp[l]!=-1)\n return dp[l];\n\n bool flag=0;\n\n if(l>=L && l<=H)\n {\n flag=1;\n }\n long long take_0 = solve(l+Z);\n long long take_1 = solve(l+O);\n\n return dp[l] = (flag + take_0 + take_1)%mod;\n }\n int countGoodStrings(int low, int high, int zero, int one) \n {\n L=low, H=high, Z=zero, O=one;\n memset(dp, -1, sizeof(dp));\n return solve(0);\n }\n};",
"memory": "13365"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int mode = 1e9+7;\n int countGoodStrings(int low, int high, int zero, int one) {\n vector<int>dp(high+1,-1);\n int ans = 0;\n for(int len = low;len<=high;len++){\n ans = (ans + solve(one,zero,dp,len))%mode;\n }\n return ans;\n }\n int solve(int one,int zero,vector<int>&dp,int len){\n if(len == 0)return 1;\n if(len<0)return 0;\n if(dp[len] != -1)return dp[len];\n long long ans = (solve(one,zero,dp,len-one) + solve(one,zero,dp,len-zero))%mode;\n return dp[len] = ans;\n }\n};",
"memory": "13596"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n const int m = 1e9 + 7;\n int solve(int zero, int one, int len, vector<int> &dp){\n if(len==0) return 1;\n if(len<0) return 0;\n\n if(dp[len]!=-1) return dp[len];\n int on = solve(zero, one, len - one, dp);\n int ze = solve(zero, one, len - zero, dp);\n\n return dp[len] = (on+ ze)%m;\n\n }\n\n int countGoodStrings(int low, int high, int zero, int one) {\n int count = 0;\n vector<int> dp(high+1, -1);\n for(int len=low; len<=high; len++){\n count=(count +solve(zero, one, len, dp))%m;\n }\n return count;\n }\n};",
"memory": "13828"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution\n{\n public:\n\n \n \t//brute force->>using recurssion-TLE \n\n \t// int mod=1e9+7;\n \t\n // int solve(int size,int zero,int one,int n){\n \t// if(size==n){\n \t// return 1;\n \t// }\n \t// if(size>n)return 0;\n\n \t// int res = 0;\n \t// res = res + solve(size + zero,zero,one,n)%mod;\n\n \t// res = res + solve(size + one,zero,one,n)%mod;\n\n \t// return res;\n \t// }\n\n \t// int countGoodStrings(int low, int high, int zero, int one) {\n \t// int ans=0;\n \t// for(int i=low;i<=high;i++){\n \t// ans=ans+solve(0,zero,one,i)%mod;\n \t// }\n\n \t// return ans; \n \t// }\n \t// };\n\n \n \n \n \n \t// using dp--->\n\n int mod = 1e9 + 7;\n\n int solve(int target, vector<int> &dp, int one, int zero)\n {\n if (target == 0)\n return 1;\n if (target < 0)\n return 0;\n if (dp[target] != -1)\n return dp[target];\n \n long long sum;\n sum = solve(target - one, dp, one, zero) + solve(target - zero, dp, one, zero);\n return dp[target] = (sum % (mod));\n }\n \n \n \n int countGoodStrings(int low, int high, int zero, int one)\n {\n vector<int> dp(high + 1, -1);\n int ans = 0;\n\n for (int i = low; i <= high; i++)\n {\n ans = ((ans % mod) + (solve(i, dp, one, zero) % mod)) % mod;\n }\n return ans;\n }\n};",
"memory": "14059"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n const int m = 1e9 + 7;\n int solve(int zero, int one, int len, vector<int> &dp){\n if(len==0) return 1;\n if(len<0) return 0;\n\n if(dp[len]!=-1) return dp[len];\n int on = solve(zero, one, len - one, dp);\n int ze = solve(zero, one, len - zero, dp);\n\n return dp[len] = (on+ ze)%m;\n\n }\n\n int countGoodStrings(int low, int high, int zero, int one) {\n int count = 0;\n vector<int> dp(high+1, -1);\n for(int len=low; len<=high; len++){\n count=(count +solve(zero, one, len, dp))%m;\n }\n return count;\n }\n};",
"memory": "14290"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n\n int dp[100100];\n const int mod = 1e9+7;\n\n int rec(int n, int zero, int one){\n //pruning\n if(n<0) return 0;\n\n //basecase\n if(n == 0) return 1;\n\n //cache check\n if(dp[n] != -1) return dp[n];\n\n //compute\n int ans = (mod + (rec(n-zero, zero, one)%mod + rec(n-one, zero, one)%mod)%mod)%mod;\n\n //save and return\n return dp[n] = ans%mod;\n }\n\n int countGoodStrings(int low, int high, int zero, int one) {\n memset(dp,-1,sizeof(dp));\n int ans = 0;\n for(int n = low; n<=high; n++){\n ans = (mod + (ans%mod + rec(n, zero, one)%mod)%mod)%mod;\n }\n \n return ans;\n }\n};",
"memory": "14521"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int p = 1000000007;\n int ans;\n int countGoodStrings(int low, int high, int zero, int one) {\n \n int ans = 0;\n vector<int> f;\n f.push_back(1);\n for(int i=1;i<=high; ++i) {\n int temp = 0;\n if (i-zero >= 0) temp += f[i-zero];\n temp %= p;\n if (i-one >= 0) temp += f[i-one];\n temp %= p;\n f.push_back(temp);\n if (i>=low) {ans = (ans + temp) % p;}\n }\n return ans;\n }\n};",
"memory": "14753"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\nint mod=1e9+7;\nint solve(int len,int zero,int one,vector<int>&dp){\n if(len<0){\n return 0;\n }\n if(len==0){\n return 1;\n }\n if(dp[len]!=-1){\n return dp[len];\n }\n int first=(solve(len-one,zero,one,dp))%mod;\n int sec=(solve(len-zero,zero,one,dp))%mod;\n return dp[len]=(first+sec)%mod;\n}\n int countGoodStrings(int low, int high, int zero, int one) {\n int ans=0;\n vector<int>dp(high+1,-1);\n for(int i=low;i<=high;i++){\nans=(ans+solve(i,zero,one,dp))%mod;\n }return ans;\n }\n};",
"memory": "14984"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\nint mod=1e9+7;\nvector<int>dp;\n/*int rec(int i,int high,int zero, int one){\n if(i>high)return 0;\n if((i>low&&i<=high))dp[i]++;\n}*/\n int countGoodStrings(int low, int high, int zero, int one) {\n for(int i=0;i<=high;i++)dp.push_back(0);\n dp[0]=1;\n vector<int>a;\n a.push_back(zero);\n a.push_back(one);\n for(int i=1;i<=high;i++){\n for(int j=0;j<2;j++){\n if(i-a[j]>=0){\n dp[i]+=dp[i-a[j]];\n dp[i]%=mod;\n }\n }\n }\n int res=0;\n for(int i=low;i<=high;i++){\n res+=dp[i];\n res%=mod;\n }\n return res;\n }\n};",
"memory": "15215"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int dp[100001];\n int L;\n int H;\n int Z;\n int O;\n int MOD = 1e9 + 7;\n int solve(int length){\n if(length > H){\n return 0;\n }\n\n if(dp[length] != -1){\n return dp[length];\n }\n\n int current = 0;\n if(length >= L && length <= H){\n current = 1; // current string ans main include karni ha to 1 assign ho gaya\n }\n\n int append_zero_left = solve(length + Z); // append zero Z times\n int append_one_right = solve(length + O); // append one O times\n\n return dp[length] = (current + append_zero_left + append_one_right) % MOD;\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n memset(dp, -1, sizeof(dp));\n\n L = low;\n H = high;\n Z = zero;\n O = one;\n\n return solve(0);\n }\n};",
"memory": "15446"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n const int mod = 1e9+7;\n int dp[100005];\n int fn(int low, int high, int zero, int one, int currSize) {\n\n if (currSize > high)\n return 0;\n \n if (dp[currSize] != -1) return dp[currSize];\n \n int add_zero = 0;\n if (currSize + zero <= high) {\n if (currSize + zero >= low)\n add_zero = 1 + fn(low, high, zero, one, currSize + zero);\n else \n add_zero = fn(low, high, zero, one, currSize + zero);\n }\n int add_one = 0;\n if (currSize + one <= high) {\n if (currSize + one >= low)\n add_one = 1 + fn(low, high, zero, one, currSize + one);\n else \n add_one = fn(low, high, zero, one, currSize + one);\n }\n return dp[currSize] = (add_one % mod + add_zero % mod) % mod;\n \n }\n\n int countGoodStrings(int low, int high, int zero, int one) {\n \n memset(dp, -1, sizeof(dp));\n return fn(low, high, zero, one, 0);\n }\n};",
"memory": "15678"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int f(int idx,int zero,int one,vector<int>& dp){\n if(idx == 0) return 1;\n // if(idx<0) return 0;\n if(dp[idx] != -1) return dp[idx];\n int cnt1 = 0,cnt0 = 0;\n if(idx>=zero) cnt0 = f(idx-zero,zero,one,dp);\n if(idx>=one) cnt1 = f(idx-one,zero,one,dp);\n return dp[idx] = (cnt0+cnt1)%1000000007;\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n long long res = 0;\n vector<int> dp(high+1,-1);\n for(int i=low;i<=high;i++){\n res += f(i,zero,one,dp);\n }\n return res%1000000007;\n }\n};",
"memory": "15678"
} |
2,562 | <p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
<ul>
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
</ul>
<p>This can be performed any number of times.</p>
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
<strong>Output:</strong> 8
<strong>Explanation:</strong>
One possible valid good string is "011".
It can be constructed as follows: "" -> "0" -> "01" -> "011".
All binary strings from "000" to "111" are good strings in this example.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
<strong>Output:</strong> 5
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
<li><code>1 <= zero, one <= low</code></li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int l, h, z, o, mod = 1e9 + 7;\n bool valid(int n) {return n >= l && n <= h;}\n int countWays(int len, vector<int>&dp) {\n if (len == 0) return 1;\n if (len < 0) return 0;\n if (dp[len] != -1) return dp[len];\n dp[len] = (countWays(len-z, dp) % mod + countWays(len-o, dp) % mod) % mod;\n return dp[len];\n }\n int countGoodStrings(int low, int high, int zero, int one) {\n l = low, h = high, z = zero, o = one;\n vector<int> dp(high+1, -1);\n int ans = 0;\n for (int i = l; i <= h; ++i) {\n ans = (ans%mod + countWays(i, dp)%mod)%mod;\n }\n return ans;\n }\n};",
"memory": "15909"
} |