mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-05 23:41:41 +08:00
批量更新数据
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<p>Given an <code>m x n</code> binary matrix <code>mat</code>, return <em>the distance of the nearest </em><code>0</code><em> for each cell</em>.</p>
|
||||
|
||||
<p>The distance between two adjacent cells is <code>1</code>.</p>
|
||||
<p>The distance between two cells sharing a common edge is <code>1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
@@ -28,3 +28,6 @@
|
||||
<li><code>mat[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
|
||||
<li>There is at least one <code>0</code> in <code>mat</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Note:</strong> This question is the same as 1765: <a href="https://leetcode.com/problems/map-of-highest-peak/description/" target="_blank">https://leetcode.com/problems/map-of-highest-peak/</a></p>
|
||||
|
@@ -16,7 +16,8 @@
|
||||
<ul>
|
||||
<li><code>1 <= text.length <= 1000</code></li>
|
||||
<li><code>text</code> consists of lowercase English letters and spaces.</li>
|
||||
<li>All the words in <code>text</code> a separated by <strong>a single space</strong>.</li>
|
||||
<li>All the words in <code>text</code> are separated by <strong>a single space</strong>.</li>
|
||||
<li><code>1 <= first.length, second.length <= 10</code></li>
|
||||
<li><code>first</code> and <code>second</code> consist of lowercase English letters.</li>
|
||||
<li><code>text</code> will not have any leading or trailing spaces.</li>
|
||||
</ul>
|
||||
|
@@ -1,37 +1,53 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array of integers <code>nums</code> of length <code>n</code>, and a <strong>positive</strong> <strong>odd</strong> integer <code>k</code>.</p>
|
||||
<p>You are given an array of integers <code>nums</code> with length <code>n</code>, and a positive <strong>odd</strong> integer <code>k</code>.</p>
|
||||
|
||||
<p>The strength of <code>x</code> subarrays is defined as <code>strength = sum[1] * x - sum[2] * (x - 1) + sum[3] * (x - 2) - sum[4] * (x - 3) + ... + sum[x] * 1</code> where <code>sum[i]</code> is the sum of the elements in the <code>i<sup>th</sup></code> subarray. Formally, strength is sum of <code>(-1)<sup>i+1</sup> * sum[i] * (x - i + 1)</code> over all <code>i</code>'s such that <code>1 <= i <= x</code>.</p>
|
||||
<p>Select exactly <b><code>k</code></b> disjoint <span data-keyword="subarray-nonempty">subarrays</span> <b><code>sub<sub>1</sub>, sub<sub>2</sub>, ..., sub<sub>k</sub></code></b> from <code>nums</code> such that the last element of <code>sub<sub>i</sub></code> appears before the first element of <code>sub<sub>{i+1}</sub></code> for all <code>1 <= i <= k-1</code>. The goal is to maximize their combined strength.</p>
|
||||
|
||||
<p>You need to select <code>k</code> <strong>disjoint <span data-keyword="subarray-nonempty">subarrays</span></strong> from <code>nums</code>, such that their <strong>strength</strong> is <strong>maximum</strong>.</p>
|
||||
<p>The strength of the selected subarrays is defined as:</p>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> possible <strong>strength</strong> that can be obtained</em>.</p>
|
||||
<p><code>strength = k * sum(sub<sub>1</sub>)- (k - 1) * sum(sub<sub>2</sub>) + (k - 2) * sum(sub<sub>3</sub>) - ... - 2 * sum(sub<sub>{k-1}</sub>) + sum(sub<sub>k</sub>)</code></p>
|
||||
|
||||
<p><strong>Note</strong> that the selected subarrays <strong>don't</strong> need to cover the entire array.</p>
|
||||
<p>where <b><code>sum(sub<sub>i</sub>)</code></b> is the sum of the elements in the <code>i</code>-th subarray.</p>
|
||||
|
||||
<p>Return the <strong>maximum</strong> possible strength that can be obtained from selecting exactly <b><code>k</code></b> disjoint subarrays from <code>nums</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that the chosen subarrays <strong>don't</strong> need to cover the entire array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,-1,2], k = 3
|
||||
<strong>Output:</strong> 22
|
||||
<strong>Explanation:</strong> The best possible way to select 3 subarrays is: nums[0..2], nums[3..3], and nums[4..4]. The strength is (1 + 2 + 3) * 3 - (-1) * 2 + 2 * 1 = 22.
|
||||
</pre>
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,3,-1,2], k = 3</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">22</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The best possible way to select 3 subarrays is: nums[0..2], nums[3..3], and nums[4..4]. The strength is calculated as follows:</p>
|
||||
|
||||
<p><code>strength = 3 * (1 + 2 + 3) - 2 * (-1) + 2 = 22</code></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [12,-2,-2,-2,-2], k = 5
|
||||
<strong>Output:</strong> 64
|
||||
<strong>Explanation:</strong> The only possible way to select 5 disjoint subarrays is: nums[0..0], nums[1..1], nums[2..2], nums[3..3], and nums[4..4]. The strength is 12 * 5 - (-2) * 4 + (-2) * 3 - (-2) * 2 + (-2) * 1 = 64.
|
||||
</pre>
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [12,-2,-2,-2,-2], k = 5</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">64</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The only possible way to select 5 disjoint subarrays is: nums[0..0], nums[1..1], nums[2..2], nums[3..3], and nums[4..4]. The strength is calculated as follows:</p>
|
||||
|
||||
<p><code>strength = 5 * 12 - 4 * (-2) + 3 * (-2) - 2 * (-2) + (-2) = 64</code></p>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-1,-2,-3], k = 1
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> The best possible way to select 1 subarray is: nums[0..0]. The strength is -1.
|
||||
</pre>
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [-1,-2,-3], k = </span>1</p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">-1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The best possible way to select 1 subarray is: nums[0..0]. The strength is -1.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
@@ -1,6 +1,6 @@
|
||||
<p>For an integer array <code>nums</code>, an <strong>inverse pair</strong> is a pair of integers <code>[i, j]</code> where <code>0 <= i < j < nums.length</code> and <code>nums[i] > nums[j]</code>.</p>
|
||||
|
||||
<p>Given two integers n and k, return the number of different arrays consist of numbers from <code>1</code> to <code>n</code> such that there are exactly <code>k</code> <strong>inverse pairs</strong>. Since the answer can be huge, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
<p>Given two integers n and k, return the number of different arrays consisting of numbers from <code>1</code> to <code>n</code> such that there are exactly <code>k</code> <strong>inverse pairs</strong>. Since the answer can be huge, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
@@ -1,40 +1,40 @@
|
||||
<p>There is a bag that consists of items, each item has a number <code>1</code>, <code>0</code>, or <code>-1</code> written on it.</p>
|
||||
|
||||
<p>You are given four <strong>non-negative </strong>integers <code>numOnes</code>, <code>numZeros</code>, <code>numNegOnes</code>, and <code>k</code>.</p>
|
||||
|
||||
<p>The bag initially contains:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>numOnes</code> items with <code>1</code>s written on them.</li>
|
||||
<li><code>numZeroes</code> items with <code>0</code>s written on them.</li>
|
||||
<li><code>numNegOnes</code> items with <code>-1</code>s written on them.</li>
|
||||
</ul>
|
||||
|
||||
<p>We want to pick exactly <code>k</code> items among the available items. Return <em>the <strong>maximum</strong> possible sum of numbers written on the items</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> numOnes = 3, numZeros = 2, numNegOnes = 0, k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 2 items with 1 written on them and get a sum in a total of 2.
|
||||
It can be proven that 2 is the maximum possible sum.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> numOnes = 3, numZeros = 2, numNegOnes = 0, k = 4
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 3 items with 1 written on them, and 1 item with 0 written on it, and get a sum in a total of 3.
|
||||
It can be proven that 3 is the maximum possible sum.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= numOnes, numZeros, numNegOnes <= 50</code></li>
|
||||
<li><code>0 <= k <= numOnes + numZeros + numNegOnes</code></li>
|
||||
</ul>
|
||||
<p>There is a bag that consists of items, each item has a number <code>1</code>, <code>0</code>, or <code>-1</code> written on it.</p>
|
||||
|
||||
<p>You are given four <strong>non-negative </strong>integers <code>numOnes</code>, <code>numZeros</code>, <code>numNegOnes</code>, and <code>k</code>.</p>
|
||||
|
||||
<p>The bag initially contains:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>numOnes</code> items with <code>1</code>s written on them.</li>
|
||||
<li><code>numZeroes</code> items with <code>0</code>s written on them.</li>
|
||||
<li><code>numNegOnes</code> items with <code>-1</code>s written on them.</li>
|
||||
</ul>
|
||||
|
||||
<p>We want to pick exactly <code>k</code> items among the available items. Return <em>the <strong>maximum</strong> possible sum of numbers written on the items</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> numOnes = 3, numZeros = 2, numNegOnes = 0, k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 2 items with 1 written on them and get a sum in a total of 2.
|
||||
It can be proven that 2 is the maximum possible sum.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> numOnes = 3, numZeros = 2, numNegOnes = 0, k = 4
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 3 items with 1 written on them, and 1 item with 0 written on it, and get a sum in a total of 3.
|
||||
It can be proven that 3 is the maximum possible sum.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= numOnes, numZeros, numNegOnes <= 50</code></li>
|
||||
<li><code>0 <= k <= numOnes + numZeros + numNegOnes</code></li>
|
||||
</ul>
|
||||
|
@@ -35,3 +35,6 @@
|
||||
<li><code>1 <= text.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>text</code> consists of lower case English letters only.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/rearrange-characters-to-make-target-string/description/" target="_blank"> 2287: Rearrange Characters to Make Target String.</a></p>
|
||||
|
@@ -1,28 +1,28 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> representing the score of students in an exam. The teacher would like to form one <strong>non-empty</strong> group of students with maximal <strong>strength</strong>, where the strength of a group of students of indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, ... , <code>i<sub>k</sub></code> is defined as <code>nums[i<sub>0</sub>] * nums[i<sub>1</sub>] * nums[i<sub>2</sub>] * ... * nums[i<sub>k</sub>]</code>.</p>
|
||||
|
||||
<p>Return <em>the maximum strength of a group the teacher can create</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,-1,-5,2,5,-9]
|
||||
<strong>Output:</strong> 1350
|
||||
<strong>Explanation:</strong> One way to form a group of maximal strength is to group the students at indices [0,2,3,4,5]. Their strength is 3 * (-5) * 2 * 5 * (-9) = 1350, which we can show is optimal.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-4,-5,-4]
|
||||
<strong>Output:</strong> 20
|
||||
<strong>Explanation:</strong> Group the students at indices [0, 1] . Then, we’ll have a resulting strength of 20. We cannot achieve greater strength.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 13</code></li>
|
||||
<li><code>-9 <= nums[i] <= 9</code></li>
|
||||
</ul>
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> representing the score of students in an exam. The teacher would like to form one <strong>non-empty</strong> group of students with maximal <strong>strength</strong>, where the strength of a group of students of indices <code>i<sub>0</sub></code>, <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, ... , <code>i<sub>k</sub></code> is defined as <code>nums[i<sub>0</sub>] * nums[i<sub>1</sub>] * nums[i<sub>2</sub>] * ... * nums[i<sub>k</sub>]</code>.</p>
|
||||
|
||||
<p>Return <em>the maximum strength of a group the teacher can create</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,-1,-5,2,5,-9]
|
||||
<strong>Output:</strong> 1350
|
||||
<strong>Explanation:</strong> One way to form a group of maximal strength is to group the students at indices [0,2,3,4,5]. Their strength is 3 * (-5) * 2 * 5 * (-9) = 1350, which we can show is optimal.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-4,-5,-4]
|
||||
<strong>Output:</strong> 20
|
||||
<strong>Explanation:</strong> Group the students at indices [0, 1] . Then, we’ll have a resulting strength of 20. We cannot achieve greater strength.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 13</code></li>
|
||||
<li><code>-9 <= nums[i] <= 9</code></li>
|
||||
</ul>
|
||||
|
@@ -1,45 +1,45 @@
|
||||
<p>We define the <strong>conversion array</strong> <code>conver</code> of an array <code>arr</code> as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>conver[i] = arr[i] + max(arr[0..i])</code> where <code>max(arr[0..i])</code> is the maximum value of <code>arr[j]</code> over <code>0 <= j <= i</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>We also define the <strong>score</strong> of an array <code>arr</code> as the sum of the values of the conversion array of <code>arr</code>.</p>
|
||||
|
||||
<p>Given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>, return <em>an array </em><code>ans</code><em> of length </em><code>n</code><em> where </em><code>ans[i]</code><em> is the score of the prefix</em> <code>nums[0..i]</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,7,5,10]
|
||||
<strong>Output:</strong> [4,10,24,36,56]
|
||||
<strong>Explanation:</strong>
|
||||
For the prefix [2], the conversion array is [4] hence the score is 4
|
||||
For the prefix [2, 3], the conversion array is [4, 6] hence the score is 10
|
||||
For the prefix [2, 3, 7], the conversion array is [4, 6, 14] hence the score is 24
|
||||
For the prefix [2, 3, 7, 5], the conversion array is [4, 6, 14, 12] hence the score is 36
|
||||
For the prefix [2, 3, 7, 5, 10], the conversion array is [4, 6, 14, 12, 20] hence the score is 56
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,2,4,8,16]
|
||||
<strong>Output:</strong> [2,4,8,16,32,64]
|
||||
<strong>Explanation:</strong>
|
||||
For the prefix [1], the conversion array is [2] hence the score is 2
|
||||
For the prefix [1, 1], the conversion array is [2, 2] hence the score is 4
|
||||
For the prefix [1, 1, 2], the conversion array is [2, 2, 4] hence the score is 8
|
||||
For the prefix [1, 1, 2, 4], the conversion array is [2, 2, 4, 8] hence the score is 16
|
||||
For the prefix [1, 1, 2, 4, 8], the conversion array is [2, 2, 4, 8, 16] hence the score is 32
|
||||
For the prefix [1, 1, 2, 4, 8, 16], the conversion array is [2, 2, 4, 8, 16, 32] hence the score is 64
|
||||
</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>
|
||||
</ul>
|
||||
<p>We define the <strong>conversion array</strong> <code>conver</code> of an array <code>arr</code> as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>conver[i] = arr[i] + max(arr[0..i])</code> where <code>max(arr[0..i])</code> is the maximum value of <code>arr[j]</code> over <code>0 <= j <= i</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>We also define the <strong>score</strong> of an array <code>arr</code> as the sum of the values of the conversion array of <code>arr</code>.</p>
|
||||
|
||||
<p>Given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>, return <em>an array </em><code>ans</code><em> of length </em><code>n</code><em> where </em><code>ans[i]</code><em> is the score of the prefix</em> <code>nums[0..i]</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,3,7,5,10]
|
||||
<strong>Output:</strong> [4,10,24,36,56]
|
||||
<strong>Explanation:</strong>
|
||||
For the prefix [2], the conversion array is [4] hence the score is 4
|
||||
For the prefix [2, 3], the conversion array is [4, 6] hence the score is 10
|
||||
For the prefix [2, 3, 7], the conversion array is [4, 6, 14] hence the score is 24
|
||||
For the prefix [2, 3, 7, 5], the conversion array is [4, 6, 14, 12] hence the score is 36
|
||||
For the prefix [2, 3, 7, 5, 10], the conversion array is [4, 6, 14, 12, 20] hence the score is 56
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,2,4,8,16]
|
||||
<strong>Output:</strong> [2,4,8,16,32,64]
|
||||
<strong>Explanation:</strong>
|
||||
For the prefix [1], the conversion array is [2] hence the score is 2
|
||||
For the prefix [1, 1], the conversion array is [2, 2] hence the score is 4
|
||||
For the prefix [1, 1, 2], the conversion array is [2, 2, 4] hence the score is 8
|
||||
For the prefix [1, 1, 2, 4], the conversion array is [2, 2, 4, 8] hence the score is 16
|
||||
For the prefix [1, 1, 2, 4, 8], the conversion array is [2, 2, 4, 8, 16] hence the score is 32
|
||||
For the prefix [1, 1, 2, 4, 8, 16], the conversion array is [2, 2, 4, 8, 16, 32] hence the score is 64
|
||||
</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>
|
||||
</ul>
|
||||
|
@@ -22,5 +22,5 @@
|
||||
<ul>
|
||||
<li><code>date.length == 10</code></li>
|
||||
<li><code>date[4] == date[7] == '-'</code>, and all other <code>date[i]</code>'s are digits</li>
|
||||
<li><code>date</code> represents a calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>th</sup>, 2019.</li>
|
||||
<li><code>date</code> represents a calendar date between Jan 1<sup>st</sup>, 1900 and Dec 31<sup>st</sup>, 2019.</li>
|
||||
</ul>
|
||||
|
@@ -1,40 +1,40 @@
|
||||
<p>Given a <code>m x n</code> binary matrix <code>mat</code>, find the <strong>0-indexed</strong> position of the row that contains the <strong>maximum</strong> count of <strong>ones,</strong> and the number of ones in that row.</p>
|
||||
|
||||
<p>In case there are multiple rows that have the maximum count of ones, the row with the <strong>smallest row number</strong> should be selected.</p>
|
||||
|
||||
<p>Return<em> an array containing the index of the row, and the number of ones in it.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[0,1],[1,0]]
|
||||
<strong>Output:</strong> [0,1]
|
||||
<strong>Explanation:</strong> Both rows have the same number of 1's. So we return the index of the smaller row, 0, and the maximum count of ones (1<code>)</code>. So, the answer is [0,1].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[0,0,0],[0,1,1]]
|
||||
<strong>Output:</strong> [1,2]
|
||||
<strong>Explanation:</strong> The row indexed 1 has the maximum count of ones <code>(2)</code>. So we return its index, <code>1</code>, and the count. So, the answer is [1,2].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[0,0],[1,1],[0,0]]
|
||||
<strong>Output:</strong> [1,2]
|
||||
<strong>Explanation:</strong> The row indexed 1 has the maximum count of ones (2). So the answer is [1,2].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == mat.length</code> </li>
|
||||
<li><code>n == mat[i].length</code> </li>
|
||||
<li><code>1 <= m, n <= 100</code> </li>
|
||||
<li><code>mat[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
|
||||
</ul>
|
||||
<p>Given a <code>m x n</code> binary matrix <code>mat</code>, find the <strong>0-indexed</strong> position of the row that contains the <strong>maximum</strong> count of <strong>ones,</strong> and the number of ones in that row.</p>
|
||||
|
||||
<p>In case there are multiple rows that have the maximum count of ones, the row with the <strong>smallest row number</strong> should be selected.</p>
|
||||
|
||||
<p>Return<em> an array containing the index of the row, and the number of ones in it.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[0,1],[1,0]]
|
||||
<strong>Output:</strong> [0,1]
|
||||
<strong>Explanation:</strong> Both rows have the same number of 1's. So we return the index of the smaller row, 0, and the maximum count of ones (1<code>)</code>. So, the answer is [0,1].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[0,0,0],[0,1,1]]
|
||||
<strong>Output:</strong> [1,2]
|
||||
<strong>Explanation:</strong> The row indexed 1 has the maximum count of ones <code>(2)</code>. So we return its index, <code>1</code>, and the count. So, the answer is [1,2].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> mat = [[0,0],[1,1],[0,0]]
|
||||
<strong>Output:</strong> [1,2]
|
||||
<strong>Explanation:</strong> The row indexed 1 has the maximum count of ones (2). So the answer is [1,2].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == mat.length</code> </li>
|
||||
<li><code>n == mat[i].length</code> </li>
|
||||
<li><code>1 <= m, n <= 100</code> </li>
|
||||
<li><code>mat[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
|
||||
</ul>
|
||||
|
@@ -1,33 +1,33 @@
|
||||
<p>Given an array <code>nums</code>. We define a running sum of an array as <code>runningSum[i] = sum(nums[0]…nums[i])</code>.</p>
|
||||
|
||||
|
||||
|
||||
<p>Return the running sum of <code>nums</code>.</p>
|
||||
|
||||
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input:</strong> nums = [1,2,3,4]
|
||||
|
||||
<strong>Output:</strong> [1,3,6,10]
|
||||
|
||||
<strong>Explanation:</strong> Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].</pre>
|
||||
|
||||
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
|
||||
|
||||
<pre>
|
||||
|
||||
<strong>Input:</strong> nums = [1,1,1,1,1]
|
||||
|
||||
<strong>Output:</strong> [1,2,3,4,5]
|
||||
<p>Given an array <code>nums</code>. We define a running sum of an array as <code>runningSum[i] = sum(nums[0]…nums[i])</code>.</p>
|
||||
|
||||
<p>Return the running sum of <code>nums</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,3,4]
|
||||
<strong>Output:</strong> [1,3,6,10]
|
||||
<strong>Explanation:</strong> Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,1,1,1]
|
||||
<strong>Output:</strong> [1,2,3,4,5]
|
||||
<strong>Explanation:</strong> Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,1,2,10,1]
|
||||
<strong>Output:</strong> [3,4,6,16,17]
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>-10^6 <= nums[i] <= 10^6</code></li>
|
||||
</ul>
|
||||
|
@@ -9,7 +9,7 @@
|
||||
<strong>Input:</strong> nums = [1,2,1,2,6,7,5,1], k = 2
|
||||
<strong>Output:</strong> [0,3,5]
|
||||
<strong>Explanation:</strong> Subarrays [1, 2], [2, 6], [7, 5] correspond to the starting indices [0, 3, 5].
|
||||
We could have also taken [2, 1], but an answer of [1, 3, 5] would be lexicographically larger.
|
||||
We could have also taken [2, 1], but an answer of [1, 3, 5] would be lexicographically smaller.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
@@ -1,41 +1,66 @@
|
||||
<p>Given a <strong>0-indexed</strong> array of strings <code>words</code> where <code>words[i]</code> is either a positive integer represented as a string or the string <code>"prev"</code>.</p>
|
||||
<p>Given an integer array <code>nums</code> where <code>nums[i]</code> is either a positive integer or <code>-1</code>. We need to find for each <code>-1</code> the respective positive integer, which we call the last visited integer.</p>
|
||||
|
||||
<p>Start iterating from the beginning of the array; for every <code>"prev"</code> string seen in <code>words</code>, find the <strong>last visited integer</strong> in <code>words</code> which is defined as follows:</p>
|
||||
<p>To achieve this goal, let's define two empty arrays: <code>seen</code> and <code>ans</code>.</p>
|
||||
|
||||
<p>Start iterating from the beginning of the array <code>nums</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>Let <code>k</code> be the number of consecutive <code>"prev"</code> strings seen so far (containing the current string). Let <code>nums</code> be the <strong>0-indexed </strong>array of <strong>integers</strong> seen so far and <code>nums_reverse</code> be the reverse of <code>nums</code>, then the integer at <code>(k - 1)<sup>th</sup></code> index of <code>nums_reverse</code> will be the <strong>last visited integer</strong> for this <code>"prev"</code>.</li>
|
||||
<li>If <code>k</code> is <strong>greater</strong> than the total visited integers, then the last visited integer will be <code>-1</code>.</li>
|
||||
<li>If a positive integer is encountered, prepend it to the <strong>front</strong> of <code>seen</code>.</li>
|
||||
<li>If <code>-1</code> is encountered, let <code>k</code> be the number of <strong>consecutive</strong> <code>-1</code>s seen so far (including the current <code>-1</code>),
|
||||
<ul>
|
||||
<li>If <code>k</code> is less than or equal to the length of <code>seen</code>, append the <code>k</code>-th element of <code>seen</code> to <code>ans</code>.</li>
|
||||
<li>If <code>k</code> is strictly greater than the length of <code>seen</code>, append <code>-1</code> to <code>ans</code>.</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer array containing the last visited integers.</em></p>
|
||||
<p>Return the array<em> </em><code>ans</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["1","2","prev","prev","prev"]
|
||||
<strong>Output:</strong> [2,1,-1]
|
||||
<strong>Explanation:</strong>
|
||||
For "prev" at index = 2, last visited integer will be 2 as here the number of consecutive "prev" strings is 1, and in the array reverse_nums, 2 will be the first element.
|
||||
For "prev" at index = 3, last visited integer will be 1 as there are a total of two consecutive "prev" strings including this "prev" which are visited, and 1 is the second last visited integer.
|
||||
For "prev" at index = 4, last visited integer will be -1 as there are a total of three consecutive "prev" strings including this "prev" which are visited, but the total number of integers visited is two.
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,-1,-1,-1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[2,1,-1]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Start with <code>seen = []</code> and <code>ans = []</code>.</p>
|
||||
|
||||
<ol>
|
||||
<li>Process <code>nums[0]</code>: The first element in nums is <code>1</code>. We prepend it to the front of <code>seen</code>. Now, <code>seen == [1]</code>.</li>
|
||||
<li>Process <code>nums[1]</code>: The next element is <code>2</code>. We prepend it to the front of <code>seen</code>. Now, <code>seen == [2, 1]</code>.</li>
|
||||
<li>Process <code>nums[2]</code>: The next element is <code>-1</code>. This is the first occurrence of <code>-1</code>, so <code>k == 1</code>. We look for the first element in seen. We append <code>2</code> to <code>ans</code>. Now, <code>ans == [2]</code>.</li>
|
||||
<li>Process <code>nums[3]</code>: Another <code>-1</code>. This is the second consecutive <code>-1</code>, so <code>k == 2</code>. The second element in <code>seen</code> is <code>1</code>, so we append <code>1</code> to <code>ans</code>. Now, <code>ans == [2, 1]</code>.</li>
|
||||
<li>Process <code>nums[4]</code>: Another <code>-1</code>, the third in a row, making <code>k = 3</code>. However, <code>seen</code> only has two elements (<code>[2, 1]</code>). Since <code>k</code> is greater than the number of elements in <code>seen</code>, we append <code>-1</code> to <code>ans</code>. Finally, <code>ans == [2, 1, -1]</code>.</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["1","prev","2","prev","prev"]
|
||||
<strong>Output:</strong> [1,2,1]
|
||||
<strong>Explanation:</strong>
|
||||
For "prev" at index = 1, last visited integer will be 1.
|
||||
For "prev" at index = 3, last visited integer will be 2.
|
||||
For "prev" at index = 4, last visited integer will be 1 as there are a total of two consecutive "prev" strings including this "prev" which are visited, and 1 is the second last visited integer.
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,-1,2,-1,-1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong><span class="example-io"> [1,2,1]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>Start with <code>seen = []</code> and <code>ans = []</code>.</p>
|
||||
|
||||
<ol>
|
||||
<li>Process <code>nums[0]</code>: The first element in nums is <code>1</code>. We prepend it to the front of <code>seen</code>. Now, <code>seen == [1]</code>.</li>
|
||||
<li>Process <code>nums[1]</code>: The next element is <code>-1</code>. This is the first occurrence of <code>-1</code>, so <code>k == 1</code>. We look for the first element in <code>seen</code>, which is <code>1</code>. Append <code>1</code> to <code>ans</code>. Now, <code>ans == [1]</code>.</li>
|
||||
<li>Process <code>nums[2]</code>: The next element is <code>2</code>. Prepend this to the front of <code>seen</code>. Now, <code>seen == [2, 1]</code>.</li>
|
||||
<li>Process <code>nums[3]</code>: The next element is <code>-1</code>. This <code>-1</code> is not consecutive to the first <code>-1</code> since <code>2</code> was in between. Thus, <code>k</code> resets to <code>1</code>. The first element in <code>seen</code> is <code>2</code>, so append <code>2</code> to <code>ans</code>. Now, <code>ans == [1, 2]</code>.</li>
|
||||
<li>Process <code>nums[4]</code>: Another <code>-1</code>. This is consecutive to the previous <code>-1</code>, so <code>k == 2</code>. The second element in <code>seen</code> is <code>1</code>, append <code>1</code> to <code>ans</code>. Finally, <code>ans == [1, 2, 1]</code>.</li>
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= words.length <= 100</code></li>
|
||||
<li><code>words[i] == "prev"</code> or <code>1 <= int(words[i]) <= 100</code></li>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>nums[i] == -1</code> or <code>1 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
||||
|
@@ -1,18 +1,18 @@
|
||||
<p>You are given a string <code>s</code>. Reorder the string using the following algorithm:</p>
|
||||
|
||||
<ol>
|
||||
<li>Pick the <strong>smallest</strong> character from <code>s</code> and <strong>append</strong> it to the result.</li>
|
||||
<li>Pick the <strong>smallest</strong> character from <code>s</code> which is greater than the last appended character to the result and <strong>append</strong> it.</li>
|
||||
<li>Repeat step 2 until you cannot pick more characters.</li>
|
||||
<li>Pick the <strong>largest</strong> character from <code>s</code> and <strong>append</strong> it to the result.</li>
|
||||
<li>Pick the <strong>largest</strong> character from <code>s</code> which is smaller than the last appended character to the result and <strong>append</strong> it.</li>
|
||||
<li>Repeat step 5 until you cannot pick more characters.</li>
|
||||
<li>Repeat the steps from 1 to 6 until you pick all characters from <code>s</code>.</li>
|
||||
<li>Remove the <strong>smallest</strong> character from <code>s</code> and <strong>append</strong> it to the result.</li>
|
||||
<li>Remove the <strong>smallest</strong> character from <code>s</code> that is greater than the last appended character, and <strong>append</strong> it to the result.</li>
|
||||
<li>Repeat step 2 until no more characters can be removed.</li>
|
||||
<li>Remove the <strong>largest</strong> character from <code>s</code> and <strong>append</strong> it to the result.</li>
|
||||
<li>Remove the <strong>largest</strong> character from <code>s</code> that is smaller than the last appended character, and <strong>append</strong> it to the result.</li>
|
||||
<li>Repeat step 5 until no more characters can be removed.</li>
|
||||
<li>Repeat steps 1 through 6 until all characters from <code>s</code> have been removed.</li>
|
||||
</ol>
|
||||
|
||||
<p>In each step, If the smallest or the largest character appears more than once you can choose any occurrence and append it to the result.</p>
|
||||
<p>If the smallest or largest character appears more than once, you may choose any occurrence to append to the result.</p>
|
||||
|
||||
<p>Return <em>the result string after sorting </em><code>s</code><em> with this algorithm</em>.</p>
|
||||
<p>Return the resulting string after reordering <code>s</code> using this algorithm.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
@@ -9,12 +9,13 @@
|
||||
| temperature | int |
|
||||
+---------------+---------+
|
||||
id is the column with unique values for this table.
|
||||
There are no different rows with the same recordDate.
|
||||
This table contains information about the temperature on a certain day.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>Write a solution to find all dates' <code>Id</code> with higher temperatures compared to its previous dates (yesterday).</p>
|
||||
<p>Write a solution to find all dates' <code>id</code> with higher temperatures compared to its previous dates (yesterday).</p>
|
||||
|
||||
<p>Return the result table in <strong>any order</strong>.</p>
|
||||
|
||||
|
@@ -1,49 +1,49 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array of non-negative integers <code>nums</code>. For each integer in <code>nums</code>, you must find its respective <strong>second greater</strong> integer.</p>
|
||||
|
||||
<p>The <strong>second greater</strong> integer of <code>nums[i]</code> is <code>nums[j]</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>j > i</code></li>
|
||||
<li><code>nums[j] > nums[i]</code></li>
|
||||
<li>There exists <strong>exactly one</strong> index <code>k</code> such that <code>nums[k] > nums[i]</code> and <code>i < k < j</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>If there is no such <code>nums[j]</code>, the second greater integer is considered to be <code>-1</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, in the array <code>[1, 2, 4, 3]</code>, the second greater integer of <code>1</code> is <code>4</code>, <code>2</code> is <code>3</code>, and that of <code>3</code> and <code>4</code> is <code>-1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> an integer array </em><code>answer</code><em>, where </em><code>answer[i]</code><em> is the second greater integer of </em><code>nums[i]</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,4,0,9,6]
|
||||
<strong>Output:</strong> [9,6,6,-1,-1]
|
||||
<strong>Explanation:</strong>
|
||||
0th index: 4 is the first integer greater than 2, and 9 is the second integer greater than 2, to the right of 2.
|
||||
1st index: 9 is the first, and 6 is the second integer greater than 4, to the right of 4.
|
||||
2nd index: 9 is the first, and 6 is the second integer greater than 0, to the right of 0.
|
||||
3rd index: There is no integer greater than 9 to its right, so the second greater integer is considered to be -1.
|
||||
4th index: There is no integer greater than 6 to its right, so the second greater integer is considered to be -1.
|
||||
Thus, we return [9,6,6,-1,-1].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,3]
|
||||
<strong>Output:</strong> [-1,-1]
|
||||
<strong>Explanation:</strong>
|
||||
We return [-1,-1] since neither integer has any integer greater than it.
|
||||
</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>
|
||||
<p>You are given a <strong>0-indexed</strong> array of non-negative integers <code>nums</code>. For each integer in <code>nums</code>, you must find its respective <strong>second greater</strong> integer.</p>
|
||||
|
||||
<p>The <strong>second greater</strong> integer of <code>nums[i]</code> is <code>nums[j]</code> such that:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>j > i</code></li>
|
||||
<li><code>nums[j] > nums[i]</code></li>
|
||||
<li>There exists <strong>exactly one</strong> index <code>k</code> such that <code>nums[k] > nums[i]</code> and <code>i < k < j</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>If there is no such <code>nums[j]</code>, the second greater integer is considered to be <code>-1</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, in the array <code>[1, 2, 4, 3]</code>, the second greater integer of <code>1</code> is <code>4</code>, <code>2</code> is <code>3</code>, and that of <code>3</code> and <code>4</code> is <code>-1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> an integer array </em><code>answer</code><em>, where </em><code>answer[i]</code><em> is the second greater integer of </em><code>nums[i]</code><em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,4,0,9,6]
|
||||
<strong>Output:</strong> [9,6,6,-1,-1]
|
||||
<strong>Explanation:</strong>
|
||||
0th index: 4 is the first integer greater than 2, and 9 is the second integer greater than 2, to the right of 2.
|
||||
1st index: 9 is the first, and 6 is the second integer greater than 4, to the right of 4.
|
||||
2nd index: 9 is the first, and 6 is the second integer greater than 0, to the right of 0.
|
||||
3rd index: There is no integer greater than 9 to its right, so the second greater integer is considered to be -1.
|
||||
4th index: There is no integer greater than 6 to its right, so the second greater integer is considered to be -1.
|
||||
Thus, we return [9,6,6,-1,-1].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,3]
|
||||
<strong>Output:</strong> [-1,-1]
|
||||
<strong>Explanation:</strong>
|
||||
We return [-1,-1] since neither integer has any integer greater than it.
|
||||
</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>
|
||||
|
@@ -1,46 +1,44 @@
|
||||
<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 class="example">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 class="example">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 class="example">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>
|
||||
<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 so that there's no such <span data-keyword="subsequence-array">subsequence</span> in </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> </p>
|
||||
<p><strong class="example">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 class="example">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 class="example">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>
|
||||
|
@@ -1,50 +1,50 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length.</p>
|
||||
|
||||
<p>As long as <code>nums</code> is <strong>not</strong> empty, you must repetitively:</p>
|
||||
|
||||
<ul>
|
||||
<li>Find the minimum number in <code>nums</code> and remove it.</li>
|
||||
<li>Find the maximum number in <code>nums</code> and remove it.</li>
|
||||
<li>Calculate the average of the two removed numbers.</li>
|
||||
</ul>
|
||||
|
||||
<p>The <strong>average</strong> of two numbers <code>a</code> and <code>b</code> is <code>(a + b) / 2</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, the average of <code>2</code> and <code>3</code> is <code>(2 + 3) / 2 = 2.5</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> the number of <strong>distinct</strong> averages calculated using the above process</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that when there is a tie for a minimum or maximum number, any can be removed.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,1,4,0,3,5]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
1. Remove 0 and 5, and the average is (0 + 5) / 2 = 2.5. Now, nums = [4,1,4,3].
|
||||
2. Remove 1 and 4. The average is (1 + 4) / 2 = 2.5, and nums = [4,3].
|
||||
3. Remove 3 and 4, and the average is (3 + 4) / 2 = 3.5.
|
||||
Since there are 2 distinct numbers among 2.5, 2.5, and 3.5, we return 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,100]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
There is only one average to be calculated after removing 1 and 100, so we return 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 100</code></li>
|
||||
<li><code>nums.length</code> is even.</li>
|
||||
<li><code>0 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length.</p>
|
||||
|
||||
<p>As long as <code>nums</code> is <strong>not</strong> empty, you must repetitively:</p>
|
||||
|
||||
<ul>
|
||||
<li>Find the minimum number in <code>nums</code> and remove it.</li>
|
||||
<li>Find the maximum number in <code>nums</code> and remove it.</li>
|
||||
<li>Calculate the average of the two removed numbers.</li>
|
||||
</ul>
|
||||
|
||||
<p>The <strong>average</strong> of two numbers <code>a</code> and <code>b</code> is <code>(a + b) / 2</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, the average of <code>2</code> and <code>3</code> is <code>(2 + 3) / 2 = 2.5</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> the number of <strong>distinct</strong> averages calculated using the above process</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that when there is a tie for a minimum or maximum number, any can be removed.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,1,4,0,3,5]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
1. Remove 0 and 5, and the average is (0 + 5) / 2 = 2.5. Now, nums = [4,1,4,3].
|
||||
2. Remove 1 and 4. The average is (1 + 4) / 2 = 2.5, and nums = [4,3].
|
||||
3. Remove 3 and 4, and the average is (3 + 4) / 2 = 3.5.
|
||||
Since there are 2 distinct numbers among 2.5, 2.5, and 3.5, we return 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,100]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
There is only one average to be calculated after removing 1 and 100, so we return 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 100</code></li>
|
||||
<li><code>nums.length</code> is even.</li>
|
||||
<li><code>0 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
||||
|
@@ -1,38 +1,38 @@
|
||||
<p>You are given a string <code>s</code> and a <strong>positive</strong> integer <code>k</code>.</p>
|
||||
|
||||
<p>Select a set of <strong>non-overlapping</strong> substrings from the string <code>s</code> that satisfy the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>The <strong>length</strong> of each substring is <strong>at least</strong> <code>k</code>.</li>
|
||||
<li>Each substring is a <strong>palindrome</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of substrings in an optimal selection</em>.</p>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abaccdbbd", k = 3
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can select the substrings underlined in s = "<u><strong>aba</strong></u>cc<u><strong>dbbd</strong></u>". Both "aba" and "dbbd" are palindromes and have a length of at least k = 3.
|
||||
It can be shown that we cannot find a selection with more than two valid substrings.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "adbcda", k = 2
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There is no palindrome substring of length at least 2 in the string.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= s.length <= 2000</code></li>
|
||||
<li><code>s</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
||||
<p>You are given a string <code>s</code> and a <strong>positive</strong> integer <code>k</code>.</p>
|
||||
|
||||
<p>Select a set of <strong>non-overlapping</strong> substrings from the string <code>s</code> that satisfy the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>The <strong>length</strong> of each substring is <strong>at least</strong> <code>k</code>.</li>
|
||||
<li>Each substring is a <strong>palindrome</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of substrings in an optimal selection</em>.</p>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abaccdbbd", k = 3
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can select the substrings underlined in s = "<u><strong>aba</strong></u>cc<u><strong>dbbd</strong></u>". Both "aba" and "dbbd" are palindromes and have a length of at least k = 3.
|
||||
It can be shown that we cannot find a selection with more than two valid substrings.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "adbcda", k = 2
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There is no palindrome substring of length at least 2 in the string.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= s.length <= 2000</code></li>
|
||||
<li><code>s</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
||||
|
@@ -18,7 +18,7 @@
|
||||
Continuous subarray of size 1: [5], [4], [2], [4].
|
||||
Continuous subarray of size 2: [5,4], [4,2], [2,4].
|
||||
Continuous subarray of size 3: [4,2,4].
|
||||
Thereare no subarrys of size 4.
|
||||
There are no subarrys of size 4.
|
||||
Total continuous subarrays = 4 + 3 + 1 = 8.
|
||||
It can be shown that there are no more continuous subarrays.
|
||||
</pre>
|
||||
|
@@ -1,37 +1,37 @@
|
||||
<p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
|
||||
|
||||
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-1,2,-3,3]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> There is no a single valid k, we return -1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>-1000 <= nums[i] <= 1000</code></li>
|
||||
<li><code>nums[i] != 0</code></li>
|
||||
</ul>
|
||||
<p>Given an integer array <code>nums</code> that <strong>does not contain</strong> any zeros, find <strong>the largest positive</strong> integer <code>k</code> such that <code>-k</code> also exists in the array.</p>
|
||||
|
||||
<p>Return <em>the positive integer </em><code>k</code>. If there is no such integer, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-1,2,-3,3]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> 3 is the only valid k we can find in the array.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-1,10,6,7,-7,1]
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> Both 1 and 7 have their corresponding negative values in the array. 7 has a larger value.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [-10,8,6,7,-2,-3]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> There is no a single valid k, we return -1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>-1000 <= nums[i] <= 1000</code></li>
|
||||
<li><code>nums[i] != 0</code></li>
|
||||
</ul>
|
||||
|
@@ -1,4 +1,4 @@
|
||||
<p>An <strong>ugly number</strong> is a positive integer whose prime factors are limited to <code>2</code>, <code>3</code>, and <code>5</code>.</p>
|
||||
<p>An <strong>ugly number</strong> is a <em>positive</em> integer which does not have a prime factor other than 2, 3, and 5.</p>
|
||||
|
||||
<p>Given an integer <code>n</code>, return <code>true</code> <em>if</em> <code>n</code> <em>is an <strong>ugly number</strong></em>.</p>
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> 1 has no prime factors, therefore all of its prime factors are limited to 2, 3, and 5.
|
||||
<strong>Explanation:</strong> 1 has no prime factors.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
@@ -2,25 +2,40 @@
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg" style="width: 422px; height: 222px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> head = [1,2,3,4]
|
||||
<strong>Output:</strong> [2,1,4,3]
|
||||
</pre>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">head = [1,2,3,4]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[2,1,4,3]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2020/10/03/swap_ex1.jpg" style="width: 422px; height: 222px;" /></p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> head = []
|
||||
<strong>Output:</strong> []
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">head = []</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> head = [1]
|
||||
<strong>Output:</strong> [1]
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">head = [1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">head = [1,2,3]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[2,1,3]</span></p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
@@ -1,45 +1,45 @@
|
||||
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bidirectional </strong>road between cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> with a distance equal to <code>distance<sub>i</sub></code>. The cities graph is not necessarily connected.</p>
|
||||
|
||||
<p>The <strong>score</strong> of a path between two cities is defined as the <strong>minimum </strong>distance of a road in this path.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum </strong>possible score of a path between cities </em><code>1</code><em> and </em><code>n</code>.</p>
|
||||
|
||||
<p><strong>Note</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>A path is a sequence of roads between two cities.</li>
|
||||
<li>It is allowed for a path to contain the same road <strong>multiple</strong> times, and you can visit cities <code>1</code> and <code>n</code> multiple times along the path.</li>
|
||||
<li>The test cases are generated such that there is <strong>at least</strong> one path between <code>1</code> and <code>n</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/12/graph11.png" style="width: 190px; height: 231px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, roads = [[1,2,9],[2,3,6],[2,4,5],[1,4,7]]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The path from city 1 to 4 with the minimum score is: 1 -> 2 -> 4. The score of this path is min(9,5) = 5.
|
||||
It can be shown that no other path has less score.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/12/graph22.png" style="width: 190px; height: 231px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, roads = [[1,2,2],[1,3,4],[3,4,7]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The path from city 1 to 4 with the minimum score is: 1 -> 2 -> 1 -> 3 -> 4. The score of this path is min(2,2,4,7) = 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= roads.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>roads[i].length == 3</code></li>
|
||||
<li><code>1 <= a<sub>i</sub>, b<sub>i</sub> <= n</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li><code>1 <= distance<sub>i</sub> <= 10<sup>4</sup></code></li>
|
||||
<li>There are no repeated edges.</li>
|
||||
<li>There is at least one path between <code>1</code> and <code>n</code>.</li>
|
||||
</ul>
|
||||
<p>You are given a positive integer <code>n</code> representing <code>n</code> cities numbered from <code>1</code> to <code>n</code>. You are also given a <strong>2D</strong> array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>, distance<sub>i</sub>]</code> indicates that there is a <strong>bidirectional </strong>road between cities <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> with a distance equal to <code>distance<sub>i</sub></code>. The cities graph is not necessarily connected.</p>
|
||||
|
||||
<p>The <strong>score</strong> of a path between two cities is defined as the <strong>minimum </strong>distance of a road in this path.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum </strong>possible score of a path between cities </em><code>1</code><em> and </em><code>n</code>.</p>
|
||||
|
||||
<p><strong>Note</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>A path is a sequence of roads between two cities.</li>
|
||||
<li>It is allowed for a path to contain the same road <strong>multiple</strong> times, and you can visit cities <code>1</code> and <code>n</code> multiple times along the path.</li>
|
||||
<li>The test cases are generated such that there is <strong>at least</strong> one path between <code>1</code> and <code>n</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/12/graph11.png" style="width: 190px; height: 231px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, roads = [[1,2,9],[2,3,6],[2,4,5],[1,4,7]]
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The path from city 1 to 4 with the minimum score is: 1 -> 2 -> 4. The score of this path is min(9,5) = 5.
|
||||
It can be shown that no other path has less score.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/12/graph22.png" style="width: 190px; height: 231px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4, roads = [[1,2,2],[1,3,4],[3,4,7]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The path from city 1 to 4 with the minimum score is: 1 -> 2 -> 1 -> 3 -> 4. The score of this path is min(2,2,4,7) = 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= roads.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>roads[i].length == 3</code></li>
|
||||
<li><code>1 <= a<sub>i</sub>, b<sub>i</sub> <= n</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li><code>1 <= distance<sub>i</sub> <= 10<sup>4</sup></code></li>
|
||||
<li>There are no repeated edges.</li>
|
||||
<li>There is at least one path between <code>1</code> and <code>n</code>.</li>
|
||||
</ul>
|
||||
|
@@ -22,7 +22,7 @@
|
||||
<li>The absolute difference between the index of the occurrence of <code>"c"</code> in <code>s</code> and the index of the occurrence of <code>"c"</code> in <code>t</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>That is, the permutation difference between <code>s</code> and <code>t</code> is equal to <code>|0 - 1| + |2 - 2| + |1 - 0| = 2</code>.</p>
|
||||
<p>That is, the permutation difference between <code>s</code> and <code>t</code> is equal to <code>|0 - 1| + |1 - 0| + |2 - 2| = 2</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
@@ -1,4 +1,4 @@
|
||||
<p>Given two integer arrays <code>nums1</code> and <code>nums2</code>, return <em>an array of their intersection</em>. Each element in the result must be <strong>unique</strong> and you may return the result in <strong>any order</strong>.</p>
|
||||
<p>Given two integer arrays <code>nums1</code> and <code>nums2</code>, return <em>an array of their <span data-keyword="array-intersection">intersection</span></em>. Each element in the result must be <strong>unique</strong> and you may return the result in <strong>any order</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
@@ -1,45 +1,45 @@
|
||||
<p>There are some prizes on the <strong>X-axis</strong>. You are given an integer array <code>prizePositions</code> that is <strong>sorted in non-decreasing order</strong>, where <code>prizePositions[i]</code> is the position of the <code>i<sup>th</sup></code> prize. There could be different prizes at the same position on the line. You are also given an integer <code>k</code>.</p>
|
||||
|
||||
<p>You are allowed to select two segments with integer endpoints. The length of each segment must be <code>k</code>. You will collect all prizes whose position falls within at least one of the two selected segments (including the endpoints of the segments). The two selected segments may intersect.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example if <code>k = 2</code>, you can choose segments <code>[1, 3]</code> and <code>[2, 4]</code>, and you will win any prize <font face="monospace">i</font> that satisfies <code>1 <= prizePositions[i] <= 3</code> or <code>2 <= prizePositions[i] <= 4</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of prizes you can win if you choose the two segments optimally</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> prizePositions = [1,1,2,2,3,3,5], k = 2
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> In this example, you can win all 7 prizes by selecting two segments [1, 3] and [3, 5].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> prizePositions = [1,2,3,4], k = 0
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> For this example, <strong>one choice</strong> for the segments is <code>[3, 3]</code> and <code>[4, 4],</code> and you will be able to get <code>2</code> prizes.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= prizePositions.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= prizePositions[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>9</sup> </code></li>
|
||||
<li><code>prizePositions</code> is sorted in non-decreasing order.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
|
||||
}
|
||||
.spoiler {overflow:hidden;}
|
||||
.spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
|
||||
.spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;}
|
||||
.spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}
|
||||
</style>
|
||||
<p>There are some prizes on the <strong>X-axis</strong>. You are given an integer array <code>prizePositions</code> that is <strong>sorted in non-decreasing order</strong>, where <code>prizePositions[i]</code> is the position of the <code>i<sup>th</sup></code> prize. There could be different prizes at the same position on the line. You are also given an integer <code>k</code>.</p>
|
||||
|
||||
<p>You are allowed to select two segments with integer endpoints. The length of each segment must be <code>k</code>. You will collect all prizes whose position falls within at least one of the two selected segments (including the endpoints of the segments). The two selected segments may intersect.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example if <code>k = 2</code>, you can choose segments <code>[1, 3]</code> and <code>[2, 4]</code>, and you will win any prize <font face="monospace">i</font> that satisfies <code>1 <= prizePositions[i] <= 3</code> or <code>2 <= prizePositions[i] <= 4</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of prizes you can win if you choose the two segments optimally</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> prizePositions = [1,1,2,2,3,3,5], k = 2
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> In this example, you can win all 7 prizes by selecting two segments [1, 3] and [3, 5].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> prizePositions = [1,2,3,4], k = 0
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> For this example, <strong>one choice</strong> for the segments is <code>[3, 3]</code> and <code>[4, 4],</code> and you will be able to get <code>2</code> prizes.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= prizePositions.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= prizePositions[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>9</sup> </code></li>
|
||||
<li><code>prizePositions</code> is sorted in non-decreasing order.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
|
||||
}
|
||||
.spoiler {overflow:hidden;}
|
||||
.spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
|
||||
.spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;}
|
||||
.spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}
|
||||
</style>
|
||||
|
@@ -6,12 +6,25 @@
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<pre><strong>Input:</strong> s1 = "this apple is sweet", s2 = "this apple is sour"
|
||||
<strong>Output:</strong> ["sweet","sour"]
|
||||
</pre><p><strong class="example">Example 2:</strong></p>
|
||||
<pre><strong>Input:</strong> s1 = "apple apple", s2 = "banana"
|
||||
<strong>Output:</strong> ["banana"]
|
||||
</pre>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s1 = "this apple is sweet", s2 = "this apple is sour"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">["sweet","sour"]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The word <code>"sweet"</code> appears only in <code>s1</code>, while the word <code>"sour"</code> appears only in <code>s2</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s1 = "apple apple", s2 = "banana"</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">["banana"]</span></p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
<p>Given a <strong>1-indexed</strong> array of integers <code>numbers</code> that is already <strong><em>sorted in non-decreasing order</em></strong>, find two numbers such that they add up to a specific <code>target</code> number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index<sub>2</sub>]</code> where <code>1 <= index<sub>1</sub> < index<sub>2</sub> < numbers.length</code>.</p>
|
||||
<p>Given a <strong>1-indexed</strong> array of integers <code>numbers</code> that is already <strong><em>sorted in non-decreasing order</em></strong>, find two numbers such that they add up to a specific <code>target</code> number. Let these two numbers be <code>numbers[index<sub>1</sub>]</code> and <code>numbers[index<sub>2</sub>]</code> where <code>1 <= index<sub>1</sub> < index<sub>2</sub> <= numbers.length</code>.</p>
|
||||
|
||||
<p>Return<em> the indices of the two numbers, </em><code>index<sub>1</sub></code><em> and </em><code>index<sub>2</sub></code><em>, <strong>added by one</strong> as an integer array </em><code>[index<sub>1</sub>, index<sub>2</sub>]</code><em> of length 2.</em></p>
|
||||
|
||||
|
@@ -1,34 +1,34 @@
|
||||
<p>An integer <code>n</code> is <strong>strictly palindromic</strong> if, for <strong>every</strong> base <code>b</code> between <code>2</code> and <code>n - 2</code> (<strong>inclusive</strong>), the string representation of the integer <code>n</code> in base <code>b</code> is <strong>palindromic</strong>.</p>
|
||||
|
||||
<p>Given an integer <code>n</code>, return <code>true</code> <em>if </em><code>n</code><em> is <strong>strictly palindromic</strong> and </em><code>false</code><em> otherwise</em>.</p>
|
||||
|
||||
<p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 9
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> In base 2: 9 = 1001 (base 2), which is palindromic.
|
||||
In base 3: 9 = 100 (base 3), which is not palindromic.
|
||||
Therefore, 9 is not strictly palindromic so we return false.
|
||||
Note that in bases 4, 5, 6, and 7, n = 9 is also not palindromic.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> We only consider base 2: 4 = 100 (base 2), which is not palindromic.
|
||||
Therefore, we return false.
|
||||
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>4 <= n <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
<p>An integer <code>n</code> is <strong>strictly palindromic</strong> if, for <strong>every</strong> base <code>b</code> between <code>2</code> and <code>n - 2</code> (<strong>inclusive</strong>), the string representation of the integer <code>n</code> in base <code>b</code> is <strong>palindromic</strong>.</p>
|
||||
|
||||
<p>Given an integer <code>n</code>, return <code>true</code> <em>if </em><code>n</code><em> is <strong>strictly palindromic</strong> and </em><code>false</code><em> otherwise</em>.</p>
|
||||
|
||||
<p>A string is <strong>palindromic</strong> if it reads the same forward and backward.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 9
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> In base 2: 9 = 1001 (base 2), which is palindromic.
|
||||
In base 3: 9 = 100 (base 3), which is not palindromic.
|
||||
Therefore, 9 is not strictly palindromic so we return false.
|
||||
Note that in bases 4, 5, 6, and 7, n = 9 is also not palindromic.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> We only consider base 2: 4 = 100 (base 2), which is not palindromic.
|
||||
Therefore, we return false.
|
||||
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>4 <= n <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
|
@@ -1,45 +1,52 @@
|
||||
<p>You are given a string <code>s</code> and an array of strings <code>words</code>. All the strings of <code>words</code> are of <strong>the same length</strong>.</p>
|
||||
|
||||
<p>A <strong>concatenated substring</strong> in <code>s</code> is a substring that contains all the strings of any permutation of <code>words</code> concatenated.</p>
|
||||
<p>A <strong>concatenated string</strong> is a string that exactly contains all the strings of any permutation of <code>words</code> concatenated.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, if <code>words = ["ab","cd","ef"]</code>, then <code>"abcdef"</code>, <code>"abefcd"</code>, <code>"cdabef"</code>, <code>"cdefab"</code>, <code>"efabcd"</code>, and <code>"efcdab"</code> are all concatenated strings. <code>"acdbef"</code> is not a concatenated substring because it is not the concatenation of any permutation of <code>words</code>.</li>
|
||||
<li>For example, if <code>words = ["ab","cd","ef"]</code>, then <code>"abcdef"</code>, <code>"abefcd"</code>, <code>"cdabef"</code>, <code>"cdefab"</code>, <code>"efabcd"</code>, and <code>"efcdab"</code> are all concatenated strings. <code>"acdbef"</code> is not a concatenated string because it is not the concatenation of any permutation of <code>words</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the starting indices of all the concatenated substrings in </em><code>s</code>. You can return the answer in <strong>any order</strong>.</p>
|
||||
<p>Return an array of <em>the starting indices</em> of all the concatenated substrings in <code>s</code>. You can return the answer in <strong>any order</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "barfoothefoobarman", words = ["foo","bar"]
|
||||
<strong>Output:</strong> [0,9]
|
||||
<strong>Explanation:</strong> Since words.length == 2 and words[i].length == 3, the concatenated substring has to be of length 6.
|
||||
The substring starting at 0 is "barfoo". It is the concatenation of ["bar","foo"] which is a permutation of words.
|
||||
The substring starting at 9 is "foobar". It is the concatenation of ["foo","bar"] which is a permutation of words.
|
||||
The output order does not matter. Returning [9,0] is fine too.
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "barfoothefoobarman", words = ["foo","bar"]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[0,9]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The substring starting at 0 is <code>"barfoo"</code>. It is the concatenation of <code>["bar","foo"]</code> which is a permutation of <code>words</code>.<br />
|
||||
The substring starting at 9 is <code>"foobar"</code>. It is the concatenation of <code>["foo","bar"]</code> which is a permutation of <code>words</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong> Since words.length == 4 and words[i].length == 4, the concatenated substring has to be of length 16.
|
||||
There is no substring of length 16 in s that is equal to the concatenation of any permutation of words.
|
||||
We return an empty array.
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>There is no concatenated substring.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
|
||||
<strong>Output:</strong> [6,9,12]
|
||||
<strong>Explanation:</strong> Since words.length == 3 and words[i].length == 3, the concatenated substring has to be of length 9.
|
||||
The substring starting at 6 is "foobarthe". It is the concatenation of ["foo","bar","the"] which is a permutation of words.
|
||||
The substring starting at 9 is "barthefoo". It is the concatenation of ["bar","the","foo"] which is a permutation of words.
|
||||
The substring starting at 12 is "thefoobar". It is the concatenation of ["the","foo","bar"] which is a permutation of words.
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[6,9,12]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The substring starting at 6 is <code>"foobarthe"</code>. It is the concatenation of <code>["foo","bar","the"]</code>.<br />
|
||||
The substring starting at 9 is <code>"barthefoo"</code>. It is the concatenation of <code>["bar","the","foo"]</code>.<br />
|
||||
The substring starting at 12 is <code>"thefoobar"</code>. It is the concatenation of <code>["the","foo","bar"]</code>.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
@@ -33,4 +33,5 @@
|
||||
<li><code>1 <= expression.length <= 20</code></li>
|
||||
<li><code>expression</code> consists of digits and the operator <code>'+'</code>, <code>'-'</code>, and <code>'*'</code>.</li>
|
||||
<li>All the integer values in the input expression are in the range <code>[0, 99]</code>.</li>
|
||||
<li>The integer values in the input expression do not have a leading <code>'-'</code> or <code>'+'</code> denoting the sign.</li>
|
||||
</ul>
|
||||
|
@@ -25,5 +25,5 @@
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 2 * 10<sup>4</sup></code></li>
|
||||
<li><code>-10 <= nums[i] <= 10</code></li>
|
||||
<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
|
||||
<li>The product of any subarray of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
|
||||
</ul>
|
||||
|
@@ -4,30 +4,45 @@
|
||||
|
||||
<p>Each person takes <strong>exactly 1 second</strong> to buy a ticket. A person can only buy <strong>1 ticket at a time</strong> and has to go back to <strong>the end</strong> of the line (which happens <strong>instantaneously</strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will <strong>leave </strong>the line.</p>
|
||||
|
||||
<p>Return <em>the <strong>time taken</strong> for the person at position </em><code>k</code><em> </em><strong><em>(0-indexed)</em> </strong><em>to finish buying tickets</em>.</p>
|
||||
<p>Return the <strong>time taken</strong> for the person <strong>initially</strong> at position <strong>k</strong><strong> </strong>(0-indexed) to finish buying tickets.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tickets = [2,3,2], k = 2
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong>
|
||||
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
|
||||
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
|
||||
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">tickets = [2,3,2], k = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">6</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The queue starts as [2,3,<u>2</u>], where the kth person is underlined.</li>
|
||||
<li>After the person at the front has bought a ticket, the queue becomes [3,<u>2</u>,1] at 1 second.</li>
|
||||
<li>Continuing this process, the queue becomes [<u>2</u>,1,2] at 2 seconds.</li>
|
||||
<li>Continuing this process, the queue becomes [1,2,<u>1</u>] at 3 seconds.</li>
|
||||
<li>Continuing this process, the queue becomes [2,<u>1</u>] at 4 seconds. Note: the person at the front left the queue.</li>
|
||||
<li>Continuing this process, the queue becomes [<u>1</u>,1] at 5 seconds.</li>
|
||||
<li>Continuing this process, the queue becomes [1] at 6 seconds. The kth person has bought all their tickets, so return 6.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tickets = [5,1,1,1], k = 0
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong>
|
||||
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
|
||||
- In the next 4 passes, only the person in position 0 is buying tickets.
|
||||
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">tickets = [5,1,1,1], k = 0</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">8</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The queue starts as [<u>5</u>,1,1,1], where the kth person is underlined.</li>
|
||||
<li>After the person at the front has bought a ticket, the queue becomes [1,1,1,<u>4</u>] at 1 second.</li>
|
||||
<li>Continuing this process for 3 seconds, the queue becomes [<u>4]</u> at 4 seconds.</li>
|
||||
<li>Continuing this process for 4 seconds, the queue becomes [] at 8 seconds. The kth person has bought all their tickets, so return 8.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
@@ -15,7 +15,7 @@
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
actions = ["EventEmitter", "emit", "subscribe", "subscribe", "emit"],
|
||||
values = [[], ["firstEvent", "function cb1() { return 5; }"], ["firstEvent", "function cb1() { return 6; }"], ["firstEvent"]]
|
||||
values = [[], ["firstEvent"], ["firstEvent", "function cb1() { return 5; }"], ["firstEvent", "function cb1() { return 6; }"], ["firstEvent"]]
|
||||
<strong>Output:</strong> [[],["emitted",[]],["subscribed"],["subscribed"],["emitted",[5,6]]]
|
||||
<strong>Explanation:</strong>
|
||||
const emitter = new EventEmitter();
|
||||
@@ -61,7 +61,7 @@ emitter.emit("firstEvent", [4, 5, 6]); // [], there are no subscriptio
|
||||
<strong>Input:</strong>
|
||||
actions = ["EventEmitter", "subscribe", "subscribe", "unsubscribe", "emit"],
|
||||
values = [[], ["firstEvent", "x => x + 1"], ["firstEvent", "x => x + 2"], [0], ["firstEvent", [5]]]
|
||||
<strong>Output:</strong> [[],["subscribed"],["emitted",["1,2,3"]],["unsubscribed",0],["emitted",[7]]]
|
||||
<strong>Output:</strong> [[],["subscribed"],["subscribed"],["unsubscribed",0],["emitted",[7]]]
|
||||
<strong>Explanation:</strong>
|
||||
const emitter = new EventEmitter();
|
||||
const sub1 = emitter.subscribe("firstEvent", x => x + 1);
|
||||
|
@@ -1,41 +1,41 @@
|
||||
<p>You are given the <code>root</code> of a <strong>binary search tree </strong>and an array <code>queries</code> of size <code>n</code> consisting of positive integers.</p>
|
||||
|
||||
<p>Find a <strong>2D</strong> array <code>answer</code> of size <code>n</code> where <code>answer[i] = [min<sub>i</sub>, max<sub>i</sub>]</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>min<sub>i</sub></code> is the <strong>largest</strong> value in the tree that is smaller than or equal to <code>queries[i]</code>. If a such value does not exist, add <code>-1</code> instead.</li>
|
||||
<li><code>max<sub>i</sub></code> is the <strong>smallest</strong> value in the tree that is greater than or equal to <code>queries[i]</code>. If a such value does not exist, add <code>-1</code> instead.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the array</em> <code>answer</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/09/28/bstreeedrawioo.png" style="width: 261px; height: 281px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [6,2,13,1,4,9,15,null,null,null,null,null,null,14], queries = [2,5,16]
|
||||
<strong>Output:</strong> [[2,2],[4,6],[15,-1]]
|
||||
<strong>Explanation:</strong> We answer the queries in the following way:
|
||||
- The largest number that is smaller or equal than 2 in the tree is 2, and the smallest number that is greater or equal than 2 is still 2. So the answer for the first query is [2,2].
|
||||
- The largest number that is smaller or equal than 5 in the tree is 4, and the smallest number that is greater or equal than 5 is 6. So the answer for the second query is [4,6].
|
||||
- The largest number that is smaller or equal than 16 in the tree is 15, and the smallest number that is greater or equal than 16 does not exist. So the answer for the third query is [15,-1].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/09/28/bstttreee.png" style="width: 101px; height: 121px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [4,null,9], queries = [3]
|
||||
<strong>Output:</strong> [[-1,4]]
|
||||
<strong>Explanation:</strong> The largest number that is smaller or equal to 3 in the tree does not exist, and the smallest number that is greater or equal to 3 is 4. So the answer for the query is [-1,4].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
|
||||
<li><code>1 <= Node.val <= 10<sup>6</sup></code></li>
|
||||
<li><code>n == queries.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= queries[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
||||
<p>You are given the <code>root</code> of a <strong>binary search tree </strong>and an array <code>queries</code> of size <code>n</code> consisting of positive integers.</p>
|
||||
|
||||
<p>Find a <strong>2D</strong> array <code>answer</code> of size <code>n</code> where <code>answer[i] = [min<sub>i</sub>, max<sub>i</sub>]</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>min<sub>i</sub></code> is the <strong>largest</strong> value in the tree that is smaller than or equal to <code>queries[i]</code>. If a such value does not exist, add <code>-1</code> instead.</li>
|
||||
<li><code>max<sub>i</sub></code> is the <strong>smallest</strong> value in the tree that is greater than or equal to <code>queries[i]</code>. If a such value does not exist, add <code>-1</code> instead.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the array</em> <code>answer</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/09/28/bstreeedrawioo.png" style="width: 261px; height: 281px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [6,2,13,1,4,9,15,null,null,null,null,null,null,14], queries = [2,5,16]
|
||||
<strong>Output:</strong> [[2,2],[4,6],[15,-1]]
|
||||
<strong>Explanation:</strong> We answer the queries in the following way:
|
||||
- The largest number that is smaller or equal than 2 in the tree is 2, and the smallest number that is greater or equal than 2 is still 2. So the answer for the first query is [2,2].
|
||||
- The largest number that is smaller or equal than 5 in the tree is 4, and the smallest number that is greater or equal than 5 is 6. So the answer for the second query is [4,6].
|
||||
- The largest number that is smaller or equal than 16 in the tree is 15, and the smallest number that is greater or equal than 16 does not exist. So the answer for the third query is [15,-1].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/09/28/bstttreee.png" style="width: 101px; height: 121px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [4,null,9], queries = [3]
|
||||
<strong>Output:</strong> [[-1,4]]
|
||||
<strong>Explanation:</strong> The largest number that is smaller or equal to 3 in the tree does not exist, and the smallest number that is greater or equal to 3 is 4. So the answer for the query is [-1,4].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.</li>
|
||||
<li><code>1 <= Node.val <= 10<sup>6</sup></code></li>
|
||||
<li><code>n == queries.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= queries[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
||||
|
@@ -1,39 +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>
|
||||
<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>
|
||||
|
@@ -2,25 +2,44 @@
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg" style="width: 125px; height: 200px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,null,2,3]
|
||||
<strong>Output:</strong> [1,3,2]
|
||||
</pre>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[1,3,2]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = []
|
||||
<strong>Output:</strong> []
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[4,2,6,5,7,1,3,9,8]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1]
|
||||
<strong>Output:</strong> [1]
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
@@ -2,25 +2,44 @@
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg" style="width: 125px; height: 200px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,null,2,3]
|
||||
<strong>Output:</strong> [1,2,3]
|
||||
</pre>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[1,2,3]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = []
|
||||
<strong>Output:</strong> []
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[1,2,4,5,6,7,3,8,9]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1]
|
||||
<strong>Output:</strong> [1]
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
@@ -2,25 +2,44 @@
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2021/02/14/tree.jpg" style="width: 401px; height: 301px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,2,3,null,5,null,4]
|
||||
<strong>Output:</strong> [1,3,4]
|
||||
</pre>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,null,5,null,4]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[1,3,4]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/11/24/tmpd5jn43fs-1.png" style="width: 400px; height: 207px;" /></p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,null,3]
|
||||
<strong>Output:</strong> [1,3]
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,null,null,null,5]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[1,3,4,5]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/11/24/tmpkpe40xeh-1.png" style="width: 400px; height: 214px;" /></p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = []
|
||||
<strong>Output:</strong> []
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">root = [1,null,3]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[1,3]</span></p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
@@ -2,25 +2,44 @@
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/28/pre1.jpg" style="width: 127px; height: 200px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,null,2,3]
|
||||
<strong>Output:</strong> [3,2,1]
|
||||
</pre>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">root = [1,null,2,3]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[3,2,1]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/screenshot-2024-08-29-202743.png" style="width: 200px; height: 264px;" /></p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = []
|
||||
<strong>Output:</strong> []
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">root = [1,2,3,4,5,null,8,null,null,6,7,9]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[4,6,7,5,2,9,8,3,1]</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2024/08/29/tree_2.png" style="width: 350px; height: 286px;" /></p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1]
|
||||
<strong>Output:</strong> [1]
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">root = []</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[]</span></p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">root = [1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">[1]</span></p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
@@ -1,41 +1,41 @@
|
||||
<p>Given the <code>root</code> of a binary tree, replace the value of each node in the tree with the <strong>sum of all its cousins' values</strong>.</p>
|
||||
|
||||
<p>Two nodes of a binary tree are <strong>cousins</strong> if they have the same depth with different parents.</p>
|
||||
|
||||
<p>Return <em>the </em><code>root</code><em> of the modified tree</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that the depth of a node is the number of edges in the path from the root node to it.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/11/example11.png" style="width: 571px; height: 151px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [5,4,9,1,10,null,7]
|
||||
<strong>Output:</strong> [0,0,0,7,7,null,11]
|
||||
<strong>Explanation:</strong> The diagram above shows the initial binary tree and the binary tree after changing the value of each node.
|
||||
- Node with value 5 does not have any cousins so its sum is 0.
|
||||
- Node with value 4 does not have any cousins so its sum is 0.
|
||||
- Node with value 9 does not have any cousins so its sum is 0.
|
||||
- Node with value 1 has a cousin with value 7 so its sum is 7.
|
||||
- Node with value 10 has a cousin with value 7 so its sum is 7.
|
||||
- Node with value 7 has cousins with values 1 and 10 so its sum is 11.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/11/diagram33.png" style="width: 481px; height: 91px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [3,1,2]
|
||||
<strong>Output:</strong> [0,0,0]
|
||||
<strong>Explanation:</strong> The diagram above shows the initial binary tree and the binary tree after changing the value of each node.
|
||||
- Node with value 3 does not have any cousins so its sum is 0.
|
||||
- Node with value 1 does not have any cousins so its sum is 0.
|
||||
- Node with value 2 does not have any cousins so its sum is 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
|
||||
<li><code>1 <= Node.val <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
||||
<p>Given the <code>root</code> of a binary tree, replace the value of each node in the tree with the <strong>sum of all its cousins' values</strong>.</p>
|
||||
|
||||
<p>Two nodes of a binary tree are <strong>cousins</strong> if they have the same depth with different parents.</p>
|
||||
|
||||
<p>Return <em>the </em><code>root</code><em> of the modified tree</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that the depth of a node is the number of edges in the path from the root node to it.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/11/example11.png" style="width: 571px; height: 151px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [5,4,9,1,10,null,7]
|
||||
<strong>Output:</strong> [0,0,0,7,7,null,11]
|
||||
<strong>Explanation:</strong> The diagram above shows the initial binary tree and the binary tree after changing the value of each node.
|
||||
- Node with value 5 does not have any cousins so its sum is 0.
|
||||
- Node with value 4 does not have any cousins so its sum is 0.
|
||||
- Node with value 9 does not have any cousins so its sum is 0.
|
||||
- Node with value 1 has a cousin with value 7 so its sum is 7.
|
||||
- Node with value 10 has a cousin with value 7 so its sum is 7.
|
||||
- Node with value 7 has cousins with values 1 and 10 so its sum is 11.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2023/01/11/diagram33.png" style="width: 481px; height: 91px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [3,1,2]
|
||||
<strong>Output:</strong> [0,0,0]
|
||||
<strong>Explanation:</strong> The diagram above shows the initial binary tree and the binary tree after changing the value of each node.
|
||||
- Node with value 3 does not have any cousins so its sum is 0.
|
||||
- Node with value 1 does not have any cousins so its sum is 0.
|
||||
- Node with value 2 does not have any cousins so its sum is 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
|
||||
<li><code>1 <= Node.val <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
||||
|
@@ -2,7 +2,7 @@
|
||||
|
||||
<p>Design an algorithm to serialize and deserialize a binary tree. There is no restriction on how your serialization/deserialization algorithm should work. You just need to ensure that a binary tree can be serialized to a string and this string can be deserialized to the original tree structure.</p>
|
||||
|
||||
<p><strong>Clarification:</strong> The input/output format is the same as <a href="https://support.leetcode.com/hc/en-us/articles/360011883654-What-does-1-null-2-3-mean-in-binary-tree-representation-" target="_blank">how LeetCode serializes a binary tree</a>. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.</p>
|
||||
<p><strong>Clarification:</strong> The input/output format is the same as <a href="https://support.leetcode.com/hc/en-us/articles/32442719377939-How-to-create-test-cases-on-LeetCode#h_01J5EGREAW3NAEJ14XC07GRW1A" target="_blank">how LeetCode serializes a binary tree</a>. You do not necessarily need to follow this format, so please be creative and come up with different approaches yourself.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
@@ -1,38 +1,38 @@
|
||||
<p>Given a positive integer <code>n</code>, there exists a <strong>0-indexed</strong> array called <code>powers</code>, composed of the <strong>minimum</strong> number of powers of <code>2</code> that sum to <code>n</code>. The array is sorted in <strong>non-decreasing</strong> order, and there is <strong>only one</strong> way to form the array.</p>
|
||||
|
||||
<p>You are also given a <strong>0-indexed</strong> 2D integer array <code>queries</code>, where <code>queries[i] = [left<sub>i</sub>, right<sub>i</sub>]</code>. Each <code>queries[i]</code> represents a query where you have to find the product of all <code>powers[j]</code> with <code>left<sub>i</sub> <= j <= right<sub>i</sub></code>.</p>
|
||||
|
||||
<p>Return<em> an array </em><code>answers</code><em>, equal in length to </em><code>queries</code><em>, where </em><code>answers[i]</code><em> is the answer to the </em><code>i<sup>th</sup></code><em> query</em>. Since the answer to the <code>i<sup>th</sup></code> query may be too large, each <code>answers[i]</code> should be returned <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> n = 15, queries = [[0,1],[2,2],[0,3]]
|
||||
<strong>Output:</strong> [2,4,64]
|
||||
<strong>Explanation:</strong>
|
||||
For n = 15, powers = [1,2,4,8]. It can be shown that powers cannot be a smaller size.
|
||||
Answer to 1st query: powers[0] * powers[1] = 1 * 2 = 2.
|
||||
Answer to 2nd query: powers[2] = 4.
|
||||
Answer to 3rd query: powers[0] * powers[1] * powers[2] * powers[3] = 1 * 2 * 4 * 8 = 64.
|
||||
Each answer modulo 10<sup>9</sup> + 7 yields the same answer, so [2,4,64] is returned.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2, queries = [[0,0]]
|
||||
<strong>Output:</strong> [2]
|
||||
<strong>Explanation:</strong>
|
||||
For n = 2, powers = [2].
|
||||
The answer to the only query is powers[0] = 2. The answer modulo 10<sup>9</sup> + 7 is the same, so [2] is returned.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= queries.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= start<sub>i</sub> <= end<sub>i</sub> < powers.length</code></li>
|
||||
</ul>
|
||||
<p>Given a positive integer <code>n</code>, there exists a <strong>0-indexed</strong> array called <code>powers</code>, composed of the <strong>minimum</strong> number of powers of <code>2</code> that sum to <code>n</code>. The array is sorted in <strong>non-decreasing</strong> order, and there is <strong>only one</strong> way to form the array.</p>
|
||||
|
||||
<p>You are also given a <strong>0-indexed</strong> 2D integer array <code>queries</code>, where <code>queries[i] = [left<sub>i</sub>, right<sub>i</sub>]</code>. Each <code>queries[i]</code> represents a query where you have to find the product of all <code>powers[j]</code> with <code>left<sub>i</sub> <= j <= right<sub>i</sub></code>.</p>
|
||||
|
||||
<p>Return<em> an array </em><code>answers</code><em>, equal in length to </em><code>queries</code><em>, where </em><code>answers[i]</code><em> is the answer to the </em><code>i<sup>th</sup></code><em> query</em>. Since the answer to the <code>i<sup>th</sup></code> query may be too large, each <code>answers[i]</code> should be returned <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> n = 15, queries = [[0,1],[2,2],[0,3]]
|
||||
<strong>Output:</strong> [2,4,64]
|
||||
<strong>Explanation:</strong>
|
||||
For n = 15, powers = [1,2,4,8]. It can be shown that powers cannot be a smaller size.
|
||||
Answer to 1st query: powers[0] * powers[1] = 1 * 2 = 2.
|
||||
Answer to 2nd query: powers[2] = 4.
|
||||
Answer to 3rd query: powers[0] * powers[1] * powers[2] * powers[3] = 1 * 2 * 4 * 8 = 64.
|
||||
Each answer modulo 10<sup>9</sup> + 7 yields the same answer, so [2,4,64] is returned.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2, queries = [[0,0]]
|
||||
<strong>Output:</strong> [2]
|
||||
<strong>Explanation:</strong>
|
||||
For n = 2, powers = [2].
|
||||
The answer to the only query is powers[0] = 2. The answer modulo 10<sup>9</sup> + 7 is the same, so [2] is returned.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= queries.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= start<sub>i</sub> <= end<sub>i</sub> < powers.length</code></li>
|
||||
</ul>
|
||||
|
@@ -1,41 +1,41 @@
|
||||
<p>You are given a binary string <code>s</code>. In one second, <strong>all</strong> occurrences of <code>"01"</code> are <strong>simultaneously</strong> replaced with <code>"10"</code>. This process <strong>repeats</strong> until no occurrences of <code>"01"</code> exist.</p>
|
||||
|
||||
<p>Return<em> the number of seconds needed to complete this process.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "0110101"
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
After one second, s becomes "1011010".
|
||||
After another second, s becomes "1101100".
|
||||
After the third second, s becomes "1110100".
|
||||
After the fourth second, s becomes "1111000".
|
||||
No occurrence of "01" exists any longer, and the process needed 4 seconds to complete,
|
||||
so we return 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "11100"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
No occurrence of "01" exists in s, and the processes needed 0 seconds to complete,
|
||||
so we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Follow up:</strong></p>
|
||||
|
||||
<p>Can you solve this problem in O(n) time complexity?</p>
|
||||
<p>You are given a binary string <code>s</code>. In one second, <strong>all</strong> occurrences of <code>"01"</code> are <strong>simultaneously</strong> replaced with <code>"10"</code>. This process <strong>repeats</strong> until no occurrences of <code>"01"</code> exist.</p>
|
||||
|
||||
<p>Return<em> the number of seconds needed to complete this process.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "0110101"
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
After one second, s becomes "1011010".
|
||||
After another second, s becomes "1101100".
|
||||
After the third second, s becomes "1110100".
|
||||
After the fourth second, s becomes "1111000".
|
||||
No occurrence of "01" exists any longer, and the process needed 4 seconds to complete,
|
||||
so we return 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "11100"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
No occurrence of "01" exists in s, and the processes needed 0 seconds to complete,
|
||||
so we return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 1000</code></li>
|
||||
<li><code>s[i]</code> is either <code>'0'</code> or <code>'1'</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Follow up:</strong></p>
|
||||
|
||||
<p>Can you solve this problem in O(n) time complexity?</p>
|
||||
|
@@ -1,36 +1,36 @@
|
||||
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> <strong>binary</strong> matrix <code>grid</code>. You can move from a cell <code>(row, col)</code> to any of the cells <code>(row + 1, col)</code> or <code>(row, col + 1)</code> that has the value <code>1</code>. The matrix is <strong>disconnected</strong> if there is no path from <code>(0, 0)</code> to <code>(m - 1, n - 1)</code>.</p>
|
||||
|
||||
<p>You can flip the value of <strong>at most one</strong> (possibly none) cell. You <strong>cannot flip</strong> the cells <code>(0, 0)</code> and <code>(m - 1, n - 1)</code>.</p>
|
||||
|
||||
<p>Return <code>true</code> <em>if it is possible to make the matrix disconnect or </em><code>false</code><em> otherwise</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that flipping a cell changes its value from <code>0</code> to <code>1</code> or from <code>1</code> to <code>0</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/07/yetgrid2drawio.png" style="width: 441px; height: 151px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,1,1],[1,0,0],[1,1,1]]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> We can change the cell shown in the diagram above. There is no path from (0, 0) to (2, 2) in the resulting grid.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/07/yetgrid3drawio.png" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,1,1],[1,0,1],[1,1,1]]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> It is not possible to change at most one cell such that there is not path from (0, 0) to (2, 2).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 1000</code></li>
|
||||
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
|
||||
<li><code>grid[0][0] == grid[m - 1][n - 1] == 1</code></li>
|
||||
</ul>
|
||||
<p>You are given a <strong>0-indexed</strong> <code>m x n</code> <strong>binary</strong> matrix <code>grid</code>. You can move from a cell <code>(row, col)</code> to any of the cells <code>(row + 1, col)</code> or <code>(row, col + 1)</code> that has the value <code>1</code>. The matrix is <strong>disconnected</strong> if there is no path from <code>(0, 0)</code> to <code>(m - 1, n - 1)</code>.</p>
|
||||
|
||||
<p>You can flip the value of <strong>at most one</strong> (possibly none) cell. You <strong>cannot flip</strong> the cells <code>(0, 0)</code> and <code>(m - 1, n - 1)</code>.</p>
|
||||
|
||||
<p>Return <code>true</code> <em>if it is possible to make the matrix disconnect or </em><code>false</code><em> otherwise</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that flipping a cell changes its value from <code>0</code> to <code>1</code> or from <code>1</code> to <code>0</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/07/yetgrid2drawio.png" style="width: 441px; height: 151px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,1,1],[1,0,0],[1,1,1]]
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> We can change the cell shown in the diagram above. There is no path from (0, 0) to (2, 2) in the resulting grid.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/12/07/yetgrid3drawio.png" />
|
||||
<pre>
|
||||
<strong>Input:</strong> grid = [[1,1,1],[1,0,1],[1,1,1]]
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> It is not possible to change at most one cell such that there is not path from (0, 0) to (2, 2).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>m == grid.length</code></li>
|
||||
<li><code>n == grid[i].length</code></li>
|
||||
<li><code>1 <= m, n <= 1000</code></li>
|
||||
<li><code>1 <= m * n <= 10<sup>5</sup></code></li>
|
||||
<li><code>grid[i][j]</code> is either <code>0</code> or <code>1</code>.</li>
|
||||
<li><code>grid[0][0] == grid[m - 1][n - 1] == 1</code></li>
|
||||
</ul>
|
||||
|
@@ -1,49 +1,49 @@
|
||||
<p>You are given a positive integer <code>n</code>. Each digit of <code>n</code> has a sign according to the following rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>The <strong>most significant digit</strong> is assigned a <strong>positive</strong> sign.</li>
|
||||
<li>Each other digit has an opposite sign to its adjacent digits.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the sum of all digits with their corresponding sign</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 521
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> (+5) + (-2) + (+1) = 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 111
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> (+1) + (-1) + (+1) = 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 886996
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> (+8) + (-8) + (+6) + (-9) + (+9) + (-6) = 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
|
||||
}
|
||||
.spoiler {overflow:hidden;}
|
||||
.spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
|
||||
.spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;}
|
||||
.spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}
|
||||
</style>
|
||||
<p>You are given a positive integer <code>n</code>. Each digit of <code>n</code> has a sign according to the following rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>The <strong>most significant digit</strong> is assigned a <strong>positive</strong> sign.</li>
|
||||
<li>Each other digit has an opposite sign to its adjacent digits.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the sum of all digits with their corresponding sign</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 521
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> (+5) + (-2) + (+1) = 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 111
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> (+1) + (-1) + (+1) = 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 886996
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> (+8) + (-8) + (+6) + (-9) + (+9) + (-6) = 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
|
||||
}
|
||||
.spoiler {overflow:hidden;}
|
||||
.spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
|
||||
.spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;}
|
||||
.spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}
|
||||
</style>
|
||||
|
@@ -1,49 +1,64 @@
|
||||
<p>You are given a 2D array <code>points</code> of size <code>n x 2</code> representing integer coordinates of some points on a 2D-plane, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>.</p>
|
||||
<p>You are given a 2D array <code>points</code> of size <code>n x 2</code> representing integer coordinates of some points on a 2D plane, where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code>.</p>
|
||||
|
||||
<p>We define the <strong>right</strong> direction as positive x-axis (<strong>increasing x-coordinate</strong>) and the <strong>left</strong> direction as negative x-axis (<strong>decreasing x-coordinate</strong>). Similarly, we define the <strong>up</strong> direction as positive y-axis (<strong>increasing y-coordinate</strong>) and the <strong>down</strong> direction as negative y-axis (<strong>decreasing y-coordinate</strong>)</p>
|
||||
|
||||
<p>You have to place <code>n</code> people, including Alice and Bob, at these points such that there is <strong>exactly one</strong> person at every point. Alice wants to be alone with Bob, so Alice will build a rectangular fence with Alice's position as the <strong>upper left corner</strong> and Bob's position as the <strong>lower right corner</strong> of the fence (<strong>Note</strong> that the fence <strong>might not</strong> enclose any area, i.e. it can be a line). If any person other than Alice and Bob is either <strong>inside</strong> the fence or <strong>on</strong> the fence, Alice will be sad.</p>
|
||||
|
||||
<p>Return <em>the number of <strong>pairs of points</strong> where you can place Alice and Bob, such that Alice <strong>does not</strong> become sad on building the fence</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that Alice can only build a fence with Alice's position as the upper left corner, and Bob's position as the lower right corner. For example, Alice cannot build either of the fences in the picture below with four corners <code>(1, 1)</code>, <code>(1, 3)</code>, <code>(3, 1)</code>, and <code>(3, 3)</code>, because:</p>
|
||||
<p>Count the number of pairs of points <code>(A, B)</code>, where</p>
|
||||
|
||||
<ul>
|
||||
<li>With Alice at <code>(3, 3)</code> and Bob at <code>(1, 1)</code>, Alice's position is not the upper left corner and Bob's position is not the lower right corner of the fence.</li>
|
||||
<li>With Alice at <code>(1, 3)</code> and Bob at <code>(1, 1)</code>, Bob's position is not the lower right corner of the fence.</li>
|
||||
<li><code>A</code> is on the <strong>upper left</strong> side of <code>B</code>, and</li>
|
||||
<li>there are no other points in the rectangle (or line) they make (<strong>including the border</strong>).</li>
|
||||
</ul>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/04/example0alicebob-1.png" style="width: 750px; height: 308px;padding: 10px; background: #fff; border-radius: .5rem;" />
|
||||
|
||||
<p>Return the count.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/01/04/example1alicebob.png" style="width: 376px; height: 308px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[1,1],[2,2],[3,3]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There is no way to place Alice and Bob such that Alice can build a fence with Alice's position as the upper left corner and Bob's position as the lower right corner. Hence we return 0.
|
||||
</pre>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">points = [[1,1],[2,2],[3,3]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2024/01/04/example1alicebob.png" style="width: 427px; height: 350px;" /></p>
|
||||
|
||||
<p>There is no way to choose <code>A</code> and <code>B</code> so <code>A</code> is on the upper left side of <code>B</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/02/04/example2alicebob.png" style="width: 1321px; height: 363px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[6,2],[4,4],[2,6]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> There are two ways to place Alice and Bob such that Alice will not be sad:
|
||||
- Place Alice at (4, 4) and Bob at (6, 2).
|
||||
- Place Alice at (2, 6) and Bob at (4, 4).
|
||||
You cannot place Alice at (2, 6) and Bob at (6, 2) because the person at (4, 4) will be inside the fence.
|
||||
</pre>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">points = [[6,2],[4,4],[2,6]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img height="365" src="https://assets.leetcode.com/uploads/2024/06/25/t2.jpg" width="1321" /></p>
|
||||
|
||||
<ul>
|
||||
<li>The left one is the pair <code>(points[1], points[0])</code>, where <code>points[1]</code> is on the upper left side of <code>points[0]</code> and the rectangle is empty.</li>
|
||||
<li>The middle one is the pair <code>(points[2], points[1])</code>, same as the left one it is a valid pair.</li>
|
||||
<li>The right one is the pair <code>(points[2], points[0])</code>, where <code>points[2]</code> is on the upper left side of <code>points[0]</code>, but <code>points[1]</code> is inside the rectangle so it's not a valid pair.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/02/04/example4alicebob.png" style="width: 1123px; height: 308px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> points = [[3,1],[1,3],[1,1]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> There are two ways to place Alice and Bob such that Alice will not be sad:
|
||||
- Place Alice at (1, 1) and Bob at (3, 1).
|
||||
- Place Alice at (1, 3) and Bob at (1, 1).
|
||||
You cannot place Alice at (1, 3) and Bob at (3, 1) because the person at (1, 1) will be on the fence.
|
||||
Note that it does not matter if the fence encloses any area, the first and second fences in the image are valid.
|
||||
</pre>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">points = [[3,1],[1,3],[1,1]]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p><img src="https://assets.leetcode.com/uploads/2024/06/25/t3.jpg" style="width: 1269px; height: 350px;" /></p>
|
||||
|
||||
<ul>
|
||||
<li>The left one is the pair <code>(points[2], points[0])</code>, where <code>points[2]</code> is on the upper left side of <code>points[0]</code> and there are no other points on the line they form. Note that it is a valid state when the two points form a line.</li>
|
||||
<li>The middle one is the pair <code>(points[1], points[2])</code>, it is a valid pair same as the left one.</li>
|
||||
<li>The right one is the pair <code>(points[1], points[0])</code>, it is not a valid pair as <code>points[2]</code> is on the border of the rectangle.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
@@ -1,46 +1,46 @@
|
||||
<p>You are given an integer array <code>banned</code> and two integers <code>n</code> and <code>maxSum</code>. You are choosing some number of integers following the below rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>The chosen integers have to be in the range <code>[1, n]</code>.</li>
|
||||
<li>Each integer can be chosen <strong>at most once</strong>.</li>
|
||||
<li>The chosen integers should not be in the array <code>banned</code>.</li>
|
||||
<li>The sum of the chosen integers should not exceed <code>maxSum</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of integers you can choose following the mentioned rules</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> banned = [1,6,5], n = 5, maxSum = 6
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> You can choose the integers 2 and 4.
|
||||
2 and 4 are from the range [1, 5], both did not appear in banned, and their sum is 6, which did not exceed maxSum.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> banned = [1,2,3,4,5,6,7], n = 8, maxSum = 1
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> You cannot choose any integer while following the mentioned conditions.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> banned = [11], n = 7, maxSum = 50
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> You can choose the integers 1, 2, 3, 4, 5, 6, and 7.
|
||||
They are from the range [1, 7], all did not appear in banned, and their sum is 28, which did not exceed maxSum.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= banned.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= banned[i], n <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= maxSum <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
||||
<p>You are given an integer array <code>banned</code> and two integers <code>n</code> and <code>maxSum</code>. You are choosing some number of integers following the below rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>The chosen integers have to be in the range <code>[1, n]</code>.</li>
|
||||
<li>Each integer can be chosen <strong>at most once</strong>.</li>
|
||||
<li>The chosen integers should not be in the array <code>banned</code>.</li>
|
||||
<li>The sum of the chosen integers should not exceed <code>maxSum</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of integers you can choose following the mentioned rules</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> banned = [1,6,5], n = 5, maxSum = 6
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> You can choose the integers 2 and 4.
|
||||
2 and 4 are from the range [1, 5], both did not appear in banned, and their sum is 6, which did not exceed maxSum.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> banned = [1,2,3,4,5,6,7], n = 8, maxSum = 1
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> You cannot choose any integer while following the mentioned conditions.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> banned = [11], n = 7, maxSum = 50
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> You can choose the integers 1, 2, 3, 4, 5, 6, and 7.
|
||||
They are from the range [1, 7], all did not appear in banned, and their sum is 28, which did not exceed maxSum.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= banned.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= banned[i], n <= 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= maxSum <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
||||
|
@@ -1,26 +1,26 @@
|
||||
Given two arrays of <strong>unique</strong> digits <code>nums1</code> and <code>nums2</code>, return <em>the <strong>smallest</strong> number that contains <strong>at least</strong> one digit from each array</em>.
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [4,1,3], nums2 = [5,7]
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> The number 15 contains the digit 1 from nums1 and the digit 5 from nums2. It can be proven that 15 is the smallest number we can have.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [3,5,2,6], nums2 = [3,1,7]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The number 3 contains the digit 3 which exists in both arrays.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums1.length, nums2.length <= 9</code></li>
|
||||
<li><code>1 <= nums1[i], nums2[i] <= 9</code></li>
|
||||
<li>All digits in each array are <strong>unique</strong>.</li>
|
||||
</ul>
|
||||
Given two arrays of <strong>unique</strong> digits <code>nums1</code> and <code>nums2</code>, return <em>the <strong>smallest</strong> number that contains <strong>at least</strong> one digit from each array</em>.
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [4,1,3], nums2 = [5,7]
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> The number 15 contains the digit 1 from nums1 and the digit 5 from nums2. It can be proven that 15 is the smallest number we can have.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [3,5,2,6], nums2 = [3,1,7]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The number 3 contains the digit 3 which exists in both arrays.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums1.length, nums2.length <= 9</code></li>
|
||||
<li><code>1 <= nums1[i], nums2[i] <= 9</code></li>
|
||||
<li>All digits in each array are <strong>unique</strong>.</li>
|
||||
</ul>
|
||||
|
@@ -8,21 +8,21 @@
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2019/04/08/recover-a-tree-from-preorder-traversal.png" style="width: 320px; height: 200px;" />
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/09/10/recover_tree_ex1.png" style="width: 423px; height: 200px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> traversal = "1-2--3--4-5--6--7"
|
||||
<strong>Output:</strong> [1,2,5,3,4,6,7]
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2019/04/11/screen-shot-2019-04-10-at-114101-pm.png" style="width: 256px; height: 250px;" />
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/09/10/recover_tree_ex2.png" style="width: 432px; height: 250px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> traversal = "1-2--3---4-5--6---7"
|
||||
<strong>Output:</strong> [1,2,5,3,null,6,null,4,null,7]
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2019/04/11/screen-shot-2019-04-10-at-114955-pm.png" style="width: 276px; height: 250px;" />
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2024/09/10/recover_tree_ex3.png" style="width: 305px; height: 250px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> traversal = "1-401--349---90--88"
|
||||
<strong>Output:</strong> [1,401,null,349,88,90]
|
||||
|
@@ -1,46 +1,46 @@
|
||||
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p>
|
||||
|
||||
<p>In one operation, you can:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose a star in <code>s</code>.</li>
|
||||
<li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the string after <strong>all</strong> stars have been removed</em>.</p>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The input will be generated such that the operation is always possible.</li>
|
||||
<li>It can be shown that the resulting string will always be unique.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "leet**cod*e"
|
||||
<strong>Output:</strong> "lecoe"
|
||||
<strong>Explanation:</strong> Performing the removals from left to right:
|
||||
- The closest character to the 1<sup>st</sup> star is 't' in "lee<strong><u>t</u></strong>**cod*e". s becomes "lee*cod*e".
|
||||
- The closest character to the 2<sup>nd</sup> star is 'e' in "le<strong><u>e</u></strong>*cod*e". s becomes "lecod*e".
|
||||
- The closest character to the 3<sup>rd</sup> star is 'd' in "leco<strong><u>d</u></strong>*e". s becomes "lecoe".
|
||||
There are no more stars, so we return "lecoe".</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "erase*****"
|
||||
<strong>Output:</strong> ""
|
||||
<strong>Explanation:</strong> The entire string is removed, so we return an empty string.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists of lowercase English letters and stars <code>*</code>.</li>
|
||||
<li>The operation above can be performed on <code>s</code>.</li>
|
||||
</ul>
|
||||
<p>You are given a string <code>s</code>, which contains stars <code>*</code>.</p>
|
||||
|
||||
<p>In one operation, you can:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose a star in <code>s</code>.</li>
|
||||
<li>Remove the closest <strong>non-star</strong> character to its <strong>left</strong>, as well as remove the star itself.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the string after <strong>all</strong> stars have been removed</em>.</p>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The input will be generated such that the operation is always possible.</li>
|
||||
<li>It can be shown that the resulting string will always be unique.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "leet**cod*e"
|
||||
<strong>Output:</strong> "lecoe"
|
||||
<strong>Explanation:</strong> Performing the removals from left to right:
|
||||
- The closest character to the 1<sup>st</sup> star is 't' in "lee<strong><u>t</u></strong>**cod*e". s becomes "lee*cod*e".
|
||||
- The closest character to the 2<sup>nd</sup> star is 'e' in "le<strong><u>e</u></strong>*cod*e". s becomes "lecod*e".
|
||||
- The closest character to the 3<sup>rd</sup> star is 'd' in "leco<strong><u>d</u></strong>*e". s becomes "lecoe".
|
||||
There are no more stars, so we return "lecoe".</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "erase*****"
|
||||
<strong>Output:</strong> ""
|
||||
<strong>Explanation:</strong> The entire string is removed, so we return an empty string.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists of lowercase English letters and stars <code>*</code>.</li>
|
||||
<li>The operation above can be performed on <code>s</code>.</li>
|
||||
</ul>
|
||||
|
@@ -1,44 +1,44 @@
|
||||
<p>You are given an integer array <code>gifts</code> denoting the number of gifts in various piles. Every second, you do the following:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose the pile with the maximum number of gifts.</li>
|
||||
<li>If there is more than one pile with the maximum number of gifts, choose any.</li>
|
||||
<li>Leave behind the floor of the square root of the number of gifts in the pile. Take the rest of the gifts.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of gifts remaining after </em><code>k</code><em> seconds.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> gifts = [25,64,9,4,100], k = 4
|
||||
<strong>Output:</strong> 29
|
||||
<strong>Explanation:</strong>
|
||||
The gifts are taken in the following way:
|
||||
- In the first second, the last pile is chosen and 10 gifts are left behind.
|
||||
- Then the second pile is chosen and 8 gifts are left behind.
|
||||
- After that the first pile is chosen and 5 gifts are left behind.
|
||||
- Finally, the last pile is chosen again and 3 gifts are left behind.
|
||||
The final remaining gifts are [5,8,9,4,3], so the total number of gifts remaining is 29.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> gifts = [1,1,1,1], k = 4
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
In this case, regardless which pile you choose, you have to leave behind 1 gift in each pile.
|
||||
That is, you can't take any pile with you.
|
||||
So, the total gifts remaining are 4.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= gifts.length <= 10<sup>3</sup></code></li>
|
||||
<li><code>1 <= gifts[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>3</sup></code></li>
|
||||
</ul>
|
||||
<p>You are given an integer array <code>gifts</code> denoting the number of gifts in various piles. Every second, you do the following:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose the pile with the maximum number of gifts.</li>
|
||||
<li>If there is more than one pile with the maximum number of gifts, choose any.</li>
|
||||
<li>Reduce the number of gifts in the pile to the floor of the square root of the original number of gifts in the pile.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of gifts remaining after </em><code>k</code><em> seconds.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> gifts = [25,64,9,4,100], k = 4
|
||||
<strong>Output:</strong> 29
|
||||
<strong>Explanation:</strong>
|
||||
The gifts are taken in the following way:
|
||||
- In the first second, the last pile is chosen and 10 gifts are left behind.
|
||||
- Then the second pile is chosen and 8 gifts are left behind.
|
||||
- After that the first pile is chosen and 5 gifts are left behind.
|
||||
- Finally, the last pile is chosen again and 3 gifts are left behind.
|
||||
The final remaining gifts are [5,8,9,4,3], so the total number of gifts remaining is 29.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> gifts = [1,1,1,1], k = 4
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong>
|
||||
In this case, regardless which pile you choose, you have to leave behind 1 gift in each pile.
|
||||
That is, you can't take any pile with you.
|
||||
So, the total gifts remaining are 4.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= gifts.length <= 10<sup>3</sup></code></li>
|
||||
<li><code>1 <= gifts[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= k <= 10<sup>3</sup></code></li>
|
||||
</ul>
|
||||
|
@@ -1,33 +1,33 @@
|
||||
<p>You are given the <code>head</code> of a linked list.</p>
|
||||
|
||||
<p>Remove every node which has a node with a greater value anywhere to the right side of it.</p>
|
||||
|
||||
<p>Return <em>the </em><code>head</code><em> of the modified linked list.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/02/drawio.png" style="width: 631px; height: 51px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> head = [5,2,13,3,8]
|
||||
<strong>Output:</strong> [13,8]
|
||||
<strong>Explanation:</strong> The nodes that should be removed are 5, 2 and 3.
|
||||
- Node 13 is to the right of node 5.
|
||||
- Node 13 is to the right of node 2.
|
||||
- Node 8 is to the right of node 3.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> head = [1,1,1,1]
|
||||
<strong>Output:</strong> [1,1,1,1]
|
||||
<strong>Explanation:</strong> Every node has value 1, so no nodes are removed.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of the nodes in the given list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
|
||||
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
<p>You are given the <code>head</code> of a linked list.</p>
|
||||
|
||||
<p>Remove every node which has a node with a greater value anywhere to the right side of it.</p>
|
||||
|
||||
<p>Return <em>the </em><code>head</code><em> of the modified linked list.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/02/drawio.png" style="width: 631px; height: 51px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> head = [5,2,13,3,8]
|
||||
<strong>Output:</strong> [13,8]
|
||||
<strong>Explanation:</strong> The nodes that should be removed are 5, 2 and 3.
|
||||
- Node 13 is to the right of node 5.
|
||||
- Node 13 is to the right of node 2.
|
||||
- Node 8 is to the right of node 3.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> head = [1,1,1,1]
|
||||
<strong>Output:</strong> [1,1,1,1]
|
||||
<strong>Explanation:</strong> Every node has value 1, so no nodes are removed.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of the nodes in the given list is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
|
||||
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
|
@@ -1,50 +1,105 @@
|
||||
<p>You have an initial <strong>power</strong> of <code>power</code>, an initial <strong>score</strong> of <code>0</code>, and a bag of <code>tokens</code> where <code>tokens[i]</code> is the value of the <code>i<sup>th</sup></code> token (0-indexed).</p>
|
||||
<p>You start with an initial <strong>power</strong> of <code>power</code>, an initial <strong>score</strong> of <code>0</code>, and a bag of tokens given as an integer array <code>tokens</code>, where each <code>tokens[i]</code> denotes the value of token<em><sub>i</sub></em>.</p>
|
||||
|
||||
<p>Your goal is to maximize your total <strong>score</strong> by potentially playing each token in one of two ways:</p>
|
||||
<p>Your goal is to <strong>maximize</strong> the total <strong>score</strong> by strategically playing these tokens. In one move, you can play an <strong>unplayed</strong> token in one of the two ways (but not both for the same token):</p>
|
||||
|
||||
<ul>
|
||||
<li>If your current <strong>power</strong> is at least <code>tokens[i]</code>, you may play the <code>i<sup>th</sup></code> token face up, losing <code>tokens[i]</code> <strong>power</strong> and gaining <code>1</code> <strong>score</strong>.</li>
|
||||
<li>If your current <strong>score</strong> is at least <code>1</code>, you may play the <code>i<sup>th</sup></code> token face down, gaining <code>tokens[i]</code> <strong>power</strong> and losing <code>1</code> <strong>score</strong>.</li>
|
||||
<li><strong>Face-up</strong>: If your current power is <strong>at least</strong> <code>tokens[i]</code>, you may play token<em><sub>i</sub></em>, losing <code>tokens[i]</code> power and gaining <code>1</code> score.</li>
|
||||
<li><strong>Face-down</strong>: If your current score is <strong>at least</strong> <code>1</code>, you may play token<em><sub>i</sub></em>, gaining <code>tokens[i]</code> power and losing <code>1</code> score.</li>
|
||||
</ul>
|
||||
|
||||
<p>Each token may be played <strong>at most</strong> once and <strong>in any order</strong>. You do <strong>not</strong> have to play all the tokens.</p>
|
||||
|
||||
<p>Return <em>the largest possible <strong>score</strong> you can achieve after playing any number of tokens</em>.</p>
|
||||
<p>Return <em>the <strong>maximum</strong> possible score you can achieve after playing <strong>any</strong> number of tokens</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tokens = [100], power = 50
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation</strong><strong>:</strong> Playing the only token in the bag is impossible because you either have too little power or too little score.
|
||||
</pre>
|
||||
<div class="example-block" style="
|
||||
border-color: var(--border-tertiary);
|
||||
border-left-width: 2px;
|
||||
color: var(--text-secondary);
|
||||
font-size: .875rem;
|
||||
margin-bottom: 1rem;
|
||||
margin-top: 1rem;
|
||||
overflow: visible;
|
||||
padding-left: 1rem;
|
||||
">
|
||||
<p><strong>Input:</strong> <span class="example-io" style="
|
||||
font-family: Menlo,sans-serif;
|
||||
font-size: 0.85rem;
|
||||
">tokens = [100], power = 50</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io" style="
|
||||
font-family: Menlo,sans-serif;
|
||||
font-size: 0.85rem;
|
||||
">0</span></p>
|
||||
|
||||
<p><strong>Explanation</strong><strong>:</strong> Since your score is <code>0</code> initially, you cannot play the token face-down. You also cannot play it face-up since your power (<code>50</code>) is less than <code>tokens[0]</code> (<code>100</code>).</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tokens = [100,200], power = 150
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> Play the 0<sup>th</sup> token (100) face up, your power becomes 50 and score becomes 1.
|
||||
There is no need to play the 1<sup>st</sup> token since you cannot play it face up to add to your score.
|
||||
</pre>
|
||||
<div class="example-block" style="
|
||||
border-color: var(--border-tertiary);
|
||||
border-left-width: 2px;
|
||||
color: var(--text-secondary);
|
||||
font-size: .875rem;
|
||||
margin-bottom: 1rem;
|
||||
margin-top: 1rem;
|
||||
overflow: visible;
|
||||
padding-left: 1rem;
|
||||
">
|
||||
<p><strong>Input:</strong> <span class="example-io" style="
|
||||
font-family: Menlo,sans-serif;
|
||||
font-size: 0.85rem;
|
||||
">tokens = [200,100], power = 150</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io" style="
|
||||
font-family: Menlo,sans-serif;
|
||||
font-size: 0.85rem;
|
||||
">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong> Play token<em><sub>1</sub></em> (<code>100</code>) face-up, reducing your power to <code>50</code> and increasing your score to <code>1</code>.</p>
|
||||
|
||||
<p>There is no need to play token<em><sub>0</sub></em>, since you cannot play it face-up to add to your score. The maximum score achievable is <code>1</code>.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tokens = [100,200,300,400], power = 200
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Play the tokens in this order to get a score of 2:
|
||||
1. Play the 0<sup>th</sup> token (100) face up, your power becomes 100 and score becomes 1.
|
||||
2. Play the 3<sup>rd</sup> token (400) face down, your power becomes 500 and score becomes 0.
|
||||
3. Play the 1<sup>st</sup> token (200) face up, your power becomes 300 and score becomes 1.
|
||||
4. Play the 2<sup>nd </sup>token (300) face up, your power becomes 0 and score becomes 2.
|
||||
</pre>
|
||||
<div class="example-block" style="
|
||||
border-color: var(--border-tertiary);
|
||||
border-left-width: 2px;
|
||||
color: var(--text-secondary);
|
||||
font-size: .875rem;
|
||||
margin-bottom: 1rem;
|
||||
margin-top: 1rem;
|
||||
overflow: visible;
|
||||
padding-left: 1rem;
|
||||
">
|
||||
<p><strong>Input:</strong> <span class="example-io" style="
|
||||
font-family: Menlo,sans-serif;
|
||||
font-size: 0.85rem;
|
||||
">tokens = [100,200,300,400], power = 200</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io" style="
|
||||
font-family: Menlo,sans-serif;
|
||||
font-size: 0.85rem;
|
||||
">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong> Play the tokens in this order to get a score of <code>2</code>:</p>
|
||||
|
||||
<ol>
|
||||
<li>Play token<em><sub>0</sub></em> (<code>100</code>) face-up, reducing power to <code>100</code> and increasing score to <code>1</code>.</li>
|
||||
<li>Play token<em><sub>3</sub></em> (<code>400</code>) face-down, increasing power to <code>500</code> and reducing score to <code>0</code>.</li>
|
||||
<li>Play token<em><sub>1</sub></em> (<code>200</code>) face-up, reducing power to <code>300</code> and increasing score to <code>1</code>.</li>
|
||||
<li>Play token<em><sub>2</sub></em> (<code>300</code>) face-up, reducing power to <code>0</code> and increasing score to <code>2</code>.</li>
|
||||
</ol>
|
||||
|
||||
<p><span style="color: var(--text-secondary); font-size: 0.875rem;">The maximum score achievable is </span><code style="color: var(--text-secondary); font-size: 0.875rem;">2</code><span style="color: var(--text-secondary); font-size: 0.875rem;">.</span></p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= tokens.length <= 1000</code></li>
|
||||
<li><code>0 <= tokens[i], power < 10<sup>4</sup></code></li>
|
||||
<li><code>0 <= tokens[i], power < 10<sup>4</sup></code></li>
|
||||
</ul>
|
||||
|
@@ -1,40 +1,217 @@
|
||||
<p>You are given an integer <code>k</code> and an integer <code>x</code>.</p>
|
||||
<p>You are given an integer <code>k</code> and an integer <code>x</code>. The price of a number <code>num</code> is calculated by the count of <span data-keyword="set-bit">set bits</span> at positions <code>x</code>, <code>2x</code>, <code>3x</code>, etc., in its binary representation, starting from the least significant bit. The following table contains examples of how price is calculated.</p>
|
||||
|
||||
<p>Consider <code>s</code> is the <strong>1-indexed </strong>binary representation of an integer <code>num</code>. The <strong>price</strong> of a number <code>num</code> is the number of <code>i</code>'s such that <code>i % x == 0</code> and <code><font face="monospace">s[i]</font></code> is a <strong>set bit</strong>.</p>
|
||||
<table border="1">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th>x</th>
|
||||
<th>num</th>
|
||||
<th>Binary Representation</th>
|
||||
<th>Price</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>1</td>
|
||||
<td>13</td>
|
||||
<td><u>0</u><u>0</u><u>0</u><u>0</u><u>0</u><strong><u>1</u></strong><strong><u>1</u></strong><u>0</u><strong><u>1</u></strong></td>
|
||||
<td>3</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>2</td>
|
||||
<td>13</td>
|
||||
<td>0<u>0</u>0<u>0</u>0<strong><u>1</u></strong>1<u>0</u>1</td>
|
||||
<td>1</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>2</td>
|
||||
<td>233</td>
|
||||
<td>0<strong><u>1</u></strong>1<strong><u>1</u></strong>0<strong><u>1</u></strong>0<u>0</u>1</td>
|
||||
<td>3</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>3</td>
|
||||
<td>13</td>
|
||||
<td><u>0</u>00<u>0</u>01<strong><u>1</u></strong>01</td>
|
||||
<td>1</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>3</td>
|
||||
<td>362</td>
|
||||
<td><strong><u>1</u></strong>01<strong><u>1</u></strong>01<u>0</u>10</td>
|
||||
<td>2</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<p>Return <em>the <b>greatest</b> integer </em><code>num</code><em> such that the sum of <strong>prices</strong> of all numbers from </em><code>1</code><em> to </em><code>num</code><em> is less than or equal to </em><code>k</code><em>.</em></p>
|
||||
<p>The <strong>accumulated price</strong> of <code>num</code> is the <b>total</b> price of numbers from <code>1</code> to <code>num</code>. <code>num</code> is considered <strong>cheap</strong> if its accumulated price is less than or equal to <code>k</code>.</p>
|
||||
|
||||
<p><strong>Note</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>In the binary representation of a number <strong>set bit</strong> is a bit of value <code>1</code>.</li>
|
||||
<li>The binary representation of a number will be indexed from right to left. For example, if <code>s == 11100</code>, <code>s[4] == 1</code> and <code>s[2] == 0</code>.</li>
|
||||
</ul>
|
||||
<p>Return the <b>greatest</b> cheap number.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> k = 9, x = 1
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> The numbers 1, 2, 3, 4, 5, and 6 can be written in binary representation as "1", "10", "11", "100", "101", and "110" respectively.
|
||||
Since x is equal to 1, the price of each number is the number of its set bits.
|
||||
The number of set bits in these numbers is 9. So the sum of the prices of the first 6 numbers is 9.
|
||||
So the answer is 6.</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">k = 9, x = 1</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">6</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>As shown in the table below, <code>6</code> is the greatest cheap number.</p>
|
||||
|
||||
<table border="1">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th>x</th>
|
||||
<th>num</th>
|
||||
<th>Binary Representation</th>
|
||||
<th>Price</th>
|
||||
<th>Accumulated Price</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>1</td>
|
||||
<td>1</td>
|
||||
<td><u>0</u><u>0</u><strong><u>1</u></strong></td>
|
||||
<td>1</td>
|
||||
<td>1</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>1</td>
|
||||
<td>2</td>
|
||||
<td><u>0</u><strong><u>1</u></strong><u>0</u></td>
|
||||
<td>1</td>
|
||||
<td>2</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>1</td>
|
||||
<td>3</td>
|
||||
<td><u>0</u><strong><u>1</u></strong><strong><u>1</u></strong></td>
|
||||
<td>2</td>
|
||||
<td>4</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>1</td>
|
||||
<td>4</td>
|
||||
<td><strong><u>1</u></strong><u>0</u><u>0</u></td>
|
||||
<td>1</td>
|
||||
<td>5</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>1</td>
|
||||
<td>5</td>
|
||||
<td><strong><u>1</u></strong><u>0</u><strong><u>1</u></strong></td>
|
||||
<td>2</td>
|
||||
<td>7</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>1</td>
|
||||
<td>6</td>
|
||||
<td><strong><u>1</u></strong><strong><u>1</u></strong><u>0</u></td>
|
||||
<td>2</td>
|
||||
<td>9</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>1</td>
|
||||
<td>7</td>
|
||||
<td><strong><u>1</u></strong><strong><u>1</u></strong><strong><u>1</u></strong></td>
|
||||
<td>3</td>
|
||||
<td>12</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> k = 7, x = 2
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> Since x is equal to 2, we should just check even<sup>th</sup> bits.
|
||||
The second bit of binary representation of numbers 2 and 3 is a set bit. So the sum of their prices is 2.
|
||||
The second bit of binary representation of numbers 6 and 7 is a set bit. So the sum of their prices is 2.
|
||||
The fourth bit of binary representation of numbers 8 and 9 is a set bit but their second bit is not. So the sum of their prices is 2.
|
||||
Numbers 1, 4, and 5 don't have set bits in their even<sup>th</sup> bits in their binary representation. So the sum of their prices is 0.
|
||||
The second and the fourth bit of the binary representation of the number 10 are a set bit. So its price is 2.
|
||||
The sum of the prices of the first 9 numbers is 6.
|
||||
Because the sum of the prices of the first 10 numbers is 8, the answer is 9.</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">k = 7, x = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">9</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>As shown in the table below, <code>9</code> is the greatest cheap number.</p>
|
||||
|
||||
<table border="1">
|
||||
<tbody>
|
||||
<tr>
|
||||
<th>x</th>
|
||||
<th>num</th>
|
||||
<th>Binary Representation</th>
|
||||
<th>Price</th>
|
||||
<th>Accumulated Price</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>2</td>
|
||||
<td>1</td>
|
||||
<td><u>0</u>0<u>0</u>1</td>
|
||||
<td>0</td>
|
||||
<td>0</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>2</td>
|
||||
<td>2</td>
|
||||
<td><u>0</u>0<strong><u>1</u></strong>0</td>
|
||||
<td>1</td>
|
||||
<td>1</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>2</td>
|
||||
<td>3</td>
|
||||
<td><u>0</u>0<strong><u>1</u></strong>1</td>
|
||||
<td>1</td>
|
||||
<td>2</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>2</td>
|
||||
<td>4</td>
|
||||
<td><u>0</u>1<u>0</u>0</td>
|
||||
<td>0</td>
|
||||
<td>2</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>2</td>
|
||||
<td>5</td>
|
||||
<td><u>0</u>1<u>0</u>1</td>
|
||||
<td>0</td>
|
||||
<td>2</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>2</td>
|
||||
<td>6</td>
|
||||
<td><u>0</u>1<strong><u>1</u></strong>0</td>
|
||||
<td>1</td>
|
||||
<td>3</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>2</td>
|
||||
<td>7</td>
|
||||
<td><u>0</u>1<strong><u>1</u></strong>1</td>
|
||||
<td>1</td>
|
||||
<td>4</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>2</td>
|
||||
<td>8</td>
|
||||
<td><strong><u>1</u></strong>0<u>0</u>0</td>
|
||||
<td>1</td>
|
||||
<td>5</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>2</td>
|
||||
<td>9</td>
|
||||
<td><strong><u>1</u></strong>0<u>0</u>1</td>
|
||||
<td>1</td>
|
||||
<td>6</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>2</td>
|
||||
<td>10</td>
|
||||
<td><strong><u>1</u></strong>0<strong><u>1</u></strong>0</td>
|
||||
<td>2</td>
|
||||
<td>8</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
@@ -1,57 +1,57 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array of positive integers <code>tasks</code>, representing tasks that need to be completed <strong>in order</strong>, where <code>tasks[i]</code> represents the <strong>type</strong> of the <code>i<sup>th</sup></code> task.</p>
|
||||
|
||||
<p>You are also given a positive integer <code>space</code>, which represents the <strong>minimum</strong> number of days that must pass <strong>after</strong> the completion of a task before another task of the <strong>same</strong> type can be performed.</p>
|
||||
|
||||
<p>Each day, until all tasks have been completed, you must either:</p>
|
||||
|
||||
<ul>
|
||||
<li>Complete the next task from <code>tasks</code>, or</li>
|
||||
<li>Take a break.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> the <strong>minimum</strong> number of days needed to complete all tasks</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = [1,2,1,2,3,1], space = 3
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong>
|
||||
One way to complete all tasks in 9 days is as follows:
|
||||
Day 1: Complete the 0th task.
|
||||
Day 2: Complete the 1st task.
|
||||
Day 3: Take a break.
|
||||
Day 4: Take a break.
|
||||
Day 5: Complete the 2nd task.
|
||||
Day 6: Complete the 3rd task.
|
||||
Day 7: Take a break.
|
||||
Day 8: Complete the 4th task.
|
||||
Day 9: Complete the 5th task.
|
||||
It can be shown that the tasks cannot be completed in less than 9 days.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = [5,8,8,5], space = 2
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong>
|
||||
One way to complete all tasks in 6 days is as follows:
|
||||
Day 1: Complete the 0th task.
|
||||
Day 2: Complete the 1st task.
|
||||
Day 3: Take a break.
|
||||
Day 4: Take a break.
|
||||
Day 5: Complete the 2nd task.
|
||||
Day 6: Complete the 3rd task.
|
||||
It can be shown that the tasks cannot be completed in less than 6 days.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= tasks.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= tasks[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= space <= tasks.length</code></li>
|
||||
</ul>
|
||||
<p>You are given a <strong>0-indexed</strong> array of positive integers <code>tasks</code>, representing tasks that need to be completed <strong>in order</strong>, where <code>tasks[i]</code> represents the <strong>type</strong> of the <code>i<sup>th</sup></code> task.</p>
|
||||
|
||||
<p>You are also given a positive integer <code>space</code>, which represents the <strong>minimum</strong> number of days that must pass <strong>after</strong> the completion of a task before another task of the <strong>same</strong> type can be performed.</p>
|
||||
|
||||
<p>Each day, until all tasks have been completed, you must either:</p>
|
||||
|
||||
<ul>
|
||||
<li>Complete the next task from <code>tasks</code>, or</li>
|
||||
<li>Take a break.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> the <strong>minimum</strong> number of days needed to complete all tasks</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = [1,2,1,2,3,1], space = 3
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong>
|
||||
One way to complete all tasks in 9 days is as follows:
|
||||
Day 1: Complete the 0th task.
|
||||
Day 2: Complete the 1st task.
|
||||
Day 3: Take a break.
|
||||
Day 4: Take a break.
|
||||
Day 5: Complete the 2nd task.
|
||||
Day 6: Complete the 3rd task.
|
||||
Day 7: Take a break.
|
||||
Day 8: Complete the 4th task.
|
||||
Day 9: Complete the 5th task.
|
||||
It can be shown that the tasks cannot be completed in less than 9 days.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = [5,8,8,5], space = 2
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong>
|
||||
One way to complete all tasks in 6 days is as follows:
|
||||
Day 1: Complete the 0th task.
|
||||
Day 2: Complete the 1st task.
|
||||
Day 3: Take a break.
|
||||
Day 4: Take a break.
|
||||
Day 5: Complete the 2nd task.
|
||||
Day 6: Complete the 3rd task.
|
||||
It can be shown that the tasks cannot be completed in less than 6 days.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= tasks.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= tasks[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= space <= tasks.length</code></li>
|
||||
</ul>
|
||||
|
@@ -1,48 +1,94 @@
|
||||
<p>Given a characters array <code>tasks</code>, representing the tasks a CPU needs to do, where each letter represents a different task. Tasks could be done in any order. Each task is done in one unit of time. For each unit of time, the CPU could complete either one task or just be idle.</p>
|
||||
<p>You are given an array of CPU <code>tasks</code>, each labeled with a letter from A to Z, and a number <code>n</code>. Each CPU interval can be idle or allow the completion of one task. Tasks can be completed in any order, but there's a constraint: there has to be a gap of <strong>at least</strong> <code>n</code> intervals between two tasks with the same label.</p>
|
||||
|
||||
<p>However, there is a non-negative integer <code>n</code> that represents the cooldown period between two <b>same tasks</b> (the same letter in the array), that is that there must be at least <code>n</code> units of time between any two same tasks.</p>
|
||||
|
||||
<p>Return <em>the least number of units of times that the CPU will take to finish all the given tasks</em>.</p>
|
||||
<p>Return the <strong>minimum</strong> number of CPU intervals required to complete all tasks.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = ["A","A","A","B","B","B"], n = 2
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong>
|
||||
A -> B -> idle -> A -> B -> idle -> A -> B
|
||||
There is at least 2 units of time between any two same tasks.
|
||||
</pre>
|
||||
<div class="example-block" style="
|
||||
border-color: var(--border-tertiary);
|
||||
border-left-width: 2px;
|
||||
color: var(--text-secondary);
|
||||
font-size: .875rem;
|
||||
margin-bottom: 1rem;
|
||||
margin-top: 1rem;
|
||||
overflow: visible;
|
||||
padding-left: 1rem;
|
||||
">
|
||||
<p><strong>Input:</strong> <span class="example-io" style="
|
||||
font-family: Menlo,sans-serif;
|
||||
font-size: 0.85rem;
|
||||
">tasks = ["A","A","A","B","B","B"], n = 2</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io" style="
|
||||
font-family: Menlo,sans-serif;
|
||||
font-size: 0.85rem;
|
||||
">8</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong> A possible sequence is: A -> B -> idle -> A -> B -> idle -> A -> B.</p>
|
||||
|
||||
<p>After completing task A, you must wait two intervals before doing A again. The same applies to task B. In the 3<sup>rd</sup> interval, neither A nor B can be done, so you idle. By the 4<sup>th</sup> interval, you can do A again as 2 intervals have passed.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = ["A","A","A","B","B","B"], n = 0
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> On this case any permutation of size 6 would work since n = 0.
|
||||
["A","A","A","B","B","B"]
|
||||
["A","B","A","B","A","B"]
|
||||
["B","B","B","A","A","A"]
|
||||
...
|
||||
And so on.
|
||||
</pre>
|
||||
<div class="example-block" style="
|
||||
border-color: var(--border-tertiary);
|
||||
border-left-width: 2px;
|
||||
color: var(--text-secondary);
|
||||
font-size: .875rem;
|
||||
margin-bottom: 1rem;
|
||||
margin-top: 1rem;
|
||||
overflow: visible;
|
||||
padding-left: 1rem;
|
||||
">
|
||||
<p><strong>Input:</strong> <span class="example-io" style="
|
||||
font-family: Menlo,sans-serif;
|
||||
font-size: 0.85rem;
|
||||
">tasks = ["A","C","A","B","D","B"], n = 1</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io" style="
|
||||
font-family: Menlo,sans-serif;
|
||||
font-size: 0.85rem;
|
||||
">6</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong> A possible sequence is: A -> B -> C -> D -> A -> B.</p>
|
||||
|
||||
<p>With a cooling interval of 1, you can repeat a task after just one other task.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> tasks = ["A","A","A","A","A","A","B","C","D","E","F","G"], n = 2
|
||||
<strong>Output:</strong> 16
|
||||
<strong>Explanation:</strong>
|
||||
One possible solution is
|
||||
A -> B -> C -> A -> D -> E -> A -> F -> G -> A -> idle -> idle -> A -> idle -> idle -> A
|
||||
</pre>
|
||||
<div class="example-block" style="
|
||||
border-color: var(--border-tertiary);
|
||||
border-left-width: 2px;
|
||||
color: var(--text-secondary);
|
||||
font-size: .875rem;
|
||||
margin-bottom: 1rem;
|
||||
margin-top: 1rem;
|
||||
overflow: visible;
|
||||
padding-left: 1rem;
|
||||
">
|
||||
<p><strong>Input:</strong> <span class="example-io" style="
|
||||
font-family: Menlo,sans-serif;
|
||||
font-size: 0.85rem;
|
||||
">tasks = ["A","A","A", "B","B","B"], n = 3</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io" style="
|
||||
font-family: Menlo,sans-serif;
|
||||
font-size: 0.85rem;
|
||||
">10</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong> A possible sequence is: A -> B -> idle -> idle -> A -> B -> idle -> idle -> A -> B.</p>
|
||||
|
||||
<p>There are only two types of tasks, A and B, which need to be separated by 3 intervals. This leads to idling twice between repetitions of these tasks.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= task.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>tasks[i]</code> is upper-case English letter.</li>
|
||||
<li>The integer <code>n</code> is in the range <code>[0, 100]</code>.</li>
|
||||
<li><code>1 <= tasks.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>tasks[i]</code> is an uppercase English letter.</li>
|
||||
<li><code>0 <= n <= 100</code></li>
|
||||
</ul>
|
||||
|
@@ -1,43 +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 class="example">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 class="example">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>
|
||||
<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 class="example">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 class="example">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>
|
||||
|
@@ -1,58 +1,58 @@
|
||||
<p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
|
||||
|
||||
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
|
||||
|
||||
<p>Meetings are allocated to rooms in the following manner:</p>
|
||||
|
||||
<ol>
|
||||
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
|
||||
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
|
||||
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
|
||||
</ol>
|
||||
|
||||
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
|
||||
|
||||
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
- At time 0, both rooms are not being used. The first meeting starts in room 0.
|
||||
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
|
||||
- At time 2, both rooms are being used. The third meeting is delayed.
|
||||
- At time 3, both rooms are being used. The fourth meeting is delayed.
|
||||
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
|
||||
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
|
||||
Both rooms 0 and 1 held 2 meetings, so we return 0.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
|
||||
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
|
||||
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
|
||||
- At time 4, all three rooms are being used. The fourth meeting is delayed.
|
||||
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
|
||||
- At time 6, all three rooms are being used. The fifth meeting is delayed.
|
||||
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
|
||||
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>meetings[i].length == 2</code></li>
|
||||
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
|
||||
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
|
||||
</ul>
|
||||
<p>You are given an integer <code>n</code>. There are <code>n</code> rooms numbered from <code>0</code> to <code>n - 1</code>.</p>
|
||||
|
||||
<p>You are given a 2D integer array <code>meetings</code> where <code>meetings[i] = [start<sub>i</sub>, end<sub>i</sub>]</code> means that a meeting will be held during the <strong>half-closed</strong> time interval <code>[start<sub>i</sub>, end<sub>i</sub>)</code>. All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</p>
|
||||
|
||||
<p>Meetings are allocated to rooms in the following manner:</p>
|
||||
|
||||
<ol>
|
||||
<li>Each meeting will take place in the unused room with the <strong>lowest</strong> number.</li>
|
||||
<li>If there are no available rooms, the meeting will be delayed until a room becomes free. The delayed meeting should have the <strong>same</strong> duration as the original meeting.</li>
|
||||
<li>When a room becomes unused, meetings that have an earlier original <strong>start</strong> time should be given the room.</li>
|
||||
</ol>
|
||||
|
||||
<p>Return<em> the <strong>number</strong> of the room that held the most meetings. </em>If there are multiple rooms, return<em> the room with the <strong>lowest</strong> number.</em></p>
|
||||
|
||||
<p>A <strong>half-closed interval</strong> <code>[a, b)</code> is the interval between <code>a</code> and <code>b</code> <strong>including</strong> <code>a</code> and <strong>not including</strong> <code>b</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong>
|
||||
- At time 0, both rooms are not being used. The first meeting starts in room 0.
|
||||
- At time 1, only room 1 is not being used. The second meeting starts in room 1.
|
||||
- At time 2, both rooms are being used. The third meeting is delayed.
|
||||
- At time 3, both rooms are being used. The fourth meeting is delayed.
|
||||
- At time 5, the meeting in room 1 finishes. The third meeting starts in room 1 for the time period [5,10).
|
||||
- At time 10, the meetings in both rooms finish. The fourth meeting starts in room 0 for the time period [10,11).
|
||||
Both rooms 0 and 1 held 2 meetings, so we return 0.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, meetings = [[1,20],[2,10],[3,5],[4,9],[6,8]]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
- At time 1, all three rooms are not being used. The first meeting starts in room 0.
|
||||
- At time 2, rooms 1 and 2 are not being used. The second meeting starts in room 1.
|
||||
- At time 3, only room 2 is not being used. The third meeting starts in room 2.
|
||||
- At time 4, all three rooms are being used. The fourth meeting is delayed.
|
||||
- At time 5, the meeting in room 2 finishes. The fourth meeting starts in room 2 for the time period [5,10).
|
||||
- At time 6, all three rooms are being used. The fifth meeting is delayed.
|
||||
- At time 10, the meetings in rooms 1 and 2 finish. The fifth meeting starts in room 1 for the time period [10,12).
|
||||
Room 0 held 1 meeting while rooms 1 and 2 each held 2 meetings, so we return 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li><code>1 <= meetings.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>meetings[i].length == 2</code></li>
|
||||
<li><code>0 <= start<sub>i</sub> < end<sub>i</sub> <= 5 * 10<sup>5</sup></code></li>
|
||||
<li>All the values of <code>start<sub>i</sub></code> are <strong>unique</strong>.</li>
|
||||
</ul>
|
||||
|
@@ -1,42 +1,47 @@
|
||||
<p>Write a function that takes the binary representation of an unsigned integer and returns the number of '1' bits it has (also known as the <a href="http://en.wikipedia.org/wiki/Hamming_weight" target="_blank">Hamming weight</a>).</p>
|
||||
|
||||
<p><strong>Note:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type. It should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.</li>
|
||||
<li>In Java, the compiler represents the signed integers using <a href="https://en.wikipedia.org/wiki/Two%27s_complement" target="_blank">2's complement notation</a>. Therefore, in <strong class="example">Example 3</strong>, the input represents the signed integer. <code>-3</code>.</li>
|
||||
</ul>
|
||||
<p>Given a positive integer <code>n</code>, write a function that returns the number of <span data-keyword="set-bit">set bits</span> in its binary representation (also known as the <a href="http://en.wikipedia.org/wiki/Hamming_weight" target="_blank">Hamming weight</a>).</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 00000000000000000000000000001011
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The input binary string <strong>00000000000000000000000000001011</strong> has a total of three '1' bits.
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 11</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">3</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The input binary string <strong>1011</strong> has a total of three set bits.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 00000000000000000000000010000000
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The input binary string <strong>00000000000000000000000010000000</strong> has a total of one '1' bit.
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 128</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The input binary string <strong>10000000</strong> has a total of one set bit.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 11111111111111111111111111111101
|
||||
<strong>Output:</strong> 31
|
||||
<strong>Explanation:</strong> The input binary string <strong>11111111111111111111111111111101</strong> has a total of thirty one '1' bits.
|
||||
</pre>
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">n = 2147483645</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">30</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The input binary string <strong>1111111111111111111111111111101</strong> has a total of thirty set bits.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The input must be a <strong>binary string</strong> of length <code>32</code>.</li>
|
||||
<li><code>1 <= n <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
@@ -34,7 +34,7 @@ You can make 8 consecutive integer values starting from 0.</pre>
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,4,10,3,1]
|
||||
<strong>Input:</strong> coins = [1,4,10,3,1]
|
||||
<strong>Output:</strong> 20</pre>
|
||||
|
||||
<p> </p>
|
||||
|
@@ -1,25 +1,27 @@
|
||||
<p>You are given three strings <code>s1</code>, <code>s2</code>, and <code>s3</code>. You have to perform the following operation on these three strings <strong>as many times</strong> as you want<!-- notionvc: b5178de7-3318-4129-b7d9-726b47e90621 -->.</p>
|
||||
<p>You are given three strings: <code>s1</code>, <code>s2</code>, and <code>s3</code>. In one operation you can choose one of these strings and delete its <strong>rightmost</strong> character. Note that you <strong>cannot</strong> completely empty a string.</p>
|
||||
|
||||
<p>In one operation you can choose one of these three strings such that its length is at least <code>2</code><!-- notionvc: 3342ac46-33c8-4010-aacd-e58678ce31ef --> and delete the <strong>rightmost</strong> character of it.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of operations you need to perform to make the three strings equal if there is a way to make them equal, otherwise, return </em><code>-1</code><em>.</em></p>
|
||||
<p>Return the <em>minimum number of operations</em> required to make the strings equal<em>. </em>If it is impossible to make them equal, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s1 = "abc", s2 = "abb", s3 = "ab"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Performing operations on s1 and s2 once will lead to three equal strings.
|
||||
It can be shown that there is no way to make them equal with less than two operations.</pre>
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>Input: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">s1 = "abc", s2 = "abb", s3 = "ab"</span></p>
|
||||
|
||||
<p><strong>Output: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">2</span></p>
|
||||
|
||||
<p><strong>Explanation: </strong>Deleting the rightmost character from both <code>s1</code> and <code>s2</code> will result in three equal strings.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s1 = "dac", s2 = "bac", s3 = "cac"
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> Because the leftmost letters<!-- notionvc: 47239f7c-eec1-49f8-af79-c206ec88cb07 --> of s1 and s2 are not equal, they could not be equal after any number of operations. So the answer is -1.
|
||||
</pre>
|
||||
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
|
||||
<p><strong>Input: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">s1 = "dac", s2 = "bac", s3 = "cac"</span></p>
|
||||
|
||||
<p><strong>Output: </strong><span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;">-1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong> Since the first letters of <code>s1</code> and <code>s2</code> differ, they cannot be made equal.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
@@ -1,46 +1,46 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>arr</code> and an integer <code>k</code>. The array <code>arr</code> is circular. In other words, the first element of the array is the next element of the last element, and the last element of the array is the previous element of the first element.</p>
|
||||
|
||||
<p>You can do the following operation any number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Pick any element from <code>arr</code> and increase or decrease it by <code>1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the minimum number of operations such that the sum of each <strong>subarray</strong> of length </em><code>k</code><em> is equal</em>.</p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous part of the array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [1,4,1,3], k = 2
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> we can do one operation on index 1 to make its value equal to 3.
|
||||
The array after the operation is [1,3,1,3]
|
||||
- Subarray starts at index 0 is [1, 3], and its sum is 4
|
||||
- Subarray starts at index 1 is [3, 1], and its sum is 4
|
||||
- Subarray starts at index 2 is [1, 3], and its sum is 4
|
||||
- Subarray starts at index 3 is [3, 1], and its sum is 4
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [2,5,5,7], k = 3
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> we can do three operations on index 0 to make its value equal to 5 and two operations on index 3 to make its value equal to 5.
|
||||
The array after the operations is [5,5,5,5]
|
||||
- Subarray starts at index 0 is [5, 5, 5], and its sum is 15
|
||||
- Subarray starts at index 1 is [5, 5, 5], and its sum is 15
|
||||
- Subarray starts at index 2 is [5, 5, 5], and its sum is 15
|
||||
- Subarray starts at index 3 is [5, 5, 5], and its sum is 15
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= arr.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>arr</code> and an integer <code>k</code>. The array <code>arr</code> is circular. In other words, the first element of the array is the next element of the last element, and the last element of the array is the previous element of the first element.</p>
|
||||
|
||||
<p>You can do the following operation any number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Pick any element from <code>arr</code> and increase or decrease it by <code>1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the minimum number of operations such that the sum of each <strong>subarray</strong> of length </em><code>k</code><em> is equal</em>.</p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous part of the array.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [1,4,1,3], k = 2
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> we can do one operation on index 1 to make its value equal to 3.
|
||||
The array after the operation is [1,3,1,3]
|
||||
- Subarray starts at index 0 is [1, 3], and its sum is 4
|
||||
- Subarray starts at index 1 is [3, 1], and its sum is 4
|
||||
- Subarray starts at index 2 is [1, 3], and its sum is 4
|
||||
- Subarray starts at index 3 is [3, 1], and its sum is 4
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> arr = [2,5,5,7], k = 3
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> we can do three operations on index 0 to make its value equal to 5 and two operations on index 3 to make its value equal to 5.
|
||||
The array after the operations is [5,5,5,5]
|
||||
- Subarray starts at index 0 is [5, 5, 5], and its sum is 15
|
||||
- Subarray starts at index 1 is [5, 5, 5], and its sum is 15
|
||||
- Subarray starts at index 2 is [5, 5, 5], and its sum is 15
|
||||
- Subarray starts at index 3 is [5, 5, 5], and its sum is 15
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= arr.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= arr[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
||||
|
@@ -1,38 +1,38 @@
|
||||
<p>You are given two <strong>0-indexed</strong> strings <code>word1</code> and <code>word2</code>.</p>
|
||||
|
||||
<p>A <strong>move</strong> consists of choosing two indices <code>i</code> and <code>j</code> such that <code>0 <= i < word1.length</code> and <code>0 <= j < word2.length</code> and swapping <code>word1[i]</code> with <code>word2[j]</code>.</p>
|
||||
|
||||
<p>Return <code>true</code> <em>if it is possible to get the number of distinct characters in</em> <code>word1</code> <em>and</em> <code>word2</code> <em>to be equal with <strong>exactly one</strong> move. </em>Return <code>false</code> <em>otherwise</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word1 = "ac", word2 = "b"
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> Any pair of swaps would yield two distinct characters in the first string, and one in the second string.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word1 = "abcc", word2 = "aab"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> We swap index 2 of the first string with index 0 of the second string. The resulting strings are word1 = "abac" and word2 = "cab", which both have 3 distinct characters.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word1 = "abcde", word2 = "fghij"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> Both resulting strings will have 5 distinct characters, regardless of which indices we swap.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word1.length, word2.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>word1</code> and <code>word2</code> consist of only lowercase English letters.</li>
|
||||
</ul>
|
||||
<p>You are given two <strong>0-indexed</strong> strings <code>word1</code> and <code>word2</code>.</p>
|
||||
|
||||
<p>A <strong>move</strong> consists of choosing two indices <code>i</code> and <code>j</code> such that <code>0 <= i < word1.length</code> and <code>0 <= j < word2.length</code> and swapping <code>word1[i]</code> with <code>word2[j]</code>.</p>
|
||||
|
||||
<p>Return <code>true</code> <em>if it is possible to get the number of distinct characters in</em> <code>word1</code> <em>and</em> <code>word2</code> <em>to be equal with <strong>exactly one</strong> move. </em>Return <code>false</code> <em>otherwise</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word1 = "ac", word2 = "b"
|
||||
<strong>Output:</strong> false
|
||||
<strong>Explanation:</strong> Any pair of swaps would yield two distinct characters in the first string, and one in the second string.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word1 = "abcc", word2 = "aab"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> We swap index 2 of the first string with index 0 of the second string. The resulting strings are word1 = "abac" and word2 = "cab", which both have 3 distinct characters.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> word1 = "abcde", word2 = "fghij"
|
||||
<strong>Output:</strong> true
|
||||
<strong>Explanation:</strong> Both resulting strings will have 5 distinct characters, regardless of which indices we swap.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= word1.length, word2.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>word1</code> and <code>word2</code> consist of only lowercase English letters.</li>
|
||||
</ul>
|
@@ -1,6 +1,8 @@
|
||||
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. In one operation, you can increase or decrease any element by 1.</p>
|
||||
|
||||
<p>Return the <strong>minimum</strong> number of operations needed to make the <strong><span data-keyword="median-array">median</span></strong> of <code>nums</code> <em>equal</em> to <code>k</code>.</p>
|
||||
<p>Return the <strong>minimum</strong> number of operations needed to make the <strong>median</strong> of <code>nums</code> <em>equal</em> to <code>k</code>.</p>
|
||||
|
||||
<p>The median of an array is defined as the middle element of the array when it is sorted in non-decreasing order. If there are two choices for a median, the larger of the two values is taken.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
@@ -1,38 +1,38 @@
|
||||
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and an integer <code>k</code>. You can perform the following operation on <code>nums1</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose two indexes <code>i</code> and <code>j</code> and increment <code>nums1[i]</code> by <code>k</code> and decrement <code>nums1[j]</code> by <code>k</code>. In other words, <code>nums1[i] = nums1[i] + k</code> and <code>nums1[j] = nums1[j] - k</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p><code>nums1</code> is said to be <strong>equal</strong> to <code>nums2</code> if for all indices <code>i</code> such that <code>0 <= i < n</code>, <code>nums1[i] == nums2[i]</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of operations required to make </em><code>nums1</code><em> equal to </em><code>nums2</code>. If it is impossible to make them equal, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [4,3,1,4], nums2 = [1,3,7,1], k = 3
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> In 2 operations, we can transform nums1 to nums2.
|
||||
1<sup>st</sup> operation: i = 2, j = 0. After applying the operation, nums1 = [1,3,4,4].
|
||||
2<sup>nd</sup> operation: i = 2, j = 3. After applying the operation, nums1 = [1,3,7,1].
|
||||
One can prove that it is impossible to make arrays equal in fewer operations.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [3,8,5,2], nums2 = [2,4,1,6], k = 1
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be proved that it is impossible to make the two arrays equal.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums1.length == nums2.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums1[i], nums2[j] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
<p>You are given two integer arrays <code>nums1</code> and <code>nums2</code> of equal length <code>n</code> and an integer <code>k</code>. You can perform the following operation on <code>nums1</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose two indexes <code>i</code> and <code>j</code> and increment <code>nums1[i]</code> by <code>k</code> and decrement <code>nums1[j]</code> by <code>k</code>. In other words, <code>nums1[i] = nums1[i] + k</code> and <code>nums1[j] = nums1[j] - k</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p><code>nums1</code> is said to be <strong>equal</strong> to <code>nums2</code> if for all indices <code>i</code> such that <code>0 <= i < n</code>, <code>nums1[i] == nums2[i]</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of operations required to make </em><code>nums1</code><em> equal to </em><code>nums2</code>. If it is impossible to make them equal, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [4,3,1,4], nums2 = [1,3,7,1], k = 3
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> In 2 operations, we can transform nums1 to nums2.
|
||||
1<sup>st</sup> operation: i = 2, j = 0. After applying the operation, nums1 = [1,3,4,4].
|
||||
2<sup>nd</sup> operation: i = 2, j = 3. After applying the operation, nums1 = [1,3,7,1].
|
||||
One can prove that it is impossible to make arrays equal in fewer operations.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums1 = [3,8,5,2], nums2 = [2,4,1,6], k = 1
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be proved that it is impossible to make the two arrays equal.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums1.length == nums2.length</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums1[i], nums2[j] <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= k <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
|
@@ -1,36 +1,36 @@
|
||||
<p>You are given a non-negative integer array <code>nums</code>. In one operation, you must:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose a positive integer <code>x</code> such that <code>x</code> is less than or equal to the <strong>smallest non-zero</strong> element in <code>nums</code>.</li>
|
||||
<li>Subtract <code>x</code> from every <strong>positive</strong> element in <code>nums</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of operations to make every element in </em><code>nums</code><em> equal to </em><code>0</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,5,0,3,5]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
In the first operation, choose x = 1. Now, nums = [0,4,0,2,4].
|
||||
In the second operation, choose x = 2. Now, nums = [0,2,0,0,2].
|
||||
In the third operation, choose x = 2. Now, nums = [0,0,0,0,0].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Each element in nums is already 0 so no operations are needed.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>0 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
||||
<p>You are given a non-negative integer array <code>nums</code>. In one operation, you must:</p>
|
||||
|
||||
<ul>
|
||||
<li>Choose a positive integer <code>x</code> such that <code>x</code> is less than or equal to the <strong>smallest non-zero</strong> element in <code>nums</code>.</li>
|
||||
<li>Subtract <code>x</code> from every <strong>positive</strong> element in <code>nums</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of operations to make every element in </em><code>nums</code><em> equal to </em><code>0</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,5,0,3,5]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
In the first operation, choose x = 1. Now, nums = [0,4,0,2,4].
|
||||
In the second operation, choose x = 2. Now, nums = [0,2,0,0,2].
|
||||
In the third operation, choose x = 2. Now, nums = [0,0,0,0,0].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Each element in nums is already 0 so no operations are needed.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 100</code></li>
|
||||
<li><code>0 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
||||
|
@@ -38,3 +38,6 @@ It can be shown that we cannot make the array empty in less than 4 operations.
|
||||
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/minimum-rounds-to-complete-all-tasks/description/" target="_blank">2244: Minimum Rounds to Complete All Tasks.</a></p>
|
||||
|
@@ -1,48 +1,48 @@
|
||||
<p>You are given an array <code>nums</code> consisting of positive integers.</p>
|
||||
|
||||
<p>You are also given an integer array <code>queries</code> of size <code>m</code>. For the <code>i<sup>th</sup></code> query, you want to make all of the elements of <code>nums</code> equal to<code> queries[i]</code>. You can perform the following operation on the array <strong>any</strong> number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Increase</strong> or <strong>decrease</strong> an element of the array by <code>1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an array </em><code>answer</code><em> of size </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>minimum</strong> number of operations to make all elements of </em><code>nums</code><em> equal to </em><code>queries[i]</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that after each query the array is reset to its original state.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,1,6,8], queries = [1,5]
|
||||
<strong>Output:</strong> [14,10]
|
||||
<strong>Explanation:</strong> For the first query we can do the following operations:
|
||||
- Decrease nums[0] 2 times, so that nums = [1,1,6,8].
|
||||
- Decrease nums[2] 5 times, so that nums = [1,1,1,8].
|
||||
- Decrease nums[3] 7 times, so that nums = [1,1,1,1].
|
||||
So the total number of operations for the first query is 2 + 5 + 7 = 14.
|
||||
For the second query we can do the following operations:
|
||||
- Increase nums[0] 2 times, so that nums = [5,1,6,8].
|
||||
- Increase nums[1] 4 times, so that nums = [5,5,6,8].
|
||||
- Decrease nums[2] 1 time, so that nums = [5,5,5,8].
|
||||
- Decrease nums[3] 3 times, so that nums = [5,5,5,5].
|
||||
So the total number of operations for the second query is 2 + 4 + 1 + 3 = 10.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,9,6,3], queries = [10]
|
||||
<strong>Output:</strong> [20]
|
||||
<strong>Explanation:</strong> We can increase each value in the array to 10. The total number of operations will be 8 + 1 + 4 + 7 = 20.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>m == queries.length</code></li>
|
||||
<li><code>1 <= n, m <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i], queries[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
||||
<p>You are given an array <code>nums</code> consisting of positive integers.</p>
|
||||
|
||||
<p>You are also given an integer array <code>queries</code> of size <code>m</code>. For the <code>i<sup>th</sup></code> query, you want to make all of the elements of <code>nums</code> equal to<code> queries[i]</code>. You can perform the following operation on the array <strong>any</strong> number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Increase</strong> or <strong>decrease</strong> an element of the array by <code>1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an array </em><code>answer</code><em> of size </em><code>m</code><em> where </em><code>answer[i]</code><em> is the <strong>minimum</strong> number of operations to make all elements of </em><code>nums</code><em> equal to </em><code>queries[i]</code>.</p>
|
||||
|
||||
<p><strong>Note</strong> that after each query the array is reset to its original state.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,1,6,8], queries = [1,5]
|
||||
<strong>Output:</strong> [14,10]
|
||||
<strong>Explanation:</strong> For the first query we can do the following operations:
|
||||
- Decrease nums[0] 2 times, so that nums = [1,1,6,8].
|
||||
- Decrease nums[2] 5 times, so that nums = [1,1,1,8].
|
||||
- Decrease nums[3] 7 times, so that nums = [1,1,1,1].
|
||||
So the total number of operations for the first query is 2 + 5 + 7 = 14.
|
||||
For the second query we can do the following operations:
|
||||
- Increase nums[0] 2 times, so that nums = [5,1,6,8].
|
||||
- Increase nums[1] 4 times, so that nums = [5,5,6,8].
|
||||
- Decrease nums[2] 1 time, so that nums = [5,5,5,8].
|
||||
- Decrease nums[3] 3 times, so that nums = [5,5,5,5].
|
||||
So the total number of operations for the second query is 2 + 4 + 1 + 3 = 10.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,9,6,3], queries = [10]
|
||||
<strong>Output:</strong> [20]
|
||||
<strong>Explanation:</strong> We can increase each value in the array to 10. The total number of operations will be 8 + 1 + 4 + 7 = 20.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length</code></li>
|
||||
<li><code>m == queries.length</code></li>
|
||||
<li><code>1 <= n, m <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i], queries[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
||||
|
@@ -19,7 +19,7 @@
|
||||
<strong>Input:</strong> nums = [3,2,1,2,1,7]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong> After 6 moves, the array could be [3, 4, 1, 2, 5, 7].
|
||||
It can be shown with 5 or less moves that it is impossible for the array to have all unique values.
|
||||
It can be shown that it is impossible for the array to have all unique values with 5 or less moves.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
@@ -1,43 +1,38 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisiting of <strong>positive</strong> integers. You can do the following operation on the array <strong>any</strong> number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Select an index <code>i</code> such that <code>0 <= i < n - 1</code> and replace either of <code>nums[i]</code> or <code>nums[i+1]</code> with their gcd value.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of operations to make all elements of </em><code>nums</code><em> equal to </em><code>1</code>. If it is impossible, return <code>-1</code>.</p>
|
||||
|
||||
<p>The gcd of two integers is the greatest common divisor of the two integers.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,6,3,4]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> We can do the following operations:
|
||||
- Choose index i = 2 and replace nums[2] with gcd(3,4) = 1. Now we have nums = [2,6,1,4].
|
||||
- Choose index i = 1 and replace nums[1] with gcd(6,1) = 1. Now we have nums = [2,1,1,4].
|
||||
- Choose index i = 0 and replace nums[0] with gcd(2,1) = 1. Now we have nums = [1,1,1,4].
|
||||
- Choose index i = 2 and replace nums[3] with gcd(1,4) = 1. Now we have nums = [1,1,1,1].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,10,6,14]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be shown that it is impossible to make all the elements equal to 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><b>Follow-up:</b></p>
|
||||
|
||||
<p>The <code>O(n)</code> time complexity solution works, but could you find an <code>O(1)</code> constant time complexity solution?</p>
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisiting of <strong>positive</strong> integers. You can do the following operation on the array <strong>any</strong> number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Select an index <code>i</code> such that <code>0 <= i < n - 1</code> and replace either of <code>nums[i]</code> or <code>nums[i+1]</code> with their gcd value.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> number of operations to make all elements of </em><code>nums</code><em> equal to </em><code>1</code>. If it is impossible, return <code>-1</code>.</p>
|
||||
|
||||
<p>The gcd of two integers is the greatest common divisor of the two integers.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,6,3,4]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> We can do the following operations:
|
||||
- Choose index i = 2 and replace nums[2] with gcd(3,4) = 1. Now we have nums = [2,6,1,4].
|
||||
- Choose index i = 1 and replace nums[1] with gcd(6,1) = 1. Now we have nums = [2,1,1,4].
|
||||
- Choose index i = 0 and replace nums[0] with gcd(2,1) = 1. Now we have nums = [1,1,1,4].
|
||||
- Choose index i = 2 and replace nums[3] with gcd(1,4) = 1. Now we have nums = [1,1,1,1].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,10,6,14]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be shown that it is impossible to make all the elements equal to 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>6</sup></code></li>
|
||||
</ul>
|
||||
|
@@ -1,51 +1,51 @@
|
||||
<p>You are given two positive integer arrays <code>nums</code> and <code>target</code>, of the same length.</p>
|
||||
|
||||
<p>In one operation, you can choose any two <strong>distinct</strong> indices <code>i</code> and <code>j</code> where <code>0 <= i, j < nums.length</code> and:</p>
|
||||
|
||||
<ul>
|
||||
<li>set <code>nums[i] = nums[i] + 2</code> and</li>
|
||||
<li>set <code>nums[j] = nums[j] - 2</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Two arrays are considered to be <strong>similar</strong> if the frequency of each element is the same.</p>
|
||||
|
||||
<p>Return <em>the minimum number of operations required to make </em><code>nums</code><em> similar to </em><code>target</code>. The test cases are generated such that <code>nums</code> can always be similar to <code>target</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [8,12,6], target = [2,14,10]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> It is possible to make nums similar to target in two operations:
|
||||
- Choose i = 0 and j = 2, nums = [10,12,4].
|
||||
- Choose i = 1 and j = 2, nums = [10,14,2].
|
||||
It can be shown that 2 is the minimum number of operations needed.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,5], target = [4,1,3]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We can make nums similar to target in one operation:
|
||||
- Choose i = 1 and j = 2, nums = [1,4,3].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,1,1,1], target = [1,1,1,1,1]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The array nums is already similiar to target.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length == target.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i], target[i] <= 10<sup>6</sup></code></li>
|
||||
<li>It is possible to make <code>nums</code> similar to <code>target</code>.</li>
|
||||
</ul>
|
||||
<p>You are given two positive integer arrays <code>nums</code> and <code>target</code>, of the same length.</p>
|
||||
|
||||
<p>In one operation, you can choose any two <strong>distinct</strong> indices <code>i</code> and <code>j</code> where <code>0 <= i, j < nums.length</code> and:</p>
|
||||
|
||||
<ul>
|
||||
<li>set <code>nums[i] = nums[i] + 2</code> and</li>
|
||||
<li>set <code>nums[j] = nums[j] - 2</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Two arrays are considered to be <strong>similar</strong> if the frequency of each element is the same.</p>
|
||||
|
||||
<p>Return <em>the minimum number of operations required to make </em><code>nums</code><em> similar to </em><code>target</code>. The test cases are generated such that <code>nums</code> can always be similar to <code>target</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [8,12,6], target = [2,14,10]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> It is possible to make nums similar to target in two operations:
|
||||
- Choose i = 0 and j = 2, nums = [10,12,4].
|
||||
- Choose i = 1 and j = 2, nums = [10,14,2].
|
||||
It can be shown that 2 is the minimum number of operations needed.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,5], target = [4,1,3]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We can make nums similar to target in one operation:
|
||||
- Choose i = 1 and j = 2, nums = [1,4,3].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,1,1,1,1], target = [1,1,1,1,1]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The array nums is already similiar to target.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length == target.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i], target[i] <= 10<sup>6</sup></code></li>
|
||||
<li>It is possible to make <code>nums</code> similar to <code>target</code>.</li>
|
||||
</ul>
|
||||
|
@@ -1,43 +1,43 @@
|
||||
<p>You are given two <strong>0-indexed</strong> arrays <code>nums</code> and <code>cost</code> consisting each of <code>n</code> <strong>positive</strong> integers.</p>
|
||||
|
||||
<p>You can do the following operation <strong>any</strong> number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Increase or decrease <strong>any</strong> element of the array <code>nums</code> by <code>1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The cost of doing one operation on the <code>i<sup>th</sup></code> element is <code>cost[i]</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> total cost such that all the elements of the array </em><code>nums</code><em> become <strong>equal</strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,5,2], cost = [2,3,1,14]
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong> We can make all the elements equal to 2 in the following way:
|
||||
- Increase the 0<sup>th</sup> element one time. The cost is 2.
|
||||
- Decrease the 1<sup><span style="font-size: 10.8333px;">st</span></sup> element one time. The cost is 3.
|
||||
- Decrease the 2<sup>nd</sup> element three times. The cost is 1 + 1 + 1 = 3.
|
||||
The total cost is 2 + 3 + 3 = 8.
|
||||
It can be shown that we cannot make the array equal with a smaller cost.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,2,2,2,2], cost = [4,2,8,1,3]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> All the elements are already equal, so no operations are needed.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length == cost.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i], cost[i] <= 10<sup>6</sup></code></li>
|
||||
<li>Test cases are generated in a way that the output doesn't exceed 2<sup>53</sup>-1</li>
|
||||
</ul>
|
||||
<p>You are given two <strong>0-indexed</strong> arrays <code>nums</code> and <code>cost</code> consisting each of <code>n</code> <strong>positive</strong> integers.</p>
|
||||
|
||||
<p>You can do the following operation <strong>any</strong> number of times:</p>
|
||||
|
||||
<ul>
|
||||
<li>Increase or decrease <strong>any</strong> element of the array <code>nums</code> by <code>1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The cost of doing one operation on the <code>i<sup>th</sup></code> element is <code>cost[i]</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> total cost such that all the elements of the array </em><code>nums</code><em> become <strong>equal</strong></em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,5,2], cost = [2,3,1,14]
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong> We can make all the elements equal to 2 in the following way:
|
||||
- Increase the 0<sup>th</sup> element one time. The cost is 2.
|
||||
- Decrease the 1<sup><span style="font-size: 10.8333px;">st</span></sup> element one time. The cost is 3.
|
||||
- Decrease the 2<sup>nd</sup> element three times. The cost is 1 + 1 + 1 = 3.
|
||||
The total cost is 2 + 3 + 3 = 8.
|
||||
It can be shown that we cannot make the array equal with a smaller cost.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [2,2,2,2,2], cost = [4,2,8,1,3]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> All the elements are already equal, so no operations are needed.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == nums.length == cost.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i], cost[i] <= 10<sup>6</sup></code></li>
|
||||
<li>Test cases are generated in a way that the output doesn't exceed 2<sup>53</sup>-1</li>
|
||||
</ul>
|
||||
|
@@ -1,6 +1,6 @@
|
||||
<p>You are given two positive integer arrays <code>nums</code> and <code>target</code>, of the same length.</p>
|
||||
|
||||
<p>In a single operation, you can select any <span data-keyword="subarray">subarray</span> of <code>nums</code> and increment or decrement each element within that subarray by 1.</p>
|
||||
<p>In a single operation, you can select any subarray of <code>nums</code> and increment each element within that subarray by 1 or decrement each element within that subarray by 1.</p>
|
||||
|
||||
<p>Return the <strong>minimum</strong> number of operations required to make <code>nums</code> equal to the array <code>target</code>.</p>
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
<p>There are <code>n</code> seats and <code>n</code> students in a room. You are given an array <code>seats</code> of length <code>n</code>, where <code>seats[i]</code> is the position of the <code>i<sup>th</sup></code> seat. You are also given the array <code>students</code> of length <code>n</code>, where <code>students[j]</code> is the position of the <code>j<sup>th</sup></code> student.</p>
|
||||
<p>There are <code>n</code> <strong>availabe </strong>seats and <code>n</code> students <strong>standing</strong> in a room. You are given an array <code>seats</code> of length <code>n</code>, where <code>seats[i]</code> is the position of the <code>i<sup>th</sup></code> seat. You are also given the array <code>students</code> of length <code>n</code>, where <code>students[j]</code> is the position of the <code>j<sup>th</sup></code> student.</p>
|
||||
|
||||
<p>You may perform the following move any number of times:</p>
|
||||
|
||||
@@ -17,9 +17,9 @@
|
||||
<strong>Input:</strong> seats = [3,1,5], students = [2,7,4]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The students are moved as follows:
|
||||
- The first student is moved from from position 2 to position 1 using 1 move.
|
||||
- The second student is moved from from position 7 to position 5 using 2 moves.
|
||||
- The third student is moved from from position 4 to position 3 using 1 move.
|
||||
- The first student is moved from position 2 to position 1 using 1 move.
|
||||
- The second student is moved from position 7 to position 5 using 2 moves.
|
||||
- The third student is moved from position 4 to position 3 using 1 move.
|
||||
In total, 1 + 2 + 1 = 4 moves were used.
|
||||
</pre>
|
||||
|
||||
@@ -30,9 +30,9 @@ In total, 1 + 2 + 1 = 4 moves were used.
|
||||
<strong>Output:</strong> 7
|
||||
<strong>Explanation:</strong> The students are moved as follows:
|
||||
- The first student is not moved.
|
||||
- The second student is moved from from position 3 to position 4 using 1 move.
|
||||
- The third student is moved from from position 2 to position 5 using 3 moves.
|
||||
- The fourth student is moved from from position 6 to position 9 using 3 moves.
|
||||
- The second student is moved from position 3 to position 4 using 1 move.
|
||||
- The third student is moved from position 2 to position 5 using 3 moves.
|
||||
- The fourth student is moved from position 6 to position 9 using 3 moves.
|
||||
In total, 0 + 1 + 3 + 3 = 7 moves were used.
|
||||
</pre>
|
||||
|
||||
@@ -43,8 +43,8 @@ In total, 0 + 1 + 3 + 3 = 7 moves were used.
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> Note that there are two seats at position 2 and two seats at position 6.
|
||||
The students are moved as follows:
|
||||
- The first student is moved from from position 1 to position 2 using 1 move.
|
||||
- The second student is moved from from position 3 to position 6 using 3 moves.
|
||||
- The first student is moved from position 1 to position 2 using 1 move.
|
||||
- The second student is moved from position 3 to position 6 using 3 moves.
|
||||
- The third student is not moved.
|
||||
- The fourth student is not moved.
|
||||
In total, 1 + 3 + 0 + 0 = 4 moves were used.
|
||||
|
@@ -1,51 +1,51 @@
|
||||
<p>You are given a string <code>s</code> and a robot that currently holds an empty string <code>t</code>. Apply one of the following operations until <code>s</code> and <code>t</code> <strong>are both empty</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>Remove the <strong>first</strong> character of a string <code>s</code> and give it to the robot. The robot will append this character to the string <code>t</code>.</li>
|
||||
<li>Remove the <strong>last</strong> character of a string <code>t</code> and give it to the robot. The robot will write this character on paper.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the lexicographically smallest string that can be written on the paper.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "zza"
|
||||
<strong>Output:</strong> "azz"
|
||||
<strong>Explanation:</strong> Let p denote the written string.
|
||||
Initially p="", s="zza", t="".
|
||||
Perform first operation three times p="", s="", t="zza".
|
||||
Perform second operation three times p="azz", s="", t="".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "bac"
|
||||
<strong>Output:</strong> "abc"
|
||||
<strong>Explanation:</strong> Let p denote the written string.
|
||||
Perform first operation twice p="", s="c", t="ba".
|
||||
Perform second operation twice p="ab", s="c", t="".
|
||||
Perform first operation p="ab", s="", t="c".
|
||||
Perform second operation p="abc", s="", t="".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "bdda"
|
||||
<strong>Output:</strong> "addb"
|
||||
<strong>Explanation:</strong> Let p denote the written string.
|
||||
Initially p="", s="bdda", t="".
|
||||
Perform first operation four times p="", s="", t="bdda".
|
||||
Perform second operation four times p="addb", s="", t="".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists of only English lowercase letters.</li>
|
||||
</ul>
|
||||
<p>You are given a string <code>s</code> and a robot that currently holds an empty string <code>t</code>. Apply one of the following operations until <code>s</code> and <code>t</code> <strong>are both empty</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>Remove the <strong>first</strong> character of a string <code>s</code> and give it to the robot. The robot will append this character to the string <code>t</code>.</li>
|
||||
<li>Remove the <strong>last</strong> character of a string <code>t</code> and give it to the robot. The robot will write this character on paper.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the lexicographically smallest string that can be written on the paper.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "zza"
|
||||
<strong>Output:</strong> "azz"
|
||||
<strong>Explanation:</strong> Let p denote the written string.
|
||||
Initially p="", s="zza", t="".
|
||||
Perform first operation three times p="", s="", t="zza".
|
||||
Perform second operation three times p="azz", s="", t="".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "bac"
|
||||
<strong>Output:</strong> "abc"
|
||||
<strong>Explanation:</strong> Let p denote the written string.
|
||||
Perform first operation twice p="", s="c", t="ba".
|
||||
Perform second operation twice p="ab", s="c", t="".
|
||||
Perform first operation p="ab", s="", t="c".
|
||||
Perform second operation p="abc", s="", t="".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "bdda"
|
||||
<strong>Output:</strong> "addb"
|
||||
<strong>Explanation:</strong> Let p denote the written string.
|
||||
Initially p="", s="bdda", t="".
|
||||
Perform first operation four times p="", s="", t="bdda".
|
||||
Perform second operation four times p="addb", s="", t="".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>s</code> consists of only English lowercase letters.</li>
|
||||
</ul>
|
||||
|
@@ -1,4 +1,4 @@
|
||||
<p>Enhance all functions to have the <code>callPolyfill</code> method. The method accepts an object <code>obj</code> as it's first parameter and any number of additional arguments. The <code>obj</code> becomes the <code>this</code> context for the function. The additional arguments are passed to the function (that the <code>callPolyfill</code> method belongs on).</p>
|
||||
<p>Enhance all functions to have the <code>callPolyfill</code> method. The method accepts an object <code>obj</code> as its first parameter and any number of additional arguments. The <code>obj</code> becomes the <code>this</code> context for the function. The additional arguments are passed to the function (that the <code>callPolyfill</code> method belongs on).</p>
|
||||
|
||||
<p>For example if you had the function:</p>
|
||||
|
||||
@@ -45,7 +45,7 @@ args = [{"item": "burger"}, 10, 1.1]
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul style="list-style-type:square;">
|
||||
<ul>
|
||||
<li><code><font face="monospace">typeof args[0] == 'object' and args[0] != null</font></code></li>
|
||||
<li><code>1 <= args.length <= 100</code></li>
|
||||
<li><code>2 <= JSON.stringify(args[0]).length <= 10<sup>5</sup></code></li>
|
||||
|
@@ -1,38 +1,38 @@
|
||||
<p>You are given a positive integer <code>n</code>.</p>
|
||||
|
||||
<p>Continuously replace <code>n</code> with the sum of its <strong>prime factors</strong>.</p>
|
||||
|
||||
<ul>
|
||||
<li>Note that if a prime factor divides <code>n</code> multiple times, it should be included in the sum as many times as it divides <code>n</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the smallest value </em><code>n</code><em> will take on.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 15
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> Initially, n = 15.
|
||||
15 = 3 * 5, so replace n with 3 + 5 = 8.
|
||||
8 = 2 * 2 * 2, so replace n with 2 + 2 + 2 = 6.
|
||||
6 = 2 * 3, so replace n with 2 + 3 = 5.
|
||||
5 is the smallest value n will take on.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Initially, n = 3.
|
||||
3 is the smallest value n will take on.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
<p>You are given a positive integer <code>n</code>.</p>
|
||||
|
||||
<p>Continuously replace <code>n</code> with the sum of its <strong>prime factors</strong>.</p>
|
||||
|
||||
<ul>
|
||||
<li>Note that if a prime factor divides <code>n</code> multiple times, it should be included in the sum as many times as it divides <code>n</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the smallest value </em><code>n</code><em> will take on.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 15
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> Initially, n = 15.
|
||||
15 = 3 * 5, so replace n with 3 + 5 = 8.
|
||||
8 = 2 * 2 * 2, so replace n with 2 + 2 + 2 = 6.
|
||||
6 = 2 * 3, so replace n with 2 + 3 = 5.
|
||||
5 is the smallest value n will take on.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Initially, n = 3.
|
||||
3 is the smallest value n will take on.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
|
@@ -27,7 +27,7 @@ There are no longer two consecutive balloons of the same color. Total time = 3.<
|
||||
<pre>
|
||||
<strong>Input:</strong> colors = "aabaa", neededTime = [1,2,3,4,1]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Bob will remove the ballons at indices 0 and 4. Each ballon takes 1 second to remove.
|
||||
<strong>Explanation:</strong> Bob will remove the balloons at indices 0 and 4. Each balloons takes 1 second to remove.
|
||||
There are no longer two consecutive balloons of the same color. Total time = 1 + 1 = 2.
|
||||
</pre>
|
||||
|
||||
|
@@ -1,61 +1,86 @@
|
||||
<p>You are given two <strong>0-indexed</strong> integer arrays <code><font face="monospace">player1</font></code> and <code>player2</code>, that represent the number of pins that player 1 and player 2 hit in a bowling game, respectively.</p>
|
||||
|
||||
<p>The bowling game consists of <code>n</code> turns, and the number of pins in each turn is exactly <code>10</code>.</p>
|
||||
|
||||
<p>Assume a player hit <code>x<sub>i</sub></code> pins in the <code>i<sup>th</sup></code> turn. The value of the <code>i<sup>th</sup></code> turn for the player is:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>2x<sub>i</sub></code> if the player hit <code>10</code> pins in any of the previous two turns.</li>
|
||||
<li>Otherwise, It is <code>x<sub>i</sub></code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The score of the player is the sum of the values of their <code>n</code> turns.</p>
|
||||
|
||||
<p>Return</p>
|
||||
|
||||
<ul>
|
||||
<li><code>1</code> <em>if the score of player 1 is more than the score of player 2,</em></li>
|
||||
<li><code>2</code> <em>if the score of player 2 is more than the score of player 1, and</em></li>
|
||||
<li><code>0</code> <em>in case of a draw.</em></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> player1 = [4,10,7,9], player2 = [6,5,2,3]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> The score of player1 is 4 + 10 + 2*7 + 2*9 = 46.
|
||||
The score of player2 is 6 + 5 + 2 + 3 = 16.
|
||||
Score of player1 is more than the score of player2, so, player1 is the winner, and the answer is 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> player1 = [3,5,7,6], player2 = [8,10,10,2]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The score of player1 is 3 + 5 + 7 + 6 = 21.
|
||||
The score of player2 is 8 + 10 + 2*10 + 2*2 = 42.
|
||||
Score of player2 is more than the score of player1, so, player2 is the winner, and the answer is 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> player1 = [2,3], player2 = [4,1]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The score of player1 is 2 + 3 = 5
|
||||
The score of player2 is 4 + 1 = 5
|
||||
The score of player1 equals to the score of player2, so, there is a draw, and the answer is 0.
|
||||
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == player1.length == player2.length</code></li>
|
||||
<li><code>1 <= n <= 1000</code></li>
|
||||
<li><code>0 <= player1[i], player2[i] <= 10</code></li>
|
||||
</ul>
|
||||
<p>You are given two <strong>0-indexed</strong> integer arrays <code><font face="monospace">player1</font></code> and <code>player2</code>, representing the number of pins that player 1 and player 2 hit in a bowling game, respectively.</p>
|
||||
|
||||
<p>The bowling game consists of <code>n</code> turns, and the number of pins in each turn is exactly 10.</p>
|
||||
|
||||
<p>Assume a player hits <code>x<sub>i</sub></code> pins in the i<sup>th</sup> turn. The value of the i<sup>th</sup> turn for the player is:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>2x<sub>i</sub></code> if the player hits 10 pins <b>in either (i - 1)<sup>th</sup> or (i - 2)<sup>th</sup> turn</b>.</li>
|
||||
<li>Otherwise, it is <code>x<sub>i</sub></code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>The <strong>score</strong> of the player is the sum of the values of their <code>n</code> turns.</p>
|
||||
|
||||
<p>Return</p>
|
||||
|
||||
<ul>
|
||||
<li>1 if the score of player 1 is more than the score of player 2,</li>
|
||||
<li>2 if the score of player 2 is more than the score of player 1, and</li>
|
||||
<li>0 in case of a draw.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">player1 = [5,10,3,2], player2 = [6,5,7,3]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">1</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The score of player 1 is 5 + 10 + 2*3 + 2*2 = 25.</p>
|
||||
|
||||
<p>The score of player 2 is 6 + 5 + 7 + 3 = 21.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">player1 = [3,5,7,6], player2 = [8,10,10,2]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The score of player 1 is 3 + 5 + 7 + 6 = 21.</p>
|
||||
|
||||
<p>The score of player 2 is 8 + 10 + 2*10 + 2*2 = 42.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">player1 = [2,3], player2 = [4,1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The score of player1 is 2 + 3 = 5.</p>
|
||||
|
||||
<p>The score of player2 is 4 + 1 = 5.</p>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 4:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">player1 = [1,1,1,10,10,10,10], player2 = [10,10,10,10,1,1,1]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">2</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<p>The score of player1 is 1 + 1 + 1 + 10 + 2*10 + 2*10 + 2*10 = 73.</p>
|
||||
|
||||
<p>The score of player2 is 10 + 2*10 + 2*10 + 2*10 + 2*1 + 2*1 + 1 = 75.</p>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == player1.length == player2.length</code></li>
|
||||
<li><code>1 <= n <= 1000</code></li>
|
||||
<li><code>0 <= player1[i], player2[i] <= 10</code></li>
|
||||
</ul>
|
||||
|
@@ -1,41 +1,50 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>The <strong>low</strong> score of <code><font face="monospace">nums</font></code> is the minimum value of <code>|nums[i] - nums[j]|</code> over all <code>0 <= i < j < nums.length</code>.</li>
|
||||
<li>The <strong>high</strong> score of <code><font face="monospace">nums</font></code> is the maximum value of <code>|nums[i] - nums[j]|</code> over all <code>0 <= i < j < nums.length</code>.</li>
|
||||
<li>The <strong>score</strong> of <code>nums</code> is the sum of the <strong>high</strong> and <strong>low</strong> scores of nums.</li>
|
||||
</ul>
|
||||
|
||||
<p>To minimize the score of <code>nums</code>, we can change the value of <strong>at most two</strong> elements of <code>nums</code>.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> possible <strong>score</strong> after changing the value of <strong>at most two</strong> elements o</em>f <code>nums</code>.</p>
|
||||
|
||||
<p>Note that <code>|x|</code> denotes the absolute value of <code>x</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,4,3]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Change value of nums[1] and nums[2] to 1 so that nums becomes [1,1,1]. Now, the value of <code>|nums[i] - nums[j]|</code> is always equal to 0, so we return 0 + 0 = 0.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,4,7,8,5]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Change nums[0] and nums[1] to be 6. Now nums becomes [6,6,7,8,5].
|
||||
Our low score is achieved when i = 0 and j = 1, in which case |<code>nums[i] - nums[j]</code>| = |6 - 6| = 0.
|
||||
Our high score is achieved when i = 3 and j = 4, in which case |<code>nums[i] - nums[j]</code>| = |8 - 5| = 3.
|
||||
The sum of our high and low score is 3, which we can prove to be minimal.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
||||
<p>You are given an integer array <code>nums</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>The <strong>low</strong> score of <code>nums</code> is the <strong>minimum</strong> absolute difference between any two integers.</li>
|
||||
<li>The <strong>high</strong> score of <code>nums</code> is the <strong>maximum</strong> absolute difference between any two integers.</li>
|
||||
<li>The <strong>score</strong> of <code>nums</code> is the sum of the <strong>high</strong> and <strong>low</strong> scores.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return the <strong>minimum score</strong> after <strong>changing two elements</strong> of <code>nums</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,4,7,8,5]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">3</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>Change <code>nums[0]</code> and <code>nums[1]</code> to be 6 so that <code>nums</code> becomes [6,6,7,8,5].</li>
|
||||
<li>The low score is the minimum absolute difference: |6 - 6| = 0.</li>
|
||||
<li>The high score is the maximum absolute difference: |8 - 5| = 3.</li>
|
||||
<li>The sum of high and low score is 3.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<div class="example-block">
|
||||
<p><strong>Input:</strong> <span class="example-io">nums = [1,4,3]</span></p>
|
||||
|
||||
<p><strong>Output:</strong> <span class="example-io">0</span></p>
|
||||
|
||||
<p><strong>Explanation:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>Change <code>nums[1]</code> and <code>nums[2]</code> to 1 so that <code>nums</code> becomes [1,1,1].</li>
|
||||
<li>The sum of maximum absolute difference and minimum absolute difference is 0.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
||||
|
@@ -1,42 +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>
|
||||
<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>
|
||||
|
@@ -1,35 +1,35 @@
|
||||
<p>Given a positive integer <code>n</code>, find the sum of all integers in the range <code>[1, n]</code> <strong>inclusive</strong> that are divisible by <code>3</code>, <code>5</code>, or <code>7</code>.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the sum of all numbers in the given range satisfying the constraint.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 7
|
||||
<strong>Output:</strong> 21
|
||||
<strong>Explanation:</strong> Numbers in the range <code>[1, 7]</code> that are divisible by <code>3</code>, <code>5,</code> or <code>7 </code>are <code>3, 5, 6, 7</code>. The sum of these numbers is <code>21</code>.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 10
|
||||
<strong>Output:</strong> 40
|
||||
<strong>Explanation:</strong> Numbers in the range <code>[1, 10] that are</code> divisible by <code>3</code>, <code>5,</code> or <code>7</code> are <code>3, 5, 6, 7, 9, 10</code>. The sum of these numbers is 40.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 9
|
||||
<strong>Output:</strong> 30
|
||||
<strong>Explanation:</strong> Numbers in the range <code>[1, 9]</code> that are divisible by <code>3</code>, <code>5</code>, or <code>7</code> are <code>3, 5, 6, 7, 9</code>. The sum of these numbers is <code>30</code>.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>3</sup></code></li>
|
||||
</ul>
|
||||
<p>Given a positive integer <code>n</code>, find the sum of all integers in the range <code>[1, n]</code> <strong>inclusive</strong> that are divisible by <code>3</code>, <code>5</code>, or <code>7</code>.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the sum of all numbers in the given range satisfying the constraint.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 7
|
||||
<strong>Output:</strong> 21
|
||||
<strong>Explanation:</strong> Numbers in the range <code>[1, 7]</code> that are divisible by <code>3</code>, <code>5,</code> or <code>7 </code>are <code>3, 5, 6, 7</code>. The sum of these numbers is <code>21</code>.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 10
|
||||
<strong>Output:</strong> 40
|
||||
<strong>Explanation:</strong> Numbers in the range <code>[1, 10] that are</code> divisible by <code>3</code>, <code>5,</code> or <code>7</code> are <code>3, 5, 6, 7, 9, 10</code>. The sum of these numbers is 40.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 9
|
||||
<strong>Output:</strong> 30
|
||||
<strong>Explanation:</strong> Numbers in the range <code>[1, 9]</code> that are divisible by <code>3</code>, <code>5</code>, or <code>7</code> are <code>3, 5, 6, 7, 9</code>. The sum of these numbers is <code>30</code>.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>3</sup></code></li>
|
||||
</ul>
|
||||
|
@@ -34,6 +34,6 @@
|
||||
<ul>
|
||||
<li><code>1 <= matrix.length <= 100</code></li>
|
||||
<li><code>1 <= matrix[0].length <= 100</code></li>
|
||||
<li><code>-1000 <= matrix[i] <= 1000</code></li>
|
||||
<li><code>-1000 <= matrix[i][j] <= 1000</code></li>
|
||||
<li><code>-10^8 <= target <= 10^8</code></li>
|
||||
</ul>
|
||||
|
@@ -1,42 +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 class="example">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 class="example">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 class="example">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>
|
||||
<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 class="example">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 class="example">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 class="example">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>
|
||||
|
@@ -1,4 +1,4 @@
|
||||
<p>Given an array <code>nums</code> of distinct integers, return <em>all the possible permutations</em>. You can return the answer in <strong>any order</strong>.</p>
|
||||
<p>Given an array <code>nums</code> of distinct integers, return all the possible <span data-keyword="permutation-array">permutations</span>. You can return the answer in <strong>any order</strong>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
@@ -1,27 +1,27 @@
|
||||
<p>Given two positive integers <code>a</code> and <code>b</code>, return <em>the number of <strong>common</strong> factors of </em><code>a</code><em> and </em><code>b</code>.</p>
|
||||
|
||||
<p>An integer <code>x</code> is a <strong>common factor</strong> of <code>a</code> and <code>b</code> if <code>x</code> divides both <code>a</code> and <code>b</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> a = 12, b = 6
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The common factors of 12 and 6 are 1, 2, 3, 6.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> a = 25, b = 30
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The common factors of 25 and 30 are 1, 5.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= a, b <= 1000</code></li>
|
||||
</ul>
|
||||
<p>Given two positive integers <code>a</code> and <code>b</code>, return <em>the number of <strong>common</strong> factors of </em><code>a</code><em> and </em><code>b</code>.</p>
|
||||
|
||||
<p>An integer <code>x</code> is a <strong>common factor</strong> of <code>a</code> and <code>b</code> if <code>x</code> divides both <code>a</code> and <code>b</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> a = 12, b = 6
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The common factors of 12 and 6 are 1, 2, 3, 6.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> a = 25, b = 30
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> The common factors of 25 and 30 are 1, 5.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= a, b <= 1000</code></li>
|
||||
</ul>
|
||||
|
@@ -1,37 +1,37 @@
|
||||
<p>Given an integer array <code>nums</code>, return <em>the most frequent even element</em>.</p>
|
||||
|
||||
<p>If there is a tie, return the <strong>smallest</strong> one. If there is no such element, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,1,2,2,4,4,1]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
The even elements are 0, 2, and 4. Of these, 2 and 4 appear the most.
|
||||
We return the smallest one, which is 2.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,4,4,9,2,4]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> 4 is the even element appears the most.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [29,47,21,41,13,37,25,7]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> There is no even element.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 2000</code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
<p>Given an integer array <code>nums</code>, return <em>the most frequent even element</em>.</p>
|
||||
|
||||
<p>If there is a tie, return the <strong>smallest</strong> one. If there is no such element, return <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,1,2,2,4,4,1]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
The even elements are 0, 2, and 4. Of these, 2 and 4 appear the most.
|
||||
We return the smallest one, which is 2.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,4,4,9,2,4]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> 4 is the even element appears the most.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [29,47,21,41,13,37,25,7]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> There is no even element.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 2000</code></li>
|
||||
<li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
|
@@ -1,77 +1,81 @@
|
||||
<p>Given a function <code>fn</code> and a time in milliseconds <code>t</code>, return a <strong>debounced</strong> version of that function.</p>
|
||||
|
||||
<p>A <strong>debounced</strong> function is a function whose execution is delayed by <code>t</code> milliseconds and whose execution is cancelled if it is called again within that window of time. The debounced function should also receive the passed parameters.</p>
|
||||
|
||||
<p>For example, let's say <code>t = 50ms</code>, and the function was called at <code>30ms</code>, <code>60ms</code>, and <code>100ms</code>. The first 2 function calls would be cancelled, and the 3rd function call would be executed at <code>150ms</code>. If instead <code>t = 35ms</code>, The 1st call would be cancelled, the 2nd would be executed at <code>95ms</code>, and the 3rd would be executed at <code>135ms</code>.</p>
|
||||
|
||||
<p><img alt="Debounce Schematic" src="https://assets.leetcode.com/uploads/2023/04/08/screen-shot-2023-04-08-at-11048-pm.png" style="width: 800px; height: 242px;" /></p>
|
||||
|
||||
<p>The above diagram shows how debounce will transform events. Each rectangle represents 100ms and the debounce time is 400ms. Each color represents a different set of inputs.</p>
|
||||
|
||||
<p>Please solve it without using lodash's <code>_.debounce()</code> function.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
t = 50
|
||||
calls = [
|
||||
{"t": 50, inputs: [1]},
|
||||
{"t": 75, inputs: [2]}
|
||||
]
|
||||
<strong>Output:</strong> [{"t": 125, inputs: [2]}]
|
||||
<strong>Explanation:</strong>
|
||||
let start = Date.now();
|
||||
function log(...inputs) {
|
||||
console.log([Date.now() - start, inputs ])
|
||||
}
|
||||
const dlog = debounce(log, 50);
|
||||
setTimeout(() => dlog(1), 50);
|
||||
setTimeout(() => dlog(2), 75);
|
||||
|
||||
The 1st call is cancelled by the 2nd call because the 2nd call occurred before 100ms
|
||||
The 2nd call is delayed by 50ms and executed at 125ms. The inputs were (2).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
t = 20
|
||||
calls = [
|
||||
{"t": 50, inputs: [1]},
|
||||
{"t": 100, inputs: [2]}
|
||||
]
|
||||
<strong>Output:</strong> [{"t": 70, inputs: [1]}, {"t": 120, inputs: [2]}]
|
||||
<strong>Explanation:</strong>
|
||||
The 1st call is delayed until 70ms. The inputs were (1).
|
||||
The 2nd call is delayed until 120ms. The inputs were (2).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
t = 150
|
||||
calls = [
|
||||
{"t": 50, inputs: [1, 2]},
|
||||
{"t": 300, inputs: [3, 4]},
|
||||
{"t": 300, inputs: [5, 6]}
|
||||
]
|
||||
<strong>Output:</strong> [{"t": 200, inputs: [1,2]}, {"t": 450, inputs: [5, 6]}]
|
||||
<strong>Explanation:</strong>
|
||||
The 1st call is delayed by 150ms and ran at 200ms. The inputs were (1, 2).
|
||||
The 2nd call is cancelled by the 3rd call
|
||||
The 3rd call is delayed by 150ms and ran at 450ms. The inputs were (5, 6).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= t <= 1000</code></li>
|
||||
<li><code>1 <= calls.length <= 10</code></li>
|
||||
<li><code>0 <= calls[i].t <= 1000</code></li>
|
||||
<li><code>0 <= calls[i].inputs.length <= 10</code></li>
|
||||
</ul>
|
||||
<p>Given a function <code>fn</code> and a time in milliseconds <code>t</code>, return a <strong>debounced</strong> version of that function.</p>
|
||||
|
||||
<p>A <strong>debounced</strong> function is a function whose execution is delayed by <code>t</code> milliseconds and whose execution is cancelled if it is called again within that window of time. The debounced function should also receive the passed parameters.</p>
|
||||
|
||||
<p>For example, let's say <code>t = 50ms</code>, and the function was called at <code>30ms</code>, <code>60ms</code>, and <code>100ms</code>.</p>
|
||||
|
||||
<p>The first 2 function calls would be cancelled, and the 3rd function call would be executed at <code>150ms</code>.</p>
|
||||
|
||||
<p>If instead <code>t = 35ms</code>, The 1st call would be cancelled, the 2nd would be executed at <code>95ms</code>, and the 3rd would be executed at <code>135ms</code>.</p>
|
||||
|
||||
<p><img alt="Debounce Schematic" src="https://assets.leetcode.com/uploads/2023/04/08/screen-shot-2023-04-08-at-11048-pm.png" style="width: 800px; height: 242px;" /></p>
|
||||
|
||||
<p>The above diagram shows how debounce will transform events. Each rectangle represents 100ms and the debounce time is 400ms. Each color represents a different set of inputs.</p>
|
||||
|
||||
<p>Please solve it without using lodash's <code>_.debounce()</code> function.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
t = 50
|
||||
calls = [
|
||||
{"t": 50, inputs: [1]},
|
||||
{"t": 75, inputs: [2]}
|
||||
]
|
||||
<strong>Output:</strong> [{"t": 125, inputs: [2]}]
|
||||
<strong>Explanation:</strong>
|
||||
let start = Date.now();
|
||||
function log(...inputs) {
|
||||
console.log([Date.now() - start, inputs ])
|
||||
}
|
||||
const dlog = debounce(log, 50);
|
||||
setTimeout(() => dlog(1), 50);
|
||||
setTimeout(() => dlog(2), 75);
|
||||
|
||||
The 1st call is cancelled by the 2nd call because the 2nd call occurred before 100ms
|
||||
The 2nd call is delayed by 50ms and executed at 125ms. The inputs were (2).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
t = 20
|
||||
calls = [
|
||||
{"t": 50, inputs: [1]},
|
||||
{"t": 100, inputs: [2]}
|
||||
]
|
||||
<strong>Output:</strong> [{"t": 70, inputs: [1]}, {"t": 120, inputs: [2]}]
|
||||
<strong>Explanation:</strong>
|
||||
The 1st call is delayed until 70ms. The inputs were (1).
|
||||
The 2nd call is delayed until 120ms. The inputs were (2).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong>
|
||||
t = 150
|
||||
calls = [
|
||||
{"t": 50, inputs: [1, 2]},
|
||||
{"t": 300, inputs: [3, 4]},
|
||||
{"t": 300, inputs: [5, 6]}
|
||||
]
|
||||
<strong>Output:</strong> [{"t": 200, inputs: [1,2]}, {"t": 450, inputs: [5, 6]}]
|
||||
<strong>Explanation:</strong>
|
||||
The 1st call is delayed by 150ms and ran at 200ms. The inputs were (1, 2).
|
||||
The 2nd call is cancelled by the 3rd call
|
||||
The 3rd call is delayed by 150ms and ran at 450ms. The inputs were (5, 6).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= t <= 1000</code></li>
|
||||
<li><code>1 <= calls.length <= 10</code></li>
|
||||
<li><code>0 <= calls[i].t <= 1000</code></li>
|
||||
<li><code>0 <= calls[i].inputs.length <= 10</code></li>
|
||||
</ul>
|
||||
|
@@ -1,38 +1,38 @@
|
||||
<p>A <strong>valid cut</strong> in a circle can be:</p>
|
||||
|
||||
<ul>
|
||||
<li>A cut that is represented by a straight line that touches two points on the edge of the circle and passes through its center, or</li>
|
||||
<li>A cut that is represented by a straight line that touches one point on the edge of the circle and its center.</li>
|
||||
</ul>
|
||||
|
||||
<p>Some valid and invalid cuts are shown in the figures below.</p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/29/alldrawio.png" style="width: 450px; height: 174px;" />
|
||||
<p>Given the integer <code>n</code>, return <em>the <strong>minimum</strong> number of cuts needed to divide a circle into </em><code>n</code><em> equal slices</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/24/11drawio.png" style="width: 200px; height: 200px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
The above figure shows how cutting the circle twice through the middle divides it into 4 equal slices.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/24/22drawio.png" style="width: 200px; height: 201px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
At least 3 cuts are needed to divide the circle into 3 equal slices.
|
||||
It can be shown that less than 3 cuts cannot result in 3 slices of equal size and shape.
|
||||
Also note that the first cut will not divide the circle into distinct parts.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
</ul>
|
||||
<p>A <strong>valid cut</strong> in a circle can be:</p>
|
||||
|
||||
<ul>
|
||||
<li>A cut that is represented by a straight line that touches two points on the edge of the circle and passes through its center, or</li>
|
||||
<li>A cut that is represented by a straight line that touches one point on the edge of the circle and its center.</li>
|
||||
</ul>
|
||||
|
||||
<p>Some valid and invalid cuts are shown in the figures below.</p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/29/alldrawio.png" style="width: 450px; height: 174px;" />
|
||||
<p>Given the integer <code>n</code>, return <em>the <strong>minimum</strong> number of cuts needed to divide a circle into </em><code>n</code><em> equal slices</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/24/11drawio.png" style="width: 200px; height: 200px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 4
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
The above figure shows how cutting the circle twice through the middle divides it into 4 equal slices.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/24/22drawio.png" style="width: 200px; height: 201px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
At least 3 cuts are needed to divide the circle into 3 equal slices.
|
||||
It can be shown that less than 3 cuts cannot result in 3 slices of equal size and shape.
|
||||
Also note that the first cut will not divide the circle into distinct parts.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
</ul>
|
||||
|
@@ -1,38 +1,38 @@
|
||||
<p>Given an array of positive integers <code>nums</code>, return <em>an array </em><code>answer</code><em> that consists of the digits of each integer in </em><code>nums</code><em> after separating them in <strong>the same order</strong> they appear in </em><code>nums</code>.</p>
|
||||
|
||||
<p>To separate the digits of an integer is to get all the digits it has in the same order.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, for the integer <code>10921</code>, the separation of its digits is <code>[1,0,9,2,1]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [13,25,83,77]
|
||||
<strong>Output:</strong> [1,3,2,5,8,3,7,7]
|
||||
<strong>Explanation:</strong>
|
||||
- The separation of 13 is [1,3].
|
||||
- The separation of 25 is [2,5].
|
||||
- The separation of 83 is [8,3].
|
||||
- The separation of 77 is [7,7].
|
||||
answer = [1,3,2,5,8,3,7,7]. Note that answer contains the separations in the same order.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [7,1,3,9]
|
||||
<strong>Output:</strong> [7,1,3,9]
|
||||
<strong>Explanation:</strong> The separation of each integer in nums is itself.
|
||||
answer = [7,1,3,9].
|
||||
</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>
|
||||
</ul>
|
||||
<p>Given an array of positive integers <code>nums</code>, return <em>an array </em><code>answer</code><em> that consists of the digits of each integer in </em><code>nums</code><em> after separating them in <strong>the same order</strong> they appear in </em><code>nums</code>.</p>
|
||||
|
||||
<p>To separate the digits of an integer is to get all the digits it has in the same order.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, for the integer <code>10921</code>, the separation of its digits is <code>[1,0,9,2,1]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [13,25,83,77]
|
||||
<strong>Output:</strong> [1,3,2,5,8,3,7,7]
|
||||
<strong>Explanation:</strong>
|
||||
- The separation of 13 is [1,3].
|
||||
- The separation of 25 is [2,5].
|
||||
- The separation of 83 is [8,3].
|
||||
- The separation of 77 is [7,7].
|
||||
answer = [1,3,2,5,8,3,7,7]. Note that answer contains the separations in the same order.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [7,1,3,9]
|
||||
<strong>Output:</strong> [7,1,3,9]
|
||||
<strong>Explanation:</strong> The separation of each integer in nums is itself.
|
||||
answer = [7,1,3,9].
|
||||
</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>
|
||||
</ul>
|
||||
|
@@ -1,39 +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>
|
||||
<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>
|
||||
|
@@ -31,3 +31,6 @@ You need to output 2.
|
||||
<li><code>0 <= s.length <= 3 * 10<sup>4</sup></code></li>
|
||||
<li><code>1 <= g[i], s[j] <= 2<sup>31</sup> - 1</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Note:</strong> This question is the same as <a href="https://leetcode.com/problems/maximum-matching-of-players-with-trainers/description/" target="_blank"> 2410: Maximum Matching of Players With Trainers.</a></p>
|
||||
|
@@ -1,4 +1,6 @@
|
||||
<p>Given an array <code>arr</code> and a chunk size <code>size</code>, return a <strong>chunked</strong> array. A <strong>chunked</strong> array contains the original elements in <code>arr</code>, but consists of subarrays each of length <code>size</code>. The length of the last subarray may be less than <code>size</code> if <code>arr.length</code> is not evenly divisible by <code>size</code>.</p>
|
||||
<p>Given an array <code>arr</code> and a chunk size <code>size</code>, return a <strong>chunked</strong> array.</p>
|
||||
|
||||
<p>A <strong>chunked</strong> array contains the original elements in <code>arr</code>, but consists of subarrays each of length <code>size</code>. The length of the last subarray may be less than <code>size</code> if <code>arr.length</code> is not evenly divisible by <code>size</code>.</p>
|
||||
|
||||
<p>You may assume the array is the output of <code>JSON.parse</code>. In other words, it is valid JSON.</p>
|
||||
|
||||
|
@@ -1,10 +1,10 @@
|
||||
<p>You are given positive integers <code>n</code> and <code>m</code>.</p>
|
||||
|
||||
<p>Define two integers, <code>num1</code> and <code>num2</code>, as follows:</p>
|
||||
<p>Define two integers as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>num1</code>: The sum of all integers in the range <code>[1, n]</code> that are <strong>not divisible</strong> by <code>m</code>.</li>
|
||||
<li><code>num2</code>: The sum of all integers in the range <code>[1, n]</code> that are <strong>divisible</strong> by <code>m</code>.</li>
|
||||
<li><code>num1</code>: The sum of all integers in the range <code>[1, n]</code> (both <strong>inclusive</strong>) that are <strong>not divisible</strong> by <code>m</code>.</li>
|
||||
<li><code>num2</code>: The sum of all integers in the range <code>[1, n]</code> (both <strong>inclusive</strong>) that are <strong>divisible</strong> by <code>m</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the integer</em> <code>num1 - num2</code>.</p>
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user