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,46 @@
|
||||
<p>You are given an integer array <code>rolls</code> of length <code>n</code> and an integer <code>k</code>. You roll a <code>k</code> sided dice numbered from <code>1</code> to <code>k</code>, <code>n</code> times, where the result of the <code>i<sup>th</sup></code> roll is <code>rolls[i]</code>.</p>
|
||||
|
||||
<p>Return<em> the length of the <strong>shortest</strong> sequence of rolls that <strong>cannot</strong> be taken from </em><code>rolls</code>.</p>
|
||||
|
||||
<p>A <strong>sequence of rolls</strong> of length <code>len</code> is the result of rolling a <code>k</code> sided dice <code>len</code> times.</p>
|
||||
|
||||
<p><strong>Note</strong> that the sequence taken does not have to be consecutive as long as it is in order.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> rolls = [4,2,1,2,3,3,2,4,1], k = 4
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Every sequence of rolls of length 1, [1], [2], [3], [4], can be taken from rolls.
|
||||
Every sequence of rolls of length 2, [1, 1], [1, 2], ..., [4, 4], can be taken from rolls.
|
||||
The sequence [1, 4, 2] cannot be taken from rolls, so we return 3.
|
||||
Note that there are other sequences that cannot be taken from rolls.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> rolls = [1,1,2,2], k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Every sequence of rolls of length 1, [1], [2], can be taken from rolls.
|
||||
The sequence [2, 1] cannot be taken from rolls, so we return 2.
|
||||
Note that there are other sequences that cannot be taken from rolls but [2, 1] is the shortest.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> rolls = [1,1,3,2,2,2,3,3], k = 4
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The sequence [4] cannot be taken from rolls, so we return 1.
|
||||
Note that there are other sequences that cannot be taken from rolls but [4] is the shortest.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == rolls.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= rolls[i] <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,43 @@
|
||||
<p>You are given a <strong>0-indexed</strong> positive integer array <code>nums</code> and a positive integer <code>k</code>.</p>
|
||||
|
||||
<p>A pair of numbers <code>(num1, num2)</code> is called <strong>excellent</strong> if the following conditions are satisfied:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Both</strong> the numbers <code>num1</code> and <code>num2</code> exist in the array <code>nums</code>.</li>
|
||||
<li>The sum of the number of set bits in <code>num1 OR num2</code> and <code>num1 AND num2</code> is greater than or equal to <code>k</code>, where <code>OR</code> is the bitwise <strong>OR</strong> operation and <code>AND</code> is the bitwise <strong>AND</strong> operation.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of <strong>distinct</strong> excellent pairs</em>.</p>
|
||||
|
||||
<p>Two pairs <code>(a, b)</code> and <code>(c, d)</code> are considered distinct if either <code>a != c</code> or <code>b != d</code>. For example, <code>(1, 2)</code> and <code>(2, 1)</code> are distinct.</p>
|
||||
|
||||
<p><strong>Note</strong> that a pair <code>(num1, num2)</code> such that <code>num1 == num2</code> can also be excellent if you have at least <strong>one</strong> occurrence of <code>num1</code> in the array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,1], k = 3
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The excellent pairs are the following:
|
||||
- (3, 3). (3 AND 3) and (3 OR 3) are both equal to (11) in binary. The total number of set bits is 2 + 2 = 4, which is greater than or equal to k = 3.
|
||||
- (2, 3) and (3, 2). (2 AND 3) is equal to (10) in binary, and (2 OR 3) is equal to (11) in binary. The total number of set bits is 1 + 2 = 3.
|
||||
- (1, 3) and (3, 1). (1 AND 3) is equal to (01) in binary, and (1 OR 3) is equal to (11) in binary. The total number of set bits is 1 + 2 = 3.
|
||||
So the number of excellent pairs is 5.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,1,1], k = 10
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are no excellent pairs for this array.
|
||||
</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 <= k <= 60</code></li>
|
||||
</ul>
|
@@ -0,0 +1,42 @@
|
||||
<p>Given an integer array <code>nums</code>, return <em>the number of <strong>subarrays</strong> filled with </em><code>0</code>.</p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous non-empty sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,0,0,2,0,0,4]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong>
|
||||
There are 4 occurrences of [0] as a subarray.
|
||||
There are 2 occurrences of [0,0] as a subarray.
|
||||
There is no occurrence of a subarray with a size more than 2 filled with 0. Therefore, we return 6.</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,0,0,2,0,0]
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:
|
||||
</strong>There are 5 occurrences of [0] as a subarray.
|
||||
There are 3 occurrences of [0,0] as a subarray.
|
||||
There is 1 occurrence of [0,0,0] as a subarray.
|
||||
There is no occurrence of a subarray with a size more than 3 filled with 0. Therefore, we return 9.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,10,2019]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There is no subarray filled with 0. Therefore, we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,51 @@
|
||||
<p>You are given an integer array <code>ranks</code> and a character array <code>suits</code>. You have <code>5</code> cards where the <code>i<sup>th</sup></code> card has a rank of <code>ranks[i]</code> and a suit of <code>suits[i]</code>.</p>
|
||||
|
||||
<p>The following are the types of <strong>poker hands</strong> you can make from best to worst:</p>
|
||||
|
||||
<ol>
|
||||
<li><code>"Flush"</code>: Five cards of the same suit.</li>
|
||||
<li><code>"Three of a Kind"</code>: Three cards of the same rank.</li>
|
||||
<li><code>"Pair"</code>: Two cards of the same rank.</li>
|
||||
<li><code>"High Card"</code>: Any single card.</li>
|
||||
</ol>
|
||||
|
||||
<p>Return <em>a string representing the <strong>best</strong> type of <strong>poker hand</strong> you can make with the given cards.</em></p>
|
||||
|
||||
<p><strong>Note</strong> that the return values are <strong>case-sensitive</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> ranks = [13,2,3,1,9], suits = ["a","a","a","a","a"]
|
||||
<strong>Output:</strong> "Flush"
|
||||
<strong>Explanation:</strong> The hand with all the cards consists of 5 cards with the same suit, so we have a "Flush".
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> ranks = [4,4,2,4,4], suits = ["d","a","a","b","c"]
|
||||
<strong>Output:</strong> "Three of a Kind"
|
||||
<strong>Explanation:</strong> The hand with the first, second, and fourth card consists of 3 cards with the same rank, so we have a "Three of a Kind".
|
||||
Note that we could also make a "Pair" hand but "Three of a Kind" is a better hand.
|
||||
Also note that other cards could be used to make the "Three of a Kind" hand.</pre>
|
||||
|
||||
<p><strong>Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> ranks = [10,10,2,12,9], suits = ["a","b","c","a","d"]
|
||||
<strong>Output:</strong> "Pair"
|
||||
<strong>Explanation:</strong> The hand with the first and second card consists of 2 cards with the same rank, so we have a "Pair".
|
||||
Note that we cannot make a "Flush" or a "Three of a Kind".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>ranks.length == suits.length == 5</code></li>
|
||||
<li><code>1 <= ranks[i] <= 13</code></li>
|
||||
<li><code>'a' <= suits[i] <= 'd'</code></li>
|
||||
<li>No two cards have the same rank and suit.</li>
|
||||
</ul>
|
@@ -0,0 +1,33 @@
|
||||
<p>Given a <strong>0-indexed</strong> <code>n x n</code> integer matrix <code>grid</code>, <em>return the number of pairs </em><code>(R<sub>i</sub>, C<sub>j</sub>)</code><em> such that row </em><code>R<sub>i</sub></code><em> and column </em><code>C<sub>j</sub></code><em> are equal</em>.</p>
|
||||
|
||||
<p>A row and column pair is considered equal if they contain the same elements in the same order (i.e. an equal array).</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/01/ex1.jpg" style="width: 150px; height: 153px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[3,2,1],[1,7,6],[2,7,7]]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> There is 1 equal row and column pair:
|
||||
- (Row 2, Column 1): [2,7,7]
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/06/01/ex2.jpg" style="width: 200px; height: 209px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> There are 3 equal row and column pairs:
|
||||
- (Row 0, Column 0): [3,1,2,2]
|
||||
- (Row 2, Column 2): [2,4,2,2]
|
||||
- (Row 3, Column 2): [2,4,2,2]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length == grid[i].length</code></li>
|
||||
<li><code>1 <= n <= 200</code></li>
|
||||
<li><code>1 <= grid[i][j] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,40 @@
|
||||
<p>Given a string <code>s</code> consisting of lowercase English letters, return <em>the first letter to appear <strong>twice</strong></em>.</p>
|
||||
|
||||
<p><strong>Note</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>A letter <code>a</code> appears twice before another letter <code>b</code> if the <strong>second</strong> occurrence of <code>a</code> is before the <strong>second</strong> occurrence of <code>b</code>.</li>
|
||||
<li><code>s</code> will contain at least one letter that appears twice.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abccbaacz"
|
||||
<strong>Output:</strong> "c"
|
||||
<strong>Explanation:</strong>
|
||||
The letter 'a' appears on the indexes 0, 5 and 6.
|
||||
The letter 'b' appears on the indexes 1 and 4.
|
||||
The letter 'c' appears on the indexes 2, 3 and 7.
|
||||
The letter 'z' appears on the index 8.
|
||||
The letter 'c' is the first letter to appear twice, because out of all the letters the index of its second occurrence is the smallest.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcdd"
|
||||
<strong>Output:</strong> "d"
|
||||
<strong>Explanation:</strong>
|
||||
The only letter that appears twice is 'd' so we return 'd'.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= s.length <= 100</code></li>
|
||||
<li><code>s</code> consists of lowercase English letters.</li>
|
||||
<li><code>s</code> has at least one repeated letter.</li>
|
||||
</ul>
|
@@ -0,0 +1,44 @@
|
||||
<p>Design a number container system that can do the following:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Insert </strong>or <strong>Replace</strong> a number at the given index in the system.</li>
|
||||
<li><strong>Return </strong>the smallest index for the given number in the system.</li>
|
||||
</ul>
|
||||
|
||||
<p>Implement the <code>NumberContainers</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>NumberContainers()</code> Initializes the number container system.</li>
|
||||
<li><code>void change(int index, int number)</code> Fills the container at <code>index</code> with the <code>number</code>. If there is already a number at that <code>index</code>, replace it.</li>
|
||||
<li><code>int find(int number)</code> Returns the smallest index for the given <code>number</code>, or <code>-1</code> if there is no index that is filled by <code>number</code> in the system.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["NumberContainers", "find", "change", "change", "change", "change", "find", "change", "find"]
|
||||
[[], [10], [2, 10], [1, 10], [3, 10], [5, 10], [10], [1, 20], [10]]
|
||||
<strong>Output</strong>
|
||||
[null, -1, null, null, null, null, 1, null, 2]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
NumberContainers nc = new NumberContainers();
|
||||
nc.find(10); // There is no index that is filled with number 10. Therefore, we return -1.
|
||||
nc.change(2, 10); // Your container at index 2 will be filled with number 10.
|
||||
nc.change(1, 10); // Your container at index 1 will be filled with number 10.
|
||||
nc.change(3, 10); // Your container at index 3 will be filled with number 10.
|
||||
nc.change(5, 10); // Your container at index 5 will be filled with number 10.
|
||||
nc.find(10); // Number 10 is at the indices 1, 2, 3, and 5. Since the smallest index that is filled with 10 is 1, we return 1.
|
||||
nc.change(1, 20); // Your container at index 1 will be filled with number 20. Note that index 1 was filled with 10 and then replaced with 20.
|
||||
nc.find(10); // Number 10 is at the indices 2, 3, and 5. The smallest index that is filled with 10 is 2. Therefore, we return 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= index, number <= 10<sup>9</sup></code></li>
|
||||
<li>At most <code>10<sup>5</sup></code> calls will be made <strong>in total</strong> to <code>change</code> and <code>find</code>.</li>
|
||||
</ul>
|
@@ -0,0 +1,63 @@
|
||||
<p>Design a food rating system that can do the following:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Modify</strong> the rating of a food item listed in the system.</li>
|
||||
<li>Return the highest-rated food item for a type of cuisine in the system.</li>
|
||||
</ul>
|
||||
|
||||
<p>Implement the <code>FoodRatings</code> class:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>FoodRatings(String[] foods, String[] cuisines, int[] ratings)</code> Initializes the system. The food items are described by <code>foods</code>, <code>cuisines</code> and <code>ratings</code>, all of which have a length of <code>n</code>.
|
||||
|
||||
<ul>
|
||||
<li><code>foods[i]</code> is the name of the <code>i<sup>th</sup></code> food,</li>
|
||||
<li><code>cuisines[i]</code> is the type of cuisine of the <code>i<sup>th</sup></code> food, and</li>
|
||||
<li><code>ratings[i]</code> is the initial rating of the <code>i<sup>th</sup></code> food.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><code>void changeRating(String food, int newRating)</code> Changes the rating of the food item with the name <code>food</code>.</li>
|
||||
<li><code>String highestRated(String cuisine)</code> Returns the name of the food item that has the highest rating for the given type of <code>cuisine</code>. If there is a tie, return the item with the <strong>lexicographically smaller</strong> name.</li>
|
||||
</ul>
|
||||
|
||||
<p>Note that a string <code>x</code> is lexicographically smaller than string <code>y</code> if <code>x</code> comes before <code>y</code> in dictionary order, that is, either <code>x</code> is a prefix of <code>y</code>, or if <code>i</code> is the first position such that <code>x[i] != y[i]</code>, then <code>x[i]</code> comes before <code>y[i]</code> in alphabetic order.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input</strong>
|
||||
["FoodRatings", "highestRated", "highestRated", "changeRating", "highestRated", "changeRating", "highestRated"]
|
||||
[[["kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"], ["korean", "japanese", "japanese", "greek", "japanese", "korean"], [9, 12, 8, 15, 14, 7]], ["korean"], ["japanese"], ["sushi", 16], ["japanese"], ["ramen", 16], ["japanese"]]
|
||||
<strong>Output</strong>
|
||||
[null, "kimchi", "ramen", null, "sushi", null, "ramen"]
|
||||
|
||||
<strong>Explanation</strong>
|
||||
FoodRatings foodRatings = new FoodRatings(["kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"], ["korean", "japanese", "japanese", "greek", "japanese", "korean"], [9, 12, 8, 15, 14, 7]);
|
||||
foodRatings.highestRated("korean"); // return "kimchi"
|
||||
// "kimchi" is the highest rated korean food with a rating of 9.
|
||||
foodRatings.highestRated("japanese"); // return "ramen"
|
||||
// "ramen" is the highest rated japanese food with a rating of 14.
|
||||
foodRatings.changeRating("sushi", 16); // "sushi" now has a rating of 16.
|
||||
foodRatings.highestRated("japanese"); // return "sushi"
|
||||
// "sushi" is the highest rated japanese food with a rating of 16.
|
||||
foodRatings.changeRating("ramen", 16); // "ramen" now has a rating of 16.
|
||||
foodRatings.highestRated("japanese"); // return "ramen"
|
||||
// Both "sushi" and "ramen" have a rating of 16.
|
||||
// However, "ramen" is lexicographically smaller than "sushi".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>n == foods.length == cuisines.length == ratings.length</code></li>
|
||||
<li><code>1 <= foods[i].length, cuisines[i].length <= 10</code></li>
|
||||
<li><code>foods[i]</code>, <code>cuisines[i]</code> consist of lowercase English letters.</li>
|
||||
<li><code>1 <= ratings[i] <= 10<sup>8</sup></code></li>
|
||||
<li>All the strings in <code>foods</code> are <strong>distinct</strong>.</li>
|
||||
<li><code>food</code> will be the name of a food item in the system across all calls to <code>changeRating</code>.</li>
|
||||
<li><code>cuisine</code> will be a type of cuisine of <strong>at least one</strong> food item in the system across all calls to <code>highestRated</code>.</li>
|
||||
<li>At most <code>2 * 10<sup>4</sup></code> calls <strong>in total</strong> will be made to <code>changeRating</code> and <code>highestRated</code>.</li>
|
||||
</ul>
|
Reference in New Issue
Block a user