mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-04 23:11:41 +08:00
update
This commit is contained in:
34
leetcode/problem/check-knight-tour-configuration.html
Normal file
34
leetcode/problem/check-knight-tour-configuration.html
Normal file
@@ -0,0 +1,34 @@
|
||||
<p>There is a knight on an <code>n x n</code> chessboard. In a valid configuration, the knight starts <strong>at the top-left cell</strong> of the board and visits every cell on the board <strong>exactly once</strong>.</p>
|
||||
|
||||
<p>You are given an <code>n x n</code> integer matrix <code>grid</code> consisting of distinct integers from the range <code>[0, n * n - 1]</code> where <code>grid[row][col]</code> indicates that the cell <code>(row, col)</code> is the <code>grid[row][col]<sup>th</sup></code> cell that the knight visited. The moves are <strong>0-indexed</strong>.</p>
|
||||
|
||||
<p>Return <code>true</code> <em>if</em> <code>grid</code> <em>represents a valid configuration of the knight's movements or</em> <code>false</code> <em>otherwise</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that a valid knight move consists of moving two squares vertically and one square horizontally, or two squares horizontally and one square vertically. The figure below illustrates all the possible eight moves of a knight from some cell.</p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2018/10/12/knight.png" style="width: 300px; height: 300px;" />
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/28/yetgriddrawio-5.png" style="width: 251px; height: 251px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,11,16,5,20],[17,4,19,10,15],[12,1,8,21,6],[3,18,23,14,9],[24,13,2,7,22]]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> The above diagram represents the grid. It can be shown that it is a valid configuration.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/28/yetgriddrawio-6.png" style="width: 151px; height: 151px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[0,3,6],[5,8,1],[2,7,4]]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> The above diagram represents the grid. The 8<sup>th</sup> move of the knight is not valid considering its position after the 7<sup>th</sup> move.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == grid.length == grid[i].length</code></li>
|
||||
<li><code>3 <= n <= 7</code></li>
|
||||
<li><code>0 <= grid[row][col] < n * n</code></li>
|
||||
<li>All integers in <code>grid</code> are <strong>unique</strong>.</li>
|
||||
</ul>
|
65
leetcode/problem/count-number-of-possible-root-nodes.html
Normal file
65
leetcode/problem/count-number-of-possible-root-nodes.html
Normal file
@@ -0,0 +1,65 @@
|
||||
<p>Alice has an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>. The tree is represented as a 2D integer array <code>edges</code> of length <code>n - 1</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.</p>
|
||||
|
||||
<p>Alice wants Bob to find the root of the tree. She allows Bob to make several <strong>guesses</strong> about her tree. In one guess, he does the following:</p>
|
||||
|
||||
<ul>
|
||||
<li>Chooses two <strong>distinct</strong> integers <code>u</code> and <code>v</code> such that there exists an edge <code>[u, v]</code> in the tree.</li>
|
||||
<li>He tells Alice that <code>u</code> is the <strong>parent</strong> of <code>v</code> in the tree.</li>
|
||||
</ul>
|
||||
|
||||
<p>Bob's guesses are represented by a 2D integer array <code>guesses</code> where <code>guesses[j] = [u<sub>j</sub>, v<sub>j</sub>]</code> indicates Bob guessed <code>u<sub>j</sub></code> to be the parent of <code>v<sub>j</sub></code>.</p>
|
||||
|
||||
<p>Alice being lazy, does not reply to each of Bob's guesses, but just says that <strong>at least</strong> <code>k</code> of his guesses are <code>true</code>.</p>
|
||||
|
||||
<p>Given the 2D integer arrays <code>edges</code>, <code>guesses</code> and the integer <code>k</code>, return <em>the <strong>number of possible nodes</strong> that can be the root of Alice's tree</em>. If there is no such tree, return <code>0</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/12/19/ex-1.png" style="width: 727px; height: 250px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1],[1,2],[1,3],[4,2]], guesses = [[1,3],[0,1],[1,0],[2,4]], k = 3
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
Root = 0, correct guesses = [1,3], [0,1], [2,4]
|
||||
Root = 1, correct guesses = [1,3], [1,0], [2,4]
|
||||
Root = 2, correct guesses = [1,3], [1,0], [2,4]
|
||||
Root = 3, correct guesses = [1,0], [2,4]
|
||||
Root = 4, correct guesses = [1,3], [1,0]
|
||||
Considering 0, 1, or 2 as root node leads to 3 correct guesses.
|
||||
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/12/19/ex-2.png" style="width: 600px; height: 303px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1],[1,2],[2,3],[3,4]], guesses = [[1,0],[3,4],[2,1],[3,2]], k = 1
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong>
|
||||
Root = 0, correct guesses = [3,4]
|
||||
Root = 1, correct guesses = [1,0], [3,4]
|
||||
Root = 2, correct guesses = [1,0], [2,1], [3,4]
|
||||
Root = 3, correct guesses = [1,0], [2,1], [3,2], [3,4]
|
||||
Root = 4, correct guesses = [1,0], [2,1], [3,2]
|
||||
Considering any node as root will give at least 1 correct guess.
|
||||
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= guesses.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub>, u<sub>j</sub>, v<sub>j</sub> <= n - 1</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li><code>u<sub>j</sub> != v<sub>j</sub></code></li>
|
||||
<li><code>edges</code> represents a valid tree.</li>
|
||||
<li><code>guesses[j]</code> is an edge of the tree.</li>
|
||||
<li><code>guesses</code> is unique.</li>
|
||||
<li><code>0 <= k <= guesses.length</code></li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. In one operation, you can:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose two different indices <code>i</code> and <code>j</code> such that <code>0 <= i, j < nums.length</code>.</li>
|
||||
<li>Choose a non-negative integer <code>k</code> such that the <code>k<sup>th</sup></code> bit (<strong>0-indexed</strong>) in the binary representation of <code>nums[i]</code> and <code>nums[j]</code> is <code>1</code>.</li>
|
||||
<li>Subtract <code>2<sup>k</sup></code> from <code>nums[i]</code> and <code>nums[j]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>A subarray is <strong>beautiful</strong> if it is possible to make all of its elements equal to <code>0</code> after applying the above operation any number of times.</p>
|
||||
|
||||
<p>Return <em>the number of <strong>beautiful subarrays</strong> in the array</em> <code>nums</code>.</p>
|
||||
|
||||
<p>A subarray is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,3,1,2,4]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> There are 2 beautiful subarrays in nums: [4,<u>3,1,2</u>,4] and [<u>4,3,1,2,4</u>].
|
||||
- We can make all elements in the subarray [3,1,2] equal to 0 in the following way:
|
||||
- Choose [<u>3</u>, 1, <u>2</u>] and k = 1. Subtract 2<sup>1</sup> from both numbers. The subarray becomes [1, 1, 0].
|
||||
- Choose [<u>1</u>, <u>1</u>, 0] and k = 0. Subtract 2<sup>0</sup> from both numbers. The subarray becomes [0, 0, 0].
|
||||
- We can make all elements in the subarray [4,3,1,2,4] equal to 0 in the following way:
|
||||
- Choose [<u>4</u>, 3, 1, 2, <u>4</u>] and k = 2. Subtract 2<sup>2</sup> from both numbers. The subarray becomes [0, 3, 1, 2, 0].
|
||||
- Choose [0, <u>3</u>, <u>1</u>, 2, 0] and k = 0. Subtract 2<sup>0</sup> from both numbers. The subarray becomes [0, 2, 0, 2, 0].
|
||||
- Choose [0, <u>2</u>, 0, <u>2</u>, 0] and k = 1. Subtract 2<sup>1</sup> from both numbers. The subarray becomes [0, 0, 0, 0, 0].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,10,4]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are no beautiful subarrays in nums.
|
||||
</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] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,41 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array of string <code>words</code> and two integers <code>left</code> and <code>right</code>.</p>
|
||||
|
||||
<p>A string is called a <strong>vowel string</strong> if it starts with a vowel character and ends with a vowel character where vowel characters are <code>'a'</code>, <code>'e'</code>, <code>'i'</code>, <code>'o'</code>, and <code>'u'</code>.</p>
|
||||
|
||||
<p>Return <em>the number of vowel strings </em><code>words[i]</code><em> where </em><code>i</code><em> belongs to the inclusive range </em><code>[left, right]</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["are","amy","u"], left = 0, right = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
- "are" is a vowel string because it starts with 'a' and ends with 'e'.
|
||||
- "amy" is not a vowel string because it does not end with a vowel.
|
||||
- "u" is a vowel string because it starts with 'u' and ends with 'u'.
|
||||
The number of vowel strings in the mentioned range is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["hey","aeo","mu","ooo","artro"], left = 1, right = 4
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
- "aeo" is a vowel string because it starts with 'a' and ends with 'o'.
|
||||
- "mu" is not a vowel string because it does not start with a vowel.
|
||||
- "ooo" is a vowel string because it starts with 'o' and ends with 'o'.
|
||||
- "artro" is a vowel string because it starts with 'a' and ends with 'o'.
|
||||
The number of vowel strings in the mentioned range is 3.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 1000</code></li>
|
||||
<li><code>1 <= words[i].length <= 10</code></li>
|
||||
<li><code>words[i]</code> consists of only lowercase English letters.</li>
|
||||
<li><code>0 <= left <= right < words.length</code></li>
|
||||
</ul>
|
34
leetcode/problem/count-total-number-of-colored-cells.html
Normal file
34
leetcode/problem/count-total-number-of-colored-cells.html
Normal file
@@ -0,0 +1,34 @@
|
||||
<p>There exists an infinitely large two-dimensional grid of uncolored unit cells. You are given a positive integer <code>n</code>, indicating that you must do the following routine for <code>n</code> minutes:</p>
|
||||
|
||||
<ul>
|
||||
<li>At the first minute, color <strong>any</strong> arbitrary unit cell blue.</li>
|
||||
<li>Every minute thereafter, color blue <strong>every</strong> uncolored cell that touches a blue cell.</li>
|
||||
</ul>
|
||||
|
||||
<p>Below is a pictorial representation of the state of the grid after minutes 1, 2, and 3.</p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/10/example-copy-2.png" style="width: 500px; height: 279px;" />
|
||||
<p>Return <em>the number of <strong>colored cells</strong> at the end of </em><code>n</code> <em>minutes</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> After 1 minute, there is only 1 blue cell, so we return 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> After 2 minutes, there are 4 colored cells on the boundary and 1 in the center, so we return 5.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
53
leetcode/problem/count-ways-to-group-overlapping-ranges.html
Normal file
53
leetcode/problem/count-ways-to-group-overlapping-ranges.html
Normal file
@@ -0,0 +1,53 @@
|
||||
<p>You are given a 2D integer array <code>ranges</code> where <code>ranges[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> denotes that all integers between <code>start<sub>i</sub></code> and <code>end<sub>i</sub></code> (both <strong>inclusive</strong>) are contained in the <code>i<sup>th</sup></code> range.</p>
|
||||
|
||||
<p>You are to split <code>ranges</code> into <strong>two</strong> (possibly empty) groups such that:</p>
|
||||
|
||||
<ul>
|
||||
<li>Each range belongs to exactly one group.</li>
|
||||
<li>Any two <strong>overlapping</strong> ranges must belong to the <strong>same</strong> group.</li>
|
||||
</ul>
|
||||
|
||||
<p>Two ranges are said to be <strong>overlapping</strong> if there exists at least <strong>one</strong> integer that is present in both ranges.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, <code>[1, 3]</code> and <code>[2, 5]</code> are overlapping because <code>2</code> and <code>3</code> occur in both ranges.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>total number</strong> of ways to split</em> <code>ranges</code> <em>into two groups</em>. Since the answer may be very 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> ranges = [[6,10],[5,15]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
The two ranges are overlapping, so they must be in the same group.
|
||||
Thus, there are two possible ways:
|
||||
- Put both the ranges together in group 1.
|
||||
- Put both the ranges together in group 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> ranges = [[1,3],[10,20],[2,5],[4,8]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
Ranges [1,3], and [2,5] are overlapping. So, they must be in the same group.
|
||||
Again, ranges [2,5] and [4,8] are also overlapping. So, they must also be in the same group.
|
||||
Thus, there are four possible ways to group them:
|
||||
- All the ranges in group 1.
|
||||
- All the ranges in group 2.
|
||||
- Ranges [1,3], [2,5], and [4,8] in group 1 and [10,20] in group 2.
|
||||
- Ranges [1,3], [2,5], and [4,8] in group 2 and [10,20] in group 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= ranges.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>ranges[i].length == 2</code></li>
|
||||
<li><code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
41
leetcode/problem/distribute-money-to-maximum-children.html
Normal file
41
leetcode/problem/distribute-money-to-maximum-children.html
Normal file
@@ -0,0 +1,41 @@
|
||||
<p>You are given an integer <code>money</code> denoting the amount of money (in dollars) that you have and another integer <code>children</code> denoting the number of children that you must distribute the money to.</p>
|
||||
|
||||
<p>You have to distribute the money according to the following rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>All money must be distributed.</li>
|
||||
<li>Everyone must receive at least <code>1</code> dollar.</li>
|
||||
<li>Nobody receives <code>4</code> dollars.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of children who may receive <strong>exactly</strong> </em><code>8</code> <em>dollars if you distribute the money according to the aforementioned rules</em>. If there is no way to distribute the money, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> money = 20, children = 3
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
The maximum number of children with 8 dollars will be 1. One of the ways to distribute the money is:
|
||||
- 8 dollars to the first child.
|
||||
- 9 dollars to the second child.
|
||||
- 3 dollars to the third child.
|
||||
It can be proven that no distribution exists such that number of children getting 8 dollars is greater than 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> money = 16, children = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Each child can be given 8 dollars.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= money <= 200</code></li>
|
||||
<li><code>2 <= children <= 30</code></li>
|
||||
</ul>
|
@@ -0,0 +1,45 @@
|
||||
<p>You are given an array <code>nums</code> consisting of positive integers.</p>
|
||||
|
||||
<p>Starting with <code>score = 0</code>, apply the following algorithm:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose the smallest integer of the array that is not marked. If there is a tie, choose the one with the smallest index.</li>
|
||||
<li>Add the value of the chosen integer to <code>score</code>.</li>
|
||||
<li>Mark <strong>the chosen element and its two adjacent elements if they exist</strong>.</li>
|
||||
<li>Repeat until all the array elements are marked.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the score you get after applying the above algorithm</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,1,3,4,5,2]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> We mark the elements as follows:
|
||||
- 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [<u>2</u>,<u>1</u>,<u>3</u>,4,5,2].
|
||||
- 2 is the smallest unmarked element, so we mark it and its left adjacent element: [<u>2</u>,<u>1</u>,<u>3</u>,4,<u>5</u>,<u>2</u>].
|
||||
- 4 is the only remaining unmarked element, so we mark it: [<u>2</u>,<u>1</u>,<u>3</u>,<u>4</u>,<u>5</u>,<u>2</u>].
|
||||
Our score is 1 + 2 + 4 = 7.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,5,1,3,2]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> We mark the elements as follows:
|
||||
- 1 is the smallest unmarked element, so we mark it and its two adjacent elements: [2,3,<u>5</u>,<u>1</u>,<u>3</u>,2].
|
||||
- 2 is the smallest unmarked element, since there are two of them, we choose the left-most one, so we mark the one at index 0 and its right adjacent element: [<u>2</u>,<u>3</u>,<u>5</u>,<u>1</u>,<u>3</u>,2].
|
||||
- 2 is the only remaining unmarked element, so we mark it: [<u>2</u>,<u>3</u>,<u>5</u>,<u>1</u>,<u>3</u>,<u>2</u>].
|
||||
Our score is 1 + 2 + 2 = 5.
|
||||
</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>6</sup></code></li>
|
||||
</ul>
|
39
leetcode/problem/kth-largest-sum-in-a-binary-tree.html
Normal file
39
leetcode/problem/kth-largest-sum-in-a-binary-tree.html
Normal file
@@ -0,0 +1,39 @@
|
||||
<p>You are given the <code>root</code> of a binary tree and a positive integer <code>k</code>.</p>
|
||||
|
||||
<p>The <strong>level sum</strong> in the tree is the sum of the values of the nodes that are on the <strong>same</strong> level.</p>
|
||||
|
||||
<p>Return<em> the </em><code>k<sup>th</sup></code><em> <strong>largest</strong> level sum in the tree (not necessarily distinct)</em>. If there are fewer than <code>k</code> levels in the tree, return <code>-1</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that two nodes are on the same level if they have the same distance from the root.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/14/binaryytreeedrawio-2.png" style="width: 301px; height: 284px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [5,8,9,2,1,3,7,4,6], k = 2
|
||||
<strong>Output:</strong> 13
|
||||
<strong>Explanation:</strong> The level sums are the following:
|
||||
- Level 1: 5.
|
||||
- Level 2: 8 + 9 = 17.
|
||||
- Level 3: 2 + 1 + 3 + 7 = 13.
|
||||
- Level 4: 4 + 6 = 10.
|
||||
The 2<sup>nd</sup> largest level sum is 13.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/14/treedrawio-3.png" style="width: 181px; height: 181px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,2,null,3], k = 1
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The largest level sum is 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 <= 10<sup>6</sup></code></li>
|
||||
<li><code>1 <= k <= n</code></li>
|
||||
</ul>
|
31
leetcode/problem/maximize-greatness-of-an-array.html
Normal file
31
leetcode/problem/maximize-greatness-of-an-array.html
Normal file
@@ -0,0 +1,31 @@
|
||||
<p>You are given a 0-indexed integer array <code>nums</code>. You are allowed to permute <code>nums</code> into a new array <code>perm</code> of your choosing.</p>
|
||||
|
||||
<p>We define the <strong>greatness</strong> of <code>nums</code> be the number of indices <code>0 <= i < nums.length</code> for which <code>perm[i] > nums[i]</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> possible greatness you can achieve after permuting</em> <code>nums</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,5,2,1,3,1]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> One of the optimal rearrangements is perm = [2,5,1,3,3,1,1].
|
||||
At indices = 0, 1, 3, and 4, perm[i] > nums[i]. Hence, we return 4.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can prove the optimal perm is [2,3,4,1].
|
||||
At indices = 0, 1, and 2, perm[i] > nums[i]. Hence, we return 3.
|
||||
</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] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
40
leetcode/problem/minimum-time-to-complete-all-tasks.html
Normal file
40
leetcode/problem/minimum-time-to-complete-all-tasks.html
Normal file
@@ -0,0 +1,40 @@
|
||||
<p>There is a computer that can run an unlimited number of tasks <strong>at the same time</strong>. You are given a 2D integer array <code>tasks</code> where <code>tasks[i] = [start<sub>i</sub>, end<sub>i</sub>, duration<sub>i</sub>]</code> indicates that the <code>i<sup>th</sup></code> task should run for a total of <code>duration<sub>i</sub></code> seconds (not necessarily continuous) within the <strong>inclusive</strong> time range <code>[start<sub>i</sub>, end<sub>i</sub>]</code>.</p>
|
||||
|
||||
<p>You may turn on the computer only when it needs to run a task. You can also turn it off if it is idle.</p>
|
||||
|
||||
<p>Return <em>the minimum time during which the computer should be turned on to complete all tasks</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = [[2,3,1],[4,5,1],[1,5,2]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
- The first task can be run in the inclusive time range [2, 2].
|
||||
- The second task can be run in the inclusive time range [5, 5].
|
||||
- The third task can be run in the two inclusive time ranges [2, 2] and [5, 5].
|
||||
The computer will be on for a total of 2 seconds.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = [[1,3,2],[2,5,3],[5,6,2]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
- The first task can be run in the inclusive time range [2, 3].
|
||||
- The second task can be run in the inclusive time ranges [2, 3] and [5, 5].
|
||||
- The third task can be run in the two inclusive time range [5, 6].
|
||||
The computer will be on for a total of 4 seconds.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= tasks.length <= 2000</code></li>
|
||||
<li><code>tasks[i].length == 3</code></li>
|
||||
<li><code>1 <= start<sub>i</sub>, end<sub>i</sub> <= 2000</code></li>
|
||||
<li><code>1 <= duration<sub>i</sub> <= end<sub>i</sub> - start<sub>i</sub> + 1 </code></li>
|
||||
</ul>
|
42
leetcode/problem/minimum-time-to-repair-cars.html
Normal file
42
leetcode/problem/minimum-time-to-repair-cars.html
Normal file
@@ -0,0 +1,42 @@
|
||||
<p>You are given an integer array <code>ranks</code> representing the <strong>ranks</strong> of some mechanics. <font face="monospace">ranks<sub>i</sub></font> is the rank of the <font face="monospace">i<sup>th</sup></font> mechanic<font face="monospace">.</font> A mechanic with a rank <code>r</code> can repair <font face="monospace">n</font> cars in <code>r * n<sup>2</sup></code> minutes.</p>
|
||||
|
||||
<p>You are also given an integer <code>cars</code> representing the total number of cars waiting in the garage to be repaired.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> time taken to repair all the cars.</em></p>
|
||||
|
||||
<p><strong>Note:</strong> All the mechanics can repair the cars simultaneously.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> ranks = [4,2,3,1], cars = 10
|
||||
<strong>Output:</strong> 16
|
||||
<strong>Explanation:</strong>
|
||||
- The first mechanic will repair two cars. The time required is 4 * 2 * 2 = 16 minutes.
|
||||
- The second mechanic will repair two cars. The time required is 2 * 2 * 2 = 8 minutes.
|
||||
- The third mechanic will repair two cars. The time required is 3 * 2 * 2 = 12 minutes.
|
||||
- The fourth mechanic will repair four cars. The time required is 1 * 4 * 4 = 16 minutes.
|
||||
It can be proved that the cars cannot be repaired in less than 16 minutes.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> ranks = [5,1,8], cars = 6
|
||||
<strong>Output:</strong> 16
|
||||
<strong>Explanation:</strong>
|
||||
- The first mechanic will repair one car. The time required is 5 * 1 * 1 = 5 minutes.
|
||||
- The second mechanic will repair four cars. The time required is 1 * 4 * 4 = 16 minutes.
|
||||
- The third mechanic will repair one car. The time required is 8 * 1 * 1 = 8 minutes.
|
||||
It can be proved that the cars cannot be repaired in less than 16 minutes.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= ranks.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= ranks[i] <= 100</code></li>
|
||||
<li><code>1 <= cars <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
35
leetcode/problem/number-of-even-and-odd-bits.html
Normal file
35
leetcode/problem/number-of-even-and-odd-bits.html
Normal file
@@ -0,0 +1,35 @@
|
||||
<p>You are given a <strong>positive</strong> integer <code>n</code>.</p>
|
||||
|
||||
<p>Let <code>even</code> denote the number of even indices in the binary representation of <code>n</code> (<strong>0-indexed</strong>) with value <code>1</code>.</p>
|
||||
|
||||
<p>Let <code>odd</code> denote the number of odd indices in the binary representation of <code>n</code> (<strong>0-indexed</strong>) with value <code>1</code>.</p>
|
||||
|
||||
<p>Return <em>an integer array </em><code>answer</code><em> where </em><code>answer = [even, odd]</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 17
|
||||
<strong>Output:</strong> [2,0]
|
||||
<strong>Explanation:</strong> The binary representation of 17 is 10001.
|
||||
It contains 1 on the 0<sup>th</sup> and 4<sup>th</sup> indices.
|
||||
There are 2 even and 0 odd indices.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2
|
||||
<strong>Output:</strong> [0,1]
|
||||
<strong>Explanation:</strong> The binary representation of 2 is 10.
|
||||
It contains 1 on the 1<sup>st</sup> index.
|
||||
There are 0 even and 1 odd indices.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 1000</code></li>
|
||||
</ul>
|
59
leetcode/problem/number-of-ways-to-earn-points.html
Normal file
59
leetcode/problem/number-of-ways-to-earn-points.html
Normal file
@@ -0,0 +1,59 @@
|
||||
<p>There is a test that has <code>n</code> types of questions. You are given an integer <code>target</code> and a <strong>0-indexed</strong> 2D integer array <code>types</code> where <code>types[i] = [count<sub>i</sub>, marks<sub>i</sub>]</code> indicates that there are <code>count<sub>i</sub></code> questions of the <code>i<sup>th</sup></code> type, and each one of them is worth <code>marks<sub>i</sub></code> points.</p>
|
||||
|
||||
<ul>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of ways you can earn <strong>exactly</strong> </em><code>target</code><em> points in the exam</em>. Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that questions of the same type are indistinguishable.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if there are <code>3</code> questions of the same type, then solving the <code>1<sup>st</sup></code> and <code>2<sup>nd</sup></code> questions is the same as solving the <code>1<sup>st</sup></code> and <code>3<sup>rd</sup></code> questions, or the <code>2<sup>nd</sup></code> and <code>3<sup>rd</sup></code> questions.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> target = 6, types = [[6,1],[3,2],[2,3]]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> You can earn 6 points in one of the seven ways:
|
||||
- Solve 6 questions of the 0<sup>th</sup> type: 1 + 1 + 1 + 1 + 1 + 1 = 6
|
||||
- Solve 4 questions of the 0<sup>th</sup> type and 1 question of the 1<sup>st</sup> type: 1 + 1 + 1 + 1 + 2 = 6
|
||||
- Solve 2 questions of the 0<sup>th</sup> type and 2 questions of the 1<sup>st</sup> type: 1 + 1 + 2 + 2 = 6
|
||||
- Solve 3 questions of the 0<sup>th</sup> type and 1 question of the 2<sup>nd</sup> type: 1 + 1 + 1 + 3 = 6
|
||||
- Solve 1 question of the 0<sup>th</sup> type, 1 question of the 1<sup>st</sup> type and 1 question of the 2<sup>nd</sup> type: 1 + 2 + 3 = 6
|
||||
- Solve 3 questions of the 1<sup>st</sup> type: 2 + 2 + 2 = 6
|
||||
- Solve 2 questions of the 2<sup>nd</sup> type: 3 + 3 = 6
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> target = 5, types = [[50,1],[50,2],[50,5]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> You can earn 5 points in one of the four ways:
|
||||
- Solve 5 questions of the 0<sup>th</sup> type: 1 + 1 + 1 + 1 + 1 = 5
|
||||
- Solve 3 questions of the 0<sup>th</sup> type and 1 question of the 1<sup>st</sup> type: 1 + 1 + 1 + 2 = 5
|
||||
- Solve 1 questions of the 0<sup>th</sup> type and 2 questions of the 1<sup>st</sup> type: 1 + 2 + 2 = 5
|
||||
- Solve 1 question of the 2<sup>nd</sup> type: 5
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> target = 18, types = [[6,1],[3,2],[2,3]]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> You can only earn 18 points by answering all questions.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= target <= 1000</code></li>
|
||||
<li><code>n == types.length</code></li>
|
||||
<li><code>1 <= n <= 50</code></li>
|
||||
<li><code>types[i].length == 2</code></li>
|
||||
<li><code>1 <= count<sub>i</sub>, marks<sub>i</sub> <= 50</code></li>
|
||||
</ul>
|
33
leetcode/problem/pass-the-pillow.html
Normal file
33
leetcode/problem/pass-the-pillow.html
Normal file
@@ -0,0 +1,33 @@
|
||||
<p>There are <code>n</code> people standing in a line labeled from <code>1</code> to <code>n</code>. The first person in the line is holding a pillow initially. Every second, the person holding the pillow passes it to the next person standing in the line. Once the pillow reaches the end of the line, the direction changes, and people continue passing the pillow in the opposite direction.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, once the pillow reaches the <code>n<sup>th</sup></code> person they pass it to the <code>n - 1<sup>th</sup></code> person, then to the <code>n - 2<sup>th</sup></code> person and so on.</li>
|
||||
</ul>
|
||||
|
||||
<p>Given the two positive integers <code>n</code> and <code>time</code>, return <em>the index of the person holding the pillow after </em><code>time</code><em> seconds</em>.</p>
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, time = 5
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> People pass the pillow in the following way: 1 -> 2 -> 3 -> 4 -> 3 -> 2.
|
||||
Afer five seconds, the pillow is given to the 2<sup>nd</sup> person.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, time = 2
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> People pass the pillow in the following way: 1 -> 2 -> 3.
|
||||
Afer two seconds, the pillow is given to the 3<sup>r</sup><sup>d</sup> person.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 1000</code></li>
|
||||
<li><code>1 <= time <= 1000</code></li>
|
||||
</ul>
|
@@ -0,0 +1,32 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>. You can rearrange the elements of <code>nums</code> to <strong>any order</strong> (including the given order).</p>
|
||||
|
||||
<p>Let <code>prefix</code> be the array containing the prefix sums of <code>nums</code> after rearranging it. In other words, <code>prefix[i]</code> is the sum of the elements from <code>0</code> to <code>i</code> in <code>nums</code> after rearranging it. The <strong>score</strong> of <code>nums</code> is the number of positive integers in the array <code>prefix</code>.</p>
|
||||
|
||||
<p>Return <em>the maximum score you can achieve</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,-1,0,1,-3,3,-3]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> We can rearrange the array into nums = [2,3,1,-1,-3,0,-3].
|
||||
prefix = [2,5,6,5,2,2,-1], so the score is 6.
|
||||
It can be shown that 6 is the maximum score we can obtain.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-2,-3,0]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Any rearrangement of the array will result in a score of 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>6</sup> <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,46 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>value</code>.</p>
|
||||
|
||||
<p>In one operation, you can add or subtract <code>value</code> from any element of <code>nums</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if <code>nums = [1,2,3]</code> and <code>value = 2</code>, you can choose to subtract <code>value</code> from <code>nums[0]</code> to make <code>nums = [-1,2,3]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The MEX (minimum excluded) of an array is the smallest missing <strong>non-negative</strong> integer in it.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, the MEX of <code>[-1,2,3]</code> is <code>0</code> while the MEX of <code>[1,0,3]</code> is <code>2</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the maximum MEX of </em><code>nums</code><em> after applying the mentioned operation <strong>any number of times</strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,-10,7,13,6,8], value = 5
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> One can achieve this result by applying the following operations:
|
||||
- Add value to nums[1] twice to make nums = [1,<strong><u>0</u></strong>,7,13,6,8]
|
||||
- Subtract value from nums[2] once to make nums = [1,0,<strong><u>2</u></strong>,13,6,8]
|
||||
- Subtract value from nums[3] twice to make nums = [1,0,2,<strong><u>3</u></strong>,6,8]
|
||||
The MEX of nums is 4. It can be shown that 4 is the maximum MEX we can achieve.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,-10,7,13,6,8], value = 7
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> One can achieve this result by applying the following operation:
|
||||
- subtract value from nums[2] once to make nums = [1,-10,<u><strong>0</strong></u>,13,6,8]
|
||||
The MEX of nums is 2. It can be shown that 2 is the maximum MEX we can achieve.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length, value <= 10<sup>5</sup></code></li>
|
||||
<li><code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@@ -0,0 +1,39 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
|
||||
|
||||
<p>A <strong>split</strong> at an index <code>i</code> where <code>0 <= i <= n - 2</code> is called <strong>valid</strong> if the product of the first <code>i + 1</code> elements and the product of the remaining elements are coprime.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if <code>nums = [2, 3, 3]</code>, then a split at the index <code>i = 0</code> is valid because <code>2</code> and <code>9</code> are coprime, while a split at the index <code>i = 1</code> is not valid because <code>6</code> and <code>3</code> are not coprime. A split at the index <code>i = 2</code> is not valid because <code>i == n - 1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the smallest index </em><code>i</code><em> at which the array can be split validly or </em><code>-1</code><em> if there is no such split</em>.</p>
|
||||
|
||||
<p>Two values <code>val1</code> and <code>val2</code> are coprime if <code>gcd(val1, val2) == 1</code> where <code>gcd(val1, val2)</code> is the greatest common divisor of <code>val1</code> and <code>val2</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/14/second.PNG" style="width: 450px; height: 211px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,7,8,15,3,5]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The table above shows the values of the product of the first i + 1 elements, the remaining elements, and their gcd at each index i.
|
||||
The only valid split is at index 2.
|
||||
</pre>
|
||||
|
||||
<p><strong>Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/14/capture.PNG" style="width: 450px; height: 215px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,7,15,8,3,5]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> The table above shows the values of the product of the first i + 1 elements, the remaining elements, and their gcd at each index i.
|
||||
There is no valid split.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
44
leetcode/problem/split-with-minimum-sum.html
Normal file
44
leetcode/problem/split-with-minimum-sum.html
Normal file
@@ -0,0 +1,44 @@
|
||||
<p>Given a positive integer <code>num</code>, split it into two non-negative integers <code>num1</code> and <code>num2</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li>The concatenation of <code>num1</code> and <code>num2</code> is a permutation of <code>num</code>.
|
||||
|
||||
<ul>
|
||||
<li>In other words, the sum of the number of occurrences of each digit in <code>num1</code> and <code>num2</code> is equal to the number of occurrences of that digit in <code>num</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><code>num1</code> and <code>num2</code> can contain leading zeros.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> possible sum of</em> <code>num1</code> <em>and</em> <code>num2</code>.</p>
|
||||
|
||||
<p><strong>Notes:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>It is guaranteed that <code>num</code> does not contain any leading zeros.</li>
|
||||
<li>The order of occurrence of the digits in <code>num1</code> and <code>num2</code> may differ from the order of occurrence of <code>num</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 4325
|
||||
<strong>Output:</strong> 59
|
||||
<strong>Explanation:</strong> We can split 4325 so that <code>num1 </code>is 24 and num2<code> is </code>35, giving a sum of 59. We can prove that 59 is indeed the minimal possible sum.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> num = 687
|
||||
<strong>Output:</strong> 75
|
||||
<strong>Explanation:</strong> We can split 687 so that <code>num1</code> is 68 and <code>num2 </code>is 7, which would give an optimal sum of 75.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>10 <= num <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
34
leetcode/problem/the-number-of-beautiful-subsets.html
Normal file
34
leetcode/problem/the-number-of-beautiful-subsets.html
Normal file
@@ -0,0 +1,34 @@
|
||||
<p>You are given an array <code>nums</code> of positive integers and a <strong>positive</strong> integer <code>k</code>.</p>
|
||||
|
||||
<p>A subset of <code>nums</code> is <strong>beautiful</strong> if it does not contain two integers with an absolute difference equal to <code>k</code>.</p>
|
||||
|
||||
<p>Return <em>the number of <strong>non-empty beautiful </strong>subsets of the array</em> <code>nums</code>.</p>
|
||||
|
||||
<p>A <strong>subset</strong> of <code>nums</code> is an array that can be obtained by deleting some (possibly none) elements from <code>nums</code>. Two subsets are different if and only if the chosen indices to delete are different.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,4,6], k = 2
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The beautiful subsets of the array nums are: [2], [4], [6], [2, 6].
|
||||
It can be proved that there are only 4 beautiful subsets in the array [2,4,6].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1], k = 1
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The beautiful subset of the array nums is [1].
|
||||
It can be proved that there is only 1 beautiful subset in the array [1].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 20</code></li>
|
||||
<li><code>1 <= nums[i], k <= 1000</code></li>
|
||||
</ul>
|
Reference in New Issue
Block a user