diff --git a/README.md b/README.md index 8ebdd386d..2b3e02071 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,22 @@ LeetCode Problems' Solutions | # | Title | Solution | Difficulty | | :-: | - | - | :-: | +| 1707 | [Maximum XOR With an Element From Array](https://leetcode.com/problems/maximum-xor-with-an-element-from-array "与数组中元素的最大异或值") | [Go](problems/maximum-xor-with-an-element-from-array) | Hard | +| 1706 | [Where Will the Ball Fall](https://leetcode.com/problems/where-will-the-ball-fall "球会落何处") | [Go](problems/where-will-the-ball-fall) | Medium | +| 1705 | [Maximum Number of Eaten Apples](https://leetcode.com/problems/maximum-number-of-eaten-apples "吃苹果的最大数目") | [Go](problems/maximum-number-of-eaten-apples) | Medium | +| 1704 | [Determine if String Halves Are Alike](https://leetcode.com/problems/determine-if-string-halves-are-alike "判断字符串的两半是否相似") | [Go](problems/determine-if-string-halves-are-alike) | Easy | +| 1703 | [Minimum Adjacent Swaps for K Consecutive Ones](https://leetcode.com/problems/minimum-adjacent-swaps-for-k-consecutive-ones "得到连续 K 个 1 的最少相邻交换次数") | [Go](problems/minimum-adjacent-swaps-for-k-consecutive-ones) | Hard | +| 1702 | [Maximum Binary String After Change](https://leetcode.com/problems/maximum-binary-string-after-change "修改后的最大二进制字符串") | [Go](problems/maximum-binary-string-after-change) | Medium | +| 1701 | [Average Waiting Time](https://leetcode.com/problems/average-waiting-time "平均等待时间") | [Go](problems/average-waiting-time) | Medium | +| 1700 | [Number of Students Unable to Eat Lunch](https://leetcode.com/problems/number-of-students-unable-to-eat-lunch "无法吃午餐的学生数量") | [Go](problems/number-of-students-unable-to-eat-lunch) | Easy | +| 1699 | [Number of Calls Between Two Persons](https://leetcode.com/problems/number-of-calls-between-two-persons) 🔒 | [MySQL](problems/number-of-calls-between-two-persons) | Medium | +| 1698 | [Number of Distinct Substrings in a String](https://leetcode.com/problems/number-of-distinct-substrings-in-a-string) 🔒 | [Go](problems/number-of-distinct-substrings-in-a-string) | Medium | +| 1697 | [Checking Existence of Edge Length Limited Paths](https://leetcode.com/problems/checking-existence-of-edge-length-limited-paths "检查边长度限制的路径是否存在") | [Go](problems/checking-existence-of-edge-length-limited-paths) | Hard | +| 1696 | [Jump Game VI](https://leetcode.com/problems/jump-game-vi "跳跃游戏 VI") | [Go](problems/jump-game-vi) | Medium | +| 1695 | [Maximum Erasure Value](https://leetcode.com/problems/maximum-erasure-value "删除子数组的最大得分") | [Go](problems/maximum-erasure-value) | Medium | +| 1694 | [Reformat Phone Number](https://leetcode.com/problems/reformat-phone-number "重新格式化电话号码") | [Go](problems/reformat-phone-number) | Easy | +| 1693 | [Daily Leads and Partners](https://leetcode.com/problems/daily-leads-and-partners) 🔒 | [MySQL](problems/daily-leads-and-partners) | Easy | +| 1692 | [Count Ways to Distribute Candies](https://leetcode.com/problems/count-ways-to-distribute-candies) 🔒 | [Go](problems/count-ways-to-distribute-candies) | Hard | | 1691 | [Maximum Height by Stacking Cuboids](https://leetcode.com/problems/maximum-height-by-stacking-cuboids "堆叠长方体的最大高度") | [Go](problems/maximum-height-by-stacking-cuboids) | Hard | | 1690 | [Stone Game VII](https://leetcode.com/problems/stone-game-vii "石子游戏 VII") | [Go](problems/stone-game-vii) | Medium | | 1689 | [Partitioning Into Minimum Number Of Deci-Binary Numbers](https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers "十-二进制数的最少数目") | [Go](problems/partitioning-into-minimum-number-of-deci-binary-numbers) | Medium | diff --git a/problems/average-waiting-time/README.md b/problems/average-waiting-time/README.md new file mode 100644 index 000000000..464b5a9ec --- /dev/null +++ b/problems/average-waiting-time/README.md @@ -0,0 +1,77 @@ + + + + + + + +[< Previous](../number-of-students-unable-to-eat-lunch "Number of Students Unable to Eat Lunch") +                 +[Next >](../maximum-binary-string-after-change "Maximum Binary String After Change") + +## [1701. Average Waiting Time (Medium)](https://leetcode.com/problems/average-waiting-time "平均等待时间") + +

There is a restaurant with a single chef. You are given an array customers, where customers[i] = [arrivali, timei]:

+ + + +

When a customer arrives, he gives the chef his order, and the chef starts preparing it once he is idle. The customer waits till the chef finishes preparing his order. The chef does not prepare food for more than one customer at a time. The chef prepares food for customers in the order they were given in the input.

+ +

Return the average waiting time of all customers. Solutions within 10-5 from the actual answer are considered accepted.

+ +

 

+

Example 1:

+ +
+Input: customers = [[1,2],[2,5],[4,3]]
+Output: 5.00000
+Explanation:
+1) The first customer arrives at time 1, the chef takes his order and starts preparing it immediately at time 1, and finishes at time 3, so the waiting time of the first customer is 3 - 1 = 2.
+2) The second customer arrives at time 2, the chef takes his order and starts preparing it at time 3, and finishes at time 8, so the waiting time of the second customer is 8 - 2 = 6.
+3) The third customer arrives at time 4, the chef takes his order and starts preparing it at time 8, and finishes at time 11, so the waiting time of the third customer is 11 - 4 = 7.
+So the average waiting time = (2 + 6 + 7) / 3 = 5.
+
+ +

Example 2:

+ +
+Input: customers = [[5,2],[5,4],[10,3],[20,1]]
+Output: 3.25000
+Explanation:
+1) The first customer arrives at time 5, the chef takes his order and starts preparing it immediately at time 5, and finishes at time 7, so the waiting time of the first customer is 7 - 5 = 2.
+2) The second customer arrives at time 5, the chef takes his order and starts preparing it at time 7, and finishes at time 11, so the waiting time of the second customer is 11 - 5 = 6.
+3) The third customer arrives at time 10, the chef takes his order and starts preparing it at time 11, and finishes at time 14, so the waiting time of the third customer is 14 - 10 = 4.
+4) The fourth customer arrives at time 20, the chef takes his order and starts preparing it immediately at time 20, and finishes at time 21, so the waiting time of the fourth customer is 21 - 20 = 1.
+So the average waiting time = (2 + 6 + 4 + 1) / 4 = 3.25.
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Array](../../tag/array/README.md)] + +### Hints +
+Hint 1 +Iterate on the customers, maintaining the time the chef will finish the previous orders. +
+ +
+Hint 2 +If that time is before the current arrival time, the chef starts immediately. Else, the current customer waits till the chef finishes, and then the chef starts. +
+ +
+Hint 3 +Update the running time by the time when the chef starts preparing + preparation time. +
diff --git a/problems/balanced-binary-tree/README.md b/problems/balanced-binary-tree/README.md index 91cc92e44..cd36c5406 100644 --- a/problems/balanced-binary-tree/README.md +++ b/problems/balanced-binary-tree/README.md @@ -52,6 +52,7 @@ ### Related Topics [[Tree](../../tag/tree/README.md)] [[Depth-first Search](../../tag/depth-first-search/README.md)] + [[Recursion](../../tag/recursion/README.md)] ### Similar Questions 1. [Maximum Depth of Binary Tree](../maximum-depth-of-binary-tree) (Easy) diff --git a/problems/basic-calculator/README.md b/problems/basic-calculator/README.md index ce4653dd8..aec611066 100644 --- a/problems/basic-calculator/README.md +++ b/problems/basic-calculator/README.md @@ -11,33 +11,37 @@ ## [224. Basic Calculator (Hard)](https://leetcode.com/problems/basic-calculator "基本计算器") -

Implement a basic calculator to evaluate a simple expression string.

- -

The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces .

+

Given a string s representing an expression, implement a basic calculator to evaluate it.

+

 

Example 1:

-Input: "1 + 1"
+Input: s = "1 + 1"
 Output: 2
 

Example 2:

-Input: " 2-1 + 2 "
-Output: 3
+Input: s = " 2-1 + 2 " +Output: 3 +

Example 3:

-Input: "(1+(4+5+2)-3)+(6+8)"
-Output: 23
-Note: +Input: s = "(1+(4+5+2)-3)+(6+8)" +Output: 23 + + +

 

+

Constraints:

### Related Topics diff --git a/problems/binary-tree-level-order-traversal-ii/README.md b/problems/binary-tree-level-order-traversal-ii/README.md index d9e2f1472..979c5576a 100644 --- a/problems/binary-tree-level-order-traversal-ii/README.md +++ b/problems/binary-tree-level-order-traversal-ii/README.md @@ -9,7 +9,7 @@                  [Next >](../convert-sorted-array-to-binary-search-tree "Convert Sorted Array to Binary Search Tree") -## [107. Binary Tree Level Order Traversal II (Easy)](https://leetcode.com/problems/binary-tree-level-order-traversal-ii "二叉树的层次遍历 II") +## [107. Binary Tree Level Order Traversal II (Easy)](https://leetcode.com/problems/binary-tree-level-order-traversal-ii "二叉树的层序遍历 II")

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).

diff --git a/problems/binary-tree-maximum-path-sum/README.md b/problems/binary-tree-maximum-path-sum/README.md index b236d9243..508e0d2df 100644 --- a/problems/binary-tree-maximum-path-sum/README.md +++ b/problems/binary-tree-maximum-path-sum/README.md @@ -41,6 +41,7 @@ ### Related Topics [[Tree](../../tag/tree/README.md)] [[Depth-first Search](../../tag/depth-first-search/README.md)] + [[Recursion](../../tag/recursion/README.md)] ### Similar Questions 1. [Path Sum](../path-sum) (Easy) diff --git a/problems/binary-tree-zigzag-level-order-traversal/README.md b/problems/binary-tree-zigzag-level-order-traversal/README.md index 10e9b8169..16ffa8bda 100644 --- a/problems/binary-tree-zigzag-level-order-traversal/README.md +++ b/problems/binary-tree-zigzag-level-order-traversal/README.md @@ -9,7 +9,7 @@                  [Next >](../maximum-depth-of-binary-tree "Maximum Depth of Binary Tree") -## [103. Binary Tree Zigzag Level Order Traversal (Medium)](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal "二叉树的锯齿形层次遍历") +## [103. Binary Tree Zigzag Level Order Traversal (Medium)](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal "二叉树的锯齿形层序遍历")

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).

diff --git a/problems/burst-balloons/README.md b/problems/burst-balloons/README.md index 8dff26fd9..1ea3f3b9f 100644 --- a/problems/burst-balloons/README.md +++ b/problems/burst-balloons/README.md @@ -11,26 +11,38 @@ ## [312. Burst Balloons (Hard)](https://leetcode.com/problems/burst-balloons "戳气球") -

Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.

+

You are given n balloons, indexed from 0 to n - 1. Each balloon is painted with a number on it represented by an array nums. You are asked to burst all the balloons.

-

Find the maximum coins you can collect by bursting the balloons wisely.

+

If you burst the ith balloon, you will get nums[left] * nums[i] * nums[right] coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.

-

Note:

+

Return the maximum coins you can collect by bursting the balloons wisely.

- +

 

+

Example 1:

+ +
+Input: nums = [3,1,5,8]
+Output: 167
+Explanation:
+nums = [3,1,5,8] --> [3,5,8] --> [3,8] --> [8] --> []
+coins =  3*1*5    +   3*5*8   +  1*3*8  + 1*8*1 = 167
-

Example:

+

Example 2:

-Input: [3,1,5,8]
-Output: 167 
-Explanation: nums = [3,1,5,8] --> [3,5,8] -->   [3,8]   -->  [8]  --> []
-             coins =  3*1*5      +  3*5*8    +  1*3*8      + 1*8*1   = 167
+Input: nums = [1,5]
+Output: 10
 
+

 

+

Constraints:

+ + + ### Related Topics [[Divide and Conquer](../../tag/divide-and-conquer/README.md)] [[Dynamic Programming](../../tag/dynamic-programming/README.md)] diff --git a/problems/check-if-array-pairs-are-divisible-by-k/README.md b/problems/check-if-array-pairs-are-divisible-by-k/README.md index d023b74de..273b50fc4 100644 --- a/problems/check-if-array-pairs-are-divisible-by-k/README.md +++ b/problems/check-if-array-pairs-are-divisible-by-k/README.md @@ -61,10 +61,10 @@ ### Related Topics diff --git a/problems/checking-existence-of-edge-length-limited-paths/README.md b/problems/checking-existence-of-edge-length-limited-paths/README.md new file mode 100644 index 000000000..1365ed803 --- /dev/null +++ b/problems/checking-existence-of-edge-length-limited-paths/README.md @@ -0,0 +1,62 @@ + + + + + + + +[< Previous](../jump-game-vi "Jump Game VI") +                 +[Next >](../number-of-distinct-substrings-in-a-string "Number of Distinct Substrings in a String") + +## [1697. Checking Existence of Edge Length Limited Paths (Hard)](https://leetcode.com/problems/checking-existence-of-edge-length-limited-paths "检查边长度限制的路径是否存在") + +

An undirected graph of n nodes is defined by edgeList, where edgeList[i] = [ui, vi, disi] denotes an edge between nodes ui and vi with distance disi. Note that there may be multiple edges between two nodes.

+ +

Given an array queries, where queries[j] = [pj, qj, limitj], your task is to determine for each queries[j] whether there is a path between pj and qj such that each edge on the path has a distance strictly less than limitj .

+ +

Return a boolean array answer, where answer.length == queries.length and the jth value of answer is true if there is a path for queries[j] is true, and false otherwise.

+ +

 

+

Example 1:

+ +
+Input: n = 3, edgeList = [[0,1,2],[1,2,4],[2,0,8],[1,0,16]], queries = [[0,1,2],[0,2,5]]
+Output: [false,true]
+Explanation: The above figure shows the given graph. Note that there are two overlapping edges between 0 and 1 with distances 2 and 16.
+For the first query, between 0 and 1 there is no path where each distance is less than 2, thus we return false for this query.
+For the second query, there is a path (0 -> 1 -> 2) of two edges with distances less than 5, thus we return true for this query.
+
+ +

Example 2:

+ +
+Input: n = 5, edgeList = [[0,1,10],[1,2,5],[2,3,9],[3,4,13]], queries = [[0,4,14],[1,4,13]]
+Output: [true,false]
+Exaplanation: The above figure shows the given graph.
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Sort](../../tag/sort/README.md)] + [[Union Find](../../tag/union-find/README.md)] + +### Hints +
+Hint 1 +All the queries are given in advance. Is there a way you can reorder the queries to avoid repeated computations? +
diff --git a/problems/cherry-pickup/README.md b/problems/cherry-pickup/README.md index d78c181cc..bda881439 100644 --- a/problems/cherry-pickup/README.md +++ b/problems/cherry-pickup/README.md @@ -11,59 +11,52 @@ ## [741. Cherry Pickup (Hard)](https://leetcode.com/problems/cherry-pickup "摘樱桃") -

In a N x N grid representing a field of cherries, each cell is one of three possible integers.

- -

 

+

You are given an n x n grid representing a field of cherries, each cell is one of three possible integers.

-

 

- -

Your task is to collect maximum number of cherries possible by following the rules below:

- -

 

+

Return the maximum number of cherries you can collect by following the rules below:

 

- -

 

- -

Example 1:

- +

Example 1:

+
-Input: grid =
-[[0, 1, -1],
- [1, 0, -1],
- [1, 1,  1]]
-Output: 5
-Explanation: 
-The player started at (0, 0) and went down, down, right right to reach (2, 2).
+Input: grid = [[0,1,-1],[1,0,-1],[1,1,1]]
+Output: 5
+Explanation: The player started at (0, 0) and went down, down, right right to reach (2, 2).
 4 cherries were picked up during this single trip, and the matrix becomes [[0,1,-1],[0,0,-1],[0,0,0]].
 Then, the player went left, up, up, left to return home, picking up one more cherry.
 The total number of cherries picked up is 5, and this is the maximum possible.
 
-

 

+

Example 2:

-

Note:

+
+Input: grid = [[1,1,-1],[1,-1,1],[-1,1,1]]
+Output: 0
+
+ +

 

+

Constraints:

### Related Topics diff --git a/problems/concatenated-words/README.md b/problems/concatenated-words/README.md index bb66dc160..df394b693 100644 --- a/problems/concatenated-words/README.md +++ b/problems/concatenated-words/README.md @@ -11,27 +11,36 @@ ## [472. Concatenated Words (Hard)](https://leetcode.com/problems/concatenated-words "连接词") -Given a list of words (without duplicates), please write a program that returns all concatenated words in the given list of words. +

Given a list of words (without duplicates), please write a program that returns all concatenated words in the given list of words.

+

A concatenated word is defined as a string that is comprised entirely of at least two shorter words in the given array.

-

Example:
+

 

+

Example 1:

+
-Input: ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
+Input: words = ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]
+Output: ["catsdogcats","dogcatsdog","ratcatdogcat"]
+Explanation: "catsdogcats" can be concatenated by "cats", "dog" and "cats"; 
+"dogcatsdog" can be concatenated by "dog", "cats" and "dog"; 
+"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat".
-Output: ["catsdogcats","dogcatsdog","ratcatdogcat"] +

Example 2:

-Explanation: "catsdogcats" can be concatenated by "cats", "dog" and "cats";
"dogcatsdog" can be concatenated by "dog", "cats" and "dog";
"ratcatdogcat" can be concatenated by "rat", "cat", "dog" and "cat". +
+Input: words = ["cat","dog","catdog"]
+Output: ["catdog"]
 
-

- -

Note:
-

    -
  1. The number of elements of the given array will not exceed 10,000 -
  2. The length sum of elements in the given array will not exceed 600,000.
  3. -
  4. All the input string will only include lower case letters.
  5. -
  6. The returned elements order does not matter.
  7. -
-

+ +

 

+

Constraints:

+ + ### Related Topics [[Depth-first Search](../../tag/depth-first-search/README.md)] diff --git a/problems/consecutive-numbers/README.md b/problems/consecutive-numbers/README.md index 6a5efe60c..2944d265c 100644 --- a/problems/consecutive-numbers/README.md +++ b/problems/consecutive-numbers/README.md @@ -11,28 +11,47 @@ ## [180. Consecutive Numbers (Medium)](https://leetcode.com/problems/consecutive-numbers "连续出现的数字") -

Write a SQL query to find all numbers that appear at least three times consecutively.

+

Table: Logs

++-------------+---------+
+| Column Name | Type    |
++-------------+---------+
+| id          | int     |
+| num         | varchar |
++-------------+---------+
+id is the primary key for this table.
+
+ +

 

+ +

Write an SQL query to find all numbers that appear at least three times consecutively.

+ +

Return the result table in any order.

+ +

The query result format is in the following example:

+ +

 

+ +
+Logs table:
 +----+-----+
 | Id | Num |
 +----+-----+
-| 1  |  1  |
-| 2  |  1  |
-| 3  |  1  |
-| 4  |  2  |
-| 5  |  1  |
-| 6  |  2  |
-| 7  |  2  |
+| 1  | 1   |
+| 2  | 1   |
+| 3  | 1   |
+| 4  | 2   |
+| 5  | 1   |
+| 6  | 2   |
+| 7  | 2   |
 +----+-----+
-
-

For example, given the above Logs table, 1 is the only number that appears consecutively for at least three times.

- -
+Result table:
 +-----------------+
 | ConsecutiveNums |
 +-----------------+
 | 1               |
 +-----------------+
+1 is the only number that appears consecutively for at least three times.
 
diff --git a/problems/count-ways-to-distribute-candies/README.md b/problems/count-ways-to-distribute-candies/README.md new file mode 100644 index 000000000..e63e5eb22 --- /dev/null +++ b/problems/count-ways-to-distribute-candies/README.md @@ -0,0 +1,38 @@ + + + + + + + +[< Previous](../maximum-height-by-stacking-cuboids "Maximum Height by Stacking Cuboids ") +                 +[Next >](../daily-leads-and-partners "Daily Leads and Partners") + +## [1692. Count Ways to Distribute Candies (Hard)](https://leetcode.com/problems/count-ways-to-distribute-candies "") + + + +### Related Topics + [[Dynamic Programming](../../tag/dynamic-programming/README.md)] + +### Hints +
+Hint 1 +Try to define a recursive approach. For the ith candies, there will be one of the two following cases: +
+ +
+Hint 2 +If the i - 1 previous candies are already distributed into k bags for the ith candy, you can have k * dp[n - 1][k] ways to distribute the ith candy. We need then to solve the state of (n - 1, k). +
+ +
+Hint 3 +If the i - 1 previous candies are already distributed into k - 1 bags for the ith candy, you can have dp[n - 1][k - 1] ways to distribute the ith candy. We need then to solve the state of (n - 1, k - 1). +
+ +
+Hint 4 +This approach will be too slow and will traverse some states more than once. We should use memoization to make the algorithm efficient. +
diff --git a/problems/daily-leads-and-partners/README.md b/problems/daily-leads-and-partners/README.md new file mode 100644 index 000000000..aaf480f21 --- /dev/null +++ b/problems/daily-leads-and-partners/README.md @@ -0,0 +1,14 @@ + + + + + + + +[< Previous](../count-ways-to-distribute-candies "Count Ways to Distribute Candies") +                 +[Next >](../reformat-phone-number "Reformat Phone Number") + +## [1693. Daily Leads and Partners (Easy)](https://leetcode.com/problems/daily-leads-and-partners "") + + diff --git a/problems/daily-leads-and-partners/mysql_schemas.sql b/problems/daily-leads-and-partners/mysql_schemas.sql new file mode 100644 index 000000000..53e2eb911 --- /dev/null +++ b/problems/daily-leads-and-partners/mysql_schemas.sql @@ -0,0 +1,12 @@ +Create table If Not Exists DailySales(date_id date, make_name varchar(20), lead_id int, partner_id int); +Truncate table DailySales; +insert into DailySales (date_id, make_name, lead_id, partner_id) values ('2020-12-8', 'toyota', '0', '1'); +insert into DailySales (date_id, make_name, lead_id, partner_id) values ('2020-12-8', 'toyota', '1', '0'); +insert into DailySales (date_id, make_name, lead_id, partner_id) values ('2020-12-8', 'toyota', '1', '2'); +insert into DailySales (date_id, make_name, lead_id, partner_id) values ('2020-12-7', 'toyota', '0', '2'); +insert into DailySales (date_id, make_name, lead_id, partner_id) values ('2020-12-7', 'toyota', '0', '1'); +insert into DailySales (date_id, make_name, lead_id, partner_id) values ('2020-12-8', 'honda', '1', '2'); +insert into DailySales (date_id, make_name, lead_id, partner_id) values ('2020-12-8', 'honda', '2', '1'); +insert into DailySales (date_id, make_name, lead_id, partner_id) values ('2020-12-7', 'honda', '0', '1'); +insert into DailySales (date_id, make_name, lead_id, partner_id) values ('2020-12-7', 'honda', '1', '2'); +insert into DailySales (date_id, make_name, lead_id, partner_id) values ('2020-12-7', 'honda', '2', '1'); diff --git a/problems/design-underground-system/README.md b/problems/design-underground-system/README.md index 18ab2eb5a..ddd1c885f 100644 --- a/problems/design-underground-system/README.md +++ b/problems/design-underground-system/README.md @@ -11,30 +11,30 @@ ## [1396. Design Underground System (Medium)](https://leetcode.com/problems/design-underground-system "设计地铁系统") -

Implement the class UndergroundSystem that supports three methods:

- -

1. checkIn(int id, string stationName, int t)

- - - -

2. checkOut(int id, string stationName, int t)

- - - -

3. getAverageTime(string startStation, string endStation) 

+

Implement the UndergroundSystem class:

-

You can assume all calls to checkIn and checkOut methods are consistent. That is, if a customer gets in at time t1 at some station, then it gets out at time t2 with t2 > t1. All events happen in chronological order.

+

You can assume all calls to checkIn and checkOut methods are consistent. If a customer gets in at time t1 at some station, they get out at time t2 with t2 > t1. All events happen in chronological order.

 

Example 1:

@@ -90,11 +90,11 @@ undergroundSystem.getAverageTime("Leyton", "Paradise"); // r

Constraints:

### Related Topics diff --git a/problems/determine-if-string-halves-are-alike/README.md b/problems/determine-if-string-halves-are-alike/README.md new file mode 100644 index 000000000..52fddc049 --- /dev/null +++ b/problems/determine-if-string-halves-are-alike/README.md @@ -0,0 +1,68 @@ + + + + + + + +[< Previous](../minimum-adjacent-swaps-for-k-consecutive-ones "Minimum Adjacent Swaps for K Consecutive Ones") +                 +[Next >](../maximum-number-of-eaten-apples "Maximum Number of Eaten Apples") + +## [1704. Determine if String Halves Are Alike (Easy)](https://leetcode.com/problems/determine-if-string-halves-are-alike "判断字符串的两半是否相似") + +

You are given a string s of even length. Split this string into two halves of equal lengths, and let a be the first half and b be the second half.

+ +

Two strings are alike if they have the same number of vowels ('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'). Notice that s contains uppercase and lowercase letters.

+ +

Return true if a and b are alike. Otherwise, return false.

+ +

 

+

Example 1:

+ +
+Input: s = "book"
+Output: true
+Explanation: a = "bo" and b = "ok". a has 1 vowel and b has 1 vowel. Therefore, they are alike.
+
+ +

Example 2:

+ +
+Input: s = "textbook"
+Output: false
+Explanation: a = "text" and b = "book". a has 1 vowel whereas b has 2. Therefore, they are not alike.
+Notice that the vowel o is counted twice.
+
+ +

Example 3:

+ +
+Input: s = "MerryChristmas"
+Output: false
+
+ +

Example 4:

+ +
+Input: s = "AbCdEfGh"
+Output: true
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[String](../../tag/string/README.md)] + +### Hints +
+Hint 1 +Create a function that checks if a character is a vowel, either uppercase or lowercase. +
diff --git a/problems/distribute-coins-in-binary-tree/README.md b/problems/distribute-coins-in-binary-tree/README.md index 5a8d2fecc..f58dc8a7c 100644 --- a/problems/distribute-coins-in-binary-tree/README.md +++ b/problems/distribute-coins-in-binary-tree/README.md @@ -11,68 +11,52 @@ ## [979. Distribute Coins in Binary Tree (Medium)](https://leetcode.com/problems/distribute-coins-in-binary-tree "在二叉树中分配硬币") -

Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there are N coins total.

+

You are given the root of a binary tree with n nodes where each node in the tree has node.val coins and there are n coins total.

-

In one move, we may choose two adjacent nodes and move one coin from one node to another.  (The move may be from parent to child, or from child to parent.)

+

In one move, we may choose two adjacent nodes and move one coin from one node to another. (A move may be from parent to child, or from child to parent.)

Return the number of moves required to make every node have exactly one coin.

 

- -

Example 1:

- -

- +
-Input: [3,0,0]
-Output: 2
+Input: root = [3,0,0]
+Output: 2
 Explanation: From the root of the tree, we move one coin to its left child, and one coin to its right child.
 
-

Example 2:

- -

- +
-Input: [0,3,0]
-Output: 3
+Input: root = [0,3,0]
+Output: 3
 Explanation: From the left child of the root, we move two coins to the root [taking two moves].  Then, we move one coin from the root of the tree to the right child.
 
-

Example 3:

- -

- +
-Input: [1,0,2]
-Output: 2
+Input: root = [1,0,2]
+Output: 2
 
-

Example 4:

- -

- +
-Input: [1,0,0,null,3]
-Output: 4
+Input: root = [1,0,0,null,3]
+Output: 4
 

 

- -

Note:

- -
    -
  1. 1<= N <= 100
  2. -
  3. 0 <= node.val <= N
  4. -
-
-
-
-
+

Constraints:

+ + ### Related Topics [[Tree](../../tag/tree/README.md)] diff --git a/problems/exchange-seats/README.md b/problems/exchange-seats/README.md index b68568d1a..f05ac0931 100644 --- a/problems/exchange-seats/README.md +++ b/problems/exchange-seats/README.md @@ -12,13 +12,12 @@ ## [626. Exchange Seats (Medium)](https://leetcode.com/problems/exchange-seats "换座位")

Mary is a teacher in a middle school and she has a table seat storing students' names and their corresponding seat ids.

-The column id is continuous increment. -

 

-Mary wants to change seats for the adjacent students. +

The column id is continuous increment.

-

 

-Can you write a SQL query to output the result for Mary? +

Mary wants to change seats for the adjacent students.

+ +

Can you write a SQL query to output the result for Mary?

 

@@ -33,9 +32,8 @@ Can you write a SQL query to output the result for Mary? | 5 | Jeames | +---------+---------+ -For the sample input, the output is: -

 

+

For the sample input, the output is:

 +---------+---------+
@@ -49,5 +47,6 @@ For the sample input, the output is:
 +---------+---------+
 
-

Note:
-If the number of students is odd, there is no need to change the last one's seat.

+

Note:

+ +

If the number of students is odd, there is no need to change the last one's seat.

diff --git a/problems/factorial-trailing-zeroes/README.md b/problems/factorial-trailing-zeroes/README.md index 10e4a76f7..d5a86fafa 100644 --- a/problems/factorial-trailing-zeroes/README.md +++ b/problems/factorial-trailing-zeroes/README.md @@ -43,7 +43,7 @@

Constraints:

### Related Topics diff --git a/problems/fibonacci-number/README.md b/problems/fibonacci-number/README.md index c4047fe08..667678df2 100644 --- a/problems/fibonacci-number/README.md +++ b/problems/fibonacci-number/README.md @@ -11,21 +11,20 @@ ## [509. Fibonacci Number (Easy)](https://leetcode.com/problems/fibonacci-number "斐波那契数") -

The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,

+

The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,

-F(0) = 0,   F(1) = 1
-F(N) = F(N - 1) + F(N - 2), for N > 1.
+F(0) = 0, F(1) = 1
+F(n) = F(n - 1) + F(n - 2), for n > 1.
 
-

Given N, calculate F(N).

+

Given n, calculate F(n).

 

-

Example 1:

-Input: 2
+Input: n = 2
 Output: 1
 Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1.
 
@@ -33,7 +32,7 @@ F(N) = F(N - 1) + F(N - 2), for N > 1.

Example 2:

-Input: 3
+Input: n = 3
 Output: 2
 Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2.
 
@@ -41,16 +40,17 @@ F(N) = F(N - 1) + F(N - 2), for N > 1.

Example 3:

-Input: 4
+Input: n = 4
 Output: 3
 Explanation: F(4) = F(3) + F(2) = 2 + 1 = 3.
 

 

+

Constraints:

-

Note:

- -

0 ≤ N ≤ 30.

+ ### Related Topics [[Array](../../tag/array/README.md)] diff --git a/problems/find-bottom-left-tree-value/README.md b/problems/find-bottom-left-tree-value/README.md index fbca72dfd..eda7a540a 100644 --- a/problems/find-bottom-left-tree-value/README.md +++ b/problems/find-bottom-left-tree-value/README.md @@ -11,43 +11,30 @@ ## [513. Find Bottom Left Tree Value (Medium)](https://leetcode.com/problems/find-bottom-left-tree-value "找树左下角的值") -

-Given a binary tree, find the leftmost value in the last row of the tree. -

+

Given the root of a binary tree, return the leftmost value in the last row of the tree.

-

Example 1:
+

 

+

Example 1:

+
-Input:
-
-    2
-   / \
-  1   3
-
-Output:
-1
+Input: root = [2,1,3]
+Output: 1
 
-

-

Example 2:
+

Example 2:

+
-Input:
-
-        1
-       / \
-      2   3
-     /   / \
-    4   5   6
-       /
-      7
-
-Output:
-7
+Input: root = [1,2,3,4,null,5,6,null,null,7]
+Output: 7
 
-

-

Note: -You may assume the tree (i.e., the given root node) is not NULL. -

+

 

+

Constraints:

+ + ### Related Topics [[Tree](../../tag/tree/README.md)] diff --git a/problems/find-the-smallest-divisor-given-a-threshold/README.md b/problems/find-the-smallest-divisor-given-a-threshold/README.md index 2dec8542a..daee6eb4b 100644 --- a/problems/find-the-smallest-divisor-given-a-threshold/README.md +++ b/problems/find-the-smallest-divisor-given-a-threshold/README.md @@ -45,9 +45,9 @@ If the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the

Constraints:

### Related Topics diff --git a/problems/friend-requests-i-overall-acceptance-rate/README.md b/problems/friend-requests-i-overall-acceptance-rate/README.md index 455c32bf6..ea607c203 100644 --- a/problems/friend-requests-i-overall-acceptance-rate/README.md +++ b/problems/friend-requests-i-overall-acceptance-rate/README.md @@ -9,7 +9,7 @@                  [Next >](../range-addition-ii "Range Addition II") -## [597. Friend Requests I: Overall Acceptance Rate (Easy)](https://leetcode.com/problems/friend-requests-i-overall-acceptance-rate "好友申请 I :总体通过率") +## [597. Friend Requests I: Overall Acceptance Rate (Easy)](https://leetcode.com/problems/friend-requests-i-overall-acceptance-rate "好友申请 I:总体通过率") In social network like Facebook or Twitter, people send friend requests and accept others’ requests as well. Now given two tables as below:

 

diff --git a/problems/game-of-life/README.md b/problems/game-of-life/README.md index 59dfaf890..5022a55d0 100644 --- a/problems/game-of-life/README.md +++ b/problems/game-of-life/README.md @@ -18,37 +18,44 @@
  1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.
  2. Any live cell with two or three live neighbors lives on to the next generation.
  3. -
  4. Any live cell with more than three live neighbors dies, as if by over-population..
  5. +
  6. Any live cell with more than three live neighbors dies, as if by over-population.
  7. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

Write a function to compute the next state (after one update) of the board given its current state. The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously.

-

Example:

+

 

+

Example 1:

+ +
+Input: board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]
+Output: [[0,0,0],[1,0,1],[0,1,1],[0,1,0]]
+
+

Example 2:

+
-Input: 
-[
-  [0,1,0],
-  [0,0,1],
-  [1,1,1],
-  [0,0,0]
-]
-Output: 
-[
-  [0,0,0],
-  [1,0,1],
-  [0,1,1],
-  [0,1,0]
-]
+Input: board = [[1,1],[1,0]]
+Output: [[1,1],[1,1]]
 
-

Follow up:

+

 

+

Constraints:

-
    -
  1. Could you solve it in-place? Remember that the board needs to be updated at the same time: You cannot update some cells first and then use their updated values to update other cells.
  2. + + +

     

    +

    Follow up:

    + +
+ ### Related Topics [[Array](../../tag/array/README.md)] diff --git a/problems/grid-illumination/README.md b/problems/grid-illumination/README.md index db83aaade..bbf36b197 100644 --- a/problems/grid-illumination/README.md +++ b/problems/grid-illumination/README.md @@ -28,7 +28,7 @@ Explanation: We have the initial grid with all lamps turned off. In the above picture we see the grid after turning on the lamp at grid[0][0] then turning on the lamp at grid[4][4]. The 0th query asks if the lamp at grid[1][1] is illuminated or not (the blue square). It is illuminated, so set ans[0] = 1. Then, we turn off all lamps in the red square. -The 1st query asks if the lamp at grid[1][0] is illuminated or not (the blue square). It is not illuminated, so set ans[1] = 1. Then, we turn off all lamps in the red rectangle. +The 1st query asks if the lamp at grid[1][0] is illuminated or not (the blue square). It is not illuminated, so set ans[1] = 0. Then, we turn off all lamps in the red rectangle. diff --git a/problems/happy-number/README.md b/problems/happy-number/README.md index 5c6fe7fc3..8240a0ad8 100644 --- a/problems/happy-number/README.md +++ b/problems/happy-number/README.md @@ -11,24 +11,45 @@ ## [202. Happy Number (Easy)](https://leetcode.com/problems/happy-number "快乐数") -

Write an algorithm to determine if a number n is "happy".

+

Write an algorithm to determine if a number n is happy.

-

A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

+

A happy number is a number defined by the following process:

-

Return True if n is a happy number, and False if not.

+ -

Example: 

+

Return true if n is a happy number, and false if not.

+ +

 

+

Example 1:

-Input: 19
+Input: n = 19
 Output: true
-Explanation: 
-12 + 92 = 82
+Explanation:
+12 + 92 = 82
 82 + 22 = 68
 62 + 82 = 100
 12 + 02 + 02 = 1
 
+

Example 2:

+ +
+Input: n = 2
+Output: false
+
+ +

 

+

Constraints:

+ + + ### Related Topics [[Hash Table](../../tag/hash-table/README.md)] [[Math](../../tag/math/README.md)] diff --git a/problems/implement-stack-using-queues/README.md b/problems/implement-stack-using-queues/README.md index ae6a8f5f7..958a77d5d 100644 --- a/problems/implement-stack-using-queues/README.md +++ b/problems/implement-stack-using-queues/README.md @@ -58,7 +58,7 @@ myStack.empty(); // return False

 

-Follow-up: Can you implement the stack such that each operation is amortized O(1) time complexity? In other words, performing n operations will take overall O(n) time even if one of those operations may take longer. +Follow-up: Can you implement the stack such that each operation is amortized O(1) time complexity? In other words, performing n operations will take overall O(n) time even if one of those operations may take longer. You can use more than two queues. ### Related Topics [[Stack](../../tag/stack/README.md)] diff --git a/problems/increasing-triplet-subsequence/README.md b/problems/increasing-triplet-subsequence/README.md index 257fbdbac..fb3e9d238 100644 --- a/problems/increasing-triplet-subsequence/README.md +++ b/problems/increasing-triplet-subsequence/README.md @@ -11,32 +11,43 @@ ## [334. Increasing Triplet Subsequence (Medium)](https://leetcode.com/problems/increasing-triplet-subsequence "递增的三元子序列") -

Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.

+

Given an integer array nums, return true if there exists a triple of indices (i, j, k) such that i < j < k and nums[i] < nums[j] < nums[k]. If no such indices exists, return false.

-

Formally the function should:

- -
Return true if there exists i, j, k
-such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < kn-1 else return false.
- -

Note: Your algorithm should run in O(n) time complexity and O(1) space complexity.

- -
+

 

Example 1:

-Input: [1,2,3,4,5]
-Output: true
+Input: nums = [1,2,3,4,5]
+Output: true
+Explanation: Any triplet where i < j < k is valid.
 
-

Example 2:

-Input: [5,4,3,2,1]
-Output: false
+Input: nums = [5,4,3,2,1]
+Output: false
+Explanation: No triplet exists.
+
+ +

Example 3:

+ +
+Input: nums = [2,1,5,0,4,6]
+Output: true
+Explanation: The triplet (3, 4, 5) is valid because nums[3] == 0 < nums[4] == 4 < nums[5] == 6.
 
-
-
+ +

 

+

Constraints:

+ + + +

 

+Follow up: Could you implement a solution that runs in O(n) time complexity and O(1) space complexity? ### Similar Questions 1. [Longest Increasing Subsequence](../longest-increasing-subsequence) (Medium) diff --git a/problems/jewels-and-stones/README.md b/problems/jewels-and-stones/README.md index 62e973d48..04d02219c 100644 --- a/problems/jewels-and-stones/README.md +++ b/problems/jewels-and-stones/README.md @@ -11,29 +11,25 @@ ## [771. Jewels and Stones (Easy)](https://leetcode.com/problems/jewels-and-stones "宝石与石头") -

You're given strings J representing the types of stones that are jewels, and S representing the stones you have.  Each character in S is a type of stone you have.  You want to know how many of the stones you have are also jewels.

+

You're given strings jewels representing the types of stones that are jewels, and stones representing the stones you have. Each character in stones is a type of stone you have. You want to know how many of the stones you have are also jewels.

-

The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a different type of stone from "A".

+

Letters are case sensitive, so "a" is considered a different type of stone from "A".

+

 

Example 1:

- -
-Input: J = "aA", S = "aAAbbbb"
+
Input: jewels = "aA", stones = "aAAbbbb"
 Output: 3
-
- -

Example 2:

- -
-Input: J = "z", S = "ZZ"
+

Example 2:

+
Input: jewels = "z", stones = "ZZ"
 Output: 0
 
- -

Note:

+

 

+

Constraints:

### Related Topics diff --git a/problems/jump-game-iii/README.md b/problems/jump-game-iii/README.md index 9b9c278ff..4b5109f5d 100644 --- a/problems/jump-game-iii/README.md +++ b/problems/jump-game-iii/README.md @@ -49,7 +49,7 @@ index 0 -> index 4 -> index 1 -> index 3

Constraints:

diff --git a/problems/jump-game-vi/README.md b/problems/jump-game-vi/README.md new file mode 100644 index 000000000..2e41a72ec --- /dev/null +++ b/problems/jump-game-vi/README.md @@ -0,0 +1,63 @@ + + + + + + + +[< Previous](../maximum-erasure-value "Maximum Erasure Value") +                 +[Next >](../checking-existence-of-edge-length-limited-paths "Checking Existence of Edge Length Limited Paths") + +## [1696. Jump Game VI (Medium)](https://leetcode.com/problems/jump-game-vi "跳跃游戏 VI") + +

You are given a 0-indexed integer array nums and an integer k.

+ +

You are initially standing at index 0. In one move, you can jump at most k steps forward without going outside the boundaries of the array. That is, you can jump from index i to any index in the range [i + 1, min(n - 1, i + k)] inclusive.

+ +

You want to reach the last index of the array (index n - 1). Your score is the sum of all nums[j] for each index j you visited in the array.

+ +

Return the maximum score you can get.

+ +

 

+

Example 1:

+ +
+Input: nums = [1,-1,-2,4,-7,3], k = 2
+Output: 7
+Explanation: You can choose your jumps forming the subsequence [1,-1,4,3] (underlined above). The sum is 7.
+
+ +

Example 2:

+ +
+Input: nums = [10,-5,-2,4,0,3], k = 3
+Output: 17
+Explanation: You can choose your jumps forming the subsequence [10,4,3] (underlined above). The sum is 17.
+
+ +

Example 3:

+ +
+Input: nums = [1,-5,-20,4,-1,3,-6,-3], k = 2
+Output: 0
+
+ +

 

+

Constraints:

+ + + +### Hints +
+Hint 1 +Let dp[i] be "the maximum score to reach the end starting at index i". The answer for dp[i] is nums[i] + min{dp[i+j]} for 1 <= j <= k. That gives an O(n*k) solution. +
+ +
+Hint 2 +Instead of checking every j for every i, keep track of the smallest dp[i] values in a heap and calculate dp[i] from right to left. When the smallest value in the heap is out of bounds of the current index, remove it and keep checking. +
diff --git a/problems/jump-game/README.md b/problems/jump-game/README.md index 566f54531..b0d1ac8b7 100644 --- a/problems/jump-game/README.md +++ b/problems/jump-game/README.md @@ -11,7 +11,7 @@ ## [55. Jump Game (Medium)](https://leetcode.com/problems/jump-game "跳跃游戏") -

Given an array of non-negative integers, you are initially positioned at the first index of the array.

+

Given an array of non-negative integers nums, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

@@ -38,8 +38,8 @@

Constraints:

### Related Topics diff --git a/problems/length-of-longest-fibonacci-subsequence/README.md b/problems/length-of-longest-fibonacci-subsequence/README.md index 9ee36d6ae..2d0bb2b6b 100644 --- a/problems/length-of-longest-fibonacci-subsequence/README.md +++ b/problems/length-of-longest-fibonacci-subsequence/README.md @@ -11,49 +11,38 @@ ## [873. Length of Longest Fibonacci Subsequence (Medium)](https://leetcode.com/problems/length-of-longest-fibonacci-subsequence "最长的斐波那契子序列的长度") -

A sequence X_1, X_2, ..., X_n is fibonacci-like if:

+

A sequence X1, X2, ..., Xn is Fibonacci-like if:

-

Given a strictly increasing array A of positive integers forming a sequence, find the length of the longest fibonacci-like subsequence of A.  If one does not exist, return 0.

+

Given a strictly increasing array arr of positive integers forming a sequence, return the length of the longest Fibonacci-like subsequence of arr. If one does not exist, return 0.

-

(Recall that a subsequence is derived from another sequence A by deleting any number of elements (including none) from A, without changing the order of the remaining elements.  For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].)

+

A subsequence is derived from another sequence arr by deleting any number of elements (including none) from arr, without changing the order of the remaining elements. For example, [3, 5, 8] is a subsequence of [3, 4, 5, 6, 7, 8].

 

- - -

Example 1:

-Input: [1,2,3,4,5,6,7,8]
-Output: 5
-Explanation:
-The longest subsequence that is fibonacci-like: [1,2,3,5,8].
-
+Input: arr = [1,2,3,4,5,6,7,8] +Output: 5 +Explanation: The longest subsequence that is fibonacci-like: [1,2,3,5,8].

Example 2:

-Input: [1,3,7,11,12,14,18]
-Output: 3
-Explanation:
-The longest subsequence that is fibonacci-like:
-[1,11,12], [3,11,14] or [7,11,18].
-
+Input: arr = [1,3,7,11,12,14,18] +Output: 3 +Explanation: The longest subsequence that is fibonacci-like: [1,11,12], [3,11,14] or [7,11,18].

 

- -

Note:

+

Constraints:

### Related Topics diff --git a/problems/longest-increasing-subsequence/README.md b/problems/longest-increasing-subsequence/README.md index f19c9c7d6..47fd7b3f7 100644 --- a/problems/longest-increasing-subsequence/README.md +++ b/problems/longest-increasing-subsequence/README.md @@ -9,7 +9,7 @@                  [Next >](../remove-invalid-parentheses "Remove Invalid Parentheses") -## [300. Longest Increasing Subsequence (Medium)](https://leetcode.com/problems/longest-increasing-subsequence "最长上升子序列") +## [300. Longest Increasing Subsequence (Medium)](https://leetcode.com/problems/longest-increasing-subsequence "最长递增子序列")

Given an integer array nums, return the length of the longest strictly increasing subsequence.

diff --git a/problems/longest-palindrome/README.md b/problems/longest-palindrome/README.md index 15fd9a6e9..7839ec9e3 100644 --- a/problems/longest-palindrome/README.md +++ b/problems/longest-palindrome/README.md @@ -44,7 +44,7 @@ One longest palindrome that can be built is "dccaccd", whose length is ### Related Topics diff --git a/problems/longest-repeating-character-replacement/README.md b/problems/longest-repeating-character-replacement/README.md index 4effd154d..24bd5af8d 100644 --- a/problems/longest-repeating-character-replacement/README.md +++ b/problems/longest-repeating-character-replacement/README.md @@ -56,5 +56,5 @@ The substring "BBBB" has the longest repeating letters, which is 4. [[Sliding Window](../../tag/sliding-window/README.md)] ### Similar Questions - 1. [Longest Substring with At Most K Distinct Characters](../longest-substring-with-at-most-k-distinct-characters) (Hard) + 1. [Longest Substring with At Most K Distinct Characters](../longest-substring-with-at-most-k-distinct-characters) (Medium) 1. [Max Consecutive Ones III](../max-consecutive-ones-iii) (Medium) diff --git a/problems/longest-substring-with-at-most-k-distinct-characters/README.md b/problems/longest-substring-with-at-most-k-distinct-characters/README.md index 2ef66431a..8c2122c1d 100644 --- a/problems/longest-substring-with-at-most-k-distinct-characters/README.md +++ b/problems/longest-substring-with-at-most-k-distinct-characters/README.md @@ -9,7 +9,7 @@                  [Next >](../flatten-nested-list-iterator "Flatten Nested List Iterator") -## [340. Longest Substring with At Most K Distinct Characters (Hard)](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters "至多包含 K 个不同字符的最长子串") +## [340. Longest Substring with At Most K Distinct Characters (Medium)](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters "至多包含 K 个不同字符的最长子串")

Given a string, find the length of the longest substring T that contains at most k distinct characters.

@@ -32,6 +32,7 @@ ### Related Topics [[Hash Table](../../tag/hash-table/README.md)] + [[Two Pointers](../../tag/two-pointers/README.md)] [[String](../../tag/string/README.md)] [[Sliding Window](../../tag/sliding-window/README.md)] diff --git a/problems/longest-substring-with-at-most-two-distinct-characters/README.md b/problems/longest-substring-with-at-most-two-distinct-characters/README.md index a8177ceef..f7fc0adc4 100644 --- a/problems/longest-substring-with-at-most-two-distinct-characters/README.md +++ b/problems/longest-substring-with-at-most-two-distinct-characters/README.md @@ -36,5 +36,5 @@ ### Similar Questions 1. [Longest Substring Without Repeating Characters](../longest-substring-without-repeating-characters) (Medium) 1. [Sliding Window Maximum](../sliding-window-maximum) (Hard) - 1. [Longest Substring with At Most K Distinct Characters](../longest-substring-with-at-most-k-distinct-characters) (Hard) + 1. [Longest Substring with At Most K Distinct Characters](../longest-substring-with-at-most-k-distinct-characters) (Medium) 1. [Subarrays with K Different Integers](../subarrays-with-k-different-integers) (Hard) diff --git a/problems/longest-substring-without-repeating-characters/README.md b/problems/longest-substring-without-repeating-characters/README.md index 7cf835f90..1eeb9dc6c 100644 --- a/problems/longest-substring-without-repeating-characters/README.md +++ b/problems/longest-substring-without-repeating-characters/README.md @@ -62,5 +62,5 @@ Notice that the answer must be a substring, "pwke" is a subsequence an ### Similar Questions 1. [Longest Substring with At Most Two Distinct Characters](../longest-substring-with-at-most-two-distinct-characters) (Medium) - 1. [Longest Substring with At Most K Distinct Characters](../longest-substring-with-at-most-k-distinct-characters) (Hard) + 1. [Longest Substring with At Most K Distinct Characters](../longest-substring-with-at-most-k-distinct-characters) (Medium) 1. [Subarrays with K Different Integers](../subarrays-with-k-different-integers) (Hard) diff --git a/problems/max-consecutive-ones-iii/README.md b/problems/max-consecutive-ones-iii/README.md index 508de2dfd..4c0876bc1 100644 --- a/problems/max-consecutive-ones-iii/README.md +++ b/problems/max-consecutive-ones-iii/README.md @@ -55,7 +55,7 @@ Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. [[Sliding Window](../../tag/sliding-window/README.md)] ### Similar Questions - 1. [Longest Substring with At Most K Distinct Characters](../longest-substring-with-at-most-k-distinct-characters) (Hard) + 1. [Longest Substring with At Most K Distinct Characters](../longest-substring-with-at-most-k-distinct-characters) (Medium) 1. [Longest Repeating Character Replacement](../longest-repeating-character-replacement) (Medium) 1. [Max Consecutive Ones](../max-consecutive-ones) (Easy) 1. [Max Consecutive Ones II](../max-consecutive-ones-ii) (Medium) diff --git a/problems/maximum-binary-string-after-change/README.md b/problems/maximum-binary-string-after-change/README.md new file mode 100644 index 000000000..c12ef6abb --- /dev/null +++ b/problems/maximum-binary-string-after-change/README.md @@ -0,0 +1,68 @@ + + + + + + + +[< Previous](../average-waiting-time "Average Waiting Time") +                 +[Next >](../minimum-adjacent-swaps-for-k-consecutive-ones "Minimum Adjacent Swaps for K Consecutive Ones") + +## [1702. Maximum Binary String After Change (Medium)](https://leetcode.com/problems/maximum-binary-string-after-change "修改后的最大二进制字符串") + +

You are given a binary string binary consisting of only 0's or 1's. You can apply each of the following operations any number of times:

+ + + +

Return the maximum binary string you can obtain after any number of operations. Binary string x is greater than binary string y if x's decimal representation is greater than y's decimal representation.

+ +

 

+

Example 1:

+ +
+Input: binary = "000110"
+Output: "111011"
+Explanation: A valid transformation sequence can be:
+"000110" -> "000101" 
+"000101" -> "100101" 
+"100101" -> "110101" 
+"110101" -> "110011" 
+"110011" -> "111011"
+
+ +

Example 2:

+ +
+Input: binary = "01"
+Output: "01"
+Explanation: "01" cannot be transformed any further.
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Greedy](../../tag/greedy/README.md)] + +### Hints +
+Hint 1 +Note that with the operations, you can always make the string only contain at most 1 zero. +
diff --git a/problems/maximum-binary-tree/README.md b/problems/maximum-binary-tree/README.md index 0c572f88c..13580bfb6 100644 --- a/problems/maximum-binary-tree/README.md +++ b/problems/maximum-binary-tree/README.md @@ -11,39 +11,39 @@ ## [654. Maximum Binary Tree (Medium)](https://leetcode.com/problems/maximum-binary-tree "最大二叉树") -

-Given an integer array with no duplicates. A maximum tree building on this array is defined as follow: +

Given an integer array with no duplicates. A maximum tree building on this array is defined as follow:

+
    -
  1. The root is the maximum number in the array.
  2. -
  3. The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
  4. -
  5. The right subtree is the maximum tree constructed from right part subarray divided by the maximum number.
  6. +
  7. The root is the maximum number in the array.
  8. +
  9. The left subtree is the maximum tree constructed from left part subarray divided by the maximum number.
  10. +
  11. The right subtree is the maximum tree constructed from right part subarray divided by the maximum number.
-

-

-Construct the maximum tree by the given array and output the root node of this tree. -

+

Construct the maximum tree by the given array and output the root node of this tree.

-

Example 1:
+

 

+

Example 1:

+
-Input: [3,2,1,6,0,5]
-Output: return the tree root node representing the following tree:
-
-      6
-    /   \
-   3     5
-    \    / 
-     2  0   
-       \
-        1
+Input: nums = [3,2,1,6,0,5]
+Output: [6,3,5,null,2,0,null,null,1]
 
-

-

Note:
-

    -
  1. The size of the given array will be in the range [1,1000].
  2. -
-

+

Example 2:

+ +
+Input: nums = [3,2,1]
+Output: [3,null,2,null,1]
+
+ +

 

+

Constraints:

+ + ### Related Topics [[Tree](../../tag/tree/README.md)] diff --git a/problems/maximum-erasure-value/README.md b/problems/maximum-erasure-value/README.md new file mode 100644 index 000000000..688835a6e --- /dev/null +++ b/problems/maximum-erasure-value/README.md @@ -0,0 +1,57 @@ + + + + + + + +[< Previous](../reformat-phone-number "Reformat Phone Number") +                 +[Next >](../jump-game-vi "Jump Game VI") + +## [1695. Maximum Erasure Value (Medium)](https://leetcode.com/problems/maximum-erasure-value "删除子数组的最大得分") + +

You are given an array of positive integers nums and want to erase a subarray containing unique elements. The score you get by erasing the subarray is equal to the sum of its elements.

+ +

Return the maximum score you can get by erasing exactly one subarray.

+ +

An array b is called to be a subarray of a if it forms a contiguous subsequence of a, that is, if it is equal to a[l],a[l+1],...,a[r] for some (l,r).

+ +

 

+

Example 1:

+ +
+Input: nums = [4,2,4,5,6]
+Output: 17
+Explanation: The optimal subarray here is [2,4,5,6].
+
+ +

Example 2:

+ +
+Input: nums = [5,2,1,2,5,2,1,2,5]
+Output: 8
+Explanation: The optimal subarray here is [5,2,1] or [1,2,5].
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Two Pointers](../../tag/two-pointers/README.md)] + +### Hints +
+Hint 1 +The main point here is for the subarray to contain unique elements for each index. Only the first subarrays starting from that index have unique elements. +
+ +
+Hint 2 +This can be solved using the two pointers technique +
diff --git a/problems/maximum-height-by-stacking-cuboids/README.md b/problems/maximum-height-by-stacking-cuboids/README.md index 2f6182622..1d77d2dec 100644 --- a/problems/maximum-height-by-stacking-cuboids/README.md +++ b/problems/maximum-height-by-stacking-cuboids/README.md @@ -7,7 +7,7 @@ [< Previous](../stone-game-vii "Stone Game VII")                  -Next > +[Next >](../count-ways-to-distribute-candies "Count Ways to Distribute Candies") ## [1691. Maximum Height by Stacking Cuboids (Hard)](https://leetcode.com/problems/maximum-height-by-stacking-cuboids "堆叠长方体的最大高度") diff --git a/problems/maximum-number-of-eaten-apples/README.md b/problems/maximum-number-of-eaten-apples/README.md new file mode 100644 index 000000000..0c47a6301 --- /dev/null +++ b/problems/maximum-number-of-eaten-apples/README.md @@ -0,0 +1,68 @@ + + + + + + + +[< Previous](../determine-if-string-halves-are-alike "Determine if String Halves Are Alike") +                 +[Next >](../where-will-the-ball-fall "Where Will the Ball Fall") + +## [1705. Maximum Number of Eaten Apples (Medium)](https://leetcode.com/problems/maximum-number-of-eaten-apples "吃苹果的最大数目") + +

There is a special kind of apple tree that grows apples every day for n days. On the ith day, the tree grows apples[i] apples that will rot after days[i] days, that is on day i + days[i] the apples will be rotten and cannot be eaten. On some days, the apple tree does not grow any apples, which are denoted by apples[i] == 0 and days[i] == 0.

+ +

You decided to eat at most one apple a day (to keep the doctors away). Note that you can keep eating after the first n days.

+ +

Given two integer arrays days and apples of length n, return the maximum number of apples you can eat.

+ +

 

+

Example 1:

+ +
+Input: apples = [1,2,3,5,2], days = [3,2,1,4,2]
+Output: 7
+Explanation: You can eat 7 apples:
+- On the first day, you eat an apple that grew on the first day.
+- On the second day, you eat an apple that grew on the second day.
+- On the third day, you eat an apple that grew on the second day. After this day, the apples that grew on the third day rot.
+- On the fourth to the seventh days, you eat apples that grew on the fourth day.
+
+ +

Example 2:

+ +
+Input: apples = [3,0,0,0,0,2], days = [3,0,0,0,0,2]
+Output: 5
+Explanation: You can eat 5 apples:
+- On the first to the third day you eat apples that grew on the first day.
+- Do nothing on the fouth and fifth days.
+- On the sixth and seventh days you eat apples that grew on the sixth day.
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Heap](../../tag/heap/README.md)] + [[Greedy](../../tag/greedy/README.md)] + +### Hints +
+Hint 1 +It's optimal to finish the apples that will rot first before those that will rot last +
+ +
+Hint 2 +You need a structure to keep the apples sorted by their finish time +
diff --git a/problems/maximum-xor-with-an-element-from-array/README.md b/problems/maximum-xor-with-an-element-from-array/README.md new file mode 100644 index 000000000..61cf253d5 --- /dev/null +++ b/problems/maximum-xor-with-an-element-from-array/README.md @@ -0,0 +1,71 @@ + + + + + + + +[< Previous](../where-will-the-ball-fall "Where Will the Ball Fall") +                 +Next > + +## [1707. Maximum XOR With an Element From Array (Hard)](https://leetcode.com/problems/maximum-xor-with-an-element-from-array "与数组中元素的最大异或值") + +

You are given an array nums consisting of non-negative integers. You are also given a queries array, where queries[i] = [xi, mi].

+ +

The answer to the ith query is the maximum bitwise XOR value of xi and any element of nums that does not exceed mi. In other words, the answer is max(nums[j] XOR xi) for all j such that nums[j] <= mi. If all elements in nums are larger than mi, then the answer is -1.

+ +

Return an integer array answer where answer.length == queries.length and answer[i] is the answer to the ith query.

+ +

 

+

Example 1:

+ +
+Input: nums = [0,1,2,3,4], queries = [[3,1],[1,3],[5,6]]
+Output: [3,3,7]
+Explanation:
+1) 0 and 1 are the only two integers not greater than 1. 0 XOR 3 = 3 and 1 XOR 3 = 2. The larger of the two is 3.
+2) 1 XOR 2 = 3.
+3) 5 XOR 2 = 7.
+
+ +

Example 2:

+ +
+Input: nums = [5,2,4,6,6,3], queries = [[12,4],[8,1],[6,3]]
+Output: [15,-1,5]
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Bit Manipulation](../../tag/bit-manipulation/README.md)] + [[Trie](../../tag/trie/README.md)] + +### Hints +
+Hint 1 +In problems involving bitwise operations, we often think on the bits level. In this problem, we can think that to maximize the result of an xor operation, we need to maximize the most significant bit, then the next one, and so on. +
+ +
+Hint 2 +If there's some number in the array that is less than m and whose the most significant bit is different than that of x, then xoring with this number maximizes the most significant bit, so I know this bit in the answer is 1. +
+ +
+Hint 3 +To check the existence of such numbers and narrow your scope for further bits based on your choice, you can use trie. +
+ +
+Hint 4 +You can sort the array and the queries, and maintain the trie such that in each query the trie consists exactly of the valid elements. +
diff --git a/problems/minimum-adjacent-swaps-for-k-consecutive-ones/README.md b/problems/minimum-adjacent-swaps-for-k-consecutive-ones/README.md new file mode 100644 index 000000000..41faceb26 --- /dev/null +++ b/problems/minimum-adjacent-swaps-for-k-consecutive-ones/README.md @@ -0,0 +1,69 @@ + + + + + + + +[< Previous](../maximum-binary-string-after-change "Maximum Binary String After Change") +                 +[Next >](../determine-if-string-halves-are-alike "Determine if String Halves Are Alike") + +## [1703. Minimum Adjacent Swaps for K Consecutive Ones (Hard)](https://leetcode.com/problems/minimum-adjacent-swaps-for-k-consecutive-ones "得到连续 K 个 1 的最少相邻交换次数") + +

You are given an integer array, nums, and an integer k. nums comprises of only 0's and 1's. In one move, you can choose two adjacent indices and swap their values.

+ +

Return the minimum number of moves required so that nums has k consecutive 1's.

+ +

 

+

Example 1:

+ +
+Input: nums = [1,0,0,1,0,1], k = 2
+Output: 1
+Explanation: In 1 move, nums could be [1,0,0,0,1,1] and have 2 consecutive 1's.
+
+ +

Example 2:

+ +
+Input: nums = [1,0,0,0,0,0,1,1], k = 3
+Output: 5
+Explanation: In 5 moves, the leftmost 1 can be shifted right until nums = [0,0,0,0,0,1,1,1].
+
+ +

Example 3:

+ +
+Input: nums = [1,1,0,1], k = 2
+Output: 0
+Explanation: nums already has 2 consecutive 1's.
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Stack](../../tag/stack/README.md)] + +### Hints +
+Hint 1 +Choose k 1s and determine how many steps are required to move them into 1 group. +
+ +
+Hint 2 +Maintain a sliding window of k 1s, and maintain the steps required to group them. +
+ +
+Hint 3 +When you slide the window across, should you move the group to the right? Once you move the group to the right, it will never need to slide to the left again. +
diff --git a/problems/minimum-genetic-mutation/README.md b/problems/minimum-genetic-mutation/README.md index 9ebb42a3c..87fca633e 100644 --- a/problems/minimum-genetic-mutation/README.md +++ b/problems/minimum-genetic-mutation/README.md @@ -68,4 +68,4 @@ return: 3

 

### Similar Questions - 1. [Word Ladder](../word-ladder) (Medium) + 1. [Word Ladder](../word-ladder) (Hard) diff --git a/problems/minimum-moves-to-equal-array-elements/README.md b/problems/minimum-moves-to-equal-array-elements/README.md index 30e37eeb0..5ec44f81f 100644 --- a/problems/minimum-moves-to-equal-array-elements/README.md +++ b/problems/minimum-moves-to-equal-array-elements/README.md @@ -9,7 +9,7 @@                  [Next >](../4sum-ii "4Sum II") -## [453. Minimum Moves to Equal Array Elements (Easy)](https://leetcode.com/problems/minimum-moves-to-equal-array-elements "最小移动次数使数组元素相等") +## [453. Minimum Moves to Equal Array Elements (Easy)](https://leetcode.com/problems/minimum-moves-to-equal-array-elements "最小操作次数使数组元素相等")

Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.

diff --git a/problems/minimum-time-visiting-all-points/README.md b/problems/minimum-time-visiting-all-points/README.md index bd6d6dc5f..b50308ecf 100644 --- a/problems/minimum-time-visiting-all-points/README.md +++ b/problems/minimum-time-visiting-all-points/README.md @@ -11,13 +11,20 @@ ## [1266. Minimum Time Visiting All Points (Easy)](https://leetcode.com/problems/minimum-time-visiting-all-points "访问所有点的最小时间") -

On a plane there are n points with integer coordinates points[i] = [xi, yi]. Your task is to find the minimum time in seconds to visit all points.

+

On a 2D plane, there are n points with integer coordinates points[i] = [xi, yi]. Return the minimum time in seconds to visit all the points in the order given by points.

-

You can move according to the next rules:

+

You can move according to these rules:

 

diff --git a/problems/most-stones-removed-with-same-row-or-column/README.md b/problems/most-stones-removed-with-same-row-or-column/README.md index 4be096fc6..8e45d05d1 100644 --- a/problems/most-stones-removed-with-same-row-or-column/README.md +++ b/problems/most-stones-removed-with-same-row-or-column/README.md @@ -11,49 +11,42 @@ ## [947. Most Stones Removed with Same Row or Column (Medium)](https://leetcode.com/problems/most-stones-removed-with-same-row-or-column "移除最多的同行或同列石头") -

On a 2D plane, we place stones at some integer coordinate points.  Each coordinate point may have at most one stone.

+

On a 2D plane, we place stones at some integer coordinate points. Each coordinate point may have at most one stone.

-

Now, a move consists of removing a stone that shares a column or row with another stone on the grid.

+

A move consists of removing a stone that shares a column or row with another stone on the grid.

-

What is the largest possible number of moves we can make?

+

Given an array stones where stones[i] = [xi, yi], return the largest possible number of moves we can make.

 

- -

Example 1:

-Input: stones = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]
-Output: 5
+Input: stones = [[0,0],[0,1],[1,0],[1,2],[2,1],[2,2]]
+Output: 5
 
-

Example 2:

-Input: stones = [[0,0],[0,2],[1,1],[2,0],[2,2]]
-Output: 3
+Input: stones = [[0,0],[0,2],[1,1],[2,0],[2,2]]
+Output: 3
 
-

Example 3:

-Input: stones = [[0,0]]
-Output: 0
+Input: stones = [[0,0]]
+Output: 0
 

 

+

Constraints:

-

Note:

- -
    +
    • 1 <= stones.length <= 1000
    • -
    • 0 <= stones[i][j] < 10000
    • -
-
-
-
+
  • 0 <= xi, yi < 104
  • +
  • stones contains unique values.
  • + ### Related Topics [[Depth-first Search](../../tag/depth-first-search/README.md)] diff --git a/problems/nth-magical-number/README.md b/problems/nth-magical-number/README.md index 88e9ae5b7..c7aaf6b59 100644 --- a/problems/nth-magical-number/README.md +++ b/problems/nth-magical-number/README.md @@ -11,60 +11,31 @@ ## [878. Nth Magical Number (Hard)](https://leetcode.com/problems/nth-magical-number "第 N 个神奇数字") -

    A positive integer is magical if it is divisible by either A or B.

    +

    A positive integer is magical if it is divisible by either a or b.

    -

    Return the N-th magical number.  Since the answer may be very large, return it modulo 10^9 + 7.

    +

    Given the three integers n, a, and b, return the nth magical number. Since the answer may be very large, return it modulo 109 + 7.

     

    - -
      -
    - -

    Example 1:

    - -
    -Input: N = 1, A = 2, B = 3
    -Output: 2
    -
    - -
    -

    Example 2:

    - -
    -Input: N = 4, A = 2, B = 3
    -Output: 6
    -
    - -
    -

    Example 3:

    - -
    -Input: N = 5, A = 2, B = 4
    -Output: 10
    +
    Input: n = 1, a = 2, b = 3
    +Output: 2
    +

    Example 2:

    +
    Input: n = 4, a = 2, b = 3
    +Output: 6
    +

    Example 3:

    +
    Input: n = 5, a = 2, b = 4
    +Output: 10
    +

    Example 4:

    +
    Input: n = 3, a = 6, b = 4
    +Output: 8
     
    - -
    -

    Example 4:

    - -
    -Input: N = 3, A = 6, B = 4
    -Output: 8
    -
    -

     

    +

    Constraints:

    -

    Note:

    - -
      -
    1. 1 <= N <= 10^9
    2. -
    3. 2 <= A <= 40000
    4. -
    5. 2 <= B <= 40000
    6. -
    -
    -
    -
    -
    + ### Related Topics [[Math](../../tag/math/README.md)] diff --git a/problems/number-of-calls-between-two-persons/README.md b/problems/number-of-calls-between-two-persons/README.md new file mode 100644 index 000000000..0a6b30900 --- /dev/null +++ b/problems/number-of-calls-between-two-persons/README.md @@ -0,0 +1,14 @@ + + + + + + + +[< Previous](../number-of-distinct-substrings-in-a-string "Number of Distinct Substrings in a String") +                 +[Next >](../number-of-students-unable-to-eat-lunch "Number of Students Unable to Eat Lunch") + +## [1699. Number of Calls Between Two Persons (Medium)](https://leetcode.com/problems/number-of-calls-between-two-persons "") + + diff --git a/problems/number-of-calls-between-two-persons/mysql_schemas.sql b/problems/number-of-calls-between-two-persons/mysql_schemas.sql new file mode 100644 index 000000000..d2578c68b --- /dev/null +++ b/problems/number-of-calls-between-two-persons/mysql_schemas.sql @@ -0,0 +1,9 @@ +Create table If Not Exists Calls (from_id int, to_id int, duration int); +Truncate table Calls; +insert into Calls (from_id, to_id, duration) values ('1', '2', '59'); +insert into Calls (from_id, to_id, duration) values ('2', '1', '11'); +insert into Calls (from_id, to_id, duration) values ('1', '3', '20'); +insert into Calls (from_id, to_id, duration) values ('3', '4', '100'); +insert into Calls (from_id, to_id, duration) values ('3', '4', '200'); +insert into Calls (from_id, to_id, duration) values ('3', '4', '200'); +insert into Calls (from_id, to_id, duration) values ('4', '3', '499'); diff --git a/problems/number-of-digit-one/README.md b/problems/number-of-digit-one/README.md index 787f8f7e0..7c3ca78c1 100644 --- a/problems/number-of-digit-one/README.md +++ b/problems/number-of-digit-one/README.md @@ -11,16 +11,30 @@ ## [233. Number of Digit One (Hard)](https://leetcode.com/problems/number-of-digit-one "数字 1 的个数") -

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.

    +

    Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.

    -

    Example:

    +

     

    +

    Example 1:

    -Input: 13
    -Output: 6 
    -Explanation: Digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.
    +Input: n = 13
    +Output: 6
     
    +

    Example 2:

    + +
    +Input: n = 0
    +Output: 0
    +
    + +

     

    +

    Constraints:

    + + + ### Related Topics [[Math](../../tag/math/README.md)] diff --git a/problems/number-of-distinct-substrings-in-a-string/README.md b/problems/number-of-distinct-substrings-in-a-string/README.md new file mode 100644 index 000000000..568bad013 --- /dev/null +++ b/problems/number-of-distinct-substrings-in-a-string/README.md @@ -0,0 +1,38 @@ + + + + + + + +[< Previous](../checking-existence-of-edge-length-limited-paths "Checking Existence of Edge Length Limited Paths") +                 +[Next >](../number-of-calls-between-two-persons "Number of Calls Between Two Persons") + +## [1698. Number of Distinct Substrings in a String (Medium)](https://leetcode.com/problems/number-of-distinct-substrings-in-a-string "") + + + +### Related Topics + [[String](../../tag/string/README.md)] + +### Hints +
    +Hint 1 +Calculate the prefix hashing array for s. +
    + +
    +Hint 2 +Use the prefix hashing array to calculate the hashing value of each substring. +
    + +
    +Hint 3 +Compare the hashing values to determine the unique substrings. +
    + +
    +Hint 4 +There could be collisions if you use hashing, what about double hashing. +
    diff --git a/problems/number-of-students-unable-to-eat-lunch/README.md b/problems/number-of-students-unable-to-eat-lunch/README.md new file mode 100644 index 000000000..8e0012d46 --- /dev/null +++ b/problems/number-of-students-unable-to-eat-lunch/README.md @@ -0,0 +1,74 @@ + + + + + + + +[< Previous](../number-of-calls-between-two-persons "Number of Calls Between Two Persons") +                 +[Next >](../average-waiting-time "Average Waiting Time") + +## [1700. Number of Students Unable to Eat Lunch (Easy)](https://leetcode.com/problems/number-of-students-unable-to-eat-lunch "无法吃午餐的学生数量") + +

    The school cafeteria offers circular and square sandwiches at lunch break, referred to by numbers 0 and 1 respectively. All students stand in a queue. Each student either prefers square or circular sandwiches.

    + +

    The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches are placed in a stack. At each step:

    + + + +

    This continues until none of the queue students want to take the top sandwich and are thus unable to eat.

    + +

    You are given two integer arrays students and sandwiches where sandwiches[i] is the type of the i​​​​​​th sandwich in the stack (i = 0 is the top of the stack) and students[j] is the preference of the j​​​​​​th student in the initial queue (j = 0 is the front of the queue). Return the number of students that are unable to eat.

    + +

     

    +

    Example 1:

    + +
    +Input: students = [1,1,0,0], sandwiches = [0,1,0,1]
    +Output: 0 
    +Explanation:
    +- Front student leaves the top sandwich and returns to the end of the line making students = [1,0,0,1].
    +- Front student leaves the top sandwich and returns to the end of the line making students = [0,0,1,1].
    +- Front student takes the top sandwich and leaves the line making students = [0,1,1] and sandwiches = [1,0,1].
    +- Front student leaves the top sandwich and returns to the end of the line making students = [1,1,0].
    +- Front student takes the top sandwich and leaves the line making students = [1,0] and sandwiches = [0,1].
    +- Front student leaves the top sandwich and returns to the end of the line making students = [0,1].
    +- Front student takes the top sandwich and leaves the line making students = [1] and sandwiches = [1].
    +- Front student takes the top sandwich and leaves the line making students = [] and sandwiches = [].
    +Hence all students are able to eat.
    +
    + +

    Example 2:

    + +
    +Input: students = [1,1,1,0,0,1], sandwiches = [1,0,0,0,1,1]
    +Output: 3
    +
    + +

     

    +

    Constraints:

    + + + +### Related Topics + [[Array](../../tag/array/README.md)] + +### Hints +
    +Hint 1 +Simulate the given in the statement +
    + +
    +Hint 2 +Calculate those who will eat instead of those who will not. +
    diff --git a/problems/numbers-with-same-consecutive-differences/README.md b/problems/numbers-with-same-consecutive-differences/README.md index 049e40dcf..ce80e4f3c 100644 --- a/problems/numbers-with-same-consecutive-differences/README.md +++ b/problems/numbers-with-same-consecutive-differences/README.md @@ -13,7 +13,7 @@

    Return all non-negative integers of length n such that the absolute difference between every two consecutive digits is k.

    -

    Note that every number in the answer must not have leading zeros except for the number 0 itself. For example, 01 has one leading zero and is invalid, but 0 is valid.

    +

    Note that every number in the answer must not have leading zeros. For example, 01 has one leading zero and is invalid.

    You may return the answer in any order.

    @@ -65,3 +65,5 @@ ### Related Topics [[Depth-first Search](../../tag/depth-first-search/README.md)] [[Breadth-first Search](../../tag/breadth-first-search/README.md)] + [[Recursion](../../tag/recursion/README.md)] + [[Backtracking](../../tag/backtracking/README.md)] diff --git a/problems/partition-array-into-three-parts-with-equal-sum/README.md b/problems/partition-array-into-three-parts-with-equal-sum/README.md index 0d9a74fe5..44cc8fba4 100644 --- a/problems/partition-array-into-three-parts-with-equal-sum/README.md +++ b/problems/partition-array-into-three-parts-with-equal-sum/README.md @@ -11,15 +11,15 @@ ## [1013. Partition Array Into Three Parts With Equal Sum (Easy)](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum "将数组分成和相等的三个部分") -

    Given an array A of integers, return true if and only if we can partition the array into three non-empty parts with equal sums.

    +

    Given an array of integers arr, return true if we can partition the array into three non-empty parts with equal sums.

    -

    Formally, we can partition the array if we can find indexes i+1 < j with (A[0] + A[1] + ... + A[i] == A[i+1] + A[i+2] + ... + A[j-1] == A[j] + A[j-1] + ... + A[A.length - 1])

    +

    Formally, we can partition the array if we can find indexes i + 1 < j with (arr[0] + arr[1] + ... + arr[i] == arr[i + 1] + arr[i + 2] + ... + arr[j - 1] == arr[j] + arr[j + 1] + ... + arr[arr.length - 1])

     

    Example 1:

    -Input: A = [0,2,1,-6,6,-7,9,1,2,0,1]
    +Input: arr = [0,2,1,-6,6,-7,9,1,2,0,1]
     Output: true
     Explanation: 0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1
     
    @@ -27,14 +27,14 @@

    Example 2:

    -Input: A = [0,2,1,-6,6,7,9,-1,2,0,1]
    +Input: arr = [0,2,1,-6,6,7,9,-1,2,0,1]
     Output: false
     

    Example 3:

    -Input: A = [3,3,6,5,-2,2,5,1,-9,4]
    +Input: arr = [3,3,6,5,-2,2,5,1,-9,4]
     Output: true
     Explanation: 3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4
     
    @@ -43,8 +43,8 @@

    Constraints:

    ### Related Topics diff --git a/problems/previous-permutation-with-one-swap/README.md b/problems/previous-permutation-with-one-swap/README.md index dffedec5a..053e6e743 100644 --- a/problems/previous-permutation-with-one-swap/README.md +++ b/problems/previous-permutation-with-one-swap/README.md @@ -11,50 +11,48 @@ ## [1053. Previous Permutation With One Swap (Medium)](https://leetcode.com/problems/previous-permutation-with-one-swap "交换一次的先前排列") -

    Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]).  If it cannot be done, then return the same array.

    +

    Given an array of positive integers arr (not necessarily distinct), return the lexicographically largest permutation that is smaller than arr, that can be made with exactly one swap (A swap exchanges the positions of two numbers arr[i] and arr[j]). If it cannot be done, then return the same array.

     

    -

    Example 1:

    -Input: [3,2,1]
    -Output: [3,1,2]
    -Explanation: Swapping 2 and 1.
    +Input: arr = [3,2,1]
    +Output: [3,1,2]
    +Explanation: Swapping 2 and 1.
     

    Example 2:

    -Input: [1,1,5]
    -Output: [1,1,5]
    -Explanation: This is already the smallest permutation.
    +Input: arr = [1,1,5]
    +Output: [1,1,5]
    +Explanation: This is already the smallest permutation.
     

    Example 3:

    -Input: [1,9,4,6,7]
    -Output: [1,7,4,6,9]
    -Explanation: Swapping 9 and 7.
    +Input: arr = [1,9,4,6,7]
    +Output: [1,7,4,6,9]
    +Explanation: Swapping 9 and 7.
     

    Example 4:

    -Input: [3,1,1,3]
    -Output: [1,3,1,3]
    -Explanation: Swapping 1 and 3.
    +Input: arr = [3,1,1,3]
    +Output: [1,3,1,3]
    +Explanation: Swapping 1 and 3.
     

     

    +

    Constraints:

    -

    Note:

    - -
      -
    1. 1 <= A.length <= 10000
    2. -
    3. 1 <= A[i] <= 10000
    4. -
    + ### Related Topics [[Greedy](../../tag/greedy/README.md)] diff --git a/problems/rectangle-area-ii/README.md b/problems/rectangle-area-ii/README.md index eac757cc8..d96646f5c 100644 --- a/problems/rectangle-area-ii/README.md +++ b/problems/rectangle-area-ii/README.md @@ -11,35 +11,35 @@ ## [850. Rectangle Area II (Hard)](https://leetcode.com/problems/rectangle-area-ii "矩形面积 II") -

    We are given a list of (axis-aligned) rectangles.  Each rectangle[i] = [x1, y1, x2, y2] , where (x1, y1) are the coordinates of the bottom-left corner, and (x2, y2) are the coordinates of the top-right corner of the ith rectangle.

    +

    We are given a list of (axis-aligned) rectangles. Each rectangle[i] = [xi1, yi1, xi2, yi2] , where (xi1, yi1) are the coordinates of the bottom-left corner, and (xi2, yi2) are the coordinates of the top-right corner of the ith rectangle.

    -

    Find the total area covered by all rectangles in the plane.  Since the answer may be too large, return it modulo 10^9 + 7.

    - -

    +

    Find the total area covered by all rectangles in the plane. Since the answer may be too large, return it modulo 109 + 7.

    +

     

    Example 1:

    - +
    -Input: [[0,0,2,2],[1,0,2,3],[1,0,3,1]]
    -Output: 6
    +Input: rectangles = [[0,0,2,2],[1,0,2,3],[1,0,3,1]]
    +Output: 6
     Explanation: As illustrated in the picture.
     

    Example 2:

    -Input: [[0,0,1000000000,1000000000]]
    -Output: 49
    -Explanation: The answer is 10^18 modulo (10^9 + 7), which is (10^9)^2 = (-7)^2 = 49.
    +Input: rectangles = [[0,0,1000000000,1000000000]]
    +Output: 49
    +Explanation: The answer is 1018 modulo (109 + 7), which is (109)2 = (-7)2 = 49.
     
    -

    Note:

    +

     

    +

    Constraints:

    ### Related Topics diff --git a/problems/redundant-connection-ii/README.md b/problems/redundant-connection-ii/README.md index 32c267e4d..493b91de8 100644 --- a/problems/redundant-connection-ii/README.md +++ b/problems/redundant-connection-ii/README.md @@ -11,40 +11,38 @@ ## [685. Redundant Connection II (Hard)](https://leetcode.com/problems/redundant-connection-ii "冗余连接 II") -

    -In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) for which all other nodes are descendants of this node, plus every node has exactly one parent, except for the root node which has no parents. -

    -The given input is a directed graph that started as a rooted tree with N nodes (with distinct values 1, 2, ..., N), with one additional directed edge added. The added edge has two different vertices chosen from 1 to N, and was not an edge that already existed. -

    -The resulting graph is given as a 2D-array of edges. Each element of edges is a pair [u, v] that represents a directed edge connecting nodes u and v, where u is a parent of child v. -

    -Return an edge that can be removed so that the resulting graph is a rooted tree of N nodes. If there are multiple answers, return the answer that occurs last in the given 2D-array. -

    Example 1:
    +

    In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) for which all other nodes are descendants of this node, plus every node has exactly one parent, except for the root node which has no parents.

    + +

    The given input is a directed graph that started as a rooted tree with n nodes (with distinct values from 1 to n), with one additional directed edge added. The added edge has two different vertices chosen from 1 to n, and was not an edge that already existed.

    + +

    The resulting graph is given as a 2D-array of edges. Each element of edges is a pair [ui, vi] that represents a directed edge connecting nodes ui and vi, where ui is a parent of child vi.

    + +

    Return an edge that can be removed so that the resulting graph is a rooted tree of n nodes. If there are multiple answers, return the answer that occurs last in the given 2D-array.

    + +

     

    +

    Example 1:

    +
    -Input: [[1,2], [1,3], [2,3]]
    -Output: [2,3]
    -Explanation: The given directed graph will be like this:
    -  1
    - / \
    -v   v
    -2-->3
    +Input: edges = [[1,2],[1,3],[2,3]]
    +Output: [2,3]
     
    -

    -

    Example 2:
    + +

    Example 2:

    +
    -Input: [[1,2], [2,3], [3,4], [4,1], [1,5]]
    -Output: [4,1]
    -Explanation: The given directed graph will be like this:
    -5 <- 1 -> 2
    -     ^    |
    -     |    v
    -     4 <- 3
    +Input: edges = [[1,2],[2,3],[3,4],[4,1],[1,5]]
    +Output: [4,1]
     
    -

    -

    Note:
    -

  • The size of the input 2D-array will be between 3 and 1000.
  • -
  • Every integer represented in the 2D-array will be between 1 and N, where N is the size of the input array.
  • -

    + +

     

    +

    Constraints:

    + + ### Related Topics [[Tree](../../tag/tree/README.md)] diff --git a/problems/reformat-phone-number/README.md b/problems/reformat-phone-number/README.md new file mode 100644 index 000000000..3efa683ad --- /dev/null +++ b/problems/reformat-phone-number/README.md @@ -0,0 +1,98 @@ + + + + + + + +[< Previous](../daily-leads-and-partners "Daily Leads and Partners") +                 +[Next >](../maximum-erasure-value "Maximum Erasure Value") + +## [1694. Reformat Phone Number (Easy)](https://leetcode.com/problems/reformat-phone-number "重新格式化电话号码") + +

    You are given a phone number as a string number. number consists of digits, spaces ' ', and/or dashes '-'.

    + +

    You would like to reformat the phone number in a certain manner. Firstly, remove all spaces and dashes. Then, group the digits from left to right into blocks of length 3 until there are 4 or fewer digits. The final digits are then grouped as follows:

    + + + +

    The blocks are then joined by dashes. Notice that the reformatting process should never produce any blocks of length 1 and produce at most two blocks of length 2.

    + +

    Return the phone number after formatting.

    + +

     

    +

    Example 1:

    + +
    +Input: number = "1-23-45 6"
    +Output: "123-456"
    +Explanation: The digits are "123456".
    +Step 1: There are more than 4 digits, so group the next 3 digits. The 1st block is "123".
    +Step 2: There are 3 digits remaining, so put them in a single block of length 3. The 2nd block is "456".
    +Joining the blocks gives "123-456".
    +
    + +

    Example 2:

    + +
    +Input: number = "123 4-567"
    +Output: "123-45-67"
    +Explanation: The digits are "1234567".
    +Step 1: There are more than 4 digits, so group the next 3 digits. The 1st block is "123".
    +Step 2: There are 4 digits left, so split them into two blocks of length 2. The blocks are "45" and "67".
    +Joining the blocks gives "123-45-67".
    +
    + +

    Example 3:

    + +
    +Input: number = "123 4-5678"
    +Output: "123-456-78"
    +Explanation: The digits are "12345678".
    +Step 1: The 1st block is "123".
    +Step 2: The 2nd block is "456".
    +Step 3: There are 2 digits left, so put them in a single block of length 2. The 3rd block is "78".
    +Joining the blocks gives "123-456-78".
    +
    + +

    Example 4:

    + +
    +Input: number = "12"
    +Output: "12"
    +
    + +

    Example 5:

    + +
    +Input: number = "--17-5 229 35-39475 "
    +Output: "175-229-353-94-75"
    +
    + +

     

    +

    Constraints:

    + + + +### Related Topics + [[String](../../tag/string/README.md)] + +### Hints +
    +Hint 1 +Discard all the spaces and dashes. +
    + +
    +Hint 2 +Use a while loop. While the string still has digits, check its length and see which rule to apply. +
    diff --git a/problems/remove-duplicates-from-sorted-array-ii/README.md b/problems/remove-duplicates-from-sorted-array-ii/README.md index 0643564e0..a6766e6c0 100644 --- a/problems/remove-duplicates-from-sorted-array-ii/README.md +++ b/problems/remove-duplicates-from-sorted-array-ii/README.md @@ -55,7 +55,7 @@ for (int i = 0; i < len; i++) {

    Constraints:

    diff --git a/problems/remove-element/README.md b/problems/remove-element/README.md index 06c8726eb..f9d2427bd 100644 --- a/problems/remove-element/README.md +++ b/problems/remove-element/README.md @@ -11,9 +11,9 @@ ## [27. Remove Element (Easy)](https://leetcode.com/problems/remove-element "移除元素") -

    Given an array nums and a value val, remove all instances of that value in-place and return the new length.

    +

    Given an array nums and a value val, remove all instances of that value in-place and return the new length.

    -

    Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

    +

    Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

    The order of elements can be changed. It doesn't matter what you leave beyond the new length.

    @@ -42,7 +42,7 @@ for (int i = 0; i < len; i++) { Input: nums = [3,2,2,3], val = 3 Output: 2, nums = [2,2] Explanation: Your function should return length = 2, with the first two elements of nums being 2. -It doesn't matter what you leave beyond the returned length. For example if you return 2 with nums = [2,2,3,3] or nums = [2,3,0,0], your answer will be accepted. +It doesn't matter what you leave beyond the returned length. For example if you return 2 with nums = [2,2,3,3] or nums = [2,2,0,0], your answer will be accepted.

    Example 2:

    diff --git a/problems/rising-temperature/README.md b/problems/rising-temperature/README.md index 6eec734af..87eccc276 100644 --- a/problems/rising-temperature/README.md +++ b/problems/rising-temperature/README.md @@ -52,5 +52,5 @@ Result table: | 4 | +----+ In 2015-01-02, temperature was higher than the previous day (10 -> 25). -In 2015-01-04, temperature was higher than the previous day (30 -> 20). +In 2015-01-04, temperature was higher than the previous day (20 -> 30). diff --git a/problems/same-tree/README.md b/problems/same-tree/README.md index 162173b56..63caeb36f 100644 --- a/problems/same-tree/README.md +++ b/problems/same-tree/README.md @@ -11,46 +11,40 @@ ## [100. Same Tree (Easy)](https://leetcode.com/problems/same-tree "相同的树") -

    Given two binary trees, write a function to check if they are the same or not.

    +

    Given the roots of two binary trees p and q, write a function to check if they are the same or not.

    -

    Two binary trees are considered the same if they are structurally identical and the nodes have the same value.

    +

    Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.

    +

     

    Example 1:

    - +
    -Input:     1         1
    -          / \       / \
    -         2   3     2   3
    -
    -        [1,2,3],   [1,2,3]
    -
    +Input: p = [1,2,3], q = [1,2,3]
     Output: true
     

    Example 2:

    - +
    -Input:     1         1
    -          /           \
    -         2             2
    -
    -        [1,2],     [1,null,2]
    -
    +Input: p = [1,2], q = [1,null,2]
     Output: false
     

    Example 3:

    - +
    -Input:     1         1
    -          / \       / \
    -         2   1     1   2
    -
    -        [1,2,1],   [1,1,2]
    -
    +Input: p = [1,2,1], q = [1,1,2]
     Output: false
     
    +

     

    +

    Constraints:

    + + + ### Related Topics [[Tree](../../tag/tree/README.md)] [[Depth-first Search](../../tag/depth-first-search/README.md)] diff --git a/problems/search-a-2d-matrix/README.md b/problems/search-a-2d-matrix/README.md index 70176897f..a17dcd2f6 100644 --- a/problems/search-a-2d-matrix/README.md +++ b/problems/search-a-2d-matrix/README.md @@ -22,21 +22,14 @@

    Example 1:

    -Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,50]], target = 3
    +Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
     Output: true
     

    Example 2:

    -Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,50]], target = 13
    -Output: false
    -
    - -

    Example 3:

    - -
    -Input: matrix = [], target = 0
    +Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
     Output: false
     
    @@ -46,7 +39,7 @@ diff --git a/problems/short-encoding-of-words/README.md b/problems/short-encoding-of-words/README.md index c18dc8979..c45811240 100644 --- a/problems/short-encoding-of-words/README.md +++ b/problems/short-encoding-of-words/README.md @@ -11,13 +11,15 @@ ## [820. Short Encoding of Words (Medium)](https://leetcode.com/problems/short-encoding-of-words "单词的压缩编码") -

    Given a list of words, we may encode it by writing a reference string s and a list of indexes a.

    +

    A valid encoding of an array of words is any reference string s and array of indices indices such that:

    -

    For example, if the list of words is ["time", "me", "bell"], we can write it as s = "time#bell#" and indexes = [0, 2, 5].

    - -

    Then for each index, we will recover the word by reading from the reference string from that index until we reach a "#" character.

    + -

    Return the length of the shortest reference string s possible that encodes the given words.

    +

    Given an array of words, return the length of the shortest reference string s possible of any valid encoding of words.

     

    Example 1:

    @@ -25,7 +27,10 @@
     Input: words = ["time", "me", "bell"]
     Output: 10
    -Explanation: s = "time#bell#" and indexes = [0, 2, 5].
    +Explanation: A valid encoding would be s = "time#bell#" and indices = [0, 2, 5].
    +words[0] = "time", the substring of s starting from indices[0] = 0 to the next '#' is underlined in "time#bell#"
    +words[1] = "me", the substring of s starting from indices[1] = 2 to the next '#' is underlined in "time#bell#"
    +words[2] = "bell", the substring of s starting from indices[2] = 5 to the next '#' is underlined in "time#bell#"
     

    Example 2:

    @@ -33,6 +38,8 @@
     Input: words = ["t"]
     Output: 2
    +Explanation: A valid encoding would be s = "t#" and indices = [0].
    +
     

     

    diff --git a/problems/single-number-iii/README.md b/problems/single-number-iii/README.md index 1052f6b57..8ff056aa4 100644 --- a/problems/single-number-iii/README.md +++ b/problems/single-number-iii/README.md @@ -42,8 +42,9 @@

    Constraints:

    ### Related Topics diff --git a/problems/sort-the-matrix-diagonally/README.md b/problems/sort-the-matrix-diagonally/README.md index d99b12887..31bb6d3b4 100644 --- a/problems/sort-the-matrix-diagonally/README.md +++ b/problems/sort-the-matrix-diagonally/README.md @@ -11,7 +11,9 @@ ## [1329. Sort the Matrix Diagonally (Medium)](https://leetcode.com/problems/sort-the-matrix-diagonally "将矩阵按对角线排序") -

    Given a m * n matrix mat of integers, sort it diagonally in ascending order from the top-left to the bottom-right then return the sorted array.

    +

    A matrix diagonal is a diagonal line of cells starting from some cell in either the topmost row or leftmost column and going in the bottom-right direction until reaching the matrix's end. For example, the matrix diagonal starting from mat[2][0], where mat is a 6 x 3 matrix, includes cells mat[2][0], mat[3][1], and mat[4][2].

    + +

    Given an m x n matrix mat of integers, sort each matrix diagonal in ascending order and return the resulting matrix.

     

    Example 1:

    diff --git a/problems/stone-game-vi/README.md b/problems/stone-game-vi/README.md index ee65e1a73..62f6a7854 100644 --- a/problems/stone-game-vi/README.md +++ b/problems/stone-game-vi/README.md @@ -17,7 +17,7 @@

    You are given two integer arrays of length n, aliceValues and bobValues. Each aliceValues[i] and bobValues[i] represents how Alice and Bob, respectively, value the ith stone.

    -

    The winner is the person with the most points after all the stones are chosen. If both players have the same amount of points, the game results in a draw. Both players will play optimally.

    +

    The winner is the person with the most points after all the stones are chosen. If both players have the same amount of points, the game results in a draw. Both players will play optimally. Both players know the other's values.

    Determine the result of the game, and:

    diff --git a/problems/string-without-aaa-or-bbb/README.md b/problems/string-without-aaa-or-bbb/README.md index a252e92b6..6be32faa8 100644 --- a/problems/string-without-aaa-or-bbb/README.md +++ b/problems/string-without-aaa-or-bbb/README.md @@ -11,41 +11,37 @@ ## [984. String Without AAA or BBB (Medium)](https://leetcode.com/problems/string-without-aaa-or-bbb "不含 AAA 或 BBB 的字符串") -

    Given two integers A and B, return any string S such that:

    +

    Given two integers a and b, return any string s such that:

     

    -

    Example 1:

    -Input: A = 1, B = 2
    -Output: "abb"
    -Explanation: "abb", "bab" and "bba" are all correct answers.
    +Input: a = 1, b = 2
    +Output: "abb"
    +Explanation: "abb", "bab" and "bba" are all correct answers.
     
    -

    Example 2:

    -Input: A = 4, B = 1
    -Output: "aabaa"
    +Input: a = 4, b = 1 +Output: "aabaa" +

     

    -
    - -

    Note:

    +

    Constraints:

    -
      -
    1. 0 <= A <= 100
    2. -
    3. 0 <= B <= 100
    4. -
    5. It is guaranteed such an S exists for the given A and B.
    6. -
    + ### Related Topics [[Greedy](../../tag/greedy/README.md)] diff --git a/problems/subarrays-with-k-different-integers/README.md b/problems/subarrays-with-k-different-integers/README.md index 0ca5646b9..baa2312b3 100644 --- a/problems/subarrays-with-k-different-integers/README.md +++ b/problems/subarrays-with-k-different-integers/README.md @@ -53,4 +53,4 @@ ### Similar Questions 1. [Longest Substring Without Repeating Characters](../longest-substring-without-repeating-characters) (Medium) 1. [Longest Substring with At Most Two Distinct Characters](../longest-substring-with-at-most-two-distinct-characters) (Medium) - 1. [Longest Substring with At Most K Distinct Characters](../longest-substring-with-at-most-k-distinct-characters) (Hard) + 1. [Longest Substring with At Most K Distinct Characters](../longest-substring-with-at-most-k-distinct-characters) (Medium) diff --git a/problems/swap-nodes-in-pairs/README.md b/problems/swap-nodes-in-pairs/README.md index aa9588786..a16086aa5 100644 --- a/problems/swap-nodes-in-pairs/README.md +++ b/problems/swap-nodes-in-pairs/README.md @@ -46,6 +46,7 @@ ### Related Topics + [[Recursion](../../tag/recursion/README.md)] [[Linked List](../../tag/linked-list/README.md)] ### Similar Questions diff --git a/problems/triangle/README.md b/problems/triangle/README.md index eaf8a2655..d7612fbb3 100644 --- a/problems/triangle/README.md +++ b/problems/triangle/README.md @@ -11,24 +11,38 @@ ## [120. Triangle (Medium)](https://leetcode.com/problems/triangle "三角形最小路径和") -

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.

    +

    Given a triangle array, return the minimum path sum from top to bottom.

    -

    For example, given the following triangle

    +

    For each step, you may move to an adjacent number on the row below.

    + +

     

    +

    Example 1:

    + +
    +Input: triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
    +Output: 11
    +Explanation: The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).
    +
    + +

    Example 2:

    -[
    -     [2],
    -    [3,4],
    -   [6,5,7],
    -  [4,1,8,3]
    -]
    +Input: triangle = [[-10]]
    +Output: -10
     
    -

    The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).

    +

     

    +

    Constraints:

    -

    Note:

    + -

    Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.

    +

     

    +Follow up: Could you do this using only O(n) extra space, where n is the total number of rows in the triangle? ### Related Topics [[Array](../../tag/array/README.md)] diff --git a/problems/valid-mountain-array/README.md b/problems/valid-mountain-array/README.md index c194f6c86..ce394ccd2 100644 --- a/problems/valid-mountain-array/README.md +++ b/problems/valid-mountain-array/README.md @@ -11,15 +11,15 @@ ## [941. Valid Mountain Array (Easy)](https://leetcode.com/problems/valid-mountain-array "有效的山脉数组") -

    Given an array of integers arr, return true if and only if it is a valid mountain array.

    +

    Given an array of integers arr, return true if and only if it is a valid mountain array.

    Recall that arr is a mountain array if and only if: