mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 10:38:13 +08:00
update
This commit is contained in:
parent
9e50b3cd07
commit
a697596e35
@ -1,6 +1,6 @@
|
||||
# 力扣题库(完整版)
|
||||
|
||||
> 最后更新日期: **2024.01.13**
|
||||
> 最后更新日期: **2024.01.30**
|
||||
>
|
||||
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
170
leetcode-cn/originData/alice-and-bob-playing-flower-game.json
Normal file
170
leetcode-cn/originData/alice-and-bob-playing-flower-game.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
170
leetcode-cn/originData/number-of-changing-keys.json
Normal file
170
leetcode-cn/originData/number-of-changing-keys.json
Normal file
File diff suppressed because one or more lines are too long
@ -0,0 +1,45 @@
|
||||
<p>Alice 和 Bob 在一个长满鲜花的环形草地玩一个回合制游戏。环形的草地上有一些鲜花,Alice 到 Bob 之间顺时针有 <code>x</code> 朵鲜花,逆时针有 <code>y</code> 朵鲜花。</p>
|
||||
|
||||
<p>游戏过程如下:</p>
|
||||
|
||||
<ol>
|
||||
<li>Alice 先行动。</li>
|
||||
<li>每一次行动中,当前玩家必须选择顺时针或者逆时针,然后在这个方向上摘一朵鲜花。</li>
|
||||
<li>一次行动结束后,如果所有鲜花都被摘完了,那么 <strong>当前</strong> 玩家抓住对手并赢得游戏的胜利。</li>
|
||||
</ol>
|
||||
|
||||
<p>给你两个整数 <code>n</code> 和 <code>m</code> ,你的任务是求出满足以下条件的所有 <code>(x, y)</code> 对:</p>
|
||||
|
||||
<ul>
|
||||
<li>按照上述规则,Alice 必须赢得游戏。</li>
|
||||
<li>Alice 顺时针方向上的鲜花数目 <code>x</code> 必须在区间 <code>[1,n]</code> 之间。</li>
|
||||
<li>Alice 逆时针方向上的鲜花数目 <code>y</code> 必须在区间 <code>[1,m]</code> 之间。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回满足题目描述的数对 <code>(x, y)</code> 的数目。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 3, m = 2
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>以下数对满足题目要求:(1,2) ,(3,2) ,(2,1) 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>n = 1, m = 1
|
||||
<b>输出:</b>0
|
||||
<b>解释:</b>没有数对满足题目要求。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n, m <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,36 @@
|
||||
<p>给你一个<strong> 正整数 </strong>数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>你需要从数组中选出一个满足下述条件的<span data-keyword="subset">子集</span>:</p>
|
||||
|
||||
<ul>
|
||||
<li>你可以将选中的元素放置在一个下标从 <strong>0</strong> 开始的数组中,并使其遵循以下模式:<code>[x, x<sup>2</sup>, x<sup>4</sup>, ..., x<sup>k/2</sup>, x<sup>k</sup>, x<sup>k/2</sup>, ..., x<sup>4</sup>, x<sup>2</sup>, x]</code>(<strong>注意</strong>,<code>k</code> 可以是任何 <strong>非负</strong> 的 2 的幂)。例如,<code>[2, 4, 16, 4, 2]</code> 和 <code>[3, 9, 3]</code> 都符合这一模式,而 <code>[2, 4, 8, 4, 2]</code> 则不符合。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回满足这些条件的子集中,元素数量的 <strong>最大值 </strong><em>。</em></p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [5,4,1,2,2]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>选择子集 {4,2,2} ,将其放在数组 [2,4,2] 中,它遵循该模式,且 2<sup>2</sup> == 4 。因此答案是 3 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [1,3,2,4]
|
||||
<strong>输出:</strong>1
|
||||
<strong>解释:</strong>选择子集 {1},将其放在数组 [1] 中,它遵循该模式。因此答案是 1 。注意我们也可以选择子集 {2} 、{4} 或 {3} ,可能存在多个子集都能得到相同的答案。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,37 @@
|
||||
<p>给你一个下标从<strong> 0</strong> 开始的字符串 <code>s</code> ,该字符串由用户输入。按键变更的定义是:使用与上次使用的按键不同的键。例如 <code>s = "ab"</code> 表示按键变更一次,而 <code>s = "bBBb"</code> 不存在按键变更。</p>
|
||||
|
||||
<p>返回用户输入过程中按键变更的次数。</p>
|
||||
|
||||
<p><strong>注意:</strong><code>shift</code> 或 <code>caps lock</code> 等修饰键不计入按键变更,也就是说,如果用户先输入字母 <code>'a'</code> 然后输入字母 <code>'A'</code> ,不算作按键变更。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "aAbBcC"
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>
|
||||
从 s[0] = 'a' 到 s[1] = 'A',不存在按键变更,因为不计入 caps lock 或 shift 。
|
||||
从 s[1] = 'A' 到 s[2] = 'b',按键变更。
|
||||
从 s[2] = 'b' 到 s[3] = 'B',不存在按键变更,因为不计入 caps lock 或 shift 。
|
||||
从 s[3] = 'B' 到 s[4] = 'c',按键变更。
|
||||
从 s[4] = 'c' 到 s[5] = 'C',不存在按键变更,因为不计入 caps lock 或 shift 。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "AaAaAaaA"
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong> 不存在按键变更,因为这个过程中只按下字母 'a' 和 'A' ,不需要进行按键变更。<!-- notionvc: 8849fe75-f31e-41dc-a2e0-b7d33d8427d2 -->
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>s</code> 仅由英文大写字母和小写字母组成。</li>
|
||||
</ul>
|
@ -0,0 +1,51 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 和一个整数 <code>k</code> 。</p>
|
||||
|
||||
<p>一次操作中,你可以选择 <code>nums</code> 中满足 <code>0 <= i < nums.length - 1</code> 的一个下标 <code>i</code> ,并将 <code>nums[i]</code> 和 <code>nums[i + 1]</code> 替换为数字 <code>nums[i] & nums[i + 1]</code> ,其中 <code>&</code> 表示按位 <code>AND</code> 操作。</p>
|
||||
|
||||
<p>请你返回 <strong>至多</strong> <code>k</code> 次操作以内,使 <code>nums</code> 中所有剩余元素按位 <code>OR</code> 结果的 <strong>最小值</strong> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [3,5,3,2,7], k = 2
|
||||
<b>输出:</b>3
|
||||
<b>解释:</b>执行以下操作:
|
||||
1. 将 nums[0] 和 nums[1] 替换为 (nums[0] & nums[1]) ,得到 nums 为 [1,3,2,7] 。
|
||||
2. 将 nums[2] 和 nums[3] 替换为 (nums[2] & nums[3]) ,得到 nums 为 [1,3,2] 。
|
||||
最终数组的按位或值为 3 。
|
||||
3 是 k 次操作以内,可以得到的剩余元素的最小按位或值。</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [7,3,15,14,2,8], k = 4
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>执行以下操作:
|
||||
1. 将 nums[0] 和 nums[1] 替换为 (nums[0] & nums[1]) ,得到 nums 为 [3,15,14,2,8] 。
|
||||
2. 将 nums[0] 和 nums[1] 替换为 (nums[0] & nums[1]) ,得到 nums 为 [3,14,2,8] 。
|
||||
3. 将 nums[0] 和 nums[1] 替换为 (nums[0] & nums[1]) ,得到 nums 为 [2,2,8] 。
|
||||
4. 将 nums[1] 和 nums[2] 替换为 (nums[1] & nums[2]) ,得到 nums 为 [2,0] 。
|
||||
最终数组的按位或值为 2 。
|
||||
2 是 k 次操作以内,可以得到的剩余元素的最小按位或值。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [10,7,10,3,9,14,9,4], k = 1
|
||||
<b>输出:</b>15
|
||||
<b>解释:</b>不执行任何操作,nums 的按位或值为 15 。
|
||||
15 是 k 次操作以内,可以得到的剩余元素的最小按位或值。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>0 <= nums[i] < 2<sup>30</sup></code></li>
|
||||
<li><code>0 <= k < nums.length</code></li>
|
||||
</ul>
|
@ -0,0 +1,43 @@
|
||||
<p>Alice and Bob are playing a turn-based game on a circular field surrounded by flowers. The circle represents the field, and there are <code>x</code> flowers in the clockwise direction between Alice and Bob, and <code>y</code> flowers in the anti-clockwise direction between them.</p>
|
||||
|
||||
<p>The game proceeds as follows:</p>
|
||||
|
||||
<ol>
|
||||
<li>Alice takes the first turn.</li>
|
||||
<li>In each turn, a player must choose either the clockwise or anti-clockwise direction and pick one flower from that side.</li>
|
||||
<li>At the end of the turn, if there are no flowers left at all, the <strong>current</strong> player captures their opponent and wins the game.</li>
|
||||
</ol>
|
||||
|
||||
<p>Given two integers, <code>n</code> and <code>m</code>, the task is to compute the number of possible pairs <code>(x, y)</code> that satisfy the conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>Alice must win the game according to the described rules.</li>
|
||||
<li>The number of flowers <code>x</code> in the clockwise direction must be in the range <code>[1,n]</code>.</li>
|
||||
<li>The number of flowers <code>y</code> in the anti-clockwise direction must be in the range <code>[1,m]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of possible pairs</em> <code>(x, y)</code> <em>that satisfy the conditions mentioned in the statement</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, m = 2
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The following pairs satisfy conditions described in the statement: (1,2), (3,2), (2,1).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1, m = 1
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> No pairs satisfy the conditions described in the statement.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n, m <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,34 @@
|
||||
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p>
|
||||
|
||||
<p>You need to select a <span data-keyword="subset">subset</span> of <code>nums</code> which satisfies the following condition:</p>
|
||||
|
||||
<ul>
|
||||
<li>You can place the selected elements in a <strong>0-indexed</strong> array such that it follows the pattern: <code>[x, x<sup>2</sup>, x<sup>4</sup>, ..., x<sup>k/2</sup>, x<sup>k</sup>, x<sup>k/2</sup>, ..., x<sup>4</sup>, x<sup>2</sup>, x]</code> (<strong>Note</strong> that <code>k</code> can be be any <strong>non-negative</strong> power of <code>2</code>). For example, <code>[2, 4, 16, 4, 2]</code> and <code>[3, 9, 3]</code> follow the pattern while <code>[2, 4, 8, 4, 2]</code> does not.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of elements in a subset that satisfies these conditions.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,4,1,2,2]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can select the subset {4,2,2}, which can be placed in the array as [2,4,2] which follows the pattern and 2<sup>2</sup> == 4. Hence the answer is 3.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,2,4]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We can select the subset {1}, which can be placed in the array as [1] which follows the pattern. Hence the answer is 1. Note that we could have also selected the subsets {2}, {4}, or {3}, there may be multiple subsets which provide the same answer.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,36 @@
|
||||
<p>You are given a <strong>0-indexed </strong>string <code>s</code> typed by a user. Changing a key is defined as using a key different from the last used key. For example, <code>s = "ab"</code> has a change of a key while <code>s = "bBBb"</code> does not have any.</p>
|
||||
|
||||
<p>Return <em>the number of times the user had to change the key. </em></p>
|
||||
|
||||
<p><strong>Note: </strong>Modifiers like <code>shift</code> or <code>caps lock</code> won't be counted in changing the key that is if a user typed the letter <code>'a'</code> and then the letter <code>'A'</code> then it will not be considered as a changing of key.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aAbBcC"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
From s[0] = 'a' to s[1] = 'A', there is no change of key as caps lock or shift is not counted.
|
||||
From s[1] = 'A' to s[2] = 'b', there is a change of key.
|
||||
From s[2] = 'b' to s[3] = 'B', there is no change of key as caps lock or shift is not counted.
|
||||
From s[3] = 'B' to s[4] = 'c', there is a change of key.
|
||||
From s[4] = 'c' to s[5] = 'C', there is no change of key as caps lock or shift is not counted.
|
||||
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "AaAaAaaA"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There is no change of key since only the letters 'a' and 'A' are<!-- notionvc: 8849fe75-f31e-41dc-a2e0-b7d33d8427d2 --> pressed which does not require change of key.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>s</code> consists of only upper case and lower case English letters.</li>
|
||||
</ul>
|
@ -0,0 +1,49 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>In one operation, you can pick any index <code>i</code> of <code>nums</code> such that <code>0 <= i < nums.length - 1</code> and replace <code>nums[i]</code> and <code>nums[i + 1]</code> with a single occurrence of <code>nums[i] & nums[i + 1]</code>, where <code>&</code> represents the bitwise <code>AND</code> operator.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> possible value of the bitwise </em><code>OR</code><em> of the remaining elements of</em> <code>nums</code> <em>after applying <strong>at most</strong></em> <code>k</code> <em>operations</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,5,3,2,7], k = 2
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Let's do the following operations:
|
||||
1. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [1,3,2,7].
|
||||
2. Replace nums[2] and nums[3] with (nums[2] & nums[3]) so that nums becomes equal to [1,3,2].
|
||||
The bitwise-or of the final array is 3.
|
||||
It can be shown that 3 is the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [7,3,15,14,2,8], k = 4
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Let's do the following operations:
|
||||
1. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [3,15,14,2,8].
|
||||
2. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [3,14,2,8].
|
||||
3. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [2,2,8].
|
||||
4. Replace nums[1] and nums[2] with (nums[1] & nums[2]) so that nums becomes equal to [2,0].
|
||||
The bitwise-or of the final array is 2.
|
||||
It can be shown that 2 is the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,7,10,3,9,14,9,4], k = 1
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> Without applying any operations, the bitwise-or of nums is 15.
|
||||
It can be shown that 15 is the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.
|
||||
</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] < 2<sup>30</sup></code></li>
|
||||
<li><code>0 <= k < nums.length</code></li>
|
||||
</ul>
|
File diff suppressed because it is too large
Load Diff
167
leetcode/originData/alice-and-bob-playing-flower-game.json
Normal file
167
leetcode/originData/alice-and-bob-playing-flower-game.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
167
leetcode/originData/number-of-changing-keys.json
Normal file
167
leetcode/originData/number-of-changing-keys.json
Normal file
File diff suppressed because one or more lines are too long
43
leetcode/problem/alice-and-bob-playing-flower-game.html
Normal file
43
leetcode/problem/alice-and-bob-playing-flower-game.html
Normal file
@ -0,0 +1,43 @@
|
||||
<p>Alice and Bob are playing a turn-based game on a circular field surrounded by flowers. The circle represents the field, and there are <code>x</code> flowers in the clockwise direction between Alice and Bob, and <code>y</code> flowers in the anti-clockwise direction between them.</p>
|
||||
|
||||
<p>The game proceeds as follows:</p>
|
||||
|
||||
<ol>
|
||||
<li>Alice takes the first turn.</li>
|
||||
<li>In each turn, a player must choose either the clockwise or anti-clockwise direction and pick one flower from that side.</li>
|
||||
<li>At the end of the turn, if there are no flowers left at all, the <strong>current</strong> player captures their opponent and wins the game.</li>
|
||||
</ol>
|
||||
|
||||
<p>Given two integers, <code>n</code> and <code>m</code>, the task is to compute the number of possible pairs <code>(x, y)</code> that satisfy the conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>Alice must win the game according to the described rules.</li>
|
||||
<li>The number of flowers <code>x</code> in the clockwise direction must be in the range <code>[1,n]</code>.</li>
|
||||
<li>The number of flowers <code>y</code> in the anti-clockwise direction must be in the range <code>[1,m]</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the number of possible pairs</em> <code>(x, y)</code> <em>that satisfy the conditions mentioned in the statement</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 3, m = 2
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> The following pairs satisfy conditions described in the statement: (1,2), (3,2), (2,1).
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> n = 1, m = 1
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> No pairs satisfy the conditions described in the statement.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= n, m <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,34 @@
|
||||
<p>You are given an array of <strong>positive</strong> integers <code>nums</code>.</p>
|
||||
|
||||
<p>You need to select a <span data-keyword="subset">subset</span> of <code>nums</code> which satisfies the following condition:</p>
|
||||
|
||||
<ul>
|
||||
<li>You can place the selected elements in a <strong>0-indexed</strong> array such that it follows the pattern: <code>[x, x<sup>2</sup>, x<sup>4</sup>, ..., x<sup>k/2</sup>, x<sup>k</sup>, x<sup>k/2</sup>, ..., x<sup>4</sup>, x<sup>2</sup>, x]</code> (<strong>Note</strong> that <code>k</code> can be be any <strong>non-negative</strong> power of <code>2</code>). For example, <code>[2, 4, 16, 4, 2]</code> and <code>[3, 9, 3]</code> follow the pattern while <code>[2, 4, 8, 4, 2]</code> does not.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of elements in a subset that satisfies these conditions.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,4,1,2,2]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> We can select the subset {4,2,2}, which can be placed in the array as [2,4,2] which follows the pattern and 2<sup>2</sup> == 4. Hence the answer is 3.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,3,2,4]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We can select the subset {1}, which can be placed in the array as [1] which follows the pattern. Hence the answer is 1. Note that we could have also selected the subsets {2}, {4}, or {3}, there may be multiple subsets which provide the same answer.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,49 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>
|
||||
|
||||
<p>In one operation, you can pick any index <code>i</code> of <code>nums</code> such that <code>0 <= i < nums.length - 1</code> and replace <code>nums[i]</code> and <code>nums[i + 1]</code> with a single occurrence of <code>nums[i] & nums[i + 1]</code>, where <code>&</code> represents the bitwise <code>AND</code> operator.</p>
|
||||
|
||||
<p>Return <em>the <strong>minimum</strong> possible value of the bitwise </em><code>OR</code><em> of the remaining elements of</em> <code>nums</code> <em>after applying <strong>at most</strong></em> <code>k</code> <em>operations</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,5,3,2,7], k = 2
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong> Let's do the following operations:
|
||||
1. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [1,3,2,7].
|
||||
2. Replace nums[2] and nums[3] with (nums[2] & nums[3]) so that nums becomes equal to [1,3,2].
|
||||
The bitwise-or of the final array is 3.
|
||||
It can be shown that 3 is the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [7,3,15,14,2,8], k = 4
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> Let's do the following operations:
|
||||
1. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [3,15,14,2,8].
|
||||
2. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [3,14,2,8].
|
||||
3. Replace nums[0] and nums[1] with (nums[0] & nums[1]) so that nums becomes equal to [2,2,8].
|
||||
4. Replace nums[1] and nums[2] with (nums[1] & nums[2]) so that nums becomes equal to [2,0].
|
||||
The bitwise-or of the final array is 2.
|
||||
It can be shown that 2 is the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,7,10,3,9,14,9,4], k = 1
|
||||
<strong>Output:</strong> 15
|
||||
<strong>Explanation:</strong> Without applying any operations, the bitwise-or of nums is 15.
|
||||
It can be shown that 15 is the minimum possible value of the bitwise OR of the remaining elements of nums after applying at most k operations.
|
||||
</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] < 2<sup>30</sup></code></li>
|
||||
<li><code>0 <= k < nums.length</code></li>
|
||||
</ul>
|
36
leetcode/problem/number-of-changing-keys.html
Normal file
36
leetcode/problem/number-of-changing-keys.html
Normal file
@ -0,0 +1,36 @@
|
||||
<p>You are given a <strong>0-indexed </strong>string <code>s</code> typed by a user. Changing a key is defined as using a key different from the last used key. For example, <code>s = "ab"</code> has a change of a key while <code>s = "bBBb"</code> does not have any.</p>
|
||||
|
||||
<p>Return <em>the number of times the user had to change the key. </em></p>
|
||||
|
||||
<p><strong>Note: </strong>Modifiers like <code>shift</code> or <code>caps lock</code> won't be counted in changing the key that is if a user typed the letter <code>'a'</code> and then the letter <code>'A'</code> then it will not be considered as a changing of key.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aAbBcC"
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
From s[0] = 'a' to s[1] = 'A', there is no change of key as caps lock or shift is not counted.
|
||||
From s[1] = 'A' to s[2] = 'b', there is a change of key.
|
||||
From s[2] = 'b' to s[3] = 'B', there is no change of key as caps lock or shift is not counted.
|
||||
From s[3] = 'B' to s[4] = 'c', there is a change of key.
|
||||
From s[4] = 'c' to s[5] = 'C', there is no change of key as caps lock or shift is not counted.
|
||||
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "AaAaAaaA"
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There is no change of key since only the letters 'a' and 'A' are<!-- notionvc: 8849fe75-f31e-41dc-a2e0-b7d33d8427d2 --> pressed which does not require change of key.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= s.length <= 100</code></li>
|
||||
<li><code>s</code> consists of only upper case and lower case English letters.</li>
|
||||
</ul>
|
Loading…
Reference in New Issue
Block a user