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,29 @@
|
||||
<p>You are given a <strong>0-indexed</strong> 2D integer array <code>nums</code> representing the coordinates of the cars parking on a number line. For any index <code>i</code>, <code>nums[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> where <code>start<sub>i</sub></code> is the starting point of the <code>i<sup>th</sup></code> car and <code>end<sub>i</sub></code> is the ending point of the <code>i<sup>th</sup></code> car.</p>
|
||||
|
||||
<p>Return <em>the number of integer points on the line that are covered with <strong>any part</strong> of a car.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [[3,6],[1,5],[4,7]]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> All the points from 1 to 7 intersect at least one car, therefore the answer would be 7.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [[1,3],[5,8]]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> Points intersecting at least one car are 1, 2, 3, 5, 6, 7, 8. There are a total of 7 points, therefore the answer would be 7.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>nums[i].length == 2</code></li>
|
||||
<li><code><font face="monospace">1 <= start<sub>i</sub> <= end<sub>i</sub> <= 100</font></code></li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of length <code>n</code> containing <strong>distinct</strong> positive integers. Return <em>the <strong>minimum</strong> number of <strong>right shifts</strong> required to sort </em><code>nums</code><em> and </em><code>-1</code><em> if this is not possible.</em></p>
|
||||
|
||||
<p>A <strong>right shift</strong> is defined as shifting the element at index <code>i</code> to index <code>(i + 1) % n</code>, for all indices.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,4,5,1,2]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
After the first right shift, nums = [2,3,4,5,1].
|
||||
After the second right shift, nums = [1,2,3,4,5].
|
||||
Now nums is sorted; therefore the answer is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,5]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> nums is already sorted therefore, the answer is 0.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,1,4]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It's impossible to sort the array using right shifts.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>1 <= nums[i] <= 100</code></li>
|
||||
<li><code>nums</code> contains distinct integers.</li>
|
||||
</ul>
|
@@ -0,0 +1,59 @@
|
||||
<p>You are given a <strong>0-indexed</strong> <strong>sorted</strong> array of integers <code>nums</code>.</p>
|
||||
|
||||
<p>You can perform the following operation any number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose <strong>two</strong> indices, <code>i</code> and <code>j</code>, where <code>i < j</code>, such that <code>nums[i] < nums[j]</code>.</li>
|
||||
<li>Then, remove the elements at indices <code>i</code> and <code>j</code> from <code>nums</code>. The remaining elements retain their original order, and the array is re-indexed.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer that denotes the <strong>minimum</strong> length of </em><code>nums</code><em> after performing the operation any number of times (<strong>including zero</strong>).</em></p>
|
||||
|
||||
<p>Note that <code>nums</code> is sorted in <strong>non-decreasing</strong> order.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,4,9]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Initially, nums = [1, 3, 4, 9].
|
||||
In the first operation, we can choose index 0 and 1 because nums[0] < nums[1] <=> 1 < 3.
|
||||
Remove indices 0 and 1, and nums becomes [4, 9].
|
||||
For the next operation, we can choose index 0 and 1 because nums[0] < nums[1] <=> 4 < 9.
|
||||
Remove indices 0 and 1, and nums becomes an empty array [].
|
||||
Hence, the minimum length achievable is 0.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,6,9]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Initially, nums = [2, 3, 6, 9].
|
||||
In the first operation, we can choose index 0 and 2 because nums[0] < nums[2] <=> 2 < 6.
|
||||
Remove indices 0 and 2, and nums becomes [3, 9].
|
||||
For the next operation, we can choose index 0 and 1 because nums[0] < nums[1] <=> 3 < 9.
|
||||
Remove indices 0 and 1, and nums becomes an empty array [].
|
||||
Hence, the minimum length achievable is 0.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,2]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> Initially, nums = [1, 1, 2].
|
||||
In an operation, we can choose index 0 and 2 because nums[0] < nums[2] <=> 1 < 2.
|
||||
Remove indices 0 and 2, and nums becomes [1].
|
||||
It is no longer possible to perform an operation on the array.
|
||||
Hence, the minimum achievable length is 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>nums</code> is sorted in <strong>non-decreasing</strong> order.</li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>You are given four integers <code>sx</code>, <code>sy</code>, <code>fx</code>, <code>fy</code>, and a <strong>non-negative</strong> integer <code>t</code>.</p>
|
||||
|
||||
<p>In an infinite 2D grid, you start at the cell <code>(sx, sy)</code>. Each second, you <strong>must</strong> move to any of its adjacent cells.</p>
|
||||
|
||||
<p>Return <code>true</code> <em>if you can reach cell </em><code>(fx, fy)</code> <em>after<strong> exactly</strong></em> <code>t</code> <strong><em>seconds</em></strong>, <em>or</em> <code>false</code> <em>otherwise</em>.</p>
|
||||
|
||||
<p>A cell's <strong>adjacent cells</strong> are the 8 cells around it that share at least one corner with it. You can visit the same cell several times.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/08/05/example2.svg" style="width: 443px; height: 243px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> sx = 2, sy = 4, fx = 7, fy = 7, t = 6
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> Starting at cell (2, 4), we can reach cell (7, 7) in exactly 6 seconds by going through the cells depicted in the picture above.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/08/05/example1.svg" style="width: 383px; height: 202px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> sx = 3, sy = 1, fx = 7, fy = 3, t = 3
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> Starting at cell (3, 1), it takes at least 4 seconds to reach cell (7, 3) by going through the cells depicted in the picture above. Hence, we cannot reach cell (7, 3) at the third second.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= sx, sy, fx, fy <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= t <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,57 @@
|
||||
<p>There is a <strong>simple directed graph</strong> with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>. The graph would form a <strong>tree</strong> if its edges were bi-directional.</p>
|
||||
|
||||
<p>You are given an integer <code>n</code> and a <strong>2D</strong> integer array <code>edges</code>, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> represents a <strong>directed edge</strong> going from node <code>u<sub>i</sub></code> to node <code>v<sub>i</sub></code>.</p>
|
||||
|
||||
<p>An <strong>edge reversal</strong> changes the direction of an edge, i.e., a directed edge going from node <code>u<sub>i</sub></code> to node <code>v<sub>i</sub></code> becomes a directed edge going from node <code>v<sub>i</sub></code> to node <code>u<sub>i</sub></code>.</p>
|
||||
|
||||
<p>For every node <code>i</code> in the range <code>[0, n - 1]</code>, your task is to <strong>independently</strong> calculate the <strong>minimum</strong> number of <strong>edge reversals</strong> required so it is possible to reach any other node starting from node <code>i</code> through a <strong>sequence</strong> of <strong>directed edges</strong>.</p>
|
||||
|
||||
<p>Return <em>an integer array </em><code>answer</code><em>, where </em><code>answer[i]</code><em> is the</em><em> </em> <em><strong>minimum</strong> number of <strong>edge reversals</strong> required so it is possible to reach any other node starting from node </em><code>i</code><em> through a <strong>sequence</strong> of <strong>directed edges</strong>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<p><img height="246" src="https://assets.leetcode.com/uploads/2023/08/26/image-20230826221104-3.png" width="312" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, edges = [[2,0],[2,1],[1,3]]
|
||||
<strong>Output:</strong> [1,1,0,2]
|
||||
<strong>Explanation:</strong> The image above shows the graph formed by the edges.
|
||||
For node 0: after reversing the edge [2,0], it is possible to reach any other node starting from node 0.
|
||||
So, answer[0] = 1.
|
||||
For node 1: after reversing the edge [2,1], it is possible to reach any other node starting from node 1.
|
||||
So, answer[1] = 1.
|
||||
For node 2: it is already possible to reach any other node starting from node 2.
|
||||
So, answer[2] = 0.
|
||||
For node 3: after reversing the edges [1,3] and [2,1], it is possible to reach any other node starting from node 3.
|
||||
So, answer[3] = 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<p><img height="217" src="https://assets.leetcode.com/uploads/2023/08/26/image-20230826225541-2.png" width="322" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, edges = [[1,2],[2,0]]
|
||||
<strong>Output:</strong> [2,0,1]
|
||||
<strong>Explanation:</strong> The image above shows the graph formed by the edges.
|
||||
For node 0: after reversing the edges [2,0] and [1,2], it is possible to reach any other node starting from node 0.
|
||||
So, answer[0] = 2.
|
||||
For node 1: it is already possible to reach any other node starting from node 1.
|
||||
So, answer[1] = 0.
|
||||
For node 2: after reversing the edge [1, 2], it is possible to reach any other node starting from node 2.
|
||||
So, answer[2] = 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= u<sub>i</sub> == edges[i][0] < n</code></li>
|
||||
<li><code>0 <= v<sub>i</sub> == edges[i][1] < n</code></li>
|
||||
<li><code>u<sub>i</sub> != v<sub>i</sub></code></li>
|
||||
<li>The input is generated such that if the edges were bi-directional, the graph would be a tree.</li>
|
||||
</ul>
|
@@ -0,0 +1,49 @@
|
||||
<p>You are given two strings <code>s</code> and <code>t</code> of equal length <code>n</code>. You can perform the following operation on the string <code>s</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li>Remove a <strong>suffix</strong> of <code>s</code> of length <code>l</code> where <code>0 < l < n</code> and append it at the start of <code>s</code>.<br />
|
||||
For example, let <code>s = 'abcd'</code> then in one operation you can remove the suffix <code>'cd'</code> and append it in front of <code>s</code> making <code>s = 'cdab'</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>You are also given an integer <code>k</code>. Return <em>the number of ways in which </em><code>s</code> <em>can be transformed into </em><code>t</code><em> in <strong>exactly</strong> </em><code>k</code><em> operations.</em></p>
|
||||
|
||||
<p>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> s = "abcd", t = "cdab", k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
First way:
|
||||
In first operation, choose suffix from index = 3, so resulting s = "dabc".
|
||||
In second operation, choose suffix from index = 3, so resulting s = "cdab".
|
||||
|
||||
Second way:
|
||||
In first operation, choose suffix from index = 1, so resulting s = "bcda".
|
||||
In second operation, choose suffix from index = 1, so resulting s = "cdab".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "ababab", t = "ababab", k = 1
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
First way:
|
||||
Choose suffix from index = 2, so resulting s = "ababab".
|
||||
|
||||
Second way:
|
||||
Choose suffix from index = 4, so resulting s = "ababab".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= s.length <= 5 * 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>15</sup></code></li>
|
||||
<li><code>s.length == t.length</code></li>
|
||||
<li><code>s</code> and <code>t</code> consist of only lowercase English alphabets.</li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>You are given a <strong>1</strong><strong>-indexed</strong> array <code>nums</code> of <code>n</code> integers.</p>
|
||||
|
||||
<p>A set of numbers is <strong>complete</strong> if the product of every pair of its elements is a perfect square.</p>
|
||||
|
||||
<p>For a subset of the indices set <code>{1, 2, ..., n}</code> represented as <code>{i<sub>1</sub>, i<sub>2</sub>, ..., i<sub>k</sub>}</code>, we define its <strong>element-sum</strong> as: <code>nums[i<sub>1</sub>] + nums[i<sub>2</sub>] + ... + nums[i<sub>k</sub>]</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum element-sum</strong> of a <strong>complete</strong> subset of the indices set</em> <code>{1, 2, ..., n}</code>.</p>
|
||||
|
||||
<p>A perfect square is a number that can be expressed as the product of an integer by itself.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [8,7,3,5,7,2,4,9]
|
||||
<strong>Output:</strong> 16
|
||||
<strong>Explanation:</strong> Apart from the subsets consisting of a single index, there are two other complete subsets of indices: {1,4} and {2,8}.
|
||||
The sum of the elements corresponding to indices 1 and 4 is equal to nums[1] + nums[4] = 8 + 5 = 13.
|
||||
The sum of the elements corresponding to indices 2 and 8 is equal to nums[2] + nums[8] = 7 + 9 = 16.
|
||||
Hence, the maximum element-sum of a complete subset of indices is 16.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,10,3,10,1,13,7,9,4]
|
||||
<strong>Output:</strong> 19
|
||||
<strong>Explanation:</strong> Apart from the subsets consisting of a single index, there are four other complete subsets of indices: {1,4}, {1,9}, {2,8}, {4,9}, and {1,4,9}.
|
||||
The sum of the elements corresponding to indices 1 and 4 is equal to nums[1] + nums[4] = 5 + 10 = 15.
|
||||
The sum of the elements corresponding to indices 1 and 9 is equal to nums[1] + nums[9] = 5 + 4 = 9.
|
||||
The sum of the elements corresponding to indices 2 and 8 is equal to nums[2] + nums[8] = 10 + 9 = 19.
|
||||
The sum of the elements corresponding to indices 4 and 9 is equal to nums[4] + nums[9] = 10 + 4 = 14.
|
||||
The sum of the elements corresponding to indices 1, 4, and 9 is equal to nums[1] + nums[4] + nums[9] = 5 + 10 + 4 = 19.
|
||||
Hence, the maximum element-sum of a complete subset of indices is 19.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n == nums.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>You are given a <strong>0-indexed</strong> 2D integer matrix <code>grid</code> of size <code>3 * 3</code>, representing the number of stones in each cell. The grid contains exactly <code>9</code> stones, and there can be <strong>multiple</strong> stones in a single cell.</p>
|
||||
|
||||
<p>In one move, you can move a single stone from its current cell to any other cell if the two cells share a side.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum number of moves</strong> required to place one stone in each cell</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/08/23/example1-3.svg" style="width: 401px; height: 281px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,1,0],[1,1,1],[1,2,1]]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> One possible sequence of moves to place one stone in each cell is:
|
||||
1- Move one stone from cell (2,1) to cell (2,2).
|
||||
2- Move one stone from cell (2,2) to cell (1,2).
|
||||
3- Move one stone from cell (1,2) to cell (0,2).
|
||||
In total, it takes 3 moves to place one stone in each cell of the grid.
|
||||
It can be shown that 3 is the minimum number of moves required to place one stone in each cell.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/08/23/example2-2.svg" style="width: 401px; height: 281px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,3,0],[1,0,0],[1,0,3]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> One possible sequence of moves to place one stone in each cell is:
|
||||
1- Move one stone from cell (0,1) to cell (0,2).
|
||||
2- Move one stone from cell (0,1) to cell (1,1).
|
||||
3- Move one stone from cell (2,2) to cell (1,2).
|
||||
4- Move one stone from cell (2,2) to cell (2,1).
|
||||
In total, it takes 4 moves to place one stone in each cell of the grid.
|
||||
It can be shown that 4 is the minimum number of moves required to place one stone in each cell.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>grid.length == grid[i].length == 3</code></li>
|
||||
<li><code>0 <= grid[i][j] <= 9</code></li>
|
||||
<li>Sum of <code>grid</code> is equal to <code>9</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,66 @@
|
||||
<p>You are the owner of a company that creates alloys using various types of metals. There are <code>n</code> different types of metals available, and you have access to <code>k</code> machines that can be used to create alloys. Each machine requires a specific amount of each metal type to create an alloy.</p>
|
||||
|
||||
<p>For the <code>i<sup>th</sup></code> machine to create an alloy, it needs <code>composition[i][j]</code> units of metal of type <code>j</code>. Initially, you have <code>stock[i]</code> units of metal type <code>i</code>, and purchasing one unit of metal type <code>i</code> costs <code>cost[i]</code> coins.</p>
|
||||
|
||||
<p>Given integers <code>n</code>, <code>k</code>, <code>budget</code>, a <strong>1-indexed</strong> 2D array <code>composition</code>, and <strong>1-indexed</strong> arrays <code>stock</code> and <code>cost</code>, your goal is to <strong>maximize</strong> the number of alloys the company can create while staying within the budget of <code>budget</code> coins.</p>
|
||||
|
||||
<p><strong>All alloys must be created with the same machine.</strong></p>
|
||||
|
||||
<p>Return <em>the maximum number of alloys that the company can create</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, k = 2, budget = 15, composition = [[1,1,1],[1,1,10]], stock = [0,0,0], cost = [1,2,3]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> It is optimal to use the 1<sup>st</sup> machine to create alloys.
|
||||
To create 2 alloys we need to buy the:
|
||||
- 2 units of metal of the 1<sup>st</sup> type.
|
||||
- 2 units of metal of the 2<sup>nd</sup> type.
|
||||
- 2 units of metal of the 3<sup>rd</sup> type.
|
||||
In total, we need 2 * 1 + 2 * 2 + 2 * 3 = 12 coins, which is smaller than or equal to budget = 15.
|
||||
Notice that we have 0 units of metal of each type and we have to buy all the required units of metal.
|
||||
It can be proven that we can create at most 2 alloys.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, k = 2, budget = 15, composition = [[1,1,1],[1,1,10]], stock = [0,0,100], cost = [1,2,3]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> It is optimal to use the 2<sup>nd</sup> machine to create alloys.
|
||||
To create 5 alloys we need to buy:
|
||||
- 5 units of metal of the 1<sup>st</sup> type.
|
||||
- 5 units of metal of the 2<sup>nd</sup> type.
|
||||
- 0 units of metal of the 3<sup>rd</sup> type.
|
||||
In total, we need 5 * 1 + 5 * 2 + 0 * 3 = 15 coins, which is smaller than or equal to budget = 15.
|
||||
It can be proven that we can create at most 5 alloys.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2, k = 3, budget = 10, composition = [[2,1],[1,2],[1,1]], stock = [1,1], cost = [5,5]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> It is optimal to use the 3<sup>rd</sup> machine to create alloys.
|
||||
To create 2 alloys we need to buy the:
|
||||
- 1 unit of metal of the 1<sup>st</sup> type.
|
||||
- 1 unit of metal of the 2<sup>nd</sup> type.
|
||||
In total, we need 1 * 5 + 1 * 5 = 10 coins, which is smaller than or equal to budget = 10.
|
||||
It can be proven that we can create at most 2 alloys.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n, k <= 100</code></li>
|
||||
<li><code>0 <= budget <= 10<sup>8</sup></code></li>
|
||||
<li><code>composition.length == k</code></li>
|
||||
<li><code>composition[i].length == n</code></li>
|
||||
<li><code>1 <= composition[i][j] <= 100</code></li>
|
||||
<li><code>stock.length == cost.length == n</code></li>
|
||||
<li><code>0 <= stock[i] <= 10<sup>8</sup></code></li>
|
||||
<li><code>1 <= cost[i] <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>You are given a <strong>2D</strong> integer array <code>coordinates</code> and an integer <code>k</code>, where <code>coordinates[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> are the coordinates of the <code>i<sup>th</sup></code> point in a 2D plane.</p>
|
||||
|
||||
<p>We define the <strong>distance</strong> between two points <code>(x<sub>1</sub>, y<sub>1</sub>)</code> and <code>(x<sub>2</sub>, y<sub>2</sub>)</code> as <code>(x1 XOR x2) + (y1 XOR y2)</code> where <code>XOR</code> is the bitwise <code>XOR</code> operation.</p>
|
||||
|
||||
<p>Return <em>the number of pairs </em><code>(i, j)</code><em> such that </em><code>i < j</code><em> and the distance between points </em><code>i</code><em> and </em><code>j</code><em> is equal to </em><code>k</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> coordinates = [[1,2],[4,2],[1,3],[5,2]], k = 5
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can choose the following pairs:
|
||||
- (0,1): Because we have (1 XOR 4) + (2 XOR 2) = 5.
|
||||
- (2,3): Because we have (1 XOR 5) + (3 XOR 2) = 5.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> coordinates = [[1,3],[1,3],[1,3],[1,3],[1,3]], k = 0
|
||||
<strong>Output:</strong> 10
|
||||
<strong>Explanation:</strong> Any two chosen pairs will have a distance of 0. There are 10 ways to choose two pairs.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= coordinates.length <= 50000</code></li>
|
||||
<li><code>0 <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>6</sup></code></li>
|
||||
<li><code>0 <= k <= 100</code></li>
|
||||
</ul>
|
@@ -0,0 +1,47 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>Return <em>an integer that denotes the <strong>sum</strong> of elements in </em><code>nums</code><em> whose corresponding <strong>indices</strong> have <strong>exactly</strong> </em><code>k</code><em> set bits in their binary representation.</em></p>
|
||||
|
||||
<p>The <strong>set bits</strong> in an integer are the <code>1</code>'s present when it is written in binary.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, the binary representation of <code>21</code> is <code>10101</code>, which has <code>3</code> set bits.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,10,1,5,2], k = 1
|
||||
<strong>Output:</strong> 13
|
||||
<strong>Explanation:</strong> The binary representation of the indices are:
|
||||
0 = 000<sub>2</sub>
|
||||
1 = 001<sub>2</sub>
|
||||
2 = 010<sub>2</sub>
|
||||
3 = 011<sub>2</sub>
|
||||
4 = 100<sub>2
|
||||
</sub>Indices 1, 2, and 4 have k = 1 set bits in their binary representation.
|
||||
Hence, the answer is nums[1] + nums[2] + nums[4] = 13.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,3,2,1], k = 2
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The binary representation of the indices are:
|
||||
0 = 00<sub>2</sub>
|
||||
1 = 01<sub>2</sub>
|
||||
2 = 10<sub>2</sub>
|
||||
3 = 11<sub>2
|
||||
</sub>Only index 3 has k = 2 set bits in its binary representation.
|
||||
Hence, the answer is nums[3] = 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= k <= 10</code></li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code> where <code>n</code> is the total number of students in the class. The class teacher tries to select a group of students so that all the students remain happy.</p>
|
||||
|
||||
<p>The <code>i<sup>th</sup></code> student will become happy if one of these two conditions is met:</p>
|
||||
|
||||
<ul>
|
||||
<li>The student is selected and the total number of selected students is<strong> strictly greater than</strong> <code>nums[i]</code>.</li>
|
||||
<li>The student is not selected and the total number of selected students is <strong>strictly</strong> <strong>less than</strong> <code>nums[i]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of ways to select a group of students so that everyone remains happy.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
The two possible ways are:
|
||||
The class teacher selects no student.
|
||||
The class teacher selects both students to form the group.
|
||||
If the class teacher selects just one student to form a group then the both students will not be happy. Therefore, there are only two possible ways.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [6,0,3,3,6,7,2,7]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
The three possible ways are:
|
||||
The class teacher selects the student with index = 1 to form the group.
|
||||
The class teacher selects the students with index = 1, 2, 3, 6 to form the group.
|
||||
The class teacher selects all the students to form the group.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] < nums.length</code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user