mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
update
This commit is contained in:
parent
5f3c3f620f
commit
932b731690
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
176
leetcode-cn/originData/apply-operations-to-an-array.json
Normal file
176
leetcode-cn/originData/apply-operations-to-an-array.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
183
leetcode-cn/originData/destroy-sequential-targets.json
Normal file
183
leetcode-cn/originData/destroy-sequential-targets.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
184
leetcode-cn/originData/minimum-total-distance-traveled.json
Normal file
184
leetcode-cn/originData/minimum-total-distance-traveled.json
Normal file
File diff suppressed because one or more lines are too long
195
leetcode-cn/originData/most-popular-video-creator.json
Normal file
195
leetcode-cn/originData/most-popular-video-creator.json
Normal file
File diff suppressed because one or more lines are too long
183
leetcode-cn/originData/odd-string-difference.json
Normal file
183
leetcode-cn/originData/odd-string-difference.json
Normal file
File diff suppressed because one or more lines are too long
190
leetcode-cn/originData/total-cost-to-hire-k-workers.json
Normal file
190
leetcode-cn/originData/total-cost-to-hire-k-workers.json
Normal file
File diff suppressed because one or more lines are too long
@ -0,0 +1,65 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的字符串 <code>street</code> 。<code>street</code> 中每个字符要么是表示房屋的 <code>'H'</code> ,要么是表示空位的 <code>'.'</code> 。</p>
|
||||
|
||||
<p>你可以在 <strong>空位</strong> 放置水桶,从相邻的房屋收集雨水。位置在 <code>i - 1</code> <strong>或者</strong> <code>i + 1</code> 的水桶可以收集位置为 <code>i</code> 处房屋的雨水。一个水桶如果相邻两个位置都有房屋,那么它可以收集 <strong>两个</strong> 房屋的雨水。</p>
|
||||
|
||||
<p>在确保 <strong>每个</strong> 房屋旁边都 <strong>至少</strong> 有一个水桶的前提下,请你返回需要的 <strong>最少</strong> 水桶数。如果无解请返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>street = "H..H"
|
||||
<b>输出:</b>2
|
||||
<strong>解释:</strong>
|
||||
我们可以在下标为 1 和 2 处放水桶。
|
||||
"H..H" -> "HBBH"('B' 表示放置水桶)。
|
||||
下标为 0 处的房屋右边有水桶,下标为 3 处的房屋左边有水桶。
|
||||
所以每个房屋旁边都至少有一个水桶收集雨水。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>street = ".H.H."
|
||||
<b>输出:</b>1
|
||||
<strong>解释:</strong>
|
||||
我们可以在下标为 2 处放置一个水桶。
|
||||
".H.H." -> ".HBH."('B' 表示放置水桶)。
|
||||
下标为 1 处的房屋右边有水桶,下标为 3 处的房屋左边有水桶。
|
||||
所以每个房屋旁边都至少有一个水桶收集雨水。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>street = ".HHH."
|
||||
<b>输出:</b>-1
|
||||
<strong>解释:</strong>
|
||||
没有空位可以放置水桶收集下标为 2 处的雨水。
|
||||
所以没有办法收集所有房屋的雨水。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 4:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>street = "H"
|
||||
<b>输出:</b>-1
|
||||
<strong>解释:</strong>
|
||||
没有空位放置水桶。
|
||||
所以没有办法收集所有房屋的雨水。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 5:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>street = "."
|
||||
<b>输出:</b>0
|
||||
<strong>解释:</strong>
|
||||
没有房屋需要收集雨水。
|
||||
所以需要 0 个水桶。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= street.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>street[i]</code> 要么是 <code>'H'</code> ,要么是 <code>'.'</code> 。</li>
|
||||
</ul>
|
@ -0,0 +1,30 @@
|
||||
<p>给你一个由正整数组成的整数数组 <code>nums</code> ,返回其中可被 <code>3</code> 整除的所有偶数的平均值。</p>
|
||||
|
||||
<p>注意:<code>n</code> 个元素的平均值等于 <code>n</code> 个元素 <strong>求和</strong> 再除以 <code>n</code> ,结果 <strong>向下取整</strong> 到最接近的整数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,3,6,10,12,15]
|
||||
<strong>输出:</strong>9
|
||||
<strong>解释:</strong>6 和 12 是可以被 3 整除的偶数。(6 + 12) / 2 = 9 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,4,7,10]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>不存在满足题目要求的整数,所以返回 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
@ -0,0 +1,50 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的数组 <code>nums</code> ,数组大小为 <code>n</code> ,且由 <strong>非负</strong> 整数组成。</p>
|
||||
|
||||
<p>你需要对数组执行 <code>n - 1</code> 步操作,其中第 <code>i</code> 步操作(从 <strong>0</strong> 开始计数)要求对 <code>nums</code> 中第 <code>i</code> 个元素执行下述指令:</p>
|
||||
|
||||
<ul>
|
||||
<li>如果 <code>nums[i] == nums[i + 1]</code> ,则 <code>nums[i]</code> 的值变成原来的 <code>2</code> 倍,<code>nums[i + 1]</code> 的值变成 <code>0</code> 。否则,跳过这步操作。</li>
|
||||
</ul>
|
||||
|
||||
<p>在执行完 <strong>全部</strong> 操作后,将所有 <code>0</code> <strong>移动</strong> 到数组的 <strong>末尾</strong> 。</p>
|
||||
|
||||
<ul>
|
||||
<li>例如,数组 <code>[1,0,2,0,0,1]</code> 将所有 <code>0</code> 移动到末尾后变为 <code>[1,2,1,0,0,0]</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回结果数组。</p>
|
||||
|
||||
<p><strong>注意</strong> 操作应当 <strong>依次有序</strong> 执行,而不是一次性全部执行。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,2,2,1,1,0]
|
||||
<strong>输出:</strong>[1,4,2,0,0,0]
|
||||
<strong>解释:</strong>执行以下操作:
|
||||
- i = 0: nums[0] 和 nums[1] 不相等,跳过这步操作。
|
||||
- i = 1: nums[1] 和 nums[2] 相等,nums[1] 的值变成原来的 2 倍,nums[2] 的值变成 0 。数组变成 [1,<em><strong>4</strong></em>,<em><strong>0</strong></em>,1,1,0] 。
|
||||
- i = 2: nums[2] 和 nums[3] 不相等,所以跳过这步操作。
|
||||
- i = 3: nums[3] 和 nums[4] 相等,nums[3] 的值变成原来的 2 倍,nums[4] 的值变成 0 。数组变成 [1,4,0,<em><strong>2</strong></em>,<em><strong>0</strong></em>,0] 。
|
||||
- i = 4: nums[4] 和 nums[5] 相等,nums[4] 的值变成原来的 2 倍,nums[5] 的值变成 0 。数组变成 [1,4,0,2,<em><strong>0</strong></em>,<em><strong>0</strong></em>] 。
|
||||
执行完所有操作后,将 0 全部移动到数组末尾,得到结果数组 [1,4,2,0,0,0] 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [0,1]
|
||||
<strong>输出:</strong>[1,0]
|
||||
<strong>解释:</strong>无法执行任何操作,只需要将 0 移动到末尾。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 2000</code></li>
|
||||
<li><code>0 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
@ -0,0 +1,44 @@
|
||||
<p>给你一个字符串数组 <code>words</code> ,每一个字符串长度都相同,令所有字符串的长度都为 <code>n</code> 。</p>
|
||||
|
||||
<p>每个字符串 <code>words[i]</code> 可以被转化为一个长度为 <code>n - 1</code> 的 <strong>差值整数数组</strong> <code>difference[i]</code> ,其中对于 <code>0 <= j <= n - 2</code> 有 <code>difference[i][j] = words[i][j+1] - words[i][j]</code> 。注意两个字母的差值定义为它们在字母表中 <strong>位置</strong> 之差,也就是说 <code>'a'</code> 的位置是 <code>0</code> ,<code>'b'</code> 的位置是 <code>1</code> ,<code>'z'</code> 的位置是 <code>25</code> 。</p>
|
||||
|
||||
<ul>
|
||||
<li>比方说,字符串 <code>"acb"</code> 的差值整数数组是 <code>[2 - 0, 1 - 2] = [2, -1]</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p><code>words</code> 中所有字符串 <strong>除了一个字符串以外</strong> ,其他字符串的差值整数数组都相同。你需要找到那个不同的字符串。</p>
|
||||
|
||||
<p>请你返回<em> </em><code>words</code>中 <strong>差值整数数组</strong> 不同的字符串。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>words = ["adc","wzy","abc"]
|
||||
<b>输出:</b>"abc"
|
||||
<b>解释:</b>
|
||||
- "adc" 的差值整数数组是 [3 - 0, 2 - 3] = [3, -1] 。
|
||||
- "wzy" 的差值整数数组是 [25 - 22, 24 - 25]= [3, -1] 。
|
||||
- "abc" 的差值整数数组是 [1 - 0, 2 - 1] = [1, 1] 。
|
||||
不同的数组是 [1, 1],所以返回对应的字符串,"abc"。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>words = ["aaa","bob","ccc","ddd"]
|
||||
<b>输出:</b>"bob"
|
||||
<b>解释:</b>除了 "bob" 的差值整数数组是 [13, -13] 以外,其他字符串的差值整数数组都是 [0, 0] 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= words.length <= 100</code></li>
|
||||
<li><code>n == words[i].length</code></li>
|
||||
<li><code>2 <= n <= 20</code></li>
|
||||
<li><code>words[i]</code> 只含有小写英文字母。</li>
|
||||
</ul>
|
@ -0,0 +1,42 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的数组 <code>nums</code> ,它包含若干正整数,表示数轴上你需要摧毁的目标所在的位置。同时给你一个整数 <code>space</code> 。</p>
|
||||
|
||||
<p>你有一台机器可以摧毁目标。给机器 <strong>输入</strong> <code>nums[i]</code> ,这台机器会摧毁所有位置在 <code>nums[i] + c * space</code> 的目标,其中 <code>c</code> 是任意非负整数。你想摧毁 <code>nums</code> 中 <strong>尽可能多</strong> 的目标。</p>
|
||||
|
||||
<p>请你返回在摧毁数目最多的前提下,<code>nums[i]</code> 的 <strong>最小值</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [3,7,8,1,1,5], space = 2
|
||||
<b>输出:</b>1
|
||||
<b>解释:</b>如果我们输入 nums[3] ,我们可以摧毁位于 1,3,5,7,9,... 这些位置的目标。
|
||||
这种情况下, 我们总共可以摧毁 5 个目标(除了 nums[2])。
|
||||
没有办法摧毁多于 5 个目标,所以我们返回 nums[3] 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [1,3,5,2,4,6], space = 2
|
||||
<b>输出:</b>1
|
||||
<b>解释:</b>输入 nums[0] 或者 nums[3] 都会摧毁 3 个目标。
|
||||
没有办法摧毁多于 3 个目标。
|
||||
由于 nums[0] 是最小的可以摧毁 3 个目标的整数,所以我们返回 1 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [6,2,5], space = 100
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>无论我们输入哪个数字,都只能摧毁 1 个目标。输入的最小整数是 nums[1] 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= space <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,64 @@
|
||||
<p>X 轴上有一些机器人和工厂。给你一个整数数组 <code>robot</code> ,其中 <code>robot[i]</code> 是第 <code>i</code> 个机器人的位置。再给你一个二维整数数组 <code>factory</code> ,其中 <code>factory[j] = [position<sub>j</sub>, limit<sub>j</sub>]</code> ,表示第 <code>j</code> 个工厂的位置在 <code>position<sub>j</sub></code> ,且第 <code>j</code> 个工厂最多可以修理 <code>limit<sub>j</sub></code> 个机器人。</p>
|
||||
|
||||
<p>每个机器人所在的位置 <strong>互不相同</strong> 。每个工厂所在的位置也 <strong>互不相同</strong> 。注意一个机器人可能一开始跟一个工厂在 <strong>相同的位置</strong> 。</p>
|
||||
|
||||
<p>所有机器人一开始都是坏的,他们会沿着设定的方向一直移动。设定的方向要么是 X 轴的正方向,要么是 X 轴的负方向。当一个机器人经过一个没达到上限的工厂时,这个工厂会维修这个机器人,且机器人停止移动。</p>
|
||||
|
||||
<p><b>任何时刻</b>,你都可以设置 <strong>部分</strong> 机器人的移动方向。你的目标是最小化所有机器人总的移动距离。</p>
|
||||
|
||||
<p>请你返回所有机器人移动的最小总距离。测试数据保证所有机器人都可以被维修。</p>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>所有机器人移动速度相同。</li>
|
||||
<li>如果两个机器人移动方向相同,它们永远不会碰撞。</li>
|
||||
<li>如果两个机器人迎面相遇,它们也不会碰撞,它们彼此之间会擦肩而过。</li>
|
||||
<li>如果一个机器人经过了一个已经达到上限的工厂,机器人会当作工厂不存在,继续移动。</li>
|
||||
<li>机器人从位置 <code>x</code> 到位置 <code>y</code> 的移动距离为 <code>|y - x|</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://pic.leetcode-cn.com/1667542978-utuiPv-image.png" style="width: 500px; height: 320px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>robot = [0,4,6], factory = [[2,2],[6,2]]
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>如上图所示:
|
||||
- 第一个机器人从位置 0 沿着正方向移动,在第一个工厂处维修。
|
||||
- 第二个机器人从位置 4 沿着负方向移动,在第一个工厂处维修。
|
||||
- 第三个机器人在位置 6 被第二个工厂维修,它不需要移动。
|
||||
第一个工厂的维修上限是 2 ,它维修了 2 个机器人。
|
||||
第二个工厂的维修上限是 2 ,它维修了 1 个机器人。
|
||||
总移动距离是 |2 - 0| + |2 - 4| + |6 - 6| = 4 。没有办法得到比 4 更少的总移动距离。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://pic.leetcode-cn.com/1667542984-OAIRFN-image.png" style="width: 500px; height: 329px;" /></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>robot = [1,-1], factory = [[-2,1],[2,1]]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>如上图所示:
|
||||
- 第一个机器人从位置 1 沿着正方向移动,在第二个工厂处维修。
|
||||
- 第二个机器人在位置 -1 沿着负方向移动,在第一个工厂处维修。
|
||||
第一个工厂的维修上限是 1 ,它维修了 1 个机器人。
|
||||
第二个工厂的维修上限是 1 ,它维修了 1 个机器人。
|
||||
总移动距离是 |2 - 1| + |(-2) - (-1)| = 2 。没有办法得到比 2 更少的总移动距离。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= robot.length, factory.length <= 100</code></li>
|
||||
<li><code>factory[j].length == 2</code></li>
|
||||
<li><code>-10<sup>9</sup> <= robot[i], position<sub>j</sub> <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= limit<sub>j</sub> <= robot.length</code></li>
|
||||
<li>测试数据保证所有机器人都可以被维修。</li>
|
||||
</ul>
|
@ -0,0 +1,48 @@
|
||||
<p>给你两个字符串数组 <code>creators</code> 和 <code>ids</code> ,和一个整数数组 <code>views</code> ,所有数组的长度都是 <code>n</code> 。平台上第 <code>i</code> 个视频者是 <code>creator[i]</code> ,视频分配的 id 是 <code>ids[i]</code> ,且播放量为 <code>views[i]</code> 。</p>
|
||||
|
||||
<p>视频创作者的 <strong>流行度</strong> 是该创作者的 <strong>所有</strong> 视频的播放量的 <strong>总和</strong> 。请找出流行度 <strong>最高</strong> 创作者以及该创作者播放量 <strong>最大</strong> 的视频的 id 。</p>
|
||||
|
||||
<ul>
|
||||
<li>如果存在多个创作者流行度都最高,则需要找出所有符合条件的创作者。</li>
|
||||
<li>如果某个创作者存在多个播放量最高的视频,则只需要找出字典序最小的 <code>id</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回一个二维字符串数组<em> </em><code>answer</code><em> </em>,其中<em> </em><code>answer[i] = [creator<sub>i</sub>, id<sub>i</sub>]</code><em> </em>表示<em> </em><code>creator<sub>i</sub></code> 的流行度 <strong>最高</strong> 且其最流行的视频 id 是<em> </em><code>id<sub>i</sub></code><em> </em>,可以按任何顺序返回该结果<em>。</em></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>creators = ["alice","bob","alice","chris"], ids = ["one","two","three","four"], views = [5,10,5,4]
|
||||
<strong>输出:</strong>[["alice","one"],["bob","two"]]
|
||||
<strong>解释:</strong>
|
||||
alice 的流行度是 5 + 5 = 10 。
|
||||
bob 的流行度是 10 。
|
||||
chris 的流行度是 4 。
|
||||
alice 和 bob 是流行度最高的创作者。
|
||||
bob 播放量最高的视频 id 为 "two" 。
|
||||
alice 播放量最高的视频 id 是 "one" 和 "three" 。由于 "one" 的字典序比 "three" 更小,所以结果中返回的 id 是 "one" 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>creators = ["alice","alice","alice"], ids = ["a","b","c"], views = [1,2,2]
|
||||
<strong>输出:</strong>[["alice","b"]]
|
||||
<strong>解释:</strong>
|
||||
id 为 "b" 和 "c" 的视频都满足播放量最高的条件。
|
||||
由于 "b" 的字典序比 "c" 更小,所以结果中返回的 id 是 "b" 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == creators.length == ids.length == views.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= creators[i].length, ids[i].length <= 5</code></li>
|
||||
<li><code>creators[i]</code> 和 <code>ids[i]</code> 仅由小写英文字母组成</li>
|
||||
<li><code>0 <= views[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,58 @@
|
||||
<p>给你一棵 <strong>二叉树</strong> 的根节点 <code>root</code> ,树中有 <code>n</code> 个节点。每个节点都可以被分配一个从 <code>1</code> 到 <code>n</code> 且互不相同的值。另给你一个长度为 <code>m</code> 的数组 <code>queries</code> 。</p>
|
||||
|
||||
<p>你必须在树上执行 <code>m</code> 个 <strong>独立</strong> 的查询,其中第 <code>i</code> 个查询你需要执行以下操作:</p>
|
||||
|
||||
<ul>
|
||||
<li>从树中 <strong>移除</strong> 以 <code>queries[i]</code> 的值作为根节点的子树。题目所用测试用例保证 <code>queries[i]</code> <strong>不</strong> 等于根节点的值。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回一个长度为 <code>m</code> 的数组<em> </em><code>answer</code><em> </em>,其中<em> </em><code>answer[i]</code><em> </em>是执行第 <code>i</code> 个查询后树的高度。</p>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>查询之间是独立的,所以在每个查询执行后,树会回到其 <strong>初始</strong> 状态。</li>
|
||||
<li>树的高度是从根到树中某个节点的 <strong>最长简单路径中的边数</strong> 。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/09/07/binaryytreeedrawio-1.png" style="width: 495px; height: 281px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [1,3,4,2,null,6,5,null,null,null,null,null,7], queries = [4]
|
||||
<strong>输出:</strong>[2]
|
||||
<strong>解释:</strong>上图展示了从树中移除以 4 为根节点的子树。
|
||||
树的高度是 2(路径为 1 -> 3 -> 2)。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/09/07/binaryytreeedrawio-2.png" style="width: 301px; height: 284px;" /></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>root = [5,8,9,2,1,3,7,4,6], queries = [3,2,4,8]
|
||||
<strong>输出:</strong>[3,2,3,2]
|
||||
<strong>解释:</strong>执行下述查询:
|
||||
- 移除以 3 为根节点的子树。树的高度变为 3(路径为 5 -> 8 -> 2 -> 4)。
|
||||
- 移除以 2 为根节点的子树。树的高度变为 2(路径为 5 -> 8 -> 1)。
|
||||
- 移除以 4 为根节点的子树。树的高度变为 3(路径为 5 -> 8 -> 2 -> 6)。
|
||||
- 移除以 8 为根节点的子树。树的高度变为 2(路径为 5 -> 9 -> 3)。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中节点的数目是 <code>n</code></li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= Node.val <= n</code></li>
|
||||
<li>树中的所有值 <strong>互不相同</strong></li>
|
||||
<li><code>m == queries.length</code></li>
|
||||
<li><code>1 <= m <= min(n, 10<sup>4</sup>)</code></li>
|
||||
<li><code>1 <= queries[i] <= n</code></li>
|
||||
<li><code>queries[i] != root.val</code></li>
|
||||
</ul>
|
@ -0,0 +1,37 @@
|
||||
<p>给你两个正整数 <code>n</code> 和 <code>target</code> 。</p>
|
||||
|
||||
<p>如果某个整数每一位上的数字相加小于或等于 <code>target</code> ,则认为这个整数是一个 <strong>美丽整数</strong> 。</p>
|
||||
|
||||
<p>找出并返回满足 <code>n + x</code> 是 <strong>美丽整数</strong> 的最小非负整数 <code>x</code> 。生成的输入保证总可以使 <code>n</code> 变成一个美丽整数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 16, target = 6
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>最初,n 是 16 ,且其每一位数字的和是 1 + 6 = 7 。在加 4 之后,n 变为 20 且每一位数字的和变成 2 + 0 = 2 。可以证明无法加上一个小于 4 的非负整数使 n 变成一个美丽整数。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 467, target = 6
|
||||
<strong>输出:</strong>33
|
||||
<strong>解释:</strong>最初,n 是 467 ,且其每一位数字的和是 4 + 6 + 7 = 17 。在加 33 之后,n 变为 500 且每一位数字的和变成 5 + 0 + 0 = 5 。可以证明无法加上一个小于 33 的非负整数使 n 变成一个美丽整数。</pre>
|
||||
|
||||
<p><strong>示例 3:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>n = 1, target = 1
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>最初,n 是 1 ,且其每一位数字的和是 1 ,已经小于等于 target 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>12</sup></code></li>
|
||||
<li><code>1 <= target <= 150</code></li>
|
||||
<li>生成的输入保证总可以使 <code>n</code> 变成一个美丽整数。</li>
|
||||
</ul>
|
@ -0,0 +1,43 @@
|
||||
<p>给你一个整数数组 <code>nums</code> 和一个整数 <code>k</code> 。请你从 <code>nums</code> 中满足下述条件的全部子数组中找出最大子数组和:</p>
|
||||
|
||||
<ul>
|
||||
<li>子数组的长度是 <code>k</code>,且</li>
|
||||
<li>子数组中的所有元素 <strong>各不相同 。</strong></li>
|
||||
</ul>
|
||||
|
||||
<p>返回满足题面要求的最大子数组和。如果不存在子数组满足这些条件,返回 <code>0</code> 。</p>
|
||||
|
||||
<p><strong>子数组</strong> 是数组中一段连续非空的元素序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [1,5,4,2,9,9,9], k = 3
|
||||
<strong>输出:</strong>15
|
||||
<strong>解释:</strong>nums 中长度为 3 的子数组是:
|
||||
- [1,5,4] 满足全部条件,和为 10 。
|
||||
- [5,4,2] 满足全部条件,和为 11 。
|
||||
- [4,2,9] 满足全部条件,和为 15 。
|
||||
- [2,9,9] 不满足全部条件,因为元素 9 出现重复。
|
||||
- [9,9,9] 不满足全部条件,因为元素 9 出现重复。
|
||||
因为 15 是满足全部条件的所有子数组中的最大子数组和,所以返回 15 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [4,4,4], k = 3
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>nums 中长度为 3 的子数组是:
|
||||
- [4,4,4] 不满足全部条件,因为元素 4 出现重复。
|
||||
因为不存在满足全部条件的子数组,所以返回 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,51 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>costs</code> ,其中 <code>costs[i]</code> 是雇佣第 <code>i</code> 位工人的代价。</p>
|
||||
|
||||
<p>同时给你两个整数 <code>k</code> 和 <code>candidates</code> 。我们想根据以下规则恰好雇佣 <code>k</code> 位工人:</p>
|
||||
|
||||
<ul>
|
||||
<li>总共进行 <code>k</code> 轮雇佣,且每一轮恰好雇佣一位工人。</li>
|
||||
<li>在每一轮雇佣中,从最前面 <code>candidates</code> 和最后面 <code>candidates</code> 人中选出代价最小的一位工人,如果有多位代价相同且最小的工人,选择下标更小的一位工人。
|
||||
<ul>
|
||||
<li>比方说,<code>costs = [3,2,7,7,1,2]</code> 且 <code>candidates = 2</code> ,第一轮雇佣中,我们选择第 <code>4</code> 位工人,因为他的代价最小 <code>[<em>3,2</em>,7,7,<em><strong>1</strong>,2</em>]</code> 。</li>
|
||||
<li>第二轮雇佣,我们选择第 <code>1</code> 位工人,因为他们的代价与第 <code>4</code> 位工人一样都是最小代价,而且下标更小,<code>[<em>3,<strong>2</strong></em>,7,<em>7,2</em>]</code> 。注意每一轮雇佣后,剩余工人的下标可能会发生变化。</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>如果剩余员工数目不足 <code>candidates</code> 人,那么下一轮雇佣他们中代价最小的一人,如果有多位代价相同且最小的工人,选择下标更小的一位工人。</li>
|
||||
<li>一位工人只能被选择一次。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回雇佣恰好<em> </em><code>k</code> 位工人的总代价。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
|
||||
<b>输出:</b>11
|
||||
<b>解释:</b>我们总共雇佣 3 位工人。总代价一开始为 0 。
|
||||
- 第一轮雇佣,我们从 [<strong><em>17,12,10,2</em></strong>,7,<strong><em>2,11,20,8</em></strong>] 中选择。最小代价是 2 ,有两位工人,我们选择下标更小的一位工人,即第 3 位工人。总代价是 0 + 2 = 2 。
|
||||
- 第二轮雇佣,我们从 [<strong><em>17,12,10,7</em></strong>,<strong><em>2,11,20,8</em></strong>] 中选择。最小代价是 2 ,下标为 4 ,总代价是 2 + 2 = 4 。
|
||||
- 第三轮雇佣,我们从 [<strong><em>17,12,10,7,11,20,8</em></strong>] 中选择,最小代价是 7 ,下标为 3 ,总代价是 4 + 7 = 11 。注意下标为 3 的工人同时在最前面和最后面 4 位工人中。
|
||||
总雇佣代价是 11 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>costs = [1,2,4,1], k = 3, candidates = 3
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>我们总共雇佣 3 位工人。总代价一开始为 0 。
|
||||
- 第一轮雇佣,我们从 [<strong><em>1,2,4,1</em></strong>] 中选择。最小代价为 1 ,有两位工人,我们选择下标更小的一位工人,即第 0 位工人,总代价是 0 + 1 = 1 。注意,下标为 1 和 2 的工人同时在最前面和最后面 3 位工人中。
|
||||
- 第二轮雇佣,我们从 [<strong><em>2,4,1</em></strong>] 中选择。最小代价为 1 ,下标为 2 ,总代价是 1 + 1 = 2 。
|
||||
- 第三轮雇佣,少于 3 位工人,我们从剩余工人 [<strong><em>2,4</em></strong>] 中选择。最小代价是 2 ,下标为 0 。总代价为 2 + 2 = 4 。
|
||||
总雇佣代价是 4 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
|
||||
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= k, candidates <= costs.length</code></li>
|
||||
</ul>
|
@ -0,0 +1,44 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>hamsters</code> where <code>hamsters[i]</code> is either:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>'H'</code> indicating that there is a hamster at index <code>i</code>, or</li>
|
||||
<li><code>'.'</code> indicating that index <code>i</code> is empty.</li>
|
||||
</ul>
|
||||
|
||||
<p>You will add some number of food buckets at the empty indices in order to feed the hamsters. A hamster can be fed if there is at least one food bucket to its left or to its right. More formally, a hamster at index <code>i</code> can be fed if you place a food bucket at index <code>i - 1</code> <strong>and/or</strong> at index <code>i + 1</code>.</p>
|
||||
|
||||
<p>Return <em>the minimum number of food buckets you should <strong>place at empty indices</strong> to feed all the hamsters or </em><code>-1</code><em> if it is impossible to feed all of them</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/11/01/example1.png" style="width: 482px; height: 162px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> hamsters = "H..H"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We place two food buckets at indices 1 and 2.
|
||||
It can be shown that if we place only one food bucket, one of the hamsters will not be fed.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/11/01/example2.png" style="width: 602px; height: 162px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> hamsters = ".H.H."
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We place one food bucket at index 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/11/01/example3.png" style="width: 602px; height: 162px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> hamsters = ".HHH."
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> If we place a food bucket at every empty index as shown, the hamster at index 2 will not be able to eat.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= hamsters.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>hamsters[i]</code> is either<code>'H'</code> or <code>'.'</code>.</li>
|
||||
</ul>
|
@ -0,0 +1,28 @@
|
||||
<p>Given an integer array <code>nums</code> of <strong>positive</strong> integers, return <em>the average value of all even integers that are divisible by</em> <code>3</code><i>.</i></p>
|
||||
|
||||
<p>Note that the <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,6,10,12,15]
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> 6 and 12 are even numbers that are divisible by 3. (6 + 12) / 2 = 9.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,4,7,10]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There is no single number that satisfies the requirement, so return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
@ -0,0 +1,48 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of size <code>n</code> consisting of <strong>non-negative</strong> integers.</p>
|
||||
|
||||
<p>You need to apply <code>n - 1</code> operations to this array where, in the <code>i<sup>th</sup></code> operation (<strong>0-indexed</strong>), you will apply the following on the <code>i<sup>th</sup></code> element of <code>nums</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li>If <code>nums[i] == nums[i + 1]</code>, then multiply <code>nums[i]</code> by <code>2</code> and set <code>nums[i + 1]</code> to <code>0</code>. Otherwise, you skip this operation.</li>
|
||||
</ul>
|
||||
|
||||
<p>After performing <strong>all</strong> the operations, <strong>shift</strong> all the <code>0</code>'s to the <strong>end</strong> of the array.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, the array <code>[1,0,2,0,0,1]</code> after shifting all its <code>0</code>'s to the end, is <code>[1,2,1,0,0,0]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the resulting array</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that the operations are applied <strong>sequentially</strong>, not all at once.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,2,1,1,0]
|
||||
<strong>Output:</strong> [1,4,2,0,0,0]
|
||||
<strong>Explanation:</strong> We do the following operations:
|
||||
- i = 0: nums[0] and nums[1] are not equal, so we skip this operation.
|
||||
- i = 1: nums[1] and nums[2] are equal, we multiply nums[1] by 2 and change nums[2] to 0. The array becomes [1,<strong><u>4</u></strong>,<strong><u>0</u></strong>,1,1,0].
|
||||
- i = 2: nums[2] and nums[3] are not equal, so we skip this operation.
|
||||
- i = 3: nums[3] and nums[4] are equal, we multiply nums[3] by 2 and change nums[4] to 0. The array becomes [1,4,0,<strong><u>2</u></strong>,<strong><u>0</u></strong>,0].
|
||||
- i = 4: nums[4] and nums[5] are equal, we multiply nums[4] by 2 and change nums[5] to 0. The array becomes [1,4,0,2,<strong><u>0</u></strong>,<strong><u>0</u></strong>].
|
||||
After that, we shift the 0's to the end, which gives the array [1,4,2,0,0,0].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,1]
|
||||
<strong>Output:</strong> [1,0]
|
||||
<strong>Explanation:</strong> No operation can be applied, we just shift the 0 to the end.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 2000</code></li>
|
||||
<li><code>0 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
@ -0,0 +1,42 @@
|
||||
<p>You are given an array of equal-length strings <code>words</code>. Assume that the length of each string is <code>n</code>.</p>
|
||||
|
||||
<p>Each string <code>words[i]</code> can be converted into a <strong>difference integer array</strong> <code>difference[i]</code> of length <code>n - 1</code> where <code>difference[i][j] = words[i][j+1] - words[i][j]</code> where <code>0 <= j <= n - 2</code>. Note that the difference between two letters is the difference between their <strong>positions</strong> in the alphabet i.e. the position of <code>'a'</code> is <code>0</code>, <code>'b'</code> is <code>1</code>, and <code>'z'</code> is <code>25</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, for the string <code>"acb"</code>, the difference integer array is <code>[2 - 0, 1 - 2] = [2, -1]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>All the strings in words have the same difference integer array, <strong>except one</strong>. You should find that string.</p>
|
||||
|
||||
<p>Return<em> the string in </em><code>words</code><em> that has different <strong>difference integer array</strong>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["adc","wzy","abc"]
|
||||
<strong>Output:</strong> "abc"
|
||||
<strong>Explanation:</strong>
|
||||
- The difference integer array of "adc" is [3 - 0, 2 - 3] = [3, -1].
|
||||
- The difference integer array of "wzy" is [25 - 22, 24 - 25]= [3, -1].
|
||||
- The difference integer array of "abc" is [1 - 0, 2 - 1] = [1, 1].
|
||||
The odd array out is [1, 1], so we return the corresponding string, "abc".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["aaa","bob","ccc","ddd"]
|
||||
<strong>Output:</strong> "bob"
|
||||
<strong>Explanation:</strong> All the integer arrays are [0, 0] except for "bob", which corresponds to [13, -13].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= words.length <= 100</code></li>
|
||||
<li><code>n == words[i].length</code></li>
|
||||
<li><code>2 <= n <= 20</code></li>
|
||||
<li><code>words[i]</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
@ -0,0 +1,43 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of positive integers, representing targets on a number line. You are also given an integer <code>space</code>.</p>
|
||||
|
||||
<p>You have a machine which can destroy targets. <strong>Seeding</strong> the machine with some <code>nums[i]</code> allows it to destroy all targets with values that can be represented as <code>nums[i] + c * space</code>, where <code>c</code> is any non-negative integer. You want to destroy the <strong>maximum</strong> number of targets in <code>nums</code>.</p>
|
||||
|
||||
<p>Return<em> the <strong>minimum value</strong> of </em><code>nums[i]</code><em> you can seed the machine with to destroy the maximum number of targets.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,7,8,1,1,5], space = 2
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> If we seed the machine with nums[3], then we destroy all targets equal to 1,3,5,7,9,...
|
||||
In this case, we would destroy 5 total targets (all except for nums[2]).
|
||||
It is impossible to destroy more than 5 targets, so we return nums[3].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,5,2,4,6], space = 2
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> Seeding the machine with nums[0], or nums[3] destroys 3 targets.
|
||||
It is not possible to destroy more than 3 targets.
|
||||
Since nums[0] is the minimal integer that can destroy 3 targets, we return 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [6,2,5], space = 100
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Whatever initial seed we select, we can only destroy 1 target. The minimal seed is nums[1].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= space <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,58 @@
|
||||
<p>There are some robots and factories on the X-axis. You are given an integer array <code>robot</code> where <code>robot[i]</code> is the position of the <code>i<sup>th</sup></code> robot. You are also given a 2D integer array <code>factory</code> where <code>factory[j] = [position<sub>j</sub>, limit<sub>j</sub>]</code> indicates that <code>position<sub>j</sub></code> is the position of the <code>j<sup>th</sup></code> factory and that the <code>j<sup>th</sup></code> factory can repair at most <code>limit<sub>j</sub></code> robots.</p>
|
||||
|
||||
<p>The positions of each robot are <strong>unique</strong>. The positions of each factory are also <strong>unique</strong>. Note that a robot can be <strong>in the same position</strong> as a factory initially.</p>
|
||||
|
||||
<p>All the robots are initially broken; they keep moving in one direction. The direction could be the negative or the positive direction of the X-axis. When a robot reaches a factory that did not reach its limit, the factory repairs the robot, and it stops moving.</p>
|
||||
|
||||
<p><strong>At any moment</strong>, you can set the initial direction of moving for <strong>some</strong> robot. Your target is to minimize the total distance traveled by all the robots.</p>
|
||||
|
||||
<p>Return <em>the minimum total distance traveled by all the robots</em>. The test cases are generated such that all the robots can be repaired.</p>
|
||||
|
||||
<p><strong>Note that</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>All robots move at the same speed.</li>
|
||||
<li>If two robots move in the same direction, they will never collide.</li>
|
||||
<li>If two robots move in opposite directions and they meet at some point, they do not collide. They cross each other.</li>
|
||||
<li>If a robot passes by a factory that reached its limits, it crosses it as if it does not exist.</li>
|
||||
<li>If the robot moved from a position <code>x</code> to a position <code>y</code>, the distance it moved is <code>|y - x|</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/09/15/example1.jpg" style="width: 500px; height: 320px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> robot = [0,4,6], factory = [[2,2],[6,2]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> As shown in the figure:
|
||||
- The first robot at position 0 moves in the positive direction. It will be repaired at the first factory.
|
||||
- The second robot at position 4 moves in the negative direction. It will be repaired at the first factory.
|
||||
- The third robot at position 6 will be repaired at the second factory. It does not need to move.
|
||||
The limit of the first factory is 2, and it fixed 2 robots.
|
||||
The limit of the second factory is 2, and it fixed 1 robot.
|
||||
The total distance is |2 - 0| + |2 - 4| + |6 - 6| = 4. It can be shown that we cannot achieve a better total distance than 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/09/15/example-2.jpg" style="width: 500px; height: 329px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> robot = [1,-1], factory = [[-2,1],[2,1]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> As shown in the figure:
|
||||
- The first robot at position 1 moves in the positive direction. It will be repaired at the second factory.
|
||||
- The second robot at position -1 moves in the negative direction. It will be repaired at the first factory.
|
||||
The limit of the first factory is 1, and it fixed 1 robot.
|
||||
The limit of the second factory is 1, and it fixed 1 robot.
|
||||
The total distance is |2 - 1| + |(-2) - (-1)| = 2. It can be shown that we cannot achieve a better total distance than 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= robot.length, factory.length <= 100</code></li>
|
||||
<li><code>factory[j].length == 2</code></li>
|
||||
<li><code>-10<sup>9</sup> <= robot[i], position<sub>j</sub> <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= limit<sub>j</sub> <= robot.length</code></li>
|
||||
<li>The input will be generated such that it is always possible to repair every robot.</li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>You are given two string arrays <code>creators</code> and <code>ids</code>, and an integer array <code>views</code>, all of length <code>n</code>. The <code>i<sup>th</sup></code> video on a platform was created by <code>creator[i]</code>, has an id of <code>ids[i]</code>, and has <code>views[i]</code> views.</p>
|
||||
|
||||
<p>The <strong>popularity</strong> of a creator is the <strong>sum</strong> of the number of views on <strong>all</strong> of the creator's videos. Find the creator with the <strong>highest</strong> popularity and the id of their <strong>most</strong> viewed video.</p>
|
||||
|
||||
<ul>
|
||||
<li>If multiple creators have the highest popularity, find all of them.</li>
|
||||
<li>If multiple videos have the highest view count for a creator, find the lexicographically <strong>smallest</strong> id.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> a 2D array of strings </em><code>answer</code><em> where </em><code>answer[i] = [creator<sub>i</sub>, id<sub>i</sub>]</code><em> means that </em><code>creator<sub>i</sub></code> <em>has the <strong>highest</strong> popularity and </em><code>id<sub>i</sub></code><em> is the id of their most popular video.</em> The answer can be returned in any order.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> creators = ["alice","bob","alice","chris"], ids = ["one","two","three","four"], views = [5,10,5,4]
|
||||
<strong>Output:</strong> [["alice","one"],["bob","two"]]
|
||||
<strong>Explanation:</strong>
|
||||
The popularity of alice is 5 + 5 = 10.
|
||||
The popularity of bob is 10.
|
||||
The popularity of chris is 4.
|
||||
alice and bob are the most popular creators.
|
||||
For bob, the video with the highest view count is "two".
|
||||
For alice, the videos with the highest view count are "one" and "three". Since "one" is lexicographically smaller than "three", it is included in the answer.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> creators = ["alice","alice","alice"], ids = ["a","b","c"], views = [1,2,2]
|
||||
<strong>Output:</strong> [["alice","b"]]
|
||||
<strong>Explanation:</strong>
|
||||
The videos with id "b" and "c" have the highest view count.
|
||||
Since "b" is lexicographically smaller than "c", it is included in the answer.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == creators.length == ids.length == views.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= creators[i].length, ids[i].length <= 5</code></li>
|
||||
<li><code>creators[i]</code> and <code>ids[i]</code> consist only of lowercase English letters.</li>
|
||||
<li><code>0 <= views[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,52 @@
|
||||
<p>You are given the <code>root</code> of a <strong>binary tree</strong> with <code>n</code> nodes. Each node is assigned a unique value from <code>1</code> to <code>n</code>. You are also given an array <code>queries</code> of size <code>m</code>.</p>
|
||||
|
||||
<p>You have to perform <code>m</code> <strong>independent</strong> queries on the tree where in the <code>i<sup>th</sup></code> query you do the following:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Remove</strong> the subtree rooted at the node with the value <code>queries[i]</code> from the tree. It is <strong>guaranteed</strong> that <code>queries[i]</code> will <strong>not</strong> be equal to the value of the root.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an array </em><code>answer</code><em> of size </em><code>m</code><em> where </em><code>answer[i]</code><em> is the height of the tree after performing the </em><code>i<sup>th</sup></code><em> query</em>.</p>
|
||||
|
||||
<p><strong>Note</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>The queries are independent, so the tree returns to its <strong>initial</strong> state after each query.</li>
|
||||
<li>The height of a tree is the <strong>number of edges in the longest simple path</strong> from the root to some node in the tree.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/09/07/binaryytreeedrawio-1.png" style="width: 495px; height: 281px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,3,4,2,null,6,5,null,null,null,null,null,7], queries = [4]
|
||||
<strong>Output:</strong> [2]
|
||||
<strong>Explanation:</strong> The diagram above shows the tree after removing the subtree rooted at node with value 4.
|
||||
The height of the tree is 2 (The path 1 -> 3 -> 2).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/09/07/binaryytreeedrawio-2.png" style="width: 301px; height: 284px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [5,8,9,2,1,3,7,4,6], queries = [3,2,4,8]
|
||||
<strong>Output:</strong> [3,2,3,2]
|
||||
<strong>Explanation:</strong> We have the following queries:
|
||||
- Removing the subtree rooted at node with value 3. The height of the tree becomes 3 (The path 5 -> 8 -> 2 -> 4).
|
||||
- Removing the subtree rooted at node with value 2. The height of the tree becomes 2 (The path 5 -> 8 -> 1).
|
||||
- Removing the subtree rooted at node with value 4. The height of the tree becomes 3 (The path 5 -> 8 -> 2 -> 6).
|
||||
- Removing the subtree rooted at node with value 8. The height of the tree becomes 2 (The path 5 -> 9 -> 3).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is <code>n</code>.</li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= Node.val <= n</code></li>
|
||||
<li>All the values in the tree are <strong>unique</strong>.</li>
|
||||
<li><code>m == queries.length</code></li>
|
||||
<li><code>1 <= m <= min(n, 10<sup>4</sup>)</code></li>
|
||||
<li><code>1 <= queries[i] <= n</code></li>
|
||||
<li><code>queries[i] != root.val</code></li>
|
||||
</ul>
|
@ -0,0 +1,39 @@
|
||||
<p>You are given two positive integers <code>n</code> and <code>target</code>.</p>
|
||||
|
||||
<p>An integer is considered <strong>beautiful</strong> if the sum of its digits is less than or equal to <code>target</code>.</p>
|
||||
|
||||
<p>Return the <em>minimum <strong>non-negative</strong> integer </em><code>x</code><em> such that </em><code>n + x</code><em> is beautiful</em>. The input will be generated such that it is always possible to make <code>n</code> beautiful.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 16, target = 6
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> Initially n is 16 and its digit sum is 1 + 6 = 7. After adding 4, n becomes 20 and digit sum becomes 2 + 0 = 2. It can be shown that we can not make n beautiful with adding non-negative integer less than 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 467, target = 6
|
||||
<strong>Output:</strong> 33
|
||||
<strong>Explanation:</strong> Initially n is 467 and its digit sum is 4 + 6 + 7 = 17. After adding 33, n becomes 500 and digit sum becomes 5 + 0 + 0 = 5. It can be shown that we can not make n beautiful with adding non-negative integer less than 33.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1, target = 1
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Initially n is 1 and its digit sum is 1, which is already smaller than or equal to target.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>12</sup></code></li>
|
||||
<li><code>1 <= target <= 150</code></li>
|
||||
<li>The input will be generated such that it is always possible to make <code>n</code> beautiful.</li>
|
||||
</ul>
|
@ -0,0 +1,43 @@
|
||||
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>. Find the maximum subarray sum of all the subarrays of <code>nums</code> that meet the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>The length of the subarray is <code>k</code>, and</li>
|
||||
<li>All the elements of the subarray are <strong>distinct</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the maximum subarray sum of all the subarrays that meet the conditions</em><em>.</em> If no subarray meets the conditions, return <code>0</code>.</p>
|
||||
|
||||
<p><em>A <strong>subarray</strong> is a contiguous non-empty sequence of elements within an array.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,5,4,2,9,9,9], k = 3
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> The subarrays of nums with length 3 are:
|
||||
- [1,5,4] which meets the requirements and has a sum of 10.
|
||||
- [5,4,2] which meets the requirements and has a sum of 11.
|
||||
- [4,2,9] which meets the requirements and has a sum of 15.
|
||||
- [2,9,9] which does not meet the requirements because the element 9 is repeated.
|
||||
- [9,9,9] which does not meet the requirements because the element 9 is repeated.
|
||||
We return 15 because it is the maximum subarray sum of all the subarrays that meet the conditions
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,4,4], k = 3
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The subarrays of nums with length 3 are:
|
||||
- [4,4,4] which does not meet the requirements because the element 4 is repeated.
|
||||
We return 0 because no subarrays meet the conditions.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,51 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
|
||||
|
||||
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
|
||||
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
|
||||
<ul>
|
||||
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
|
||||
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
|
||||
<li>A worker can only be chosen once.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
|
||||
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
|
||||
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
|
||||
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
|
||||
The total hiring cost is 11.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
|
||||
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
|
||||
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
|
||||
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
|
||||
The total hiring cost is 4.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
|
||||
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= k, candidates <= costs.length</code></li>
|
||||
</ul>
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
173
leetcode/originData/apply-operations-to-an-array.json
Normal file
173
leetcode/originData/apply-operations-to-an-array.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
180
leetcode/originData/destroy-sequential-targets.json
Normal file
180
leetcode/originData/destroy-sequential-targets.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
181
leetcode/originData/minimum-total-distance-traveled.json
Normal file
181
leetcode/originData/minimum-total-distance-traveled.json
Normal file
File diff suppressed because one or more lines are too long
192
leetcode/originData/most-popular-video-creator.json
Normal file
192
leetcode/originData/most-popular-video-creator.json
Normal file
File diff suppressed because one or more lines are too long
199
leetcode/originData/next-greater-element-iv.json
Normal file
199
leetcode/originData/next-greater-element-iv.json
Normal file
File diff suppressed because one or more lines are too long
180
leetcode/originData/odd-string-difference.json
Normal file
180
leetcode/originData/odd-string-difference.json
Normal file
File diff suppressed because one or more lines are too long
187
leetcode/originData/total-cost-to-hire-k-workers.json
Normal file
187
leetcode/originData/total-cost-to-hire-k-workers.json
Normal file
File diff suppressed because one or more lines are too long
175
leetcode/originData/words-within-two-edits-of-dictionary.json
Normal file
175
leetcode/originData/words-within-two-edits-of-dictionary.json
Normal file
File diff suppressed because one or more lines are too long
48
leetcode/problem/apply-operations-to-an-array.html
Normal file
48
leetcode/problem/apply-operations-to-an-array.html
Normal file
@ -0,0 +1,48 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of size <code>n</code> consisting of <strong>non-negative</strong> integers.</p>
|
||||
|
||||
<p>You need to apply <code>n - 1</code> operations to this array where, in the <code>i<sup>th</sup></code> operation (<strong>0-indexed</strong>), you will apply the following on the <code>i<sup>th</sup></code> element of <code>nums</code>:</p>
|
||||
|
||||
<ul>
|
||||
<li>If <code>nums[i] == nums[i + 1]</code>, then multiply <code>nums[i]</code> by <code>2</code> and set <code>nums[i + 1]</code> to <code>0</code>. Otherwise, you skip this operation.</li>
|
||||
</ul>
|
||||
|
||||
<p>After performing <strong>all</strong> the operations, <strong>shift</strong> all the <code>0</code>'s to the <strong>end</strong> of the array.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, the array <code>[1,0,2,0,0,1]</code> after shifting all its <code>0</code>'s to the end, is <code>[1,2,1,0,0,0]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the resulting array</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that the operations are applied <strong>sequentially</strong>, not all at once.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,2,1,1,0]
|
||||
<strong>Output:</strong> [1,4,2,0,0,0]
|
||||
<strong>Explanation:</strong> We do the following operations:
|
||||
- i = 0: nums[0] and nums[1] are not equal, so we skip this operation.
|
||||
- i = 1: nums[1] and nums[2] are equal, we multiply nums[1] by 2 and change nums[2] to 0. The array becomes [1,<strong><u>4</u></strong>,<strong><u>0</u></strong>,1,1,0].
|
||||
- i = 2: nums[2] and nums[3] are not equal, so we skip this operation.
|
||||
- i = 3: nums[3] and nums[4] are equal, we multiply nums[3] by 2 and change nums[4] to 0. The array becomes [1,4,0,<strong><u>2</u></strong>,<strong><u>0</u></strong>,0].
|
||||
- i = 4: nums[4] and nums[5] are equal, we multiply nums[4] by 2 and change nums[5] to 0. The array becomes [1,4,0,2,<strong><u>0</u></strong>,<strong><u>0</u></strong>].
|
||||
After that, we shift the 0's to the end, which gives the array [1,4,2,0,0,0].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [0,1]
|
||||
<strong>Output:</strong> [1,0]
|
||||
<strong>Explanation:</strong> No operation can be applied, we just shift the 0 to the end.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 2000</code></li>
|
||||
<li><code>0 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
@ -0,0 +1,28 @@
|
||||
<p>Given an integer array <code>nums</code> of <strong>positive</strong> integers, return <em>the average value of all even integers that are divisible by</em> <code>3</code><i>.</i></p>
|
||||
|
||||
<p>Note that the <strong>average</strong> of <code>n</code> elements is the <strong>sum</strong> of the <code>n</code> elements divided by <code>n</code> and <strong>rounded down</strong> to the nearest integer.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,6,10,12,15]
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> 6 and 12 are even numbers that are divisible by 3. (6 + 12) / 2 = 9.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,2,4,7,10]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There is no single number that satisfies the requirement, so return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i] <= 1000</code></li>
|
||||
</ul>
|
43
leetcode/problem/destroy-sequential-targets.html
Normal file
43
leetcode/problem/destroy-sequential-targets.html
Normal file
@ -0,0 +1,43 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> consisting of positive integers, representing targets on a number line. You are also given an integer <code>space</code>.</p>
|
||||
|
||||
<p>You have a machine which can destroy targets. <strong>Seeding</strong> the machine with some <code>nums[i]</code> allows it to destroy all targets with values that can be represented as <code>nums[i] + c * space</code>, where <code>c</code> is any non-negative integer. You want to destroy the <strong>maximum</strong> number of targets in <code>nums</code>.</p>
|
||||
|
||||
<p>Return<em> the <strong>minimum value</strong> of </em><code>nums[i]</code><em> you can seed the machine with to destroy the maximum number of targets.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,7,8,1,1,5], space = 2
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> If we seed the machine with nums[3], then we destroy all targets equal to 1,3,5,7,9,...
|
||||
In this case, we would destroy 5 total targets (all except for nums[2]).
|
||||
It is impossible to destroy more than 5 targets, so we return nums[3].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,5,2,4,6], space = 2
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> Seeding the machine with nums[0], or nums[3] destroys 3 targets.
|
||||
It is not possible to destroy more than 3 targets.
|
||||
Since nums[0] is the minimal integer that can destroy 3 targets, we return 1.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [6,2,5], space = 100
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Whatever initial seed we select, we can only destroy 1 target. The minimal seed is nums[1].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
<li><code>1 <= space <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,52 @@
|
||||
<p>You are given the <code>root</code> of a <strong>binary tree</strong> with <code>n</code> nodes. Each node is assigned a unique value from <code>1</code> to <code>n</code>. You are also given an array <code>queries</code> of size <code>m</code>.</p>
|
||||
|
||||
<p>You have to perform <code>m</code> <strong>independent</strong> queries on the tree where in the <code>i<sup>th</sup></code> query you do the following:</p>
|
||||
|
||||
<ul>
|
||||
<li><strong>Remove</strong> the subtree rooted at the node with the value <code>queries[i]</code> from the tree. It is <strong>guaranteed</strong> that <code>queries[i]</code> will <strong>not</strong> be equal to the value of the root.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an array </em><code>answer</code><em> of size </em><code>m</code><em> where </em><code>answer[i]</code><em> is the height of the tree after performing the </em><code>i<sup>th</sup></code><em> query</em>.</p>
|
||||
|
||||
<p><strong>Note</strong>:</p>
|
||||
|
||||
<ul>
|
||||
<li>The queries are independent, so the tree returns to its <strong>initial</strong> state after each query.</li>
|
||||
<li>The height of a tree is the <strong>number of edges in the longest simple path</strong> from the root to some node in the tree.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/09/07/binaryytreeedrawio-1.png" style="width: 495px; height: 281px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,3,4,2,null,6,5,null,null,null,null,null,7], queries = [4]
|
||||
<strong>Output:</strong> [2]
|
||||
<strong>Explanation:</strong> The diagram above shows the tree after removing the subtree rooted at node with value 4.
|
||||
The height of the tree is 2 (The path 1 -> 3 -> 2).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/09/07/binaryytreeedrawio-2.png" style="width: 301px; height: 284px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [5,8,9,2,1,3,7,4,6], queries = [3,2,4,8]
|
||||
<strong>Output:</strong> [3,2,3,2]
|
||||
<strong>Explanation:</strong> We have the following queries:
|
||||
- Removing the subtree rooted at node with value 3. The height of the tree becomes 3 (The path 5 -> 8 -> 2 -> 4).
|
||||
- Removing the subtree rooted at node with value 2. The height of the tree becomes 2 (The path 5 -> 8 -> 1).
|
||||
- Removing the subtree rooted at node with value 4. The height of the tree becomes 3 (The path 5 -> 8 -> 2 -> 6).
|
||||
- Removing the subtree rooted at node with value 8. The height of the tree becomes 2 (The path 5 -> 9 -> 3).
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is <code>n</code>.</li>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= Node.val <= n</code></li>
|
||||
<li>All the values in the tree are <strong>unique</strong>.</li>
|
||||
<li><code>m == queries.length</code></li>
|
||||
<li><code>1 <= m <= min(n, 10<sup>4</sup>)</code></li>
|
||||
<li><code>1 <= queries[i] <= n</code></li>
|
||||
<li><code>queries[i] != root.val</code></li>
|
||||
</ul>
|
@ -0,0 +1,43 @@
|
||||
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>. Find the maximum subarray sum of all the subarrays of <code>nums</code> that meet the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>The length of the subarray is <code>k</code>, and</li>
|
||||
<li>All the elements of the subarray are <strong>distinct</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the maximum subarray sum of all the subarrays that meet the conditions</em><em>.</em> If no subarray meets the conditions, return <code>0</code>.</p>
|
||||
|
||||
<p><em>A <strong>subarray</strong> is a contiguous non-empty sequence of elements within an array.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,5,4,2,9,9,9], k = 3
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> The subarrays of nums with length 3 are:
|
||||
- [1,5,4] which meets the requirements and has a sum of 10.
|
||||
- [5,4,2] which meets the requirements and has a sum of 11.
|
||||
- [4,2,9] which meets the requirements and has a sum of 15.
|
||||
- [2,9,9] which does not meet the requirements because the element 9 is repeated.
|
||||
- [9,9,9] which does not meet the requirements because the element 9 is repeated.
|
||||
We return 15 because it is the maximum subarray sum of all the subarrays that meet the conditions
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,4,4], k = 3
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> The subarrays of nums with length 3 are:
|
||||
- [4,4,4] which does not meet the requirements because the element 4 is repeated.
|
||||
We return 0 because no subarrays meet the conditions.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,39 @@
|
||||
<p>You are given two positive integers <code>n</code> and <code>target</code>.</p>
|
||||
|
||||
<p>An integer is considered <strong>beautiful</strong> if the sum of its digits is less than or equal to <code>target</code>.</p>
|
||||
|
||||
<p>Return the <em>minimum <strong>non-negative</strong> integer </em><code>x</code><em> such that </em><code>n + x</code><em> is beautiful</em>. The input will be generated such that it is always possible to make <code>n</code> beautiful.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 16, target = 6
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> Initially n is 16 and its digit sum is 1 + 6 = 7. After adding 4, n becomes 20 and digit sum becomes 2 + 0 = 2. It can be shown that we can not make n beautiful with adding non-negative integer less than 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 467, target = 6
|
||||
<strong>Output:</strong> 33
|
||||
<strong>Explanation:</strong> Initially n is 467 and its digit sum is 4 + 6 + 7 = 17. After adding 33, n becomes 500 and digit sum becomes 5 + 0 + 0 = 5. It can be shown that we can not make n beautiful with adding non-negative integer less than 33.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1, target = 1
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Initially n is 1 and its digit sum is 1, which is already smaller than or equal to target.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n <= 10<sup>12</sup></code></li>
|
||||
<li><code>1 <= target <= 150</code></li>
|
||||
<li>The input will be generated such that it is always possible to make <code>n</code> beautiful.</li>
|
||||
</ul>
|
@ -0,0 +1,44 @@
|
||||
<p>You are given a <strong>0-indexed</strong> string <code>hamsters</code> where <code>hamsters[i]</code> is either:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>'H'</code> indicating that there is a hamster at index <code>i</code>, or</li>
|
||||
<li><code>'.'</code> indicating that index <code>i</code> is empty.</li>
|
||||
</ul>
|
||||
|
||||
<p>You will add some number of food buckets at the empty indices in order to feed the hamsters. A hamster can be fed if there is at least one food bucket to its left or to its right. More formally, a hamster at index <code>i</code> can be fed if you place a food bucket at index <code>i - 1</code> <strong>and/or</strong> at index <code>i + 1</code>.</p>
|
||||
|
||||
<p>Return <em>the minimum number of food buckets you should <strong>place at empty indices</strong> to feed all the hamsters or </em><code>-1</code><em> if it is impossible to feed all of them</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/11/01/example1.png" style="width: 482px; height: 162px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> hamsters = "H..H"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We place two food buckets at indices 1 and 2.
|
||||
It can be shown that if we place only one food bucket, one of the hamsters will not be fed.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/11/01/example2.png" style="width: 602px; height: 162px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> hamsters = ".H.H."
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We place one food bucket at index 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/11/01/example3.png" style="width: 602px; height: 162px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> hamsters = ".HHH."
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> If we place a food bucket at every empty index as shown, the hamster at index 2 will not be able to eat.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= hamsters.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>hamsters[i]</code> is either<code>'H'</code> or <code>'.'</code>.</li>
|
||||
</ul>
|
58
leetcode/problem/minimum-total-distance-traveled.html
Normal file
58
leetcode/problem/minimum-total-distance-traveled.html
Normal file
@ -0,0 +1,58 @@
|
||||
<p>There are some robots and factories on the X-axis. You are given an integer array <code>robot</code> where <code>robot[i]</code> is the position of the <code>i<sup>th</sup></code> robot. You are also given a 2D integer array <code>factory</code> where <code>factory[j] = [position<sub>j</sub>, limit<sub>j</sub>]</code> indicates that <code>position<sub>j</sub></code> is the position of the <code>j<sup>th</sup></code> factory and that the <code>j<sup>th</sup></code> factory can repair at most <code>limit<sub>j</sub></code> robots.</p>
|
||||
|
||||
<p>The positions of each robot are <strong>unique</strong>. The positions of each factory are also <strong>unique</strong>. Note that a robot can be <strong>in the same position</strong> as a factory initially.</p>
|
||||
|
||||
<p>All the robots are initially broken; they keep moving in one direction. The direction could be the negative or the positive direction of the X-axis. When a robot reaches a factory that did not reach its limit, the factory repairs the robot, and it stops moving.</p>
|
||||
|
||||
<p><strong>At any moment</strong>, you can set the initial direction of moving for <strong>some</strong> robot. Your target is to minimize the total distance traveled by all the robots.</p>
|
||||
|
||||
<p>Return <em>the minimum total distance traveled by all the robots</em>. The test cases are generated such that all the robots can be repaired.</p>
|
||||
|
||||
<p><strong>Note that</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>All robots move at the same speed.</li>
|
||||
<li>If two robots move in the same direction, they will never collide.</li>
|
||||
<li>If two robots move in opposite directions and they meet at some point, they do not collide. They cross each other.</li>
|
||||
<li>If a robot passes by a factory that reached its limits, it crosses it as if it does not exist.</li>
|
||||
<li>If the robot moved from a position <code>x</code> to a position <code>y</code>, the distance it moved is <code>|y - x|</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/09/15/example1.jpg" style="width: 500px; height: 320px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> robot = [0,4,6], factory = [[2,2],[6,2]]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> As shown in the figure:
|
||||
- The first robot at position 0 moves in the positive direction. It will be repaired at the first factory.
|
||||
- The second robot at position 4 moves in the negative direction. It will be repaired at the first factory.
|
||||
- The third robot at position 6 will be repaired at the second factory. It does not need to move.
|
||||
The limit of the first factory is 2, and it fixed 2 robots.
|
||||
The limit of the second factory is 2, and it fixed 1 robot.
|
||||
The total distance is |2 - 0| + |2 - 4| + |6 - 6| = 4. It can be shown that we cannot achieve a better total distance than 4.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/09/15/example-2.jpg" style="width: 500px; height: 329px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> robot = [1,-1], factory = [[-2,1],[2,1]]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> As shown in the figure:
|
||||
- The first robot at position 1 moves in the positive direction. It will be repaired at the second factory.
|
||||
- The second robot at position -1 moves in the negative direction. It will be repaired at the first factory.
|
||||
The limit of the first factory is 1, and it fixed 1 robot.
|
||||
The limit of the second factory is 1, and it fixed 1 robot.
|
||||
The total distance is |2 - 1| + |(-2) - (-1)| = 2. It can be shown that we cannot achieve a better total distance than 2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= robot.length, factory.length <= 100</code></li>
|
||||
<li><code>factory[j].length == 2</code></li>
|
||||
<li><code>-10<sup>9</sup> <= robot[i], position<sub>j</sub> <= 10<sup>9</sup></code></li>
|
||||
<li><code>0 <= limit<sub>j</sub> <= robot.length</code></li>
|
||||
<li>The input will be generated such that it is always possible to repair every robot.</li>
|
||||
</ul>
|
46
leetcode/problem/most-popular-video-creator.html
Normal file
46
leetcode/problem/most-popular-video-creator.html
Normal file
@ -0,0 +1,46 @@
|
||||
<p>You are given two string arrays <code>creators</code> and <code>ids</code>, and an integer array <code>views</code>, all of length <code>n</code>. The <code>i<sup>th</sup></code> video on a platform was created by <code>creator[i]</code>, has an id of <code>ids[i]</code>, and has <code>views[i]</code> views.</p>
|
||||
|
||||
<p>The <strong>popularity</strong> of a creator is the <strong>sum</strong> of the number of views on <strong>all</strong> of the creator's videos. Find the creator with the <strong>highest</strong> popularity and the id of their <strong>most</strong> viewed video.</p>
|
||||
|
||||
<ul>
|
||||
<li>If multiple creators have the highest popularity, find all of them.</li>
|
||||
<li>If multiple videos have the highest view count for a creator, find the lexicographically <strong>smallest</strong> id.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> a 2D array of strings </em><code>answer</code><em> where </em><code>answer[i] = [creator<sub>i</sub>, id<sub>i</sub>]</code><em> means that </em><code>creator<sub>i</sub></code> <em>has the <strong>highest</strong> popularity and </em><code>id<sub>i</sub></code><em> is the id of their most popular video.</em> The answer can be returned in any order.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> creators = ["alice","bob","alice","chris"], ids = ["one","two","three","four"], views = [5,10,5,4]
|
||||
<strong>Output:</strong> [["alice","one"],["bob","two"]]
|
||||
<strong>Explanation:</strong>
|
||||
The popularity of alice is 5 + 5 = 10.
|
||||
The popularity of bob is 10.
|
||||
The popularity of chris is 4.
|
||||
alice and bob are the most popular creators.
|
||||
For bob, the video with the highest view count is "two".
|
||||
For alice, the videos with the highest view count are "one" and "three". Since "one" is lexicographically smaller than "three", it is included in the answer.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> creators = ["alice","alice","alice"], ids = ["a","b","c"], views = [1,2,2]
|
||||
<strong>Output:</strong> [["alice","b"]]
|
||||
<strong>Explanation:</strong>
|
||||
The videos with id "b" and "c" have the highest view count.
|
||||
Since "b" is lexicographically smaller than "c", it is included in the answer.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>n == creators.length == ids.length == views.length</code></li>
|
||||
<li><code>1 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= creators[i].length, ids[i].length <= 5</code></li>
|
||||
<li><code>creators[i]</code> and <code>ids[i]</code> consist only of lowercase English letters.</li>
|
||||
<li><code>0 <= views[i] <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
49
leetcode/problem/next-greater-element-iv.html
Normal file
49
leetcode/problem/next-greater-element-iv.html
Normal file
@ -0,0 +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>
|
42
leetcode/problem/odd-string-difference.html
Normal file
42
leetcode/problem/odd-string-difference.html
Normal file
@ -0,0 +1,42 @@
|
||||
<p>You are given an array of equal-length strings <code>words</code>. Assume that the length of each string is <code>n</code>.</p>
|
||||
|
||||
<p>Each string <code>words[i]</code> can be converted into a <strong>difference integer array</strong> <code>difference[i]</code> of length <code>n - 1</code> where <code>difference[i][j] = words[i][j+1] - words[i][j]</code> where <code>0 <= j <= n - 2</code>. Note that the difference between two letters is the difference between their <strong>positions</strong> in the alphabet i.e. the position of <code>'a'</code> is <code>0</code>, <code>'b'</code> is <code>1</code>, and <code>'z'</code> is <code>25</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, for the string <code>"acb"</code>, the difference integer array is <code>[2 - 0, 1 - 2] = [2, -1]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>All the strings in words have the same difference integer array, <strong>except one</strong>. You should find that string.</p>
|
||||
|
||||
<p>Return<em> the string in </em><code>words</code><em> that has different <strong>difference integer array</strong>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["adc","wzy","abc"]
|
||||
<strong>Output:</strong> "abc"
|
||||
<strong>Explanation:</strong>
|
||||
- The difference integer array of "adc" is [3 - 0, 2 - 3] = [3, -1].
|
||||
- The difference integer array of "wzy" is [25 - 22, 24 - 25]= [3, -1].
|
||||
- The difference integer array of "abc" is [1 - 0, 2 - 1] = [1, 1].
|
||||
The odd array out is [1, 1], so we return the corresponding string, "abc".
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> words = ["aaa","bob","ccc","ddd"]
|
||||
<strong>Output:</strong> "bob"
|
||||
<strong>Explanation:</strong> All the integer arrays are [0, 0] except for "bob", which corresponds to [13, -13].
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= words.length <= 100</code></li>
|
||||
<li><code>n == words[i].length</code></li>
|
||||
<li><code>2 <= n <= 20</code></li>
|
||||
<li><code>words[i]</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
51
leetcode/problem/total-cost-to-hire-k-workers.html
Normal file
51
leetcode/problem/total-cost-to-hire-k-workers.html
Normal file
@ -0,0 +1,51 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>costs</code> where <code>costs[i]</code> is the cost of hiring the <code>i<sup>th</sup></code> worker.</p>
|
||||
|
||||
<p>You are also given two integers <code>k</code> and <code>candidates</code>. We want to hire exactly <code>k</code> workers according to the following rules:</p>
|
||||
|
||||
<ul>
|
||||
<li>You will run <code>k</code> sessions and hire exactly one worker in each session.</li>
|
||||
<li>In each hiring session, choose the worker with the lowest cost from either the first <code>candidates</code> workers or the last <code>candidates</code> workers. Break the tie by the smallest index.
|
||||
<ul>
|
||||
<li>For example, if <code>costs = [3,2,7,7,1,2]</code> and <code>candidates = 2</code>, then in the first hiring session, we will choose the <code>4<sup>th</sup></code> worker because they have the lowest cost <code>[<u>3,2</u>,7,7,<u><strong>1</strong>,2</u>]</code>.</li>
|
||||
<li>In the second hiring session, we will choose <code>1<sup>st</sup></code> worker because they have the same lowest cost as <code>4<sup>th</sup></code> worker but they have the smallest index <code>[<u>3,<strong>2</strong></u>,7,<u>7,2</u>]</code>. Please note that the indexing may be changed in the process.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>If there are fewer than candidates workers remaining, choose the worker with the lowest cost among them. Break the tie by the smallest index.</li>
|
||||
<li>A worker can only be chosen once.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the total cost to hire exactly </em><code>k</code><em> workers.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> costs = [17,12,10,2,7,2,11,20,8], k = 3, candidates = 4
|
||||
<strong>Output:</strong> 11
|
||||
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
|
||||
- In the first hiring round we choose the worker from [<u>17,12,10,2</u>,7,<u>2,11,20,8</u>]. The lowest cost is 2, and we break the tie by the smallest index, which is 3. The total cost = 0 + 2 = 2.
|
||||
- In the second hiring round we choose the worker from [<u>17,12,10,7</u>,<u>2,11,20,8</u>]. The lowest cost is 2 (index 4). The total cost = 2 + 2 = 4.
|
||||
- In the third hiring round we choose the worker from [<u>17,12,10,7,11,20,8</u>]. The lowest cost is 7 (index 3). The total cost = 4 + 7 = 11. Notice that the worker with index 3 was common in the first and last four workers.
|
||||
The total hiring cost is 11.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> costs = [1,2,4,1], k = 3, candidates = 3
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> We hire 3 workers in total. The total cost is initially 0.
|
||||
- In the first hiring round we choose the worker from [<u>1,2,4,1</u>]. The lowest cost is 1, and we break the tie by the smallest index, which is 0. The total cost = 0 + 1 = 1. Notice that workers with index 1 and 2 are common in the first and last 3 workers.
|
||||
- In the second hiring round we choose the worker from [<u>2,4,1</u>]. The lowest cost is 1 (index 2). The total cost = 1 + 1 = 2.
|
||||
- In the third hiring round there are less than three candidates. We choose the worker from the remaining workers [<u>2,4</u>]. The lowest cost is 2 (index 0). The total cost = 2 + 2 = 4.
|
||||
The total hiring cost is 4.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= costs.length <= 10<sup>5 </sup></code></li>
|
||||
<li><code>1 <= costs[i] <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= k, candidates <= costs.length</code></li>
|
||||
</ul>
|
38
leetcode/problem/words-within-two-edits-of-dictionary.html
Normal file
38
leetcode/problem/words-within-two-edits-of-dictionary.html
Normal file
@ -0,0 +1,38 @@
|
||||
<p>You are given two string arrays, <code>queries</code> and <code>dictionary</code>. All words in each array comprise of lowercase English letters and have the same length.</p>
|
||||
|
||||
<p>In one <strong>edit</strong> you can take a word from <code>queries</code>, and change any letter in it to any other letter. Find all words from <code>queries</code> that, after a <strong>maximum</strong> of two edits, equal some word from <code>dictionary</code>.</p>
|
||||
|
||||
<p>Return<em> a list of all words from </em><code>queries</code><em>, </em><em>that match with some word from </em><code>dictionary</code><em> after a maximum of <strong>two edits</strong></em>. Return the words in the <strong>same order</strong> they appear in <code>queries</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> queries = ["word","note","ants","wood"], dictionary = ["wood","joke","moat"]
|
||||
<strong>Output:</strong> ["word","note","wood"]
|
||||
<strong>Explanation:</strong>
|
||||
- Changing the 'r' in "word" to 'o' allows it to equal the dictionary word "wood".
|
||||
- Changing the 'n' to 'j' and the 't' to 'k' in "note" changes it to "joke".
|
||||
- It would take more than 2 edits for "ants" to equal a dictionary word.
|
||||
- "wood" can remain unchanged (0 edits) and match the corresponding dictionary word.
|
||||
Thus, we return ["word","note","wood"].
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> queries = ["yes"], dictionary = ["not"]
|
||||
<strong>Output:</strong> []
|
||||
<strong>Explanation:</strong>
|
||||
Applying any two edits to "yes" cannot make it equal to "not". Thus, we return an empty array.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= queries.length, dictionary.length <= 100</code></li>
|
||||
<li><code>n == queries[i].length == dictionary[j].length</code></li>
|
||||
<li><code>1 <= n <= 100</code></li>
|
||||
<li>All <code>queries[i]</code> and <code>dictionary[j]</code> are composed of lowercase English letters.</li>
|
||||
</ul>
|
Loading…
Reference in New Issue
Block a user