Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b41cbca

Browse files
authored
Added descriptions 121-140.
1 parent 3d6ad31 commit b41cbca

File tree

20 files changed

+741
-0
lines changed

20 files changed

+741
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
121\. Best Time to Buy and Sell Stock
2+
3+
Easy
4+
5+
You are given an array `prices` where `prices[i]` is the price of a given stock on the `ith` day.
6+
7+
You want to maximize your profit by choosing a **single day** to buy one stock and choosing a **different day in the future** to sell that stock.
8+
9+
Return _the maximum profit you can achieve from this transaction_. If you cannot achieve any profit, return `0`.
10+
11+
**Example 1:**
12+
13+
**Input:** prices = \[7,1,5,3,6,4\]
14+
15+
**Output:** 5
16+
17+
**Explanation:** Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.
18+
19+
**Example 2:**
20+
21+
**Input:** prices = \[7,6,4,3,1\]
22+
23+
**Output:** 0
24+
25+
**Explanation:** In this case, no transactions are done and the max profit = 0.
26+
27+
**Constraints:**
28+
29+
* <code>1 <= prices.length <= 10<sup>5</sup></code>
30+
* <code>0 <= prices[i] <= 10<sup>4</sup></code>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
122\. Best Time to Buy and Sell Stock II
2+
3+
Medium
4+
5+
You are given an integer array `prices` where `prices[i]` is the price of a given stock on the `ith` day.
6+
7+
On each day, you may decide to buy and/or sell the stock. You can only hold **at most one** share of the stock at any time. However, you can buy it then immediately sell it on the **same day**.
8+
9+
Find and return _the **maximum** profit you can achieve_.
10+
11+
**Example 1:**
12+
13+
**Input:** prices = \[7,1,5,3,6,4\]
14+
15+
**Output:** 7
16+
17+
**Explanation:** Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4. Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3. Total profit is 4 + 3 = 7.
18+
19+
**Example 2:**
20+
21+
**Input:** prices = \[1,2,3,4,5\]
22+
23+
**Output:** 4
24+
25+
**Explanation:** Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. Total profit is 4.
26+
27+
**Example 3:**
28+
29+
**Input:** prices = \[7,6,4,3,1\]
30+
31+
**Output:** 0
32+
33+
**Explanation:** There is no way to make a positive profit, so we never buy the stock to achieve the maximum profit of 0.
34+
35+
**Constraints:**
36+
37+
* <code>1 <= prices.length <= 3 * 10<sup>4</sup></code>
38+
* <code>0 <= prices[i] <= 10<sup>4</sup></code>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
123\. Best Time to Buy and Sell Stock III
2+
3+
Hard
4+
5+
You are given an array `prices` where `prices[i]` is the price of a given stock on the `ith` day.
6+
7+
Find the maximum profit you can achieve. You may complete **at most two transactions**.
8+
9+
**Note:** You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
10+
11+
**Example 1:**
12+
13+
**Input:** prices = \[3,3,5,0,0,3,1,4\]
14+
15+
**Output:** 6
16+
17+
**Explanation:** Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3. Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.
18+
19+
**Example 2:**
20+
21+
**Input:** prices = \[1,2,3,4,5\]
22+
23+
**Output:** 4
24+
25+
**Explanation:** Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the same time. You must sell before buying again.
26+
27+
**Example 3:**
28+
29+
**Input:** prices = \[7,6,4,3,1\]
30+
31+
**Output:** 0
32+
33+
**Explanation:** In this case, no transaction is done, i.e. max profit = 0.
34+
35+
**Example 4:**
36+
37+
**Input:** prices = \[1\]
38+
39+
**Output:** 0
40+
41+
**Constraints:**
42+
43+
* <code>1 <= prices.length <= 10<sup>5</sup></code>
44+
* <code>0 <= prices[i] <= 10<sup>5</sup></code>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
124\. Binary Tree Maximum Path Sum
2+
3+
Hard
4+
5+
A **path** in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence **at most once**. Note that the path does not need to pass through the root.
6+
7+
The **path sum** of a path is the sum of the node's values in the path.
8+
9+
Given the `root` of a binary tree, return _the maximum **path sum** of any **non-empty** path_.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2020/10/13/exx1.jpg)
14+
15+
**Input:** root = \[1,2,3\]
16+
17+
**Output:** 6
18+
19+
**Explanation:** The optimal path is 2 -> 1 -> 3 with a path sum of 2 + 1 + 3 = 6.
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2020/10/13/exx2.jpg)
24+
25+
**Input:** root = \[-10,9,20,null,null,15,7\]
26+
27+
**Output:** 42
28+
29+
**Explanation:** The optimal path is 15 -> 20 -> 7 with a path sum of 15 + 20 + 7 = 42.
30+
31+
**Constraints:**
32+
33+
* The number of nodes in the tree is in the range <code>[1, 3 * 10<sup>4</sup>]</code>.
34+
* `-1000 <= Node.val <= 1000`
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
125\. Valid Palindrome
2+
3+
Easy
4+
5+
A phrase is a **palindrome** if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
6+
7+
Given a string `s`, return `true` _if it is a **palindrome**, or_ `false` _otherwise_.
8+
9+
**Example 1:**
10+
11+
**Input:** s = "A man, a plan, a canal: Panama"
12+
13+
**Output:** true
14+
15+
**Explanation:** "amanaplanacanalpanama" is a palindrome.
16+
17+
**Example 2:**
18+
19+
**Input:** s = "race a car"
20+
21+
**Output:** false
22+
23+
**Explanation:** "raceacar" is not a palindrome.
24+
25+
**Example 3:**
26+
27+
**Input:** s = " "
28+
29+
**Output:** true
30+
31+
**Explanation:** s is an empty string "" after removing non-alphanumeric characters. Since an empty string reads the same forward and backward, it is a palindrome.
32+
33+
**Constraints:**
34+
35+
* <code>1 <= s.length <= 2 * 10<sup>5</sup></code>
36+
* `s` consists only of printable ASCII characters.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
126\. Word Ladder II
2+
3+
Hard
4+
5+
A **transformation sequence** from word `beginWord` to word `endWord` using a dictionary `wordList` is a sequence of words <code>beginWord -> s<sub>1</sub> -> s<sub>2</sub> -> ... -> s<sub>k</sub></code> such that:
6+
7+
* Every adjacent pair of words differs by a single letter.
8+
* Every <code>s<sub>i</sub></code> for `1 <= i <= k` is in `wordList`. Note that `beginWord` does not need to be in `wordList`.
9+
* <code>s<sub>k</sub> == endWord</code>
10+
11+
Given two words, `beginWord` and `endWord`, and a dictionary `wordList`, return _all the **shortest transformation sequences** from_ `beginWord` _to_ `endWord`_, or an empty list if no such sequence exists. Each sequence should be returned as a list of the words_ <code>[beginWord, s<sub>1</sub>, s<sub>2</sub>, ..., s<sub>k</sub>]</code>.
12+
13+
**Example 1:**
14+
15+
**Input:** beginWord = "hit", endWord = "cog", wordList = \["hot","dot","dog","lot","log","cog"\]
16+
17+
**Output:** \[\["hit","hot","dot","dog","cog"\],\["hit","hot","lot","log","cog"\]\]
18+
19+
**Explanation:** There are 2 shortest transformation sequences: "hit" -> "hot" -> "dot" -> "dog" -> "cog" "hit" -> "hot" -> "lot" -> "log" -> "cog"
20+
21+
**Example 2:**
22+
23+
**Input:** beginWord = "hit", endWord = "cog", wordList = \["hot","dot","dog","lot","log"\]
24+
25+
**Output:** \[\]
26+
27+
**Explanation:** The endWord "cog" is not in wordList, therefore there is no valid transformation sequence.
28+
29+
**Constraints:**
30+
31+
* `1 <= beginWord.length <= 5`
32+
* `endWord.length == beginWord.length`
33+
* `1 <= wordList.length <= 1000`
34+
* `wordList[i].length == beginWord.length`
35+
* `beginWord`, `endWord`, and `wordList[i]` consist of lowercase English letters.
36+
* `beginWord != endWord`
37+
* All the words in `wordList` are **unique**.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
127\. Word Ladder
2+
3+
Hard
4+
5+
A **transformation sequence** from word `beginWord` to word `endWord` using a dictionary `wordList` is a sequence of words <code>beginWord -> s<sub>1</sub> -> s<sub>2</sub> -> ... -> s<sub>k</sub></code> such that:
6+
7+
* Every adjacent pair of words differs by a single letter.
8+
* Every <code>s<sub>i</sub></code> for `1 <= i <= k` is in `wordList`. Note that `beginWord` does not need to be in `wordList`.
9+
* <code>s<sub>k</sub> == endWord</code>
10+
11+
Given two words, `beginWord` and `endWord`, and a dictionary `wordList`, return _the **number of words** in the **shortest transformation sequence** from_ `beginWord` _to_ `endWord`_, or_ `0` _if no such sequence exists._
12+
13+
**Example 1:**
14+
15+
**Input:** beginWord = "hit", endWord = "cog", wordList = \["hot","dot","dog","lot","log","cog"\]
16+
17+
**Output:** 5
18+
19+
**Explanation:** One shortest transformation sequence is "hit" -> "hot" -> "dot" -> "dog" -> cog", which is 5 words long.
20+
21+
**Example 2:**
22+
23+
**Input:** beginWord = "hit", endWord = "cog", wordList = \["hot","dot","dog","lot","log"\]
24+
25+
**Output:** 0
26+
27+
**Explanation:** The endWord "cog" is not in wordList, therefore there is no valid transformation sequence.
28+
29+
**Constraints:**
30+
31+
* `1 <= beginWord.length <= 10`
32+
* `endWord.length == beginWord.length`
33+
* `1 <= wordList.length <= 5000`
34+
* `wordList[i].length == beginWord.length`
35+
* `beginWord`, `endWord`, and `wordList[i]` consist of lowercase English letters.
36+
* `beginWord != endWord`
37+
* All the words in `wordList` are **unique**.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
128\. Longest Consecutive Sequence
2+
3+
Medium
4+
5+
Given an unsorted array of integers `nums`, return _the length of the longest consecutive elements sequence._
6+
7+
You must write an algorithm that runs in `O(n)` time.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = \[100,4,200,1,3,2\]
12+
13+
**Output:** 4
14+
15+
**Explanation:** The longest consecutive elements sequence is `[1, 2, 3, 4]`. Therefore its length is 4.
16+
17+
**Example 2:**
18+
19+
**Input:** nums = \[0,3,7,2,5,8,4,6,0,1\]
20+
21+
**Output:** 9
22+
23+
**Constraints:**
24+
25+
* <code>0 <= nums.length <= 10<sup>5</sup></code>
26+
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
129\. Sum Root to Leaf Numbers
2+
3+
Medium
4+
5+
You are given the `root` of a binary tree containing digits from `0` to `9` only.
6+
7+
Each root-to-leaf path in the tree represents a number.
8+
9+
* For example, the root-to-leaf path `1 -> 2 -> 3` represents the number `123`.
10+
11+
Return _the total sum of all root-to-leaf numbers_. Test cases are generated so that the answer will fit in a **32-bit** integer.
12+
13+
A **leaf** node is a node with no children.
14+
15+
**Example 1:**
16+
17+
![](https://assets.leetcode.com/uploads/2021/02/19/num1tree.jpg)
18+
19+
**Input:** root = \[1,2,3\]
20+
21+
**Output:** 25
22+
23+
**Explanation:** The root-to-leaf path `1->2` represents the number `12`. The root-to-leaf path `1->3` represents the number `13`. Therefore, sum = 12 + 13 = `25`.
24+
25+
**Example 2:**
26+
27+
![](https://assets.leetcode.com/uploads/2021/02/19/num2tree.jpg)
28+
29+
**Input:** root = \[4,9,0,5,1\]
30+
31+
**Output:** 1026
32+
33+
**Explanation:** The root-to-leaf path `4->9->5` represents the number 495. The root-to-leaf path `4->9->1` represents the number 491. The root-to-leaf path `4->0` represents the number 40. Therefore, sum = 495 + 491 + 40 = `1026`.
34+
35+
**Constraints:**
36+
37+
* The number of nodes in the tree is in the range `[1, 1000]`.
38+
* `0 <= Node.val <= 9`
39+
* The depth of the tree will not exceed `10`.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
130\. Surrounded Regions
2+
3+
Medium
4+
5+
Given an `m x n` matrix `board` containing `'X'` and `'O'`, _capture all regions that are 4-directionally surrounded by_ `'X'`.
6+
7+
A region is **captured** by flipping all `'O'`s into `'X'`s in that surrounded region.
8+
9+
**Example 1:**
10+
11+
![](https://assets.leetcode.com/uploads/2021/02/19/xogrid.jpg)
12+
13+
**Input:** board = \[\["X","X","X","X"\],\["X","O","O","X"\],\["X","X","O","X"\],\["X","O","X","X"\]\]
14+
15+
**Output:** \[\["X","X","X","X"\],\["X","X","X","X"\],\["X","X","X","X"\],\["X","O","X","X"\]\]
16+
17+
**Explanation:** Surrounded regions should not be on the border, which means that any 'O' on the border of the board are not flipped to 'X'. Any 'O' that is not on the border and it is not connected to an 'O' on the border will be flipped to 'X'. Two cells are connected if they are adjacent cells connected horizontally or vertically.
18+
19+
**Example 2:**
20+
21+
**Input:** board = \[\["X"\]\]
22+
23+
**Output:** \[\["X"\]\]
24+
25+
**Constraints:**
26+
27+
* `m == board.length`
28+
* `n == board[i].length`
29+
* `1 <= m, n <= 200`
30+
* `board[i][j]` is `'X'` or `'O'`.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /