1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 10:38:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
程序员小墨 2022-09-27 22:06:44 +08:00
parent 068dd21d97
commit b43009ddf3
33 changed files with 14898 additions and 11628 deletions

View File

@ -1,6 +1,6 @@
# 力扣题库(完整版)
> 最后更新日期: **2022.09.24**
> 最后更新日期: **2022.09.27**
>
> 使用脚本前请务必仔细完整阅读本 `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

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,33 @@
「力扣嘉年华」的中心广场放置了一个巨型的二叉树形状的装饰树。每个节点上均有一盏灯和三个开关。节点值为 `0` 表示灯处于「关闭」状态,节点值为 `1` 表示灯处于「开启」状态。每个节点上的三个开关各自功能如下:
- 开关 `1`:切换当前节点的灯的状态;
- 开关 `2`:切换 **以当前节点为根** 的子树中,所有节点上的灯的状态,;
- 开关 `3`:切换 **当前节点及其左右子节点**(若存在的话) 上的灯的状态;
给定该装饰的初始状态 `root`,请返回最少需要操作多少次开关,可以关闭所有节点的灯。
**示例 1**
>输入:`root = [1,1,0,null,null,null,1]`
>
>输出:`2`
>
>解释:以下是最佳的方案之一,如图所示
![b71b95bf405e3b223e00b2820a062ba4.gif](https://pic.leetcode-cn.com/1629357030-GSbzpY-b71b95bf405e3b223e00b2820a062ba4.gif){:width="300px"}
**示例 2**
>输入:`root = [1,1,1,1,null,null,1]`
>
>输出:`1`
>
>解释:以下是最佳的方案,如图所示
![a4091b6448a0089b4d9e8f0390ff9ac6.gif](https://pic.leetcode-cn.com/1629356950-HZsKZC-a4091b6448a0089b4d9e8f0390ff9ac6.gif){:width="300px"}
**示例 3**
>输入:`root = [0,null,0]`
>
>输出:`0`
>
>解释:无需操作开关,当前所有节点上的灯均已关闭
**提示:**
- `1 <= 节点个数 <= 10^5`
- `0 <= Node.val <= 1`

View File

@ -0,0 +1,33 @@
为了缓解「力扣嘉年华」期间的人流压力,组委会在活动期间开设了一些交通专线。`path[i] = [a, b]` 表示有一条从地点 `a`通往地点 `b`**单向** 交通专线。
若存在一个地点,满足以下要求,我们则称之为 **交通枢纽**
- 所有地点(除自身外)均有一条 **单向** 专线 **直接** 通往该地点;
- 该地点不存在任何 **通往其他地点** 的单向专线。
请返回交通专线的 **交通枢纽**。若不存在,则返回 `-1`
**注意:**
- 对于任意一个地点,至少被一条专线连通。
**示例 1**
>输入:`path = [[0,1],[0,3],[1,3],[2,0],[2,3]]`
>
>输出:`3`
>
>解释:如下图所示:
> 地点 `0,1,2` 各有一条通往地点 `3` 的交通专线,
> 且地点 `3` 不存在任何**通往其他地点**的交通专线。
>![image.png](https://pic.leetcode-cn.com/1663902572-yOlUCr-image.png){:width=200px}
**示例 2**
>输入:`path = [[0,3],[1,0],[1,3],[2,0],[3,0],[3,2]]`
>
>输出:`-1`
>
>解释:如下图所示:不存在满足 **交通枢纽** 的地点。
>![image.png](https://pic.leetcode-cn.com/1663902595-McsEkY-image.png){:width=200px}
**提示:**
- `1 <= path.length <= 1000`
- `0 <= path[i][0], path[i][1] <= 1000`
- `path[i][0]``path[i][1]` 不相等

View File

@ -0,0 +1,62 @@
<p>给你一棵 <code>n</code>&nbsp;个节点的树(连通无向无环的图),节点编号从&nbsp;<code>0</code>&nbsp;&nbsp;<code>n - 1</code>&nbsp;且恰好有&nbsp;<code>n - 1</code>&nbsp;条边。</p>
<p>给你一个长度为 <code>n</code>&nbsp;下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>vals</code>&nbsp;,分别表示每个节点的值。同时给你一个二维整数数组&nbsp;<code>edges</code>&nbsp;,其中&nbsp;<code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code>&nbsp;表示节点&nbsp;<code>a<sub>i</sub></code>&nbsp;<code>b<sub>i</sub></code><sub>&nbsp;</sub>之间有一条&nbsp;<strong>无向</strong>&nbsp;边。</p>
<p>一条 <strong>好路径</strong>&nbsp;需要满足以下条件:</p>
<ol>
<li>开始节点和结束节点的值 <strong>相同</strong>&nbsp;</li>
<li>开始节点和结束节点中间的所有节点值都 <strong>小于等于</strong>&nbsp;开始节点的值(也就是说开始节点的值应该是路径上所有节点的最大值)。</li>
</ol>
<p>请你返回不同好路径的数目。</p>
<p>注意,一条路径和它反向的路径算作 <strong>同一</strong>&nbsp;路径。比方说,&nbsp;<code>0 -&gt; 1</code>&nbsp;&nbsp;<code>1 -&gt; 0</code>&nbsp;视为同一条路径。单个节点也视为一条合法路径。</p>
<p>&nbsp;</p>
<p><strong>示例&nbsp;1</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;"></p>
<pre><b>输入:</b>vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]]
<b>输出:</b>6
<b>解释:</b>总共有 5 条单个节点的好路径。
还有 1 条好路径1 -&gt; 0 -&gt; 2 -&gt; 4 。
(反方向的路径 4 -&gt; 2 -&gt; 0 -&gt; 1 视为跟 1 -&gt; 0 -&gt; 2 -&gt; 4 一样的路径)
注意 0 -&gt; 2 -&gt; 3 不是一条好路径,因为 vals[2] &gt; vals[0] 。
</pre>
<p><strong>示例 2</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;"></p>
<pre><b>输入:</b>vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]]
<b>输出:</b>7
<strong>解释:</strong>总共有 5 条单个节点的好路径。
还有 2 条好路径0 -&gt; 1 和 2 -&gt; 3 。
</pre>
<p><strong>示例 3</strong></p>
<p><img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;"></p>
<pre><b>输入:</b>vals = [1], edges = []
<b>输出:</b>1
<b>解释:</b>这棵树只有一个节点,所以只有一条好路径。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == vals.length</code></li>
<li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li>
<li><code>0 &lt;= vals[i] &lt;= 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 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code>&nbsp;表示一棵合法的树。</li>
</ul>

View File

@ -0,0 +1,54 @@
欢迎各位来到「力扣嘉年华」,接下来将为各位介绍在活动中广受好评的弹珠游戏。
`N*M` 大小的弹珠盘的初始状态信息记录于一维字符串型数组 `plate` 中,数组中的每个元素为仅由 `"O"`、`"W"`、`"E"`、`"."` 组成的字符串。其中:
- `"O"` 表示弹珠洞(弹珠到达后会落入洞中,并停止前进);
- `"W"` 表示逆时针转向器(弹珠经过时方向将逆时针旋转 90 度);
- `"E"` 表示顺时针转向器(弹珠经过时方向将顺时针旋转 90 度);
- `"."` 表示空白区域(弹珠可通行)。
游戏规则要求仅能在边缘位置的 **空白区域** 处(弹珠盘的四角除外)沿 **与边缘垂直** 的方向打入弹珠,并且打入后的每颗弹珠最多能 **前进** `num` 步。请返回符合上述要求且可以使弹珠最终入洞的所有打入位置。你可以 **按任意顺序** 返回答案。
**注意:**
- 若弹珠已到达弹珠盘边缘并且仍沿着出界方向继续前进,则将直接出界。
**示例 1**
> 输入
>`num = 4`
>`plate = ["..E.",".EOW","..W."]`
>
> 输出:`[[2,1]]`
>
> 解释:
> 在 `[2,1]` 处打入弹珠,弹珠前进 1 步后遇到转向器,前进方向顺时针旋转 90 度,再前进 1 步进入洞中。
![b054955158a99167b8d51da0e22a54da.gif](https://pic.leetcode-cn.com/1630392649-BoQncz-b054955158a99167b8d51da0e22a54da.gif){:width="300px"}
**示例 2**
> 输入
>`num = 5`
>`plate = [".....","..E..",".WO..","....."]`
>
> 输出:`[[0,1],[1,0],[2,4],[3,2]]`
>
> 解释:
> 在 `[0,1]` 处打入弹珠,弹珠前进 2 步,遇到转向器后前进方向逆时针旋转 90 度,再前进 1 步进入洞中。
> 在 `[1,0]` 处打入弹珠,弹珠前进 2 步,遇到转向器后前进方向顺时针旋转 90 度,再前进 1 步进入洞中。
> 在 `[2,4]` 处打入弹珠,弹珠前进 2 步后进入洞中。
> 在 `[3,2]` 处打入弹珠,弹珠前进 1 步后进入洞中。
![b44e9963239ae368badf3d00b7563087.gif](https://pic.leetcode-cn.com/1630392625-rckbdy-b44e9963239ae368badf3d00b7563087.gif){:width="350px"}
**示例 3**
> 输入
>`num = 3`
>`plate = [".....","....O","....O","....."]`
>
> 输出:`[]`
>
> 解释:
> 由于弹珠被击中后只能前进 3 步,且不能在弹珠洞和弹珠盘四角打入弹珠,故不存在能让弹珠入洞的打入位置。
**提示:**
- `1 <= num <= 10^6`
- `1 <= plate.length, plate[i].length <= 1000`
- `plate[i][j]` 仅包含 `"O"`、`"W"`、`"E"`、`"."`

View File

@ -0,0 +1,41 @@
<p>给你一个大小为 <code>n</code>&nbsp;下标从 <strong>0</strong>&nbsp;开始的整数数组&nbsp;<code>nums</code>&nbsp;和一个正整数&nbsp;<code>k</code>&nbsp;</p>
<p>对于&nbsp;<code>k &lt;= i &lt; n - k</code>&nbsp;之间的一个下标&nbsp;<code>i</code>&nbsp;,如果它满足以下条件,我们就称它为一个&nbsp;<strong></strong>&nbsp;下标:</p>
<ul>
<li>下标 <code>i</code> <strong>之前</strong><code>k</code>&nbsp;个元素是 <strong>非递增的</strong>&nbsp;</li>
<li>下标 <code>i</code> <strong>之后</strong>&nbsp;<code>k</code>&nbsp;个元素是 <strong>非递减的</strong>&nbsp;</li>
</ul>
<p><strong>升序</strong>&nbsp;返回所有好下标。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>nums = [2,1,1,1,3,4,1], k = 2
<b>输出:</b>[2,3]
<b>解释:</b>数组中有两个好下标:
- 下标 2 。子数组 [2,1] 是非递增的,子数组 [1,3] 是非递减的。
- 下标 3 。子数组 [1,1] 是非递增的,子数组 [3,4] 是非递减的。
注意,下标 4 不是好下标,因为 [4,1] 不是非递减的。</pre>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>nums = [2,1,1,2], k = 2
<b>输出:</b>[]
<b>解释:</b>数组中没有好下标。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
<li><code>1 &lt;= k &lt;= n / 2</code></li>
</ul>

View File

@ -0,0 +1,44 @@
<p>给你一个长度为 <code>n</code> 的整数数组 <code>nums</code></p>
<p>考虑 <code>nums</code> 中进行 <strong>按位与bitwise AND</strong>运算得到的值 <strong>最大</strong><strong>非空</strong> 子数组。</p>
<ul>
<li>换句话说,令 <code>k</code><code>nums</code> <strong>任意</strong> 子数组执行按位与运算所能得到的最大值。那么,只需要考虑那些执行一次按位与运算后等于 <code>k</code> 的子数组。</li>
</ul>
<p>返回满足要求的 <strong>最长</strong> 子数组的长度。</p>
<p>数组的按位与就是对数组中的所有数字进行按位与运算。</p>
<p><strong>子数组</strong> 是数组中的一个连续元素序列。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [1,2,3,3,2,2]
<strong>输出:</strong>2
<strong>解释:</strong>
子数组按位与运算的最大值是 3 。
能得到此结果的最长子数组是 [3,3],所以返回 2 。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [1,2,3,4]
<strong>输出:</strong>1
<strong>解释:</strong>
子数组按位与运算的最大值是 4 。
能得到此结果的最长子数组是 [4],所以返回 1 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
</ul>

View File

@ -0,0 +1,34 @@
<p>给你一个字符串数组 <code>names</code> ,和一个由 <strong>互不相同</strong> 的正整数组成的数组 <code>heights</code> 。两个数组的长度均为 <code>n</code></p>
<p>对于每个下标 <code>i</code><code>names[i]</code><code>heights[i]</code> 表示第 <code>i</code> 个人的名字和身高。</p>
<p>请按身高 <strong>降序</strong> 顺序返回对应的名字数组 <code>names</code></p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>names = ["Mary","John","Emma"], heights = [180,165,170]
<strong>输出:</strong>["Mary","Emma","John"]
<strong>解释:</strong>Mary 最高,接着是 Emma 和 John 。
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>names = ["Alice","Bob","Bob"], heights = [155,185,150]
<strong>输出:</strong>["Bob","Alice","Bob"]
<strong>解释:</strong>第一个 Bob 最高,然后是 Alice 和第二个 Bob 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == names.length == heights.length</code></li>
<li><code>1 &lt;= n &lt;= 10<sup>3</sup></code></li>
<li><code>1 &lt;= names[i].length &lt;= 20</code></li>
<li><code>1 &lt;= heights[i] &lt;= 10<sup>5</sup></code></li>
<li><code>names[i]</code> 由大小写英文字母组成</li>
<li><code>heights</code> 中的所有值互不相同</li>
</ul>

View File

@ -0,0 +1,30 @@
力扣城计划在两地设立「力扣嘉年华」的分会场,气象小组正在分析两地区的气温变化趋势,对于第 `i ~ (i+1)` 天的气温变化趋势,将根据以下规则判断:
- 若第 `i+1` 天的气温 **高于**`i` 天,为 **上升** 趋势
- 若第 `i+1` 天的气温 **等于**`i` 天,为 **平稳** 趋势
- 若第 `i+1` 天的气温 **低于**`i` 天,为 **下降** 趋势
已知 `temperatureA[i]``temperatureB[i]` 分别表示第 `i` 天两地区的气温。
组委会希望找到一段天数尽可能多,且两地气温变化趋势相同的时间举办嘉年华活动。请分析并返回两地气温变化趋势**相同的最大连续天数**。
> 即最大的 `n`,使得第 `i~i+n` 天之间,两地气温变化趋势相同
**示例 1**
>输入:
>`temperatureA = [21,18,18,18,31]`
>`temperatureB = [34,32,16,16,17]`
>
>输出:`2`
>
>解释:如下表所示, 第 `24` 天两地气温变化趋势相同,且持续时间最长,因此返回 `4-2=2`
![image.png](https://pic.leetcode-cn.com/1663902654-hlrSvs-image.png){:width=1000px}
**示例 2**
>输入:
>`temperatureA = [5,10,16,-6,15,11,3]`
>`temperatureB = [16,22,23,23,25,3,-16]`
>
>输出:`3`
**提示:**
- `2 <= temperatureA.length == temperatureB.length <= 1000`
- `-20 <= temperatureA[i], temperatureB[i] <= 40`

View File

@ -0,0 +1,28 @@
力扣嘉年华为了确保更舒适的游览环境条件,在会场的各处设置了湿度调节装置,这些调节装置受控于总控室中的一台控制器。
控制器中已经预设了一些调节指令,整数数组`operate[i]` 表示第 `i` 条指令增加空气湿度的大小。现在你可以将任意数量的指令修改为降低湿度(变化的数值不变),以确保湿度尽可能的适宜:
- 控制器会选择 **一段连续的指令** ,从而进行湿度调节的操作;
- 这段指令最终对湿度影响的绝对值,即为当前操作的「不适宜度」
- 在控制器所有可能的操作中,**最大** 的「不适宜度」即为「整体不适宜度」
请返回在所有修改指令的方案中,可以得到的 **最小** 「整体不适宜度」。
**示例 1**
> 输入:`operate = [5,3,7]`
>
> 输出:`8`
>
> 解释:对于方案 `2``[5,3,-7]`
>操作指令 `[5],[3],[-7]` 的「不适宜度」分别为 `5,3,7`
>操作指令 `[5,3],[3,-7]` 的「不适宜度」分别为 `8,4`
>操作指令 `[5,3,-7]` 的「不适宜度」为 `1`
>因此对于方案 `[5,3,-7]`的「整体不适宜度」为 `8`,其余方案的「整体不适宜度」均不小于 `8`,如下表所示:
![image.png](https://pic.leetcode-cn.com/1663902759-dgDCxn-image.png){:width=650px}
**示例 2**
> 输入:`operate = [20,10]`
>
> 输出:`20`
**提示:**
- `1 <= operate.length <= 1000`
- `1 <= operate[i] <= 1000`

View File

@ -0,0 +1,57 @@
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p>
<p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p>
<ol>
<li>The starting node and the ending node have the <strong>same</strong> value.</li>
<li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li>
</ol>
<p>Return <em>the number of distinct good paths</em>.</p>
<p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" />
<pre>
<strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]]
<strong>Output:</strong> 6
<strong>Explanation:</strong> There are 5 good paths consisting of a single node.
There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4.
(The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.)
Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0].
</pre>
<p><strong>Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" />
<pre>
<strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> There are 5 good paths consisting of a single node.
There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3.
</pre>
<p><strong>Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" />
<pre>
<strong>Input:</strong> vals = [1], edges = []
<strong>Output:</strong> 1
<strong>Explanation:</strong> The tree consists of only one node, so there is one good path.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == vals.length</code></li>
<li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li>
<li><code>0 &lt;= vals[i] &lt;= 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 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
</ul>

View File

@ -0,0 +1,39 @@
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>n</code> and a positive integer <code>k</code>.</p>
<p>We call an index <code>i</code> in the range <code>k &lt;= i &lt; n - k</code> <strong>good</strong> if the following conditions are satisfied:</p>
<ul>
<li>The <code>k</code> elements that are just <strong>before</strong> the index <code>i</code> are in <strong>non-increasing</strong> order.</li>
<li>The <code>k</code> elements that are just <strong>after</strong> the index <code>i</code> are in <strong>non-decreasing</strong> order.</li>
</ul>
<p>Return <em>an array of all good indices sorted in <strong>increasing</strong> order</em>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,1,1,1,3,4,1], k = 2
<strong>Output:</strong> [2,3]
<strong>Explanation:</strong> There are two good indices in the array:
- Index 2. The subarray [2,1] is in non-increasing order, and the subarray [1,3] is in non-decreasing order.
- Index 3. The subarray [1,1] is in non-increasing order, and the subarray [3,4] is in non-decreasing order.
Note that the index 4 is not good because [4,1] is not non-decreasing.</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,1,1,2], k = 2
<strong>Output:</strong> []
<strong>Explanation:</strong> There are no good indices in this array.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
<li><code>1 &lt;= k &lt;= n / 2</code></li>
</ul>

View File

@ -0,0 +1,42 @@
<p>You are given an integer array <code>nums</code> of size <code>n</code>.</p>
<p>Consider a <strong>non-empty</strong> subarray from <code>nums</code> that has the <strong>maximum</strong> possible <strong>bitwise AND</strong>.</p>
<ul>
<li>In other words, let <code>k</code> be the maximum value of the bitwise AND of <strong>any</strong> subarray of <code>nums</code>. Then, only subarrays with a bitwise AND equal to <code>k</code> should be considered.</li>
</ul>
<p>Return <em>the length of the <strong>longest</strong> such subarray</em>.</p>
<p>The bitwise AND of an array is the bitwise AND of all the numbers in it.</p>
<p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,3,2,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The maximum possible bitwise AND of a subarray is 3.
The longest subarray with that value is [3,3], so we return 2.
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
The maximum possible bitwise AND of a subarray is 4.
The longest subarray with that value is [4], so we return 1.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
</ul>

View File

@ -0,0 +1,34 @@
<p>You are given an array of strings <code>names</code>, and an array <code>heights</code> that consists of <strong>distinct</strong> positive integers. Both arrays are of length <code>n</code>.</p>
<p>For each index <code>i</code>, <code>names[i]</code> and <code>heights[i]</code> denote the name and height of the <code>i<sup>th</sup></code> person.</p>
<p>Return <code>names</code><em> sorted in <strong>descending</strong> order by the people&#39;s heights</em>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> names = [&quot;Mary&quot;,&quot;John&quot;,&quot;Emma&quot;], heights = [180,165,170]
<strong>Output:</strong> [&quot;Mary&quot;,&quot;Emma&quot;,&quot;John&quot;]
<strong>Explanation:</strong> Mary is the tallest, followed by Emma and John.
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> names = [&quot;Alice&quot;,&quot;Bob&quot;,&quot;Bob&quot;], heights = [155,185,150]
<strong>Output:</strong> [&quot;Bob&quot;,&quot;Alice&quot;,&quot;Bob&quot;]
<strong>Explanation:</strong> The first Bob is the tallest, followed by Alice and the second Bob.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == names.length == heights.length</code></li>
<li><code>1 &lt;= n &lt;= 10<sup>3</sup></code></li>
<li><code>1 &lt;= names[i].length &lt;= 20</code></li>
<li><code>1 &lt;= heights[i] &lt;= 10<sup>5</sup></code></li>
<li><code>names[i]</code> consists of lower and upper case English letters.</li>
<li>All the values of <code>heights</code> are distinct.</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

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,39 @@
<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> of size <code>n</code> and a positive integer <code>k</code>.</p>
<p>We call an index <code>i</code> in the range <code>k &lt;= i &lt; n - k</code> <strong>good</strong> if the following conditions are satisfied:</p>
<ul>
<li>The <code>k</code> elements that are just <strong>before</strong> the index <code>i</code> are in <strong>non-increasing</strong> order.</li>
<li>The <code>k</code> elements that are just <strong>after</strong> the index <code>i</code> are in <strong>non-decreasing</strong> order.</li>
</ul>
<p>Return <em>an array of all good indices sorted in <strong>increasing</strong> order</em>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,1,1,1,3,4,1], k = 2
<strong>Output:</strong> [2,3]
<strong>Explanation:</strong> There are two good indices in the array:
- Index 2. The subarray [2,1] is in non-increasing order, and the subarray [1,3] is in non-decreasing order.
- Index 3. The subarray [1,1] is in non-increasing order, and the subarray [3,4] is in non-decreasing order.
Note that the index 4 is not good because [4,1] is not non-decreasing.</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [2,1,1,2], k = 2
<strong>Output:</strong> []
<strong>Explanation:</strong> There are no good indices in this array.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>3 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
<li><code>1 &lt;= k &lt;= n / 2</code></li>
</ul>

View File

@ -0,0 +1,42 @@
<p>You are given an integer array <code>nums</code> of size <code>n</code>.</p>
<p>Consider a <strong>non-empty</strong> subarray from <code>nums</code> that has the <strong>maximum</strong> possible <strong>bitwise AND</strong>.</p>
<ul>
<li>In other words, let <code>k</code> be the maximum value of the bitwise AND of <strong>any</strong> subarray of <code>nums</code>. Then, only subarrays with a bitwise AND equal to <code>k</code> should be considered.</li>
</ul>
<p>Return <em>the length of the <strong>longest</strong> such subarray</em>.</p>
<p>The bitwise AND of an array is the bitwise AND of all the numbers in it.</p>
<p>A <strong>subarray</strong> is a contiguous sequence of elements within an array.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,3,2,2]
<strong>Output:</strong> 2
<strong>Explanation:</strong>
The maximum possible bitwise AND of a subarray is 3.
The longest subarray with that value is [3,3], so we return 2.
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong>
The maximum possible bitwise AND of a subarray is 4.
The longest subarray with that value is [4], so we return 1.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
</ul>

View File

@ -0,0 +1,57 @@
<p>There is a tree (i.e. a connected, undirected graph with no cycles) consisting of <code>n</code> nodes numbered from <code>0</code> to <code>n - 1</code> and exactly <code>n - 1</code> edges.</p>
<p>You are given a <strong>0-indexed</strong> integer array <code>vals</code> of length <code>n</code> where <code>vals[i]</code> denotes the value of the <code>i<sup>th</sup></code> node. You are also given a 2D integer array <code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists an <strong>undirected</strong> edge connecting nodes <code>a<sub>i</sub></code> and <code>b<sub>i</sub></code>.</p>
<p>A <strong>good path</strong> is a simple path that satisfies the following conditions:</p>
<ol>
<li>The starting node and the ending node have the <strong>same</strong> value.</li>
<li>All nodes between the starting node and the ending node have values <strong>less than or equal to</strong> the starting node (i.e. the starting node&#39;s value should be the maximum value along the path).</li>
</ol>
<p>Return <em>the number of distinct good paths</em>.</p>
<p>Note that a path and its reverse are counted as the <strong>same</strong> path. For example, <code>0 -&gt; 1</code> is considered to be the same as <code>1 -&gt; 0</code>. A single node is also considered as a valid path.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/f9caaac15b383af9115c5586779dec5.png" style="width: 400px; height: 333px;" />
<pre>
<strong>Input:</strong> vals = [1,3,2,1,3], edges = [[0,1],[0,2],[2,3],[2,4]]
<strong>Output:</strong> 6
<strong>Explanation:</strong> There are 5 good paths consisting of a single node.
There is 1 additional good path: 1 -&gt; 0 -&gt; 2 -&gt; 4.
(The reverse path 4 -&gt; 2 -&gt; 0 -&gt; 1 is treated as the same as 1 -&gt; 0 -&gt; 2 -&gt; 4.)
Note that 0 -&gt; 2 -&gt; 3 is not a good path because vals[2] &gt; vals[0].
</pre>
<p><strong>Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/149d3065ec165a71a1b9aec890776ff.png" style="width: 273px; height: 350px;" />
<pre>
<strong>Input:</strong> vals = [1,1,2,2,3], edges = [[0,1],[1,2],[2,3],[2,4]]
<strong>Output:</strong> 7
<strong>Explanation:</strong> There are 5 good paths consisting of a single node.
There are 2 additional good paths: 0 -&gt; 1 and 2 -&gt; 3.
</pre>
<p><strong>Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/08/04/31705e22af3d9c0a557459bc7d1b62d.png" style="width: 100px; height: 88px;" />
<pre>
<strong>Input:</strong> vals = [1], edges = []
<strong>Output:</strong> 1
<strong>Explanation:</strong> The tree consists of only one node, so there is one good path.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == vals.length</code></li>
<li><code>1 &lt;= n &lt;= 3 * 10<sup>4</sup></code></li>
<li><code>0 &lt;= vals[i] &lt;= 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 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
<li><code>edges</code> represents a valid tree.</li>
</ul>

View File

@ -0,0 +1,34 @@
<p>You are given an array of strings <code>names</code>, and an array <code>heights</code> that consists of <strong>distinct</strong> positive integers. Both arrays are of length <code>n</code>.</p>
<p>For each index <code>i</code>, <code>names[i]</code> and <code>heights[i]</code> denote the name and height of the <code>i<sup>th</sup></code> person.</p>
<p>Return <code>names</code><em> sorted in <strong>descending</strong> order by the people&#39;s heights</em>.</p>
<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> names = [&quot;Mary&quot;,&quot;John&quot;,&quot;Emma&quot;], heights = [180,165,170]
<strong>Output:</strong> [&quot;Mary&quot;,&quot;Emma&quot;,&quot;John&quot;]
<strong>Explanation:</strong> Mary is the tallest, followed by Emma and John.
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> names = [&quot;Alice&quot;,&quot;Bob&quot;,&quot;Bob&quot;], heights = [155,185,150]
<strong>Output:</strong> [&quot;Bob&quot;,&quot;Alice&quot;,&quot;Bob&quot;]
<strong>Explanation:</strong> The first Bob is the tallest, followed by Alice and the second Bob.
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == names.length == heights.length</code></li>
<li><code>1 &lt;= n &lt;= 10<sup>3</sup></code></li>
<li><code>1 &lt;= names[i].length &lt;= 20</code></li>
<li><code>1 &lt;= heights[i] &lt;= 10<sup>5</sup></code></li>
<li><code>names[i]</code> consists of lower and upper case English letters.</li>
<li>All the values of <code>heights</code> are distinct.</li>
</ul>