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
538ae7bb8b
commit
25625a52f2
@ -1,6 +1,6 @@
|
||||
# 力扣题库(完整版)
|
||||
|
||||
> 最后更新日期: **2023.10.15**
|
||||
> 最后更新日期: **2023.10.25**
|
||||
>
|
||||
> 使用脚本前请务必仔细完整阅读本 `README.md` 文件
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
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/minimum-sum-of-mountain-triplets-i.json
Normal file
170
leetcode-cn/originData/minimum-sum-of-mountain-triplets-i.json
Normal file
File diff suppressed because one or more lines are too long
171
leetcode-cn/originData/minimum-sum-of-mountain-triplets-ii.json
Normal file
171
leetcode-cn/originData/minimum-sum-of-mountain-triplets-ii.json
Normal file
File diff suppressed because one or more lines are too long
@ -0,0 +1,51 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>如果下标三元组 <code>(i, j, k)</code> 满足下述全部条件,则认为它是一个 <strong>山形三元组</strong> :</p>
|
||||
|
||||
<ul>
|
||||
<li><code>i < j < k</code></li>
|
||||
<li><code>nums[i] < nums[j]</code> 且 <code>nums[k] < nums[j]</code></li>
|
||||
</ul>
|
||||
|
||||
<p>请你找出 <code>nums</code> 中 <strong>元素和最小</strong> 的山形三元组,并返回其 <strong>元素和</strong> 。如果不存在满足条件的三元组,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [8,6,1,5,3]
|
||||
<strong>输出:</strong>9
|
||||
<strong>解释:</strong>三元组 (2, 3, 4) 是一个元素和等于 9 的山形三元组,因为:
|
||||
- 2 < 3 < 4
|
||||
- nums[2] < nums[3] 且 nums[4] < nums[3]
|
||||
这个三元组的元素和等于 nums[2] + nums[3] + nums[4] = 9 。可以证明不存在元素和小于 9 的山形三元组。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [5,4,8,7,10,2]
|
||||
<strong>输出:</strong>13
|
||||
<strong>解释:</strong>三元组 (1, 3, 5) 是一个元素和等于 13 的山形三元组,因为:
|
||||
- 1 < 3 < 5
|
||||
- nums[1] < nums[3] 且 nums[5] < nums[3]
|
||||
这个三元组的元素和等于 nums[1] + nums[3] + nums[5] = 13 。可以证明不存在元素和小于 13 的山形三元组。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [6,5,4,3,4,5]
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>可以证明 nums 中不存在山形三元组。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= nums.length <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 50</code></li>
|
||||
</ul>
|
@ -0,0 +1,51 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>如果下标三元组 <code>(i, j, k)</code> 满足下述全部条件,则认为它是一个 <strong>山形三元组</strong> :</p>
|
||||
|
||||
<ul>
|
||||
<li><code>i < j < k</code></li>
|
||||
<li><code>nums[i] < nums[j]</code> 且 <code>nums[k] < nums[j]</code></li>
|
||||
</ul>
|
||||
|
||||
<p>请你找出 <code>nums</code> 中 <strong>元素和最小</strong> 的山形三元组,并返回其 <strong>元素和</strong> 。如果不存在满足条件的三元组,返回 <code>-1</code> 。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [8,6,1,5,3]
|
||||
<strong>输出:</strong>9
|
||||
<strong>解释:</strong>三元组 (2, 3, 4) 是一个元素和等于 9 的山形三元组,因为:
|
||||
- 2 < 3 < 4
|
||||
- nums[2] < nums[3] 且 nums[4] < nums[3]
|
||||
这个三元组的元素和等于 nums[2] + nums[3] + nums[4] = 9 。可以证明不存在元素和小于 9 的山形三元组。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [5,4,8,7,10,2]
|
||||
<strong>输出:</strong>13
|
||||
<strong>解释:</strong>三元组 (1, 3, 5) 是一个元素和等于 13 的山形三元组,因为:
|
||||
- 1 < 3 < 5
|
||||
- nums[1] < nums[3] 且 nums[5] < nums[3]
|
||||
这个三元组的元素和等于 nums[1] + nums[3] + nums[5] = 13 。可以证明不存在元素和小于 13 的山形三元组。
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>nums = [6,5,4,3,4,5]
|
||||
<strong>输出:</strong>-1
|
||||
<strong>解释:</strong>可以证明 nums 中不存在山形三元组。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>8</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,53 @@
|
||||
<p>给你一个长度为 <code>n</code> 下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>我们想将下标进行分组,使得 <code>[0, n - 1]</code> 内所有下标 <code>i</code> 都 <strong>恰好</strong> 被分到其中一组。</p>
|
||||
|
||||
<p>如果以下条件成立,我们说这个分组方案是合法的:</p>
|
||||
|
||||
<ul>
|
||||
<li>对于每个组 <code>g</code> ,同一组内所有下标在 <code>nums</code> 中对应的数值都相等。</li>
|
||||
<li>对于任意两个组 <code>g<sub>1</sub></code> 和 <code>g<sub>2</sub></code> ,两个组中 <strong>下标数量</strong> 的 <strong>差值不超过 </strong><code>1</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回一个整数,表示得到一个合法分组方案的 <strong>最少</strong> 组数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [3,2,3,2,3]
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>一个得到 2 个分组的方案如下,中括号内的数字都是下标:
|
||||
组 1 -> [0,2,4]
|
||||
组 2 -> [1,3]
|
||||
所有下标都只属于一个组。
|
||||
组 1 中,nums[0] == nums[2] == nums[4] ,所有下标对应的数值都相等。
|
||||
组 2 中,nums[1] == nums[3] ,所有下标对应的数值都相等。
|
||||
组 1 中下标数目为 3 ,组 2 中下标数目为 2 。
|
||||
两者之差不超过 1 。
|
||||
无法得到一个小于 2 组的答案,因为如果只有 1 组,组内所有下标对应的数值都要相等。
|
||||
所以答案为 2 。</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>nums = [10,10,10,3,1,1]
|
||||
<b>输出:</b>4
|
||||
<b>解释:</b>一个得到 2 个分组的方案如下,中括号内的数字都是下标:
|
||||
组 1 -> [0]
|
||||
组 2 -> [1,2]
|
||||
组 3 -> [3]
|
||||
组 4 -> [4,5]
|
||||
分组方案满足题目要求的两个条件。
|
||||
无法得到一个小于 4 组的答案。
|
||||
所以答案为 4 。</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>
|
||||
</ul>
|
@ -0,0 +1,48 @@
|
||||
<p>给你一个字符串 <code>s</code> 和一个整数 <code>k</code> ,请你将 <code>s</code> 分成 <code>k</code> 个<strong> 子字符串</strong> ,使得每个 <strong>子字符串</strong> 变成 <strong>半回文串</strong> 需要修改的字符数目最少。</p>
|
||||
|
||||
<p>请你返回一个整数,表示需要修改的 <strong>最少</strong> 字符数目。</p>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>如果一个字符串从左往右和从右往左读是一样的,那么它是一个 <strong>回文串</strong> 。</li>
|
||||
<li>如果长度为 <code>len</code> 的字符串存在一个满足 <code>1 <= d < len</code> 的正整数 <code>d</code> ,<code>len % d == 0</code> 成立且所有对 <code>d</code> 做除法余数相同的下标对应的字符连起来得到的字符串都是 <strong>回文串</strong> ,那么我们说这个字符串是 <strong>半回文串</strong> 。比方说 <code>"aa"</code> ,<code>"aba"</code> ,<code>"adbgad"</code> 和 <code>"abab"</code> 都是 <strong>半回文串</strong> ,而 <code>"a"</code> ,<code>"ab"</code> 和 <code>"abca"</code> 不是。</li>
|
||||
<li><strong>子字符串</strong> 指的是一个字符串中一段连续的字符序列。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong class="example">示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "abcac", k = 2
|
||||
<b>输出:</b>1
|
||||
<b>解释:</b>我们可以将 s 分成子字符串 "ab" 和 "cac" 。子字符串 "cac" 已经是半回文串。如果我们将 "ab" 变成 "aa" ,它也会变成一个 d = 1 的半回文串。
|
||||
该方案是将 s 分成 2 个子字符串的前提下,得到 2 个半回文子字符串需要的最少修改次数。所以答案为 1 。</pre>
|
||||
|
||||
<p><strong class="example">示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "abcdef", k = 2
|
||||
<b>输出:</b>2
|
||||
<b>解释:</b>我们可以将 s 分成子字符串 "abc" 和 "def" 。子字符串 "abc" 和 "def" 都需要修改一个字符得到半回文串,所以我们总共需要 2 次字符修改使所有子字符串变成半回文串。
|
||||
该方案是将 s 分成 2 个子字符串的前提下,得到 2 个半回文子字符串需要的最少修改次数。所以答案为 2 。</pre>
|
||||
|
||||
<p><strong class="example">示例 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<b>输入:</b>s = "aabbaa", k = 3
|
||||
<b>输出:</b>0
|
||||
<b>解释:</b>我们可以将 s 分成子字符串 "aa" ,"bb" 和 "aa" 。
|
||||
字符串 "aa" 和 "bb" 都已经是半回文串了。所以答案为 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= s.length <= 200</code></li>
|
||||
<li><code>1 <= k <= s.length / 2</code></li>
|
||||
<li><code>s</code> 只包含小写英文字母。</li>
|
||||
</ul>
|
@ -0,0 +1,49 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of integers.</p>
|
||||
|
||||
<p>A triplet of indices <code>(i, j, k)</code> is a <strong>mountain</strong> if:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>i < j < k</code></li>
|
||||
<li><code>nums[i] < nums[j]</code> and <code>nums[k] < nums[j]</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum possible sum</strong> of a mountain triplet of</em> <code>nums</code>. <em>If no such triplet exists, return</em> <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [8,6,1,5,3]
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> Triplet (2, 3, 4) is a mountain triplet of sum 9 since:
|
||||
- 2 < 3 < 4
|
||||
- nums[2] < nums[3] and nums[4] < nums[3]
|
||||
And the sum of this triplet is nums[2] + nums[3] + nums[4] = 9. It can be shown that there are no mountain triplets with a sum of less than 9.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,4,8,7,10,2]
|
||||
<strong>Output:</strong> 13
|
||||
<strong>Explanation:</strong> Triplet (1, 3, 5) is a mountain triplet of sum 13 since:
|
||||
- 1 < 3 < 5
|
||||
- nums[1] < nums[3] and nums[5] < nums[3]
|
||||
And the sum of this triplet is nums[1] + nums[3] + nums[5] = 13. It can be shown that there are no mountain triplets with a sum of less than 13.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [6,5,4,3,4,5]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be shown that there are no mountain triplets in nums.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= nums.length <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 50</code></li>
|
||||
</ul>
|
@ -0,0 +1,49 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of integers.</p>
|
||||
|
||||
<p>A triplet of indices <code>(i, j, k)</code> is a <strong>mountain</strong> if:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>i < j < k</code></li>
|
||||
<li><code>nums[i] < nums[j]</code> and <code>nums[k] < nums[j]</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum possible sum</strong> of a mountain triplet of</em> <code>nums</code>. <em>If no such triplet exists, return</em> <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [8,6,1,5,3]
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> Triplet (2, 3, 4) is a mountain triplet of sum 9 since:
|
||||
- 2 < 3 < 4
|
||||
- nums[2] < nums[3] and nums[4] < nums[3]
|
||||
And the sum of this triplet is nums[2] + nums[3] + nums[4] = 9. It can be shown that there are no mountain triplets with a sum of less than 9.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,4,8,7,10,2]
|
||||
<strong>Output:</strong> 13
|
||||
<strong>Explanation:</strong> Triplet (1, 3, 5) is a mountain triplet of sum 13 since:
|
||||
- 1 < 3 < 5
|
||||
- nums[1] < nums[3] and nums[5] < nums[3]
|
||||
And the sum of this triplet is nums[1] + nums[3] + nums[5] = 13. It can be shown that there are no mountain triplets with a sum of less than 13.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [6,5,4,3,4,5]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be shown that there are no mountain triplets in nums.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>8</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,51 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
|
||||
|
||||
<p>We want to group the indices so for each index <code>i</code> in the range <code>[0, n - 1]</code>, it is assigned to <strong>exactly one</strong> group.</p>
|
||||
|
||||
<p>A group<strong> </strong>assignment is <strong>valid</strong> if the following conditions hold:</p>
|
||||
|
||||
<ul>
|
||||
<li>For every group <code>g</code>, all indices <code>i</code> assigned to group <code>g</code> have the same value in <code>nums</code>.</li>
|
||||
<li>For any two groups <code>g<sub>1</sub></code> and <code>g<sub>2</sub></code>, the <strong>difference</strong> between the <strong>number of indices</strong> assigned to <code>g<sub>1</sub></code> and <code>g<sub>2</sub></code> should <strong>not exceed</strong> <code>1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer denoting </em><em>the <strong>minimum</strong> number of groups needed to create a valid group assignment.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,3,2,3]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> One way the indices can be assigned to 2 groups is as follows, where the values in square brackets are indices:
|
||||
group 1 -> [0,2,4]
|
||||
group 2 -> [1,3]
|
||||
All indices are assigned to one group.
|
||||
In group 1, nums[0] == nums[2] == nums[4], so all indices have the same value.
|
||||
In group 2, nums[1] == nums[3], so all indices have the same value.
|
||||
The number of indices assigned to group 1 is 3, and the number of indices assigned to group 2 is 2.
|
||||
Their difference doesn't exceed 1.
|
||||
It is not possible to use fewer than 2 groups because, in order to use just 1 group, all indices assigned to that group must have the same value.
|
||||
Hence, the answer is 2.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,10,10,3,1,1]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> One way the indices can be assigned to 4 groups is as follows, where the values in square brackets are indices:
|
||||
group 1 -> [0]
|
||||
group 2 -> [1,2]
|
||||
group 3 -> [3]
|
||||
group 4 -> [4,5]
|
||||
The group assignment above satisfies both conditions.
|
||||
It can be shown that it is not possible to create a valid assignment using fewer than 4 groups.
|
||||
Hence, the answer is 4.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,46 @@
|
||||
<p>Given a string <code>s</code> and an integer <code>k</code>, partition <code>s</code> into <code>k</code> <strong>substrings</strong> such that the sum of the number of letter changes required to turn each <strong>substring</strong> into a <strong>semi-palindrome</strong> is minimized.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>minimum</strong> number of letter changes required.</em></p>
|
||||
|
||||
<p><strong>Notes</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>A string is a <strong>palindrome</strong> if it can be read the same way from left to right and right to left.</li>
|
||||
<li>A string with a length of <code>len</code> is considered a <strong>semi-palindrome</strong> if there exists a positive integer <code>d</code> such that <code>1 <= d < len</code> and <code>len % d == 0</code>, and if we take indices that have the same modulo by <code>d</code>, they form a <strong>palindrome</strong>. For example, <code>"aa"</code>, <code>"aba"</code>, <code>"adbgad"</code>, and, <code>"abab"</code> are <strong>semi-palindrome</strong> and <code>"a"</code>, <code>"ab"</code>, and, <code>"abca"</code> are not.</li>
|
||||
<li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcac", k = 2
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We can divide s into substrings "ab" and "cac". The string "cac" is already a semi-palindrome. If we change "ab" to "aa", it becomes a semi-palindrome with d = 1.
|
||||
It can be shown that there is no way to divide the string "abcac" into two semi-palindrome substrings. Therefore, the answer would be at least 1.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcdef", k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can divide it into substrings "abc" and "def". Each of the substrings "abc" and "def" requires one change to become a semi-palindrome, so we need 2 changes in total to make all substrings semi-palindrome.
|
||||
It can be shown that we cannot divide the given string into two substrings in a way that it would require less than 2 changes.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aabbaa", k = 3
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> We can divide it into substrings "aa", "bb" and "aa".
|
||||
The strings "aa" and "bb" are already semi-palindromes. Thus, the answer is zero.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= s.length <= 200</code></li>
|
||||
<li><code>1 <= k <= s.length / 2</code></li>
|
||||
<li><code>s</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
File diff suppressed because it is too large
Load Diff
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/minimum-sum-of-mountain-triplets-i.json
Normal file
167
leetcode/originData/minimum-sum-of-mountain-triplets-i.json
Normal file
File diff suppressed because one or more lines are too long
168
leetcode/originData/minimum-sum-of-mountain-triplets-ii.json
Normal file
168
leetcode/originData/minimum-sum-of-mountain-triplets-ii.json
Normal file
File diff suppressed because one or more lines are too long
@ -0,0 +1,46 @@
|
||||
<p>Given a string <code>s</code> and an integer <code>k</code>, partition <code>s</code> into <code>k</code> <strong>substrings</strong> such that the sum of the number of letter changes required to turn each <strong>substring</strong> into a <strong>semi-palindrome</strong> is minimized.</p>
|
||||
|
||||
<p>Return <em>an integer denoting the <strong>minimum</strong> number of letter changes required.</em></p>
|
||||
|
||||
<p><strong>Notes</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>A string is a <strong>palindrome</strong> if it can be read the same way from left to right and right to left.</li>
|
||||
<li>A string with a length of <code>len</code> is considered a <strong>semi-palindrome</strong> if there exists a positive integer <code>d</code> such that <code>1 <= d < len</code> and <code>len % d == 0</code>, and if we take indices that have the same modulo by <code>d</code>, they form a <strong>palindrome</strong>. For example, <code>"aa"</code>, <code>"aba"</code>, <code>"adbgad"</code>, and, <code>"abab"</code> are <strong>semi-palindrome</strong> and <code>"a"</code>, <code>"ab"</code>, and, <code>"abca"</code> are not.</li>
|
||||
<li>A <strong>substring</strong> is a contiguous sequence of characters within a string.</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcac", k = 2
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong> We can divide s into substrings "ab" and "cac". The string "cac" is already a semi-palindrome. If we change "ab" to "aa", it becomes a semi-palindrome with d = 1.
|
||||
It can be shown that there is no way to divide the string "abcac" into two semi-palindrome substrings. Therefore, the answer would be at least 1.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abcdef", k = 2
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can divide it into substrings "abc" and "def". Each of the substrings "abc" and "def" requires one change to become a semi-palindrome, so we need 2 changes in total to make all substrings semi-palindrome.
|
||||
It can be shown that we cannot divide the given string into two substrings in a way that it would require less than 2 changes.</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "aabbaa", k = 3
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> We can divide it into substrings "aa", "bb" and "aa".
|
||||
The strings "aa" and "bb" are already semi-palindromes. Thus, the answer is zero.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= s.length <= 200</code></li>
|
||||
<li><code>1 <= k <= s.length / 2</code></li>
|
||||
<li><code>s</code> consists only of lowercase English letters.</li>
|
||||
</ul>
|
@ -0,0 +1,51 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of length <code>n</code>.</p>
|
||||
|
||||
<p>We want to group the indices so for each index <code>i</code> in the range <code>[0, n - 1]</code>, it is assigned to <strong>exactly one</strong> group.</p>
|
||||
|
||||
<p>A group<strong> </strong>assignment is <strong>valid</strong> if the following conditions hold:</p>
|
||||
|
||||
<ul>
|
||||
<li>For every group <code>g</code>, all indices <code>i</code> assigned to group <code>g</code> have the same value in <code>nums</code>.</li>
|
||||
<li>For any two groups <code>g<sub>1</sub></code> and <code>g<sub>2</sub></code>, the <strong>difference</strong> between the <strong>number of indices</strong> assigned to <code>g<sub>1</sub></code> and <code>g<sub>2</sub></code> should <strong>not exceed</strong> <code>1</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>an integer denoting </em><em>the <strong>minimum</strong> number of groups needed to create a valid group assignment.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,2,3,2,3]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> One way the indices can be assigned to 2 groups is as follows, where the values in square brackets are indices:
|
||||
group 1 -> [0,2,4]
|
||||
group 2 -> [1,3]
|
||||
All indices are assigned to one group.
|
||||
In group 1, nums[0] == nums[2] == nums[4], so all indices have the same value.
|
||||
In group 2, nums[1] == nums[3], so all indices have the same value.
|
||||
The number of indices assigned to group 1 is 3, and the number of indices assigned to group 2 is 2.
|
||||
Their difference doesn't exceed 1.
|
||||
It is not possible to use fewer than 2 groups because, in order to use just 1 group, all indices assigned to that group must have the same value.
|
||||
Hence, the answer is 2.</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [10,10,10,3,1,1]
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> One way the indices can be assigned to 4 groups is as follows, where the values in square brackets are indices:
|
||||
group 1 -> [0]
|
||||
group 2 -> [1,2]
|
||||
group 3 -> [3]
|
||||
group 4 -> [4,5]
|
||||
The group assignment above satisfies both conditions.
|
||||
It can be shown that it is not possible to create a valid assignment using fewer than 4 groups.
|
||||
Hence, the answer is 4.</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
|
||||
</ul>
|
49
leetcode/problem/minimum-sum-of-mountain-triplets-i.html
Normal file
49
leetcode/problem/minimum-sum-of-mountain-triplets-i.html
Normal file
@ -0,0 +1,49 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of integers.</p>
|
||||
|
||||
<p>A triplet of indices <code>(i, j, k)</code> is a <strong>mountain</strong> if:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>i < j < k</code></li>
|
||||
<li><code>nums[i] < nums[j]</code> and <code>nums[k] < nums[j]</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum possible sum</strong> of a mountain triplet of</em> <code>nums</code>. <em>If no such triplet exists, return</em> <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [8,6,1,5,3]
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> Triplet (2, 3, 4) is a mountain triplet of sum 9 since:
|
||||
- 2 < 3 < 4
|
||||
- nums[2] < nums[3] and nums[4] < nums[3]
|
||||
And the sum of this triplet is nums[2] + nums[3] + nums[4] = 9. It can be shown that there are no mountain triplets with a sum of less than 9.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,4,8,7,10,2]
|
||||
<strong>Output:</strong> 13
|
||||
<strong>Explanation:</strong> Triplet (1, 3, 5) is a mountain triplet of sum 13 since:
|
||||
- 1 < 3 < 5
|
||||
- nums[1] < nums[3] and nums[5] < nums[3]
|
||||
And the sum of this triplet is nums[1] + nums[3] + nums[5] = 13. It can be shown that there are no mountain triplets with a sum of less than 13.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [6,5,4,3,4,5]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be shown that there are no mountain triplets in nums.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= nums.length <= 50</code></li>
|
||||
<li><code>1 <= nums[i] <= 50</code></li>
|
||||
</ul>
|
49
leetcode/problem/minimum-sum-of-mountain-triplets-ii.html
Normal file
49
leetcode/problem/minimum-sum-of-mountain-triplets-ii.html
Normal file
@ -0,0 +1,49 @@
|
||||
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of integers.</p>
|
||||
|
||||
<p>A triplet of indices <code>(i, j, k)</code> is a <strong>mountain</strong> if:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>i < j < k</code></li>
|
||||
<li><code>nums[i] < nums[j]</code> and <code>nums[k] < nums[j]</code></li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>minimum possible sum</strong> of a mountain triplet of</em> <code>nums</code>. <em>If no such triplet exists, return</em> <code>-1</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [8,6,1,5,3]
|
||||
<strong>Output:</strong> 9
|
||||
<strong>Explanation:</strong> Triplet (2, 3, 4) is a mountain triplet of sum 9 since:
|
||||
- 2 < 3 < 4
|
||||
- nums[2] < nums[3] and nums[4] < nums[3]
|
||||
And the sum of this triplet is nums[2] + nums[3] + nums[4] = 9. It can be shown that there are no mountain triplets with a sum of less than 9.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [5,4,8,7,10,2]
|
||||
<strong>Output:</strong> 13
|
||||
<strong>Explanation:</strong> Triplet (1, 3, 5) is a mountain triplet of sum 13 since:
|
||||
- 1 < 3 < 5
|
||||
- nums[1] < nums[3] and nums[5] < nums[3]
|
||||
And the sum of this triplet is nums[1] + nums[3] + nums[5] = 13. It can be shown that there are no mountain triplets with a sum of less than 13.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [6,5,4,3,4,5]
|
||||
<strong>Output:</strong> -1
|
||||
<strong>Explanation:</strong> It can be shown that there are no mountain triplets in nums.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>3 <= nums.length <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= nums[i] <= 10<sup>8</sup></code></li>
|
||||
</ul>
|
Loading…
Reference in New Issue
Block a user