mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-02 05:13:29 +08:00
update
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>hamsters</code> where <code>hamsters[i]</code> is either:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>'H'</code> indicating that there is a hamster at index <code>i</code>, or</li>
|
||||
<li><code>'.'</code> indicating that index <code>i</code> is empty.</li>
|
||||
</ul>
|
||||
|
||||
<p>You will add some number of food buckets at the empty indices in order to feed the hamsters. A hamster can be fed if there is at least one food bucket to its left or to its right. More formally, a hamster at index <code>i</code> can be fed if you place a food bucket at index <code>i - 1</code> <strong>and/or</strong> at index <code>i + 1</code>.</p>
|
||||
|
||||
<p>Return <em>the minimum number of food buckets you should <strong>place at empty indices</strong> to feed all the hamsters or </em><code>-1</code><em> if it is impossible to feed all of them</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/11/01/example1.png" style="width: 482px; height: 162px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> hamsters = "H..H"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We place two food buckets at indices 1 and 2.
|
||||
It can be shown that if we place only one food bucket, one of the hamsters will not be fed.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/11/01/example2.png" style="width: 602px; height: 162px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> hamsters = ".H.H."
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We place one food bucket at index 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/11/01/example3.png" style="width: 602px; height: 162px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> hamsters = ".HHH."
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> If we place a food bucket at every empty index as shown, the hamster at index 2 will not be able to eat.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= hamsters.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>hamsters[i]</code> is either<code>'H'</code> or <code>'.'</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,28 @@
|
||||
<p>Given an integer array <code>nums</code> of <strong>positive</strong> integers, return <em>the average value of all even integers that are divisible by</em> <code>3</code><i>.</i></p>
|
||||
|
||||
<p>Note that the <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,6,10,12,15]
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> 6 and 12 are even numbers that are divisible by 3. (6 + 12) / 2 = 9.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,4,7,10]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There is no single number that satisfies the requirement, so return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,48 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of size <code>n</code> consisting of <strong>non-negative</strong> integers.</p>
|
||||
|
||||
<p>You need to apply <code>n - 1</code> operations to this array where, in the <code>i<sup>th</sup></code> operation (<strong>0-indexed</strong>), you will apply the following on the <code>i<sup>th</sup></code> element of <code>nums</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li>If <code>nums[i] == nums[i + 1]</code>, then multiply <code>nums[i]</code> by <code>2</code> and set <code>nums[i + 1]</code> to <code>0</code>. Otherwise, you skip this operation.</li>
|
||||
</ul>
|
||||
|
||||
<p>After performing <strong>all</strong> the operations, <strong>shift</strong> all the <code>0</code>'s to the <strong>end</strong> of the array.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, the array <code>[1,0,2,0,0,1]</code> after shifting all its <code>0</code>'s to the end, is <code>[1,2,1,0,0,0]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the resulting array</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that the operations are applied <strong>sequentially</strong>, not all at once.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,2,1,1,0]
|
||||
<strong>Output:</strong> [1,4,2,0,0,0]
|
||||
<strong>Explanation:</strong> We do the following operations:
|
||||
- i = 0: nums[0] and nums[1] are not equal, so we skip this operation.
|
||||
- i = 1: nums[1] and nums[2] are equal, we multiply nums[1] by 2 and change nums[2] to 0. The array becomes [1,<strong><u>4</u></strong>,<strong><u>0</u></strong>,1,1,0].
|
||||
- i = 2: nums[2] and nums[3] are not equal, so we skip this operation.
|
||||
- i = 3: nums[3] and nums[4] are equal, we multiply nums[3] by 2 and change nums[4] to 0. The array becomes [1,4,0,<strong><u>2</u></strong>,<strong><u>0</u></strong>,0].
|
||||
- i = 4: nums[4] and nums[5] are equal, we multiply nums[4] by 2 and change nums[5] to 0. The array becomes [1,4,0,2,<strong><u>0</u></strong>,<strong><u>0</u></strong>].
|
||||
After that, we shift the 0's to the end, which gives the array [1,4,2,0,0,0].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,1]
|
||||
<strong>Output:</strong> [1,0]
|
||||
<strong>Explanation:</strong> No operation can be applied, we just shift the 0 to the end.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 2000</code></li>
|
||||
<li><code>0 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>You are given an array of equal-length strings <code>words</code>. Assume that the length of each string is <code>n</code>.</p>
|
||||
|
||||
<p>Each string <code>words[i]</code> can be converted into a <strong>difference integer array</strong> <code>difference[i]</code> of length <code>n - 1</code> where <code>difference[i][j] = words[i][j+1] - words[i][j]</code> where <code>0 <= j <= n - 2</code>. Note that the difference between two letters is the difference between their <strong>positions</strong> in the alphabet i.e. the position of <code>'a'</code> is <code>0</code>, <code>'b'</code> is <code>1</code>, and <code>'z'</code> is <code>25</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, for the string <code>"acb"</code>, the difference integer array is <code>[2 - 0, 1 - 2] = [2, -1]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>All the strings in words have the same difference integer array, <strong>except one</strong>. You should find that string.</p>
|
||||
|
||||
<p>Return<em> the string in </em><code>words</code><em> that has different <strong>difference integer array</strong>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["adc","wzy","abc"]
|
||||
<strong>Output:</strong> "abc"
|
||||
<strong>Explanation:</strong>
|
||||
- The difference integer array of "adc" is [3 - 0, 2 - 3] = [3, -1].
|
||||
- The difference integer array of "wzy" is [25 - 22, 24 - 25]= [3, -1].
|
||||
- The difference integer array of "abc" is [1 - 0, 2 - 1] = [1, 1].
|
||||
The odd array out is [1, 1], so we return the corresponding string, "abc".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["aaa","bob","ccc","ddd"]
|
||||
<strong>Output:</strong> "bob"
|
||||
<strong>Explanation:</strong> All the integer arrays are [0, 0] except for "bob", which corresponds to [13, -13].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= words.length <= 100</code></li>
|
||||
<li><code>n == words[i].length</code></li>
|
||||
<li><code>2 <= n <= 20</code></li>
|
||||
<li><code>words[i]</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of positive integers, representing targets on a number line. You are also given an integer <code>space</code>.</p>
|
||||
|
||||
<p>You have a machine which can destroy targets. <strong>Seeding</strong> the machine with some <code>nums[i]</code> allows it to destroy all targets with values that can be represented as <code>nums[i] + c * space</code>, where <code>c</code> is any non-negative integer. You want to destroy the <strong>maximum</strong> number of targets in <code>nums</code>.</p>
|
||||
|
||||
<p>Return<em> the <strong>minimum value</strong> of </em><code>nums[i]</code><em> you can seed the machine with to destroy the maximum number of targets.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,7,8,1,1,5], space = 2
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> If we seed the machine with nums[3], then we destroy all targets equal to 1,3,5,7,9,...
|
||||
In this case, we would destroy 5 total targets (all except for nums[2]).
|
||||
It is impossible to destroy more than 5 targets, so we return nums[3].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,5,2,4,6], space = 2
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> Seeding the machine with nums[0], or nums[3] destroys 3 targets.
|
||||
It is not possible to destroy more than 3 targets.
|
||||
Since nums[0] is the minimal integer that can destroy 3 targets, we return 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [6,2,5], space = 100
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Whatever initial seed we select, we can only destroy 1 target. The minimal seed is nums[1].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= space <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,58 @@
|
||||
<p>There are some robots and factories on the X-axis. You are given an integer array <code>robot</code> where <code>robot[i]</code> is the position of the <code>i<sup>th</sup></code> robot. You are also given a 2D integer array <code>factory</code> where <code>factory[j] = [position<sub>j</sub>, limit<sub>j</sub>]</code> indicates that <code>position<sub>j</sub></code> is the position of the <code>j<sup>th</sup></code> factory and that the <code>j<sup>th</sup></code> factory can repair at most <code>limit<sub>j</sub></code> robots.</p>
|
||||
|
||||
<p>The positions of each robot are <strong>unique</strong>. The positions of each factory are also <strong>unique</strong>. Note that a robot can be <strong>in the same position</strong> as a factory initially.</p>
|
||||
|
||||
<p>All the robots are initially broken; they keep moving in one direction. The direction could be the negative or the positive direction of the X-axis. When a robot reaches a factory that did not reach its limit, the factory repairs the robot, and it stops moving.</p>
|
||||
|
||||
<p><strong>At any moment</strong>, you can set the initial direction of moving for <strong>some</strong> robot. Your target is to minimize the total distance traveled by all the robots.</p>
|
||||
|
||||
<p>Return <em>the minimum total distance traveled by all the robots</em>. The test cases are generated such that all the robots can be repaired.</p>
|
||||
|
||||
<p><strong>Note that</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>All robots move at the same speed.</li>
|
||||
<li>If two robots move in the same direction, they will never collide.</li>
|
||||
<li>If two robots move in opposite directions and they meet at some point, they do not collide. They cross each other.</li>
|
||||
<li>If a robot passes by a factory that reached its limits, it crosses it as if it does not exist.</li>
|
||||
<li>If the robot moved from a position <code>x</code> to a position <code>y</code>, the distance it moved is <code>|y - x|</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/09/15/example1.jpg" style="width: 500px; height: 320px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> robot = [0,4,6], factory = [[2,2],[6,2]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> As shown in the figure:
|
||||
- The first robot at position 0 moves in the positive direction. It will be repaired at the first factory.
|
||||
- The second robot at position 4 moves in the negative direction. It will be repaired at the first factory.
|
||||
- The third robot at position 6 will be repaired at the second factory. It does not need to move.
|
||||
The limit of the first factory is 2, and it fixed 2 robots.
|
||||
The limit of the second factory is 2, and it fixed 1 robot.
|
||||
The total distance is |2 - 0| + |2 - 4| + |6 - 6| = 4. It can be shown that we cannot achieve a better total distance than 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/09/15/example-2.jpg" style="width: 500px; height: 329px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> robot = [1,-1], factory = [[-2,1],[2,1]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> As shown in the figure:
|
||||
- The first robot at position 1 moves in the positive direction. It will be repaired at the second factory.
|
||||
- The second robot at position -1 moves in the negative direction. It will be repaired at the first factory.
|
||||
The limit of the first factory is 1, and it fixed 1 robot.
|
||||
The limit of the second factory is 1, and it fixed 1 robot.
|
||||
The total distance is |2 - 1| + |(-2) - (-1)| = 2. It can be shown that we cannot achieve a better total distance than 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= robot.length, factory.length <= 100</code></li>
|
||||
<li><code>factory[j].length == 2</code></li>
|
||||
<li><code>-10<sup>9</sup> <= robot[i], position<sub>j</sub> <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= limit<sub>j</sub> <= robot.length</code></li>
|
||||
<li>The input will be generated such that it is always possible to repair every robot.</li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>You are given two string arrays <code>creators</code> and <code>ids</code>, and an integer array <code>views</code>, all of length <code>n</code>. The <code>i<sup>th</sup></code> video on a platform was created by <code>creator[i]</code>, has an id of <code>ids[i]</code>, and has <code>views[i]</code> views.</p>
|
||||
|
||||
<p>The <strong>popularity</strong> of a creator is the <strong>sum</strong> of the number of views on <strong>all</strong> of the creator's videos. Find the creator with the <strong>highest</strong> popularity and the id of their <strong>most</strong> viewed video.</p>
|
||||
|
||||
<ul>
|
||||
<li>If multiple creators have the highest popularity, find all of them.</li>
|
||||
<li>If multiple videos have the highest view count for a creator, find the lexicographically <strong>smallest</strong> id.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> a 2D array of strings </em><code>answer</code><em> where </em><code>answer[i] = [creator<sub>i</sub>, id<sub>i</sub>]</code><em> means that </em><code>creator<sub>i</sub></code> <em>has the <strong>highest</strong> popularity and </em><code>id<sub>i</sub></code><em> is the id of their most popular video.</em> The answer can be returned in any order.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> creators = ["alice","bob","alice","chris"], ids = ["one","two","three","four"], views = [5,10,5,4]
|
||||
<strong>Output:</strong> [["alice","one"],["bob","two"]]
|
||||
<strong>Explanation:</strong>
|
||||
The popularity of alice is 5 + 5 = 10.
|
||||
The popularity of bob is 10.
|
||||
The popularity of chris is 4.
|
||||
alice and bob are the most popular creators.
|
||||
For bob, the video with the highest view count is "two".
|
||||
For alice, the videos with the highest view count are "one" and "three". Since "one" is lexicographically smaller than "three", it is included in the answer.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> creators = ["alice","alice","alice"], ids = ["a","b","c"], views = [1,2,2]
|
||||
<strong>Output:</strong> [["alice","b"]]
|
||||
<strong>Explanation:</strong>
|
||||
The videos with id "b" and "c" have the highest view count.
|
||||
Since "b" is lexicographically smaller than "c", it is included in the answer.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == creators.length == ids.length == views.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= creators[i].length, ids[i].length <= 5</code></li>
|
||||
<li><code>creators[i]</code> and <code>ids[i]</code> consist only of lowercase English letters.</li>
|
||||
<li><code>0 <= views[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,52 @@
|
||||
<p>You are given the <code>root</code> of a <strong>binary tree</strong> with <code>n</code> nodes. Each node is assigned a unique value from <code>1</code> to <code>n</code>. You are also given an array <code>queries</code> of size <code>m</code>.</p>
|
||||
|
||||
<p>You have to perform <code>m</code> <strong>independent</strong> queries on the tree where in the <code>i<sup>th</sup></code> query you do the following:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Remove</strong> the subtree rooted at the node with the value <code>queries[i]</code> from the tree. It is <strong>guaranteed</strong> that <code>queries[i]</code> will <strong>not</strong> be equal to the value of the root.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an array </em><code>answer</code><em> of size </em><code>m</code><em> where </em><code>answer[i]</code><em> is the height of the tree after performing the </em><code>i<sup>th</sup></code><em> query</em>.</p>
|
||||
|
||||
<p><strong>Note</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>The queries are independent, so the tree returns to its <strong>initial</strong> state after each query.</li>
|
||||
<li>The height of a tree is the <strong>number of edges in the longest simple path</strong> from the root to some node in the tree.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/09/07/binaryytreeedrawio-1.png" style="width: 495px; height: 281px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,3,4,2,null,6,5,null,null,null,null,null,7], queries = [4]
|
||||
<strong>Output:</strong> [2]
|
||||
<strong>Explanation:</strong> The diagram above shows the tree after removing the subtree rooted at node with value 4.
|
||||
The height of the tree is 2 (The path 1 -> 3 -> 2).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/09/07/binaryytreeedrawio-2.png" style="width: 301px; height: 284px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [5,8,9,2,1,3,7,4,6], queries = [3,2,4,8]
|
||||
<strong>Output:</strong> [3,2,3,2]
|
||||
<strong>Explanation:</strong> We have the following queries:
|
||||
- Removing the subtree rooted at node with value 3. The height of the tree becomes 3 (The path 5 -> 8 -> 2 -> 4).
|
||||
- Removing the subtree rooted at node with value 2. The height of the tree becomes 2 (The path 5 -> 8 -> 1).
|
||||
- Removing the subtree rooted at node with value 4. The height of the tree becomes 3 (The path 5 -> 8 -> 2 -> 6).
|
||||
- Removing the subtree rooted at node with value 8. The height of the tree becomes 2 (The path 5 -> 9 -> 3).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is <code>n</code>.</li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= Node.val <= n</code></li>
|
||||
<li>All the values in the tree are <strong>unique</strong>.</li>
|
||||
<li><code>m == queries.length</code></li>
|
||||
<li><code>1 <= m <= min(n, 10<sup>4</sup>)</code></li>
|
||||
<li><code>1 <= queries[i] <= n</code></li>
|
||||
<li><code>queries[i] != root.val</code></li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>You are given two positive integers <code>n</code> and <code>target</code>.</p>
|
||||
|
||||
<p>An integer is considered <strong>beautiful</strong> if the sum of its digits is less than or equal to <code>target</code>.</p>
|
||||
|
||||
<p>Return the <em>minimum <strong>non-negative</strong> integer </em><code>x</code><em> such that </em><code>n + x</code><em> is beautiful</em>. The input will be generated such that it is always possible to make <code>n</code> beautiful.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 16, target = 6
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> Initially n is 16 and its digit sum is 1 + 6 = 7. After adding 4, n becomes 20 and digit sum becomes 2 + 0 = 2. It can be shown that we can not make n beautiful with adding non-negative integer less than 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 467, target = 6
|
||||
<strong>Output:</strong> 33
|
||||
<strong>Explanation:</strong> Initially n is 467 and its digit sum is 4 + 6 + 7 = 17. After adding 33, n becomes 500 and digit sum becomes 5 + 0 + 0 = 5. It can be shown that we can not make n beautiful with adding non-negative integer less than 33.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1, target = 1
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Initially n is 1 and its digit sum is 1, which is already smaller than or equal to target.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>12</sup></code></li>
|
||||
<li><code>1 <= target <= 150</code></li>
|
||||
<li>The input will be generated such that it is always possible to make <code>n</code> beautiful.</li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>. Find the maximum subarray sum of all the subarrays of <code>nums</code> that meet the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>The length of the subarray is <code>k</code>, and</li>
|
||||
<li>All the elements of the subarray are <strong>distinct</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the maximum subarray sum of all the subarrays that meet the conditions</em><em>.</em> If no subarray meets the conditions, return <code>0</code>.</p>
|
||||
|
||||
<p><em>A <strong>subarray</strong> is a contiguous non-empty sequence of elements within an array.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,5,4,2,9,9,9], k = 3
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> The subarrays of nums with length 3 are:
|
||||
- [1,5,4] which meets the requirements and has a sum of 10.
|
||||
- [5,4,2] which meets the requirements and has a sum of 11.
|
||||
- [4,2,9] which meets the requirements and has a sum of 15.
|
||||
- [2,9,9] which does not meet the requirements because the element 9 is repeated.
|
||||
- [9,9,9] which does not meet the requirements because the element 9 is repeated.
|
||||
We return 15 because it is the maximum subarray sum of all the subarrays that meet the conditions
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,4,4], k = 3
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The subarrays of nums with length 3 are:
|
||||
- [4,4,4] which does not meet the requirements because the element 4 is repeated.
|
||||
We return 0 because no subarrays meet the conditions.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<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>
|
Reference in New Issue
Block a user