mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-04-18 08:39:41 +08:00
update
This commit is contained in:
parent
932b731690
commit
8a37a26300
leetcode-cn
origin-data.json
originData
[no content]minimum-subarrays-in-a-valid-split.jsonconvert-the-temperature.jsoncount-ways-to-build-good-strings.jsonmaximum-number-of-non-overlapping-palindrome-substrings.jsonminimum-number-of-operations-to-sort-a-binary-tree-by-level.jsonmost-profitable-path-in-a-tree.jsonnumber-of-distinct-averages.jsonnumber-of-subarrays-with-lcm-equal-to-k.jsonsplit-message-based-on-limit.json
problem (Chinese)
不同的平均值数目 [number-of-distinct-averages].html不重叠回文子字符串的最大数目 [maximum-number-of-non-overlapping-palindrome-substrings].html最小公倍数为 K 的子数组数目 [number-of-subarrays-with-lcm-equal-to-k].html树上最大得分和路径 [most-profitable-path-in-a-tree].html根据限制分割消息 [split-message-based-on-limit].html温度转换 [convert-the-temperature].html统计构造好字符串的方案数 [count-ways-to-build-good-strings].html逐层排序二叉树所需的最少操作数目 [minimum-number-of-operations-to-sort-a-binary-tree-by-level].html
problem (English)
不同的平均值数目(English) [number-of-distinct-averages].html不重叠回文子字符串的最大数目(English) [maximum-number-of-non-overlapping-palindrome-substrings].html最小公倍数为 K 的子数组数目(English) [number-of-subarrays-with-lcm-equal-to-k].html树上最大得分和路径(English) [most-profitable-path-in-a-tree].html根据限制分割消息(English) [split-message-based-on-limit].html温度转换(English) [convert-the-temperature].html统计构造好字符串的方案数(English) [count-ways-to-build-good-strings].html逐层排序二叉树所需的最少操作数目(English) [minimum-number-of-operations-to-sort-a-binary-tree-by-level].html
leetcode
origin-data.json
originData
[no content]minimum-subarrays-in-a-valid-split.jsonconvert-the-temperature.jsoncount-ways-to-build-good-strings.jsonmaximum-number-of-non-overlapping-palindrome-substrings.jsonminimum-number-of-operations-to-sort-a-binary-tree-by-level.jsonmost-profitable-path-in-a-tree.jsonnumber-of-distinct-averages.jsonnumber-of-subarrays-with-lcm-equal-to-k.jsonsplit-message-based-on-limit.json
problem
convert-the-temperature.htmlcount-ways-to-build-good-strings.htmlmaximum-number-of-non-overlapping-palindrome-substrings.htmlminimum-number-of-operations-to-sort-a-binary-tree-by-level.htmlmost-profitable-path-in-a-tree.htmlnumber-of-distinct-averages.htmlnumber-of-subarrays-with-lcm-equal-to-k.htmlsplit-message-based-on-limit.html
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
163
leetcode-cn/originData/convert-the-temperature.json
Normal file
163
leetcode-cn/originData/convert-the-temperature.json
Normal file
File diff suppressed because one or more lines are too long
164
leetcode-cn/originData/count-ways-to-build-good-strings.json
Normal file
164
leetcode-cn/originData/count-ways-to-build-good-strings.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
166
leetcode-cn/originData/minimum-number-of-operations-to-sort-a-binary-tree-by-level.json
Normal file
166
leetcode-cn/originData/minimum-number-of-operations-to-sort-a-binary-tree-by-level.json
Normal file
File diff suppressed because one or more lines are too long
165
leetcode-cn/originData/most-profitable-path-in-a-tree.json
Normal file
165
leetcode-cn/originData/most-profitable-path-in-a-tree.json
Normal file
File diff suppressed because one or more lines are too long
164
leetcode-cn/originData/number-of-distinct-averages.json
Normal file
164
leetcode-cn/originData/number-of-distinct-averages.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
165
leetcode-cn/originData/split-message-based-on-limit.json
Normal file
165
leetcode-cn/originData/split-message-based-on-limit.json
Normal file
File diff suppressed because one or more lines are too long
@ -0,0 +1,50 @@
|
||||
<p>给你一个下标从 <strong>0</strong> 开始长度为 <strong>偶数</strong> 的整数数组 <code>nums</code> 。</p>
|
||||
|
||||
<p>只要 <code>nums</code> <strong>不是</strong> 空数组,你就重复执行以下步骤:</p>
|
||||
|
||||
<ul>
|
||||
<li>找到 <code>nums</code> 中的最小值,并删除它。</li>
|
||||
<li>找到 <code>nums</code> 中的最大值,并删除它。</li>
|
||||
<li>计算删除两数的平均值。</li>
|
||||
</ul>
|
||||
|
||||
<p>两数 <code>a</code> 和 <code>b</code> 的 <strong>平均值</strong> 为 <code>(a + b) / 2</code> 。</p>
|
||||
|
||||
<ul>
|
||||
<li>比方说,<code>2</code> 和 <code>3</code> 的平均值是 <code>(2 + 3) / 2 = 2.5</code> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回上述过程能得到的 <strong>不同</strong> 平均值的数目。</p>
|
||||
|
||||
<p><strong>注意</strong> ,如果最小值或者最大值有重复元素,可以删除任意一个。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [4,1,4,0,3,5]
|
||||
<b>输出:</b>2
|
||||
<strong>解释:</strong>
|
||||
1. 删除 0 和 5 ,平均值是 (0 + 5) / 2 = 2.5 ,现在 nums = [4,1,4,3] 。
|
||||
2. 删除 1 和 4 ,平均值是 (1 + 4) / 2 = 2.5 ,现在 nums = [4,3] 。
|
||||
3. 删除 3 和 4 ,平均值是 (3 + 4) / 2 = 3.5 。
|
||||
2.5 ,2.5 和 3.5 之中总共有 2 个不同的数,我们返回 2 。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>nums = [1,100]
|
||||
<b>输出:</b>1
|
||||
<strong>解释:</strong>
|
||||
删除 1 和 100 后只有一个平均值,所以我们返回 1 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 100</code></li>
|
||||
<li><code>nums.length</code> 是偶数。</li>
|
||||
<li><code>0 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
40
leetcode-cn/problem (Chinese)/不重叠回文子字符串的最大数目 [maximum-number-of-non-overlapping-palindrome-substrings].html
Normal file
40
leetcode-cn/problem (Chinese)/不重叠回文子字符串的最大数目 [maximum-number-of-non-overlapping-palindrome-substrings].html
Normal file
@ -0,0 +1,40 @@
|
||||
<p>给你一个字符串 <code>s</code> 和一个 <strong>正</strong> 整数 <code>k</code> 。</p>
|
||||
|
||||
<p>从字符串 <code>s</code> 中选出一组满足下述条件且 <strong>不重叠</strong> 的子字符串:</p>
|
||||
|
||||
<ul>
|
||||
<li>每个子字符串的长度 <strong>至少</strong> 为 <code>k</code> 。</li>
|
||||
<li>每个子字符串是一个 <strong>回文串</strong> 。</li>
|
||||
</ul>
|
||||
|
||||
<p>返回最优方案中能选择的子字符串的 <strong>最大</strong> 数目。</p>
|
||||
|
||||
<p><strong>子字符串</strong> 是字符串中一个连续的字符序列。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1 :</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "abaccdbbd", k = 3
|
||||
<strong>输出:</strong>2
|
||||
<strong>解释:</strong>可以选择 s = "<em><strong>aba</strong></em>cc<em><strong>dbbd</strong></em>" 中斜体加粗的子字符串。"aba" 和 "dbbd" 都是回文,且长度至少为 k = 3 。
|
||||
可以证明,无法选出两个以上的有效子字符串。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2 :</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>s = "adbcda", k = 2
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>字符串中不存在长度至少为 2 的回文子字符串。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= s.length <= 2000</code></li>
|
||||
<li><code>s</code> 仅由小写英文字母组成</li>
|
||||
</ul>
|
34
leetcode-cn/problem (Chinese)/最小公倍数为 K 的子数组数目 [number-of-subarrays-with-lcm-equal-to-k].html
Normal file
34
leetcode-cn/problem (Chinese)/最小公倍数为 K 的子数组数目 [number-of-subarrays-with-lcm-equal-to-k].html
Normal file
@ -0,0 +1,34 @@
|
||||
<p>给你一个整数数组 <code>nums</code> 和一个整数 <code>k</code> ,请你统计并返回 <code>nums</code> 的 <strong>子数组</strong> 中满足 <em>元素最小公倍数为 <code>k</code> </em>的子数组数目。</p>
|
||||
|
||||
<p><strong>子数组</strong> 是数组中一个连续非空的元素序列。</p>
|
||||
|
||||
<p><strong>数组的最小公倍数</strong> 是可被所有数组元素整除的最小正整数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1 :</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [3,6,2,7,1], k = 6
|
||||
<strong>输出:</strong>4
|
||||
<strong>解释:</strong>以 6 为最小公倍数的子数组是:
|
||||
- [<em><strong>3</strong></em>,<em><strong>6</strong></em>,2,7,1]
|
||||
- [<em><strong>3</strong></em>,<em><strong>6</strong></em>,<em><strong>2</strong></em>,7,1]
|
||||
- [3,<em><strong>6</strong></em>,2,7,1]
|
||||
- [3,<em><strong>6</strong></em>,<em><strong>2</strong></em>,7,1]
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2 :</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>nums = [3], k = 2
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>不存在以 2 为最小公倍数的子数组。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i], k <= 1000</code></li>
|
||||
</ul>
|
@ -0,0 +1,73 @@
|
||||
<p>一个 <code>n</code> 个节点的无向树,节点编号为 <code>0</code> 到 <code>n - 1</code> ,树的根结点是 <code>0</code> 号节点。给你一个长度为 <code>n - 1</code> 的二维整数数组 <code>edges</code> ,其中 <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> ,表示节点 <code>a<sub>i</sub></code> 和 <code>b<sub>i</sub></code> 在树中有一条边。</p>
|
||||
|
||||
<p>在每一个节点 <code>i</code> 处有一扇门。同时给你一个都是偶数的数组 <code>amount</code> ,其中 <code>amount[i]</code> 表示:</p>
|
||||
|
||||
<ul>
|
||||
<li>如果 <code>amount[i]</code> 的值是负数,那么它表示打开节点 <code>i</code> 处门扣除的分数。</li>
|
||||
<li>如果 <code>amount[i]</code> 的值是正数,那么它表示打开节点 <code>i</code> 处门加上的分数。</li>
|
||||
</ul>
|
||||
|
||||
<p>游戏按照如下规则进行:</p>
|
||||
|
||||
<ul>
|
||||
<li>一开始,Alice 在节点 <code>0</code> 处,Bob 在节点 <code>bob</code> 处。</li>
|
||||
<li>每一秒钟,Alice 和 Bob <strong>分别</strong> 移动到相邻的节点。Alice 朝着某个 <strong>叶子结点</strong> 移动,Bob 朝着节点 <code>0</code> 移动。</li>
|
||||
<li>对于他们之间路径上的 <strong>每一个</strong> 节点,Alice 和 Bob 要么打开门并扣分,要么打开门并加分。注意:
|
||||
<ul>
|
||||
<li>如果门 <strong>已经打开</strong> (被另一个人打开),不会有额外加分也不会扣分。</li>
|
||||
<li>如果 Alice 和 Bob <strong>同时</strong> 到达一个节点,他们会共享这个节点的加分或者扣分。换言之,如果打开这扇门扣 <code>c</code> 分,那么 Alice 和 Bob 分别扣 <code>c / 2</code> 分。如果这扇门的加分为 <code>c</code> ,那么他们分别加 <code>c / 2</code> 分。</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>如果 Alice 到达了一个叶子结点,她会停止移动。类似的,如果 Bob 到达了节点 <code>0</code> ,他也会停止移动。注意这些事件互相 <strong>独立</strong> ,不会影响另一方移动。</li>
|
||||
</ul>
|
||||
|
||||
<p>请你返回 Alice 朝最优叶子结点移动的 <strong>最大</strong> 净得分。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/10/29/eg1.png" style="width: 275px; height: 275px;"></p>
|
||||
|
||||
<pre><b>输入:</b>edges = [[0,1],[1,2],[1,3],[3,4]], bob = 3, amount = [-2,4,2,-4,6]
|
||||
<b>输出:</b>6
|
||||
<b>解释:</b>
|
||||
上图展示了输入给出的一棵树。游戏进行如下:
|
||||
- Alice 一开始在节点 0 处,Bob 在节点 3 处。他们分别打开所在节点的门。
|
||||
Alice 得分为 -2 。
|
||||
- Alice 和 Bob 都移动到节点 1 。
|
||||
因为他们同时到达这个节点,他们一起打开门并平分得分。
|
||||
Alice 的得分变为 -2 + (4 / 2) = 0 。
|
||||
- Alice 移动到节点 3 。因为 Bob 已经打开了这扇门,Alice 得分不变。
|
||||
Bob 移动到节点 0 ,并停止移动。
|
||||
- Alice 移动到节点 4 并打开这个节点的门,她得分变为 0 + 6 = 6 。
|
||||
现在,Alice 和 Bob 都不能进行任何移动了,所以游戏结束。
|
||||
Alice 无法得到更高分数。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/10/29/eg2.png" style="width: 250px; height: 78px;"></p>
|
||||
|
||||
<pre><b>输入:</b>edges = [[0,1]], bob = 1, amount = [-7280,2350]
|
||||
<b>输出:</b>-7280
|
||||
<b>解释:</b>
|
||||
Alice 按照路径 0->1 移动,同时 Bob 按照路径 1->0 移动。
|
||||
所以 Alice 只打开节点 0 处的门,她的得分为 -7280 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li><code>edges</code> 表示一棵有效的树。</li>
|
||||
<li><code>1 <= bob < n</code></li>
|
||||
<li><code>amount.length == n</code></li>
|
||||
<li><code>amount[i]</code> 是范围 <code>[-10<sup>4</sup>, 10<sup>4</sup>]</code> 之间的一个 <strong>偶数</strong> 。</li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>给你一个字符串 <code>message</code> 和一个正整数 <code>limit</code> 。</p>
|
||||
|
||||
<p>你需要根据 <code>limit</code> 将 <code>message</code> <strong>分割</strong> 成一个或多个 <strong>部分</strong> 。每个部分的结尾都是 <code>"<a/b>"</code> ,其中 <code>"b"</code> 用分割出来的总数 <b>替换</b>, <code>"a"</code> 用当前部分所在的编号 <strong>替换</strong> ,编号从 <code>1</code> 到 <code>b</code> 依次编号。除此以外,除了最后一部分长度 <strong>小于等于</strong> <code>limit</code> 以外,其他每一部分(包括结尾部分)的长度都应该 <strong>等于</strong> <code>limit</code> 。</p>
|
||||
|
||||
<p>你需要确保分割后的结果数组,删掉每部分的结尾并<strong> 按顺序 </strong>连起来后,能够得到 <code>message</code> 。同时,结果数组越短越好。</p>
|
||||
|
||||
<p>请你返回<em> </em><code>message</code> 分割后得到的结果数组。如果无法按要求分割 <code>message</code> ,返回一个空数组。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>message = "this is really a very awesome message", limit = 9
|
||||
<b>输出:</b>["thi<1/14>","s i<2/14>","s r<3/14>","eal<4/14>","ly <5/14>","a v<6/14>","ery<7/14>"," aw<8/14>","eso<9/14>","me<10/14>"," m<11/14>","es<12/14>","sa<13/14>","ge<14/14>"]
|
||||
<strong>解释:</strong>
|
||||
前面 9 个部分分别从 message 中得到 3 个字符。
|
||||
接下来的 5 个部分分别从 message 中得到 2 个字符。
|
||||
这个例子中,包含最后一个部分在内,每个部分的长度都为 9 。
|
||||
可以证明没有办法分割成少于 14 个部分。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>message = "short message", limit = 15
|
||||
<b>输出:</b>["short mess<1/2>","age<2/2>"]
|
||||
<strong>解释:</strong>
|
||||
在给定限制下,字符串可以分成两个部分:
|
||||
- 第一个部分包含 10 个字符,长度为 15 。
|
||||
- 第二个部分包含 3 个字符,长度为 8 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= message.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>message</code> 只包含小写英文字母和 <code>' '</code> 。</li>
|
||||
<li><code>1 <= limit <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,35 @@
|
||||
<p>给你一个四舍五入到两位小数的非负浮点数 <code>celsius</code> 来表示温度,以 <strong>摄氏度</strong>(<strong>Celsius</strong>)为单位。</p>
|
||||
|
||||
<p>你需要将摄氏度转换为 <strong>开氏度</strong>(<strong>Kelvin</strong>)和 <strong>华氏度</strong>(<strong>Fahrenheit</strong>),并以数组 <code>ans = [kelvin, fahrenheit]</code> 的形式返回结果。</p>
|
||||
|
||||
<p>返回数组<em> <code>ans</code></em> 。与实际答案误差不超过 <code>10<sup>-5</sup></code> 的会视为正确答案<strong>。</strong></p>
|
||||
|
||||
<p><strong>注意:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>开氏度 = 摄氏度 + 273.15</code></li>
|
||||
<li><code>华氏度 = 摄氏度 * 1.80 + 32.00</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1 :</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>celsius = 36.50
|
||||
<strong>输出:</strong>[309.65000,97.70000]
|
||||
<strong>解释:</strong>36.50 摄氏度:转换为开氏度是 309.65 ,转换为华氏度是 97.70 。</pre>
|
||||
|
||||
<p><strong>示例 2 :</strong></p>
|
||||
|
||||
<pre><strong>输入:</strong>celsius = 122.11
|
||||
<strong>输出:</strong>[395.26000,251.79800]
|
||||
<strong>解释:</strong>122.11 摄氏度:转换为开氏度是 395.26 ,转换为华氏度是 251.798 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= celsius <= 1000</code></li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>给你整数 <code>zero</code> ,<code>one</code> ,<code>low</code> 和 <code>high</code> ,我们从空字符串开始构造一个字符串,每一步执行下面操作中的一种:</p>
|
||||
|
||||
<ul>
|
||||
<li>将 <code>'0'</code> 在字符串末尾添加 <code>zero</code> 次。</li>
|
||||
<li>将 <code>'1'</code> 在字符串末尾添加 <code>one</code> 次。</li>
|
||||
</ul>
|
||||
|
||||
<p>以上操作可以执行任意次。</p>
|
||||
|
||||
<p>如果通过以上过程得到一个 <strong>长度</strong> 在 <code>low</code> 和 <code>high</code> 之间(包含上下边界)的字符串,那么这个字符串我们称为 <strong>好</strong> 字符串。</p>
|
||||
|
||||
<p>请你返回满足以上要求的 <strong>不同</strong> 好字符串数目。由于答案可能很大,请将结果对 <code>10<sup>9</sup> + 7</code> <strong>取余</strong> 后返回。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>low = 3, high = 3, zero = 1, one = 1
|
||||
<b>输出:</b>8
|
||||
<b>解释:</b>
|
||||
一个可能的好字符串是 "011" 。
|
||||
可以这样构造得到:"" -> "0" -> "01" -> "011" 。
|
||||
从 "000" 到 "111" 之间所有的二进制字符串都是好字符串。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre><b>输入:</b>low = 2, high = 3, zero = 1, one = 2
|
||||
<b>输出:</b>5
|
||||
<b>解释:</b>好字符串为 "00" ,"11" ,"000" ,"110" 和 "011" 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= zero, one <= low</code></li>
|
||||
</ul>
|
50
leetcode-cn/problem (Chinese)/逐层排序二叉树所需的最少操作数目 [minimum-number-of-operations-to-sort-a-binary-tree-by-level].html
Normal file
50
leetcode-cn/problem (Chinese)/逐层排序二叉树所需的最少操作数目 [minimum-number-of-operations-to-sort-a-binary-tree-by-level].html
Normal file
@ -0,0 +1,50 @@
|
||||
<p>给你一个 <strong>值互不相同</strong> 的二叉树的根节点 <code>root</code> 。</p>
|
||||
|
||||
<p>在一步操作中,你可以选择 <strong>同一层</strong> 上任意两个节点,交换这两个节点的值。</p>
|
||||
|
||||
<p>返回每一层按 <strong>严格递增顺序</strong> 排序所需的最少操作数目。</p>
|
||||
|
||||
<p>节点的 <strong>层数</strong> 是该节点和根节点之间的路径的边数。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例 1 :</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174006-2.png" style="width: 500px; height: 324px;">
|
||||
<pre><strong>输入:</strong>root = [1,4,3,7,6,8,5,null,null,null,null,9,null,10]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:</strong>
|
||||
- 交换 4 和 3 。第 2 层变为 [3,4] 。
|
||||
- 交换 7 和 5 。第 3 层变为 [5,6,8,7] 。
|
||||
- 交换 8 和 7 。第 3 层变为 [5,6,7,8] 。
|
||||
共计用了 3 步操作,所以返回 3 。
|
||||
可以证明 3 是需要的最少操作数目。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2 :</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174026-3.png" style="width: 400px; height: 303px;">
|
||||
<pre><strong>输入:</strong>root = [1,3,2,7,6,5,4]
|
||||
<strong>输出:</strong>3
|
||||
<strong>解释:
|
||||
</strong>- 交换 3 和 2 。第 2 层变为 [2,3] 。
|
||||
- 交换 7 和 4 。第 3 层变为 [4,6,5,7] 。
|
||||
- 交换 6 和 5 。第 3 层变为 [4,5,6,7] 。
|
||||
共计用了 3 步操作,所以返回 3 。
|
||||
可以证明 3 是需要的最少操作数目。
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 3 :</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174052-4.png" style="width: 400px; height: 274px;">
|
||||
<pre><strong>输入:</strong>root = [1,2,3,4,5,6]
|
||||
<strong>输出:</strong>0
|
||||
<strong>解释:</strong>每一层已经按递增顺序排序,所以返回 0 。
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>树中节点的数目在范围 <code>[1, 10<sup>5</sup>]</code> 。</li>
|
||||
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
|
||||
<li>树中的所有值 <strong>互不相同</strong> 。</li>
|
||||
</ul>
|
@ -0,0 +1,50 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length.</p>
|
||||
|
||||
<p>As long as <code>nums</code> is <strong>not</strong> empty, you must repetitively:</p>
|
||||
|
||||
<ul>
|
||||
<li>Find the minimum number in <code>nums</code> and remove it.</li>
|
||||
<li>Find the maximum number in <code>nums</code> and remove it.</li>
|
||||
<li>Calculate the average of the two removed numbers.</li>
|
||||
</ul>
|
||||
|
||||
<p>The <strong>average</strong> of two numbers <code>a</code> and <code>b</code> is <code>(a + b) / 2</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, the average of <code>2</code> and <code>3</code> is <code>(2 + 3) / 2 = 2.5</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> the number of <strong>distinct</strong> averages calculated using the above process</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that when there is a tie for a minimum or maximum number, any can be removed.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,1,4,0,3,5]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
1. Remove 0 and 5, and the average is (0 + 5) / 2 = 2.5. Now, nums = [4,1,4,3].
|
||||
2. Remove 1 and 4. The average is (1 + 4) / 2 = 2.5, and nums = [4,3].
|
||||
3. Remove 3 and 4, and the average is (3 + 4) / 2 = 3.5.
|
||||
Since there are 2 distinct numbers among 2.5, 2.5, and 3.5, we return 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,100]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
There is only one average to be calculated after removing 1 and 100, so we return 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 100</code></li>
|
||||
<li><code>nums.length</code> is even.</li>
|
||||
<li><code>0 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
38
leetcode-cn/problem (English)/不重叠回文子字符串的最大数目(English) [maximum-number-of-non-overlapping-palindrome-substrings].html
Normal file
38
leetcode-cn/problem (English)/不重叠回文子字符串的最大数目(English) [maximum-number-of-non-overlapping-palindrome-substrings].html
Normal file
@ -0,0 +1,38 @@
|
||||
<p>You are given a string <code>s</code> and a <strong>positive</strong> integer <code>k</code>.</p>
|
||||
|
||||
<p>Select a set of <strong>non-overlapping</strong> substrings from the string <code>s</code> that satisfy the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>The <strong>length</strong> of each substring is <strong>at least</strong> <code>k</code>.</li>
|
||||
<li>Each substring is a <strong>palindrome</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of substrings in an optimal selection</em>.</p>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abaccdbbd", k = 3
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can select the substrings underlined in s = "<u><strong>aba</strong></u>cc<u><strong>dbbd</strong></u>". Both "aba" and "dbbd" are palindromes and have a length of at least k = 3.
|
||||
It can be shown that we cannot find a selection with more than two valid substrings.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "adbcda", k = 2
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There is no palindrome substring of length at least 2 in the string.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= s.length <= 2000</code></li>
|
||||
<li><code>s</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
34
leetcode-cn/problem (English)/最小公倍数为 K 的子数组数目(English) [number-of-subarrays-with-lcm-equal-to-k].html
Normal file
34
leetcode-cn/problem (English)/最小公倍数为 K 的子数组数目(English) [number-of-subarrays-with-lcm-equal-to-k].html
Normal file
@ -0,0 +1,34 @@
|
||||
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the number of <strong>subarrays</strong> of </em><code>nums</code><em> where the least common multiple of the subarray's elements is </em><code>k</code>.</p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous non-empty sequence of elements within an array.</p>
|
||||
|
||||
<p>The <strong>least common multiple of an array</strong> is the smallest positive integer that is divisible by all the array elements.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,6,2,7,1], k = 6
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The subarrays of nums where 6 is the least common multiple of all the subarray's elements are:
|
||||
- [<u><strong>3</strong></u>,<u><strong>6</strong></u>,2,7,1]
|
||||
- [<u><strong>3</strong></u>,<u><strong>6</strong></u>,<u><strong>2</strong></u>,7,1]
|
||||
- [3,<u><strong>6</strong></u>,2,7,1]
|
||||
- [3,<u><strong>6</strong></u>,<u><strong>2</strong></u>,7,1]
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3], k = 2
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are no subarrays of nums where 2 is the least common multiple of all the subarray's elements.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i], k <= 1000</code></li>
|
||||
</ul>
|
69
leetcode-cn/problem (English)/树上最大得分和路径(English) [most-profitable-path-in-a-tree].html
Normal file
69
leetcode-cn/problem (English)/树上最大得分和路径(English) [most-profitable-path-in-a-tree].html
Normal file
@ -0,0 +1,69 @@
|
||||
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.</p>
|
||||
|
||||
<p>At every node <code>i</code>, there is a gate. You are also given an array of even integers <code>amount</code>, where <code>amount[i]</code> represents:</p>
|
||||
|
||||
<ul>
|
||||
<li>the price needed to open the gate at node <code>i</code>, if <code>amount[i]</code> is negative, or,</li>
|
||||
<li>the cash reward obtained on opening the gate at node <code>i</code>, otherwise.</li>
|
||||
</ul>
|
||||
|
||||
<p>The game goes on as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li>Initially, Alice is at node <code>0</code> and Bob is at node <code>bob</code>.</li>
|
||||
<li>At every second, Alice and Bob <b>each</b> move to an adjacent node. Alice moves towards some <strong>leaf node</strong>, while Bob moves towards node <code>0</code>.</li>
|
||||
<li>For <strong>every</strong> node along their path, Alice and Bob either spend money to open the gate at that node, or accept the reward. Note that:
|
||||
<ul>
|
||||
<li>If the gate is <strong>already open</strong>, no price will be required, nor will there be any cash reward.</li>
|
||||
<li>If Alice and Bob reach the node <strong>simultaneously</strong>, they share the price/reward for opening the gate there. In other words, if the price to open the gate is <code>c</code>, then both Alice and Bob pay <code>c / 2</code> each. Similarly, if the reward at the gate is <code>c</code>, both of them receive <code>c / 2</code> each.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>If Alice reaches a leaf node, she stops moving. Similarly, if Bob reaches node <code>0</code>, he stops moving. Note that these events are <strong>independent</strong> of each other.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> the <strong>maximum</strong> net income Alice can have if she travels towards the optimal leaf node.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/29/eg1.png" style="width: 275px; height: 275px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1],[1,2],[1,3],[3,4]], bob = 3, amount = [-2,4,2,-4,6]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong>
|
||||
The above diagram represents the given tree. The game goes as follows:
|
||||
- Alice is initially on node 0, Bob on node 3. They open the gates of their respective nodes.
|
||||
Alice's net income is now -2.
|
||||
- Both Alice and Bob move to node 1.
|
||||
Since they reach here simultaneously, they open the gate together and share the reward.
|
||||
Alice's net income becomes -2 + (4 / 2) = 0.
|
||||
- Alice moves on to node 3. Since Bob already opened its gate, Alice's income remains unchanged.
|
||||
Bob moves on to node 0, and stops moving.
|
||||
- Alice moves on to node 4 and opens the gate there. Her net income becomes 0 + 6 = 6.
|
||||
Now, neither Alice nor Bob can make any further moves, and the game ends.
|
||||
It is not possible for Alice to get a higher net income.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/29/eg2.png" style="width: 250px; height: 78px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1]], bob = 1, amount = [-7280,2350]
|
||||
<strong>Output:</strong> -7280
|
||||
<strong>Explanation:</strong>
|
||||
Alice follows the path 0->1 whereas Bob follows the path 1->0.
|
||||
Thus, Alice opens the gate at node 0 only. Hence, her net income is -7280.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li><code>edges</code> represents a valid tree.</li>
|
||||
<li><code>1 <= bob < n</code></li>
|
||||
<li><code>amount.length == n</code></li>
|
||||
<li><code>amount[i]</code> is an <strong>even</strong> integer in the range <code>[-10<sup>4</sup>, 10<sup>4</sup>]</code>.</li>
|
||||
</ul>
|
@ -0,0 +1,40 @@
|
||||
<p>You are given a string, <code>message</code>, and a positive integer, <code>limit</code>.</p>
|
||||
|
||||
<p>You must <strong>split</strong> <code>message</code> into one or more <strong>parts</strong> based on <code>limit</code>. Each resulting part should have the suffix <code>"<a/b>"</code>, where <code>"b"</code> is to be <strong>replaced</strong> with the total number of parts and <code>"a"</code> is to be <strong>replaced</strong> with the index of the part, starting from <code>1</code> and going up to <code>b</code>. Additionally, the length of each resulting part (including its suffix) should be <strong>equal</strong> to <code>limit</code>, except for the last part whose length can be <strong>at most</strong> <code>limit</code>.</p>
|
||||
|
||||
<p>The resulting parts should be formed such that when their suffixes are removed and they are all concatenated <strong>in order</strong>, they should be equal to <code>message</code>. Also, the result should contain as few parts as possible.</p>
|
||||
|
||||
<p>Return<em> the parts </em><code>message</code><em> would be split into as an array of strings</em>. If it is impossible to split <code>message</code> as required, return<em> an empty array</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> message = "this is really a very awesome message", limit = 9
|
||||
<strong>Output:</strong> ["thi<1/14>","s i<2/14>","s r<3/14>","eal<4/14>","ly <5/14>","a v<6/14>","ery<7/14>"," aw<8/14>","eso<9/14>","me<10/14>"," m<11/14>","es<12/14>","sa<13/14>","ge<14/14>"]
|
||||
<strong>Explanation:</strong>
|
||||
The first 9 parts take 3 characters each from the beginning of message.
|
||||
The next 5 parts take 2 characters each to finish splitting message.
|
||||
In this example, each part, including the last, has length 9.
|
||||
It can be shown it is not possible to split message into less than 14 parts.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> message = "short message", limit = 15
|
||||
<strong>Output:</strong> ["short mess<1/2>","age<2/2>"]
|
||||
<strong>Explanation:</strong>
|
||||
Under the given constraints, the string can be split into two parts:
|
||||
- The first part comprises of the first 10 characters, and has a length 15.
|
||||
- The next part comprises of the last 3 characters, and has a length 8.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= message.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>message</code> consists only of lowercase English letters and <code>' '</code>.</li>
|
||||
<li><code>1 <= limit <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
@ -0,0 +1,36 @@
|
||||
<p>You are given a non-negative floating point number rounded to two decimal places <code>celsius</code>, that denotes the <strong>temperature in Celsius</strong>.</p>
|
||||
|
||||
<p>You should convert Celsius into <strong>Kelvin</strong> and <strong>Fahrenheit</strong> and return it as an array <code>ans = [kelvin, fahrenheit]</code>.</p>
|
||||
|
||||
<p>Return <em>the array <code>ans</code>. </em>Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.</p>
|
||||
|
||||
<p><strong>Note that:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>Kelvin = Celsius + 273.15</code></li>
|
||||
<li><code>Fahrenheit = Celsius * 1.80 + 32.00</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> celsius = 36.50
|
||||
<strong>Output:</strong> [309.65000,97.70000]
|
||||
<strong>Explanation:</strong> Temperature at 36.50 Celsius converted in Kelvin is 309.65 and converted in Fahrenheit is 97.70.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> celsius = 122.11
|
||||
<strong>Output:</strong> [395.26000,251.79800]
|
||||
<strong>Explanation:</strong> Temperature at 122.11 Celsius converted in Kelvin is 395.26 and converted in Fahrenheit is 251.798.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= celsius <= 1000</code></li>
|
||||
</ul>
|
40
leetcode-cn/problem (English)/统计构造好字符串的方案数(English) [count-ways-to-build-good-strings].html
Normal file
40
leetcode-cn/problem (English)/统计构造好字符串的方案数(English) [count-ways-to-build-good-strings].html
Normal file
@ -0,0 +1,40 @@
|
||||
<p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
|
||||
|
||||
<ul>
|
||||
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
|
||||
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
|
||||
</ul>
|
||||
|
||||
<p>This can be performed any number of times.</p>
|
||||
|
||||
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
|
||||
|
||||
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong>
|
||||
One possible valid good string is "011".
|
||||
It can be constructed as follows: "" -> "0" -> "01" -> "011".
|
||||
All binary strings from "000" to "111" are good strings in this example.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= zero, one <= low</code></li>
|
||||
</ul>
|
51
leetcode-cn/problem (English)/逐层排序二叉树所需的最少操作数目(English) [minimum-number-of-operations-to-sort-a-binary-tree-by-level].html
Normal file
51
leetcode-cn/problem (English)/逐层排序二叉树所需的最少操作数目(English) [minimum-number-of-operations-to-sort-a-binary-tree-by-level].html
Normal file
@ -0,0 +1,51 @@
|
||||
<p>You are given the <code>root</code> of a binary tree with <strong>unique values</strong>.</p>
|
||||
|
||||
<p>In one operation, you can choose any two nodes <strong>at the same level</strong> and swap their values.</p>
|
||||
|
||||
<p>Return <em>the minimum number of operations needed to make the values at each level sorted in a <strong>strictly increasing order</strong></em>.</p>
|
||||
|
||||
<p>The <strong>level</strong> of a node is the number of edges along the path between it and the root node<em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174006-2.png" style="width: 500px; height: 324px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,4,3,7,6,8,5,null,null,null,null,9,null,10]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
- Swap 4 and 3. The 2<sup>nd</sup> level becomes [3,4].
|
||||
- Swap 7 and 5. The 3<sup>rd</sup> level becomes [5,6,8,7].
|
||||
- Swap 8 and 7. The 3<sup>rd</sup> level becomes [5,6,7,8].
|
||||
We used 3 operations so return 3.
|
||||
It can be proven that 3 is the minimum number of operations needed.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174026-3.png" style="width: 400px; height: 303px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,3,2,7,6,5,4]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
- Swap 3 and 2. The 2<sup>nd</sup> level becomes [2,3].
|
||||
- Swap 7 and 4. The 3<sup>rd</sup> level becomes [4,6,5,7].
|
||||
- Swap 6 and 5. The 3<sup>rd</sup> level becomes [4,5,6,7].
|
||||
We used 3 operations so return 3.
|
||||
It can be proven that 3 is the minimum number of operations needed.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174052-4.png" style="width: 400px; height: 274px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,2,3,4,5,6]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Each level is already sorted in increasing order so return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
|
||||
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
|
||||
<li>All the values of the tree are <strong>unique</strong>.</li>
|
||||
</ul>
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
160
leetcode/originData/convert-the-temperature.json
Normal file
160
leetcode/originData/convert-the-temperature.json
Normal file
File diff suppressed because one or more lines are too long
161
leetcode/originData/count-ways-to-build-good-strings.json
Normal file
161
leetcode/originData/count-ways-to-build-good-strings.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
163
leetcode/originData/minimum-number-of-operations-to-sort-a-binary-tree-by-level.json
Normal file
163
leetcode/originData/minimum-number-of-operations-to-sort-a-binary-tree-by-level.json
Normal file
File diff suppressed because one or more lines are too long
162
leetcode/originData/most-profitable-path-in-a-tree.json
Normal file
162
leetcode/originData/most-profitable-path-in-a-tree.json
Normal file
File diff suppressed because one or more lines are too long
161
leetcode/originData/number-of-distinct-averages.json
Normal file
161
leetcode/originData/number-of-distinct-averages.json
Normal file
File diff suppressed because one or more lines are too long
162
leetcode/originData/number-of-subarrays-with-lcm-equal-to-k.json
Normal file
162
leetcode/originData/number-of-subarrays-with-lcm-equal-to-k.json
Normal file
File diff suppressed because one or more lines are too long
162
leetcode/originData/split-message-based-on-limit.json
Normal file
162
leetcode/originData/split-message-based-on-limit.json
Normal file
File diff suppressed because one or more lines are too long
36
leetcode/problem/convert-the-temperature.html
Normal file
36
leetcode/problem/convert-the-temperature.html
Normal file
@ -0,0 +1,36 @@
|
||||
<p>You are given a non-negative floating point number rounded to two decimal places <code>celsius</code>, that denotes the <strong>temperature in Celsius</strong>.</p>
|
||||
|
||||
<p>You should convert Celsius into <strong>Kelvin</strong> and <strong>Fahrenheit</strong> and return it as an array <code>ans = [kelvin, fahrenheit]</code>.</p>
|
||||
|
||||
<p>Return <em>the array <code>ans</code>. </em>Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.</p>
|
||||
|
||||
<p><strong>Note that:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>Kelvin = Celsius + 273.15</code></li>
|
||||
<li><code>Fahrenheit = Celsius * 1.80 + 32.00</code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> celsius = 36.50
|
||||
<strong>Output:</strong> [309.65000,97.70000]
|
||||
<strong>Explanation:</strong> Temperature at 36.50 Celsius converted in Kelvin is 309.65 and converted in Fahrenheit is 97.70.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> celsius = 122.11
|
||||
<strong>Output:</strong> [395.26000,251.79800]
|
||||
<strong>Explanation:</strong> Temperature at 122.11 Celsius converted in Kelvin is 395.26 and converted in Fahrenheit is 251.798.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= celsius <= 1000</code></li>
|
||||
</ul>
|
40
leetcode/problem/count-ways-to-build-good-strings.html
Normal file
40
leetcode/problem/count-ways-to-build-good-strings.html
Normal file
@ -0,0 +1,40 @@
|
||||
<p>Given the integers <code>zero</code>, <code>one</code>, <code>low</code>, and <code>high</code>, we can construct a string by starting with an empty string, and then at each step perform either of the following:</p>
|
||||
|
||||
<ul>
|
||||
<li>Append the character <code>'0'</code> <code>zero</code> times.</li>
|
||||
<li>Append the character <code>'1'</code> <code>one</code> times.</li>
|
||||
</ul>
|
||||
|
||||
<p>This can be performed any number of times.</p>
|
||||
|
||||
<p>A <strong>good</strong> string is a string constructed by the above process having a <strong>length</strong> between <code>low</code> and <code>high</code> (<strong>inclusive</strong>).</p>
|
||||
|
||||
<p>Return <em>the number of <strong>different</strong> good strings that can be constructed satisfying these properties.</em> Since the answer can be large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> low = 3, high = 3, zero = 1, one = 1
|
||||
<strong>Output:</strong> 8
|
||||
<strong>Explanation:</strong>
|
||||
One possible valid good string is "011".
|
||||
It can be constructed as follows: "" -> "0" -> "01" -> "011".
|
||||
All binary strings from "000" to "111" are good strings in this example.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> low = 2, high = 3, zero = 1, one = 2
|
||||
<strong>Output:</strong> 5
|
||||
<strong>Explanation:</strong> The good strings are "00", "11", "000", "110", and "011".
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= low <= high <= 10<sup>5</sup></code></li>
|
||||
<li><code>1 <= zero, one <= low</code></li>
|
||||
</ul>
|
@ -0,0 +1,38 @@
|
||||
<p>You are given a string <code>s</code> and a <strong>positive</strong> integer <code>k</code>.</p>
|
||||
|
||||
<p>Select a set of <strong>non-overlapping</strong> substrings from the string <code>s</code> that satisfy the following conditions:</p>
|
||||
|
||||
<ul>
|
||||
<li>The <strong>length</strong> of each substring is <strong>at least</strong> <code>k</code>.</li>
|
||||
<li>Each substring is a <strong>palindrome</strong>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return <em>the <strong>maximum</strong> number of substrings in an optimal selection</em>.</p>
|
||||
|
||||
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "abaccdbbd", k = 3
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong> We can select the substrings underlined in s = "<u><strong>aba</strong></u>cc<u><strong>dbbd</strong></u>". Both "aba" and "dbbd" are palindromes and have a length of at least k = 3.
|
||||
It can be shown that we cannot find a selection with more than two valid substrings.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> s = "adbcda", k = 2
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There is no palindrome substring of length at least 2 in the string.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= k <= s.length <= 2000</code></li>
|
||||
<li><code>s</code> consists of lowercase English letters.</li>
|
||||
</ul>
|
@ -0,0 +1,51 @@
|
||||
<p>You are given the <code>root</code> of a binary tree with <strong>unique values</strong>.</p>
|
||||
|
||||
<p>In one operation, you can choose any two nodes <strong>at the same level</strong> and swap their values.</p>
|
||||
|
||||
<p>Return <em>the minimum number of operations needed to make the values at each level sorted in a <strong>strictly increasing order</strong></em>.</p>
|
||||
|
||||
<p>The <strong>level</strong> of a node is the number of edges along the path between it and the root node<em>.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174006-2.png" style="width: 500px; height: 324px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,4,3,7,6,8,5,null,null,null,null,9,null,10]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
- Swap 4 and 3. The 2<sup>nd</sup> level becomes [3,4].
|
||||
- Swap 7 and 5. The 3<sup>rd</sup> level becomes [5,6,8,7].
|
||||
- Swap 8 and 7. The 3<sup>rd</sup> level becomes [5,6,7,8].
|
||||
We used 3 operations so return 3.
|
||||
It can be proven that 3 is the minimum number of operations needed.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174026-3.png" style="width: 400px; height: 303px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,3,2,7,6,5,4]
|
||||
<strong>Output:</strong> 3
|
||||
<strong>Explanation:</strong>
|
||||
- Swap 3 and 2. The 2<sup>nd</sup> level becomes [2,3].
|
||||
- Swap 7 and 4. The 3<sup>rd</sup> level becomes [4,6,5,7].
|
||||
- Swap 6 and 5. The 3<sup>rd</sup> level becomes [4,5,6,7].
|
||||
We used 3 operations so return 3.
|
||||
It can be proven that 3 is the minimum number of operations needed.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 3:</strong></p>
|
||||
<img src="https://assets.leetcode.com/uploads/2022/09/18/image-20220918174052-4.png" style="width: 400px; height: 274px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> root = [1,2,3,4,5,6]
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> Each level is already sorted in increasing order so return 0.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>The number of nodes in the tree is in the range <code>[1, 10<sup>5</sup>]</code>.</li>
|
||||
<li><code>1 <= Node.val <= 10<sup>5</sup></code></li>
|
||||
<li>All the values of the tree are <strong>unique</strong>.</li>
|
||||
</ul>
|
69
leetcode/problem/most-profitable-path-in-a-tree.html
Normal file
69
leetcode/problem/most-profitable-path-in-a-tree.html
Normal file
@ -0,0 +1,69 @@
|
||||
<p>There is an undirected tree with <code>n</code> nodes labeled from <code>0</code> to <code>n - 1</code>, rooted at node <code>0</code>. You are given a 2D integer array <code>edges</code> of length <code>n - 1</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an edge between nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code> in the tree.</p>
|
||||
|
||||
<p>At every node <code>i</code>, there is a gate. You are also given an array of even integers <code>amount</code>, where <code>amount[i]</code> represents:</p>
|
||||
|
||||
<ul>
|
||||
<li>the price needed to open the gate at node <code>i</code>, if <code>amount[i]</code> is negative, or,</li>
|
||||
<li>the cash reward obtained on opening the gate at node <code>i</code>, otherwise.</li>
|
||||
</ul>
|
||||
|
||||
<p>The game goes on as follows:</p>
|
||||
|
||||
<ul>
|
||||
<li>Initially, Alice is at node <code>0</code> and Bob is at node <code>bob</code>.</li>
|
||||
<li>At every second, Alice and Bob <b>each</b> move to an adjacent node. Alice moves towards some <strong>leaf node</strong>, while Bob moves towards node <code>0</code>.</li>
|
||||
<li>For <strong>every</strong> node along their path, Alice and Bob either spend money to open the gate at that node, or accept the reward. Note that:
|
||||
<ul>
|
||||
<li>If the gate is <strong>already open</strong>, no price will be required, nor will there be any cash reward.</li>
|
||||
<li>If Alice and Bob reach the node <strong>simultaneously</strong>, they share the price/reward for opening the gate there. In other words, if the price to open the gate is <code>c</code>, then both Alice and Bob pay <code>c / 2</code> each. Similarly, if the reward at the gate is <code>c</code>, both of them receive <code>c / 2</code> each.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>If Alice reaches a leaf node, she stops moving. Similarly, if Bob reaches node <code>0</code>, he stops moving. Note that these events are <strong>independent</strong> of each other.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> the <strong>maximum</strong> net income Alice can have if she travels towards the optimal leaf node.</em></p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/29/eg1.png" style="width: 275px; height: 275px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1],[1,2],[1,3],[3,4]], bob = 3, amount = [-2,4,2,-4,6]
|
||||
<strong>Output:</strong> 6
|
||||
<strong>Explanation:</strong>
|
||||
The above diagram represents the given tree. The game goes as follows:
|
||||
- Alice is initially on node 0, Bob on node 3. They open the gates of their respective nodes.
|
||||
Alice's net income is now -2.
|
||||
- Both Alice and Bob move to node 1.
|
||||
Since they reach here simultaneously, they open the gate together and share the reward.
|
||||
Alice's net income becomes -2 + (4 / 2) = 0.
|
||||
- Alice moves on to node 3. Since Bob already opened its gate, Alice's income remains unchanged.
|
||||
Bob moves on to node 0, and stops moving.
|
||||
- Alice moves on to node 4 and opens the gate there. Her net income becomes 0 + 6 = 6.
|
||||
Now, neither Alice nor Bob can make any further moves, and the game ends.
|
||||
It is not possible for Alice to get a higher net income.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
<img alt="" src="https://assets.leetcode.com/uploads/2022/10/29/eg2.png" style="width: 250px; height: 78px;" />
|
||||
<pre>
|
||||
<strong>Input:</strong> edges = [[0,1]], bob = 1, amount = [-7280,2350]
|
||||
<strong>Output:</strong> -7280
|
||||
<strong>Explanation:</strong>
|
||||
Alice follows the path 0->1 whereas Bob follows the path 1->0.
|
||||
Thus, Alice opens the gate at node 0 only. Hence, her net income is -7280.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= n <= 10<sup>5</sup></code></li>
|
||||
<li><code>edges.length == n - 1</code></li>
|
||||
<li><code>edges[i].length == 2</code></li>
|
||||
<li><code>0 <= a<sub>i</sub>, b<sub>i</sub> < n</code></li>
|
||||
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
|
||||
<li><code>edges</code> represents a valid tree.</li>
|
||||
<li><code>1 <= bob < n</code></li>
|
||||
<li><code>amount.length == n</code></li>
|
||||
<li><code>amount[i]</code> is an <strong>even</strong> integer in the range <code>[-10<sup>4</sup>, 10<sup>4</sup>]</code>.</li>
|
||||
</ul>
|
50
leetcode/problem/number-of-distinct-averages.html
Normal file
50
leetcode/problem/number-of-distinct-averages.html
Normal file
@ -0,0 +1,50 @@
|
||||
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of <strong>even</strong> length.</p>
|
||||
|
||||
<p>As long as <code>nums</code> is <strong>not</strong> empty, you must repetitively:</p>
|
||||
|
||||
<ul>
|
||||
<li>Find the minimum number in <code>nums</code> and remove it.</li>
|
||||
<li>Find the maximum number in <code>nums</code> and remove it.</li>
|
||||
<li>Calculate the average of the two removed numbers.</li>
|
||||
</ul>
|
||||
|
||||
<p>The <strong>average</strong> of two numbers <code>a</code> and <code>b</code> is <code>(a + b) / 2</code>.</p>
|
||||
|
||||
<ul>
|
||||
<li>For example, the average of <code>2</code> and <code>3</code> is <code>(2 + 3) / 2 = 2.5</code>.</li>
|
||||
</ul>
|
||||
|
||||
<p>Return<em> the number of <strong>distinct</strong> averages calculated using the above process</em>.</p>
|
||||
|
||||
<p><strong>Note</strong> that when there is a tie for a minimum or maximum number, any can be removed.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [4,1,4,0,3,5]
|
||||
<strong>Output:</strong> 2
|
||||
<strong>Explanation:</strong>
|
||||
1. Remove 0 and 5, and the average is (0 + 5) / 2 = 2.5. Now, nums = [4,1,4,3].
|
||||
2. Remove 1 and 4. The average is (1 + 4) / 2 = 2.5, and nums = [4,3].
|
||||
3. Remove 3 and 4, and the average is (3 + 4) / 2 = 3.5.
|
||||
Since there are 2 distinct numbers among 2.5, 2.5, and 3.5, we return 2.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [1,100]
|
||||
<strong>Output:</strong> 1
|
||||
<strong>Explanation:</strong>
|
||||
There is only one average to be calculated after removing 1 and 100, so we return 1.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>2 <= nums.length <= 100</code></li>
|
||||
<li><code>nums.length</code> is even.</li>
|
||||
<li><code>0 <= nums[i] <= 100</code></li>
|
||||
</ul>
|
@ -0,0 +1,34 @@
|
||||
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return <em>the number of <strong>subarrays</strong> of </em><code>nums</code><em> where the least common multiple of the subarray's elements is </em><code>k</code>.</p>
|
||||
|
||||
<p>A <strong>subarray</strong> is a contiguous non-empty sequence of elements within an array.</p>
|
||||
|
||||
<p>The <strong>least common multiple of an array</strong> is the smallest positive integer that is divisible by all the array elements.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3,6,2,7,1], k = 6
|
||||
<strong>Output:</strong> 4
|
||||
<strong>Explanation:</strong> The subarrays of nums where 6 is the least common multiple of all the subarray's elements are:
|
||||
- [<u><strong>3</strong></u>,<u><strong>6</strong></u>,2,7,1]
|
||||
- [<u><strong>3</strong></u>,<u><strong>6</strong></u>,<u><strong>2</strong></u>,7,1]
|
||||
- [3,<u><strong>6</strong></u>,2,7,1]
|
||||
- [3,<u><strong>6</strong></u>,<u><strong>2</strong></u>,7,1]
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> nums = [3], k = 2
|
||||
<strong>Output:</strong> 0
|
||||
<strong>Explanation:</strong> There are no subarrays of nums where 2 is the least common multiple of all the subarray's elements.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= nums.length <= 1000</code></li>
|
||||
<li><code>1 <= nums[i], k <= 1000</code></li>
|
||||
</ul>
|
40
leetcode/problem/split-message-based-on-limit.html
Normal file
40
leetcode/problem/split-message-based-on-limit.html
Normal file
@ -0,0 +1,40 @@
|
||||
<p>You are given a string, <code>message</code>, and a positive integer, <code>limit</code>.</p>
|
||||
|
||||
<p>You must <strong>split</strong> <code>message</code> into one or more <strong>parts</strong> based on <code>limit</code>. Each resulting part should have the suffix <code>"<a/b>"</code>, where <code>"b"</code> is to be <strong>replaced</strong> with the total number of parts and <code>"a"</code> is to be <strong>replaced</strong> with the index of the part, starting from <code>1</code> and going up to <code>b</code>. Additionally, the length of each resulting part (including its suffix) should be <strong>equal</strong> to <code>limit</code>, except for the last part whose length can be <strong>at most</strong> <code>limit</code>.</p>
|
||||
|
||||
<p>The resulting parts should be formed such that when their suffixes are removed and they are all concatenated <strong>in order</strong>, they should be equal to <code>message</code>. Also, the result should contain as few parts as possible.</p>
|
||||
|
||||
<p>Return<em> the parts </em><code>message</code><em> would be split into as an array of strings</em>. If it is impossible to split <code>message</code> as required, return<em> an empty array</em>.</p>
|
||||
|
||||
<p> </p>
|
||||
<p><strong class="example">Example 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> message = "this is really a very awesome message", limit = 9
|
||||
<strong>Output:</strong> ["thi<1/14>","s i<2/14>","s r<3/14>","eal<4/14>","ly <5/14>","a v<6/14>","ery<7/14>"," aw<8/14>","eso<9/14>","me<10/14>"," m<11/14>","es<12/14>","sa<13/14>","ge<14/14>"]
|
||||
<strong>Explanation:</strong>
|
||||
The first 9 parts take 3 characters each from the beginning of message.
|
||||
The next 5 parts take 2 characters each to finish splitting message.
|
||||
In this example, each part, including the last, has length 9.
|
||||
It can be shown it is not possible to split message into less than 14 parts.
|
||||
</pre>
|
||||
|
||||
<p><strong class="example">Example 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>Input:</strong> message = "short message", limit = 15
|
||||
<strong>Output:</strong> ["short mess<1/2>","age<2/2>"]
|
||||
<strong>Explanation:</strong>
|
||||
Under the given constraints, the string can be split into two parts:
|
||||
- The first part comprises of the first 10 characters, and has a length 15.
|
||||
- The next part comprises of the last 3 characters, and has a length 8.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
<p><strong>Constraints:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>1 <= message.length <= 10<sup>4</sup></code></li>
|
||||
<li><code>message</code> consists only of lowercase English letters and <code>' '</code>.</li>
|
||||
<li><code>1 <= limit <= 10<sup>4</sup></code></li>
|
||||
</ul>
|
Loading…
x
Reference in New Issue
Block a user