From fdac83e3250cf9effda6f7e8252a6c9c44d965bd Mon Sep 17 00:00:00 2001 From: Shuo Date: Mon, 11 May 2020 13:00:30 +0800 Subject: [PATCH 1/2] A: new --- README.md | 5 ++ .../README.md | 81 +++++++++++++++++ .../README.md | 88 +++++++++++++++++++ .../evaluate-boolean-expression/README.md | 14 +++ .../mysql_schemas.sql | 12 +++ problems/find-k-closest-elements/README.md | 50 ++++------- .../README.md | 2 +- .../README.md | 10 +++ .../median-of-two-sorted-arrays/README.md | 2 +- .../README.md | 71 +++++++++++++++ problems/number-complement/README.md | 31 +++---- .../README.md | 78 ++++++++++++++++ problems/ransom-note/README.md | 36 ++++---- problems/subarray-sum-equals-k/README.md | 19 ++-- problems/subtree-of-another-tree/README.md | 20 +++-- readme/1-300.md | 2 +- tag/array/README.md | 3 +- tag/binary-search/README.md | 2 +- tag/bit-manipulation/README.md | 1 + tag/depth-first-search/README.md | 1 + tag/divide-and-conquer/README.md | 2 +- tag/dynamic-programming/README.md | 1 + tag/math/README.md | 1 + tag/stack/README.md | 1 + tag/tree/README.md | 1 + 25 files changed, 444 insertions(+), 90 deletions(-) create mode 100644 problems/build-an-array-with-stack-operations/README.md create mode 100644 problems/count-triplets-that-can-form-two-arrays-of-equal-xor/README.md create mode 100644 problems/evaluate-boolean-expression/README.md create mode 100644 problems/evaluate-boolean-expression/mysql_schemas.sql create mode 100644 problems/minimum-time-to-collect-all-apples-in-a-tree/README.md create mode 100644 problems/number-of-ways-of-cutting-a-pizza/README.md diff --git a/README.md b/README.md index caac8d57b..6a142e8f8 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,11 @@ LeetCode Problems' Solutions | # | Title | Solution | Difficulty | | :-: | - | - | :-: | +| 1444 | [Number of Ways of Cutting a Pizza](https://leetcode.com/problems/number-of-ways-of-cutting-a-pizza "切披萨的方案数") | [Go](problems/number-of-ways-of-cutting-a-pizza) | Hard | +| 1443 | [Minimum Time to Collect All Apples in a Tree](https://leetcode.com/problems/minimum-time-to-collect-all-apples-in-a-tree "收集树上所有苹果的最少时间") | [Go](problems/minimum-time-to-collect-all-apples-in-a-tree) | Medium | +| 1442 | [Count Triplets That Can Form Two Arrays of Equal XOR](https://leetcode.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor "形成两个异或相等数组的三元组数目") | [Go](problems/count-triplets-that-can-form-two-arrays-of-equal-xor) | Medium | +| 1441 | [Build an Array With Stack Operations](https://leetcode.com/problems/build-an-array-with-stack-operations "用栈操作构建数组") | [Go](problems/build-an-array-with-stack-operations) | Easy | +| 1440 | [Evaluate Boolean Expression](https://leetcode.com/problems/evaluate-boolean-expression) 🔒 | [MySQL](problems/evaluate-boolean-expression) | Medium | | 1439 | [Find the Kth Smallest Sum of a Matrix With Sorted Rows](https://leetcode.com/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows "有序矩阵中的第 k 个最小数组和") | [Go](problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows) | Hard | | 1438 | [Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit](https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit "绝对差不超过限制的最长连续子数组") | [Go](problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit) | Medium | | 1437 | [Check If All 1's Are at Least Length K Places Away](https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away "是否所有 1 都至少相隔 k 个元素") | [Go](problems/check-if-all-1s-are-at-least-length-k-places-away) | Medium | diff --git a/problems/build-an-array-with-stack-operations/README.md b/problems/build-an-array-with-stack-operations/README.md new file mode 100644 index 000000000..0f11e7d05 --- /dev/null +++ b/problems/build-an-array-with-stack-operations/README.md @@ -0,0 +1,81 @@ + + + + + + + +[< Previous](../evaluate-boolean-expression "Evaluate Boolean Expression") +                 +[Next >](../count-triplets-that-can-form-two-arrays-of-equal-xor "Count Triplets That Can Form Two Arrays of Equal XOR") + +## [1441. Build an Array With Stack Operations (Easy)](https://leetcode.com/problems/build-an-array-with-stack-operations "用栈操作构建数组") + +

Given an array target and an integer n. In each iteration, you will read a number from  list = {1,2,3..., n}.

+ +

Build the target array using the following operations:

+ + + +

You are guaranteed that the target array is strictly increasing, only containing numbers between 1 to n inclusive.

+ +

Return the operations to build the target array.

+ +

You are guaranteed that the answer is unique.

+ +

 

+

Example 1:

+ +
+Input: target = [1,3], n = 3
+Output: ["Push","Push","Pop","Push"]
+Explanation: 
+Read number 1 and automatically push in the array -> [1]
+Read number 2 and automatically push in the array then Pop it -> [1]
+Read number 3 and automatically push in the array -> [1,3]
+
+ +

Example 2:

+ +
+Input: target = [1,2,3], n = 3
+Output: ["Push","Push","Push"]
+
+ +

Example 3:

+ +
+Input: target = [1,2], n = 4
+Output: ["Push","Push"]
+Explanation: You only need to read the first 2 numbers and stop.
+
+ +

Example 4:

+ +
+Input: target = [2,3,4], n = 4
+Output: ["Push","Pop","Push","Push","Push"]
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Stack](../../tag/stack/README.md)] + +### Hints +
+Hint 1 +Use “Push” for numbers to be kept in target array and [“Push”, “Pop”] for numbers to be discarded. +
diff --git a/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/README.md b/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/README.md new file mode 100644 index 000000000..f73d02316 --- /dev/null +++ b/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/README.md @@ -0,0 +1,88 @@ + + + + + + + +[< Previous](../build-an-array-with-stack-operations "Build an Array With Stack Operations") +                 +[Next >](../minimum-time-to-collect-all-apples-in-a-tree "Minimum Time to Collect All Apples in a Tree") + +## [1442. Count Triplets That Can Form Two Arrays of Equal XOR (Medium)](https://leetcode.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor "形成两个异或相等数组的三元组数目") + +

Given an array of integers arr.

+ +

We want to select three indices i, j and k where (0 <= i < j <= k < arr.length).

+ +

Let's define a and b as follows:

+ + + +

Note that ^ denotes the bitwise-xor operation.

+ +

Return the number of triplets (i, j and k) Where a == b.

+ +

 

+

Example 1:

+ +
+Input: arr = [2,3,1,6,7]
+Output: 4
+Explanation: The triplets are (0,1,2), (0,2,2), (2,3,4) and (2,4,4)
+
+ +

Example 2:

+ +
+Input: arr = [1,1,1,1,1]
+Output: 10
+
+ +

Example 3:

+ +
+Input: arr = [2,3]
+Output: 0
+
+ +

Example 4:

+ +
+Input: arr = [1,3,5,7,9]
+Output: 3
+
+ +

Example 5:

+ +
+Input: arr = [7,11,12,9,5,2,7,17,22]
+Output: 8
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Bit Manipulation](../../tag/bit-manipulation/README.md)] + [[Array](../../tag/array/README.md)] + [[Math](../../tag/math/README.md)] + +### Hints +
+Hint 1 +We are searching for sub-array of length ≥ 2 and we need to split it to 2 non-empty arrays so that the xor of the first array is equal to the xor of the second array. This is equivalent to searching for sub-array with xor = 0. +
+ +
+Hint 2 +Keep the prefix xor of arr in another array, check the xor of all sub-arrays in O(n^2), if the xor of sub-array of length x is 0 add x-1 to the answer. +
diff --git a/problems/evaluate-boolean-expression/README.md b/problems/evaluate-boolean-expression/README.md new file mode 100644 index 000000000..b3ccf69e9 --- /dev/null +++ b/problems/evaluate-boolean-expression/README.md @@ -0,0 +1,14 @@ + + + + + + + +[< Previous](../find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows "Find the Kth Smallest Sum of a Matrix With Sorted Rows") +                 +[Next >](../build-an-array-with-stack-operations "Build an Array With Stack Operations") + +## [1440. Evaluate Boolean Expression (Medium)](https://leetcode.com/problems/evaluate-boolean-expression "") + + diff --git a/problems/evaluate-boolean-expression/mysql_schemas.sql b/problems/evaluate-boolean-expression/mysql_schemas.sql new file mode 100644 index 000000000..1b0c6f56c --- /dev/null +++ b/problems/evaluate-boolean-expression/mysql_schemas.sql @@ -0,0 +1,12 @@ +Create Table If Not Exists Variables (name varchar(3), value int); +Create Table If Not Exists Expressions (left_operand varchar(3), operator ENUM('>', '<', '='), right_operand varchar(3)); +Truncate table Variables; +insert into Variables (name, value) values ('x', '66'); +insert into Variables (name, value) values ('y', '77'); +Truncate table Expressions; +insert into Expressions (left_operand, operator, right_operand) values ('x', '>', 'y'); +insert into Expressions (left_operand, operator, right_operand) values ('x', '<', 'y'); +insert into Expressions (left_operand, operator, right_operand) values ('x', '=', 'y'); +insert into Expressions (left_operand, operator, right_operand) values ('y', '>', 'x'); +insert into Expressions (left_operand, operator, right_operand) values ('y', '<', 'x'); +insert into Expressions (left_operand, operator, right_operand) values ('x', '=', 'x'); diff --git a/problems/find-k-closest-elements/README.md b/problems/find-k-closest-elements/README.md index 0849ebd14..eadcf4ad5 100644 --- a/problems/find-k-closest-elements/README.md +++ b/problems/find-k-closest-elements/README.md @@ -11,40 +11,24 @@ ## [658. Find K Closest Elements (Medium)](https://leetcode.com/problems/find-k-closest-elements "找到 K 个最接近的元素") -

-Given a sorted array, two integers k and x, find the k closest elements to x in the array. The result should also be sorted in ascending order. -If there is a tie, the smaller elements are always preferred. -

- -

Example 1:
-

-Input: [1,2,3,4,5], k=4, x=3
-Output: [1,2,3,4]
-
-

- - -

Example 2:
-

-Input: [1,2,3,4,5], k=4, x=-1
-Output: [1,2,3,4]
+

Given a sorted array arr, two integers k and x, find the k closest elements to x in the array. The result should also be sorted in ascending order. If there is a tie, the smaller elements are always preferred.

+ +

 

+

Example 1:

+
Input: arr = [1,2,3,4,5], k = 4, x = 3
+Output: [1,2,3,4]
+

Example 2:

+
Input: arr = [1,2,3,4,5], k = 4, x = -1
+Output: [1,2,3,4]
 
-

- -

Note:
-

    -
  1. The value k is positive and will always be smaller than the length of the sorted array.
  2. -
  3. Length of the given array is positive and will not exceed 104
  4. -
  5. Absolute value of elements in the array and x will not exceed 104
  6. -
-

- -
- -

-UPDATE (2017/9/19):
-The arr parameter had been changed to an array of integers (instead of a list of integers). Please reload the code definition to get the latest changes. -

+

 

+

Constraints:

+ + ### Related Topics [[Binary Search](../../tag/binary-search/README.md)] diff --git a/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows/README.md b/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows/README.md index a821a5a06..0b694d57e 100644 --- a/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows/README.md +++ b/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows/README.md @@ -7,7 +7,7 @@ [< Previous](../longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit "Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit")                  -Next > +[Next >](../evaluate-boolean-expression "Evaluate Boolean Expression") ## [1439. Find the Kth Smallest Sum of a Matrix With Sorted Rows (Hard)](https://leetcode.com/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows "有序矩阵中的第 k 个最小数组和") diff --git a/problems/insert-into-a-binary-search-tree/README.md b/problems/insert-into-a-binary-search-tree/README.md index aadc7c3a0..11807618d 100644 --- a/problems/insert-into-a-binary-search-tree/README.md +++ b/problems/insert-into-a-binary-search-tree/README.md @@ -49,6 +49,16 @@ And the value to insert: 5 4
+

 

+

Constraints:

+ + + ### Related Topics [[Tree](../../tag/tree/README.md)] diff --git a/problems/median-of-two-sorted-arrays/README.md b/problems/median-of-two-sorted-arrays/README.md index b840cf7e6..c49c2fa3c 100644 --- a/problems/median-of-two-sorted-arrays/README.md +++ b/problems/median-of-two-sorted-arrays/README.md @@ -9,7 +9,7 @@                  [Next >](../longest-palindromic-substring "Longest Palindromic Substring") -## [4. Median of Two Sorted Arrays (Hard)](https://leetcode.com/problems/median-of-two-sorted-arrays "寻找两个有序数组的中位数") +## [4. Median of Two Sorted Arrays (Hard)](https://leetcode.com/problems/median-of-two-sorted-arrays "寻找两个正序数组的中位数")

There are two sorted arrays nums1 and nums2 of size m and n respectively.

diff --git a/problems/minimum-time-to-collect-all-apples-in-a-tree/README.md b/problems/minimum-time-to-collect-all-apples-in-a-tree/README.md new file mode 100644 index 000000000..0a303646b --- /dev/null +++ b/problems/minimum-time-to-collect-all-apples-in-a-tree/README.md @@ -0,0 +1,71 @@ + + + + + + + +[< Previous](../count-triplets-that-can-form-two-arrays-of-equal-xor "Count Triplets That Can Form Two Arrays of Equal XOR") +                 +[Next >](../number-of-ways-of-cutting-a-pizza "Number of Ways of Cutting a Pizza") + +## [1443. Minimum Time to Collect All Apples in a Tree (Medium)](https://leetcode.com/problems/minimum-time-to-collect-all-apples-in-a-tree "收集树上所有苹果的最少时间") + +

Given an undirected tree consisting of n vertices numbered from 0 to n-1, which has some apples in their vertices. You spend 1 second to walk over one edge of the tree. Return the minimum time in seconds you have to spend in order to collect all apples in the tree starting at vertex 0 and coming back to this vertex.

+ +

The edges of the undirected tree are given in the array edges, where edges[i] = [fromi, toi] means that exists an edge connecting the vertices fromi and toi. Additionally, there is a boolean array hasApple, where hasApple[i] = true means that vertex i has an apple, otherwise, it does not have any apple.

+ +

 

+

Example 1:

+ +

+ +
+Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,true,false,true,true,false]
+Output: 8 
+Explanation: The figure above represents the given tree where red vertices have an apple. One optimal path to collect all apples is shown by the green arrows.  
+
+ +

Example 2:

+ +

+ +
+Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,true,false,false,true,false]
+Output: 6
+Explanation: The figure above represents the given tree where red vertices have an apple. One optimal path to collect all apples is shown by the green arrows.  
+
+ +

Example 3:

+ +
+Input: n = 7, edges = [[0,1],[0,2],[1,4],[1,5],[2,3],[2,6]], hasApple = [false,false,false,false,false,false,false]
+Output: 0
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Tree](../../tag/tree/README.md)] + [[Depth-first Search](../../tag/depth-first-search/README.md)] + +### Hints +
+Hint 1 +Note that if a node u contains an apple then all edges in the path from the root to the node u have to be used forward and backward (2 times). +
+ +
+Hint 2 +Therefore use a depth-first search (DFS) to check if an edge will be used or not. +
diff --git a/problems/number-complement/README.md b/problems/number-complement/README.md index 522c757ad..10d005f24 100644 --- a/problems/number-complement/README.md +++ b/problems/number-complement/README.md @@ -11,37 +11,34 @@ ## [476. Number Complement (Easy)](https://leetcode.com/problems/number-complement "数字的补数") -

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

+

Given a positive integer num, output its complement number. The complement strategy is to flip the bits of its binary representation.

 

- -

Example 1:

+

Example 1:

-Input: 5
-Output: 2
-Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
+Input: num = 5
+Output: 2
+Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
 
-

 

- -

Example 2:

+

Example 2:

-Input: 1
-Output: 0
-Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
+Input: num = 1
+Output: 0
+Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
 

 

+

Constraints:

-

Note:

- -
    -
  1. The given integer is guaranteed to fit within the range of a 32-bit signed integer.
  2. +
+ ### Related Topics [[Bit Manipulation](../../tag/bit-manipulation/README.md)] diff --git a/problems/number-of-ways-of-cutting-a-pizza/README.md b/problems/number-of-ways-of-cutting-a-pizza/README.md new file mode 100644 index 000000000..cb88c5e62 --- /dev/null +++ b/problems/number-of-ways-of-cutting-a-pizza/README.md @@ -0,0 +1,78 @@ + + + + + + + +[< Previous](../minimum-time-to-collect-all-apples-in-a-tree "Minimum Time to Collect All Apples in a Tree") +                 +Next > + +## [1444. Number of Ways of Cutting a Pizza (Hard)](https://leetcode.com/problems/number-of-ways-of-cutting-a-pizza "切披萨的方案数") + +

Given a rectangular pizza represented as a rows x cols matrix containing the following characters: 'A' (an apple) and '.' (empty cell) and given the integer k. You have to cut the pizza into k pieces using k-1 cuts. 

+ +

For each cut you choose the direction: vertical or horizontal, then you choose a cut position at the cell boundary and cut the pizza into two pieces. If you cut the pizza vertically, give the left part of the pizza to a person. If you cut the pizza horizontally, give the upper part of the pizza to a person. Give the last piece of pizza to the last person.

+ +

Return the number of ways of cutting the pizza such that each piece contains at least one apple. Since the answer can be a huge number, return this modulo 10^9 + 7.

+ +

 

+

Example 1:

+ +

+ +
+Input: pizza = ["A..","AAA","..."], k = 3
+Output: 3 
+Explanation: The figure above shows the three ways to cut the pizza. Note that pieces must contain at least one apple.
+
+ +

Example 2:

+ +
+Input: pizza = ["A..","AA.","..."], k = 3
+Output: 1
+
+ +

Example 3:

+ +
+Input: pizza = ["A..","A..","..."], k = 1
+Output: 1
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Dynamic Programming](../../tag/dynamic-programming/README.md)] + +### Hints +
+Hint 1 +Note that after each cut the remaining piece of pizza always has the lower right coordinate at (rows-1,cols-1). +
+ +
+Hint 2 +Use dynamic programming approach with states (row1, col1, c) which computes the number of ways of cutting the pizza using "c" cuts where the current piece of pizza has upper left coordinate at (row1,col1) and lower right coordinate at (rows-1,cols-1). +
+ +
+Hint 3 +For the transitions try all vertical and horizontal cuts such that the piece of pizza you have to give a person must contain at least one apple. The base case is when c=k-1. +
+ +
+Hint 4 +Additionally use a 2D dynamic programming to respond in O(1) if a piece of pizza contains at least one apple. +
diff --git a/problems/ransom-note/README.md b/problems/ransom-note/README.md index 4b4f4adc0..d6a177735 100644 --- a/problems/ransom-note/README.md +++ b/problems/ransom-note/README.md @@ -11,23 +11,27 @@ ## [383. Ransom Note (Easy)](https://leetcode.com/problems/ransom-note "赎金信") -

-Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom -note can be constructed from the magazines ; otherwise, it will return false. -

-

-Each letter in the magazine string can only be used once in your ransom note. -

- -

Note:
-You may assume that both strings contain only lowercase letters. -

- -
-canConstruct("a", "b") -> false
-canConstruct("aa", "ab") -> false
-canConstruct("aa", "aab") -> true
+

Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.

+ +

Each letter in the magazine string can only be used once in your ransom note.

+ +

 

+

Example 1:

+
Input: ransomNote = "a", magazine = "b"
+Output: false
+

Example 2:

+
Input: ransomNote = "aa", magazine = "ab"
+Output: false
+

Example 3:

+
Input: ransomNote = "aa", magazine = "aab"
+Output: true
 
+

 

+

Constraints:

+ + ### Related Topics [[String](../../tag/string/README.md)] diff --git a/problems/subarray-sum-equals-k/README.md b/problems/subarray-sum-equals-k/README.md index ca30d116a..40472369f 100644 --- a/problems/subarray-sum-equals-k/README.md +++ b/problems/subarray-sum-equals-k/README.md @@ -13,19 +13,20 @@

Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.

-

Example 1:
+

Example 1:

+
 Input:nums = [1,1,1], k = 2
 Output: 2
 
-

- -

Note:
-

    -
  1. The length of the array is in range [1, 20,000].
  2. -
  3. The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7].
  4. -
-

+ +

 

+

Constraints:

+ + ### Related Topics [[Array](../../tag/array/README.md)] diff --git a/problems/subtree-of-another-tree/README.md b/problems/subtree-of-another-tree/README.md index 6135f27a0..de3ec8219 100644 --- a/problems/subtree-of-another-tree/README.md +++ b/problems/subtree-of-another-tree/README.md @@ -11,13 +11,11 @@ ## [572. Subtree of Another Tree (Easy)](https://leetcode.com/problems/subtree-of-another-tree "另一个树的子树") -

-Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node's descendants. The tree s could also be considered as a subtree of itself. -

+

Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node's descendants. The tree s could also be considered as a subtree of itself.

-

Example 1:
+

Example 1:
+Given tree s:

-Given tree s:
      3
     / \
@@ -26,17 +24,19 @@ Given tree s:
  1   2
 
Given tree t: +
    4 
   / \
  1   2
 
Return true, because t has the same structure and node values with a subtree of s. -

-

Example 2:
+

 

+ +

Example 2:
+Given tree s:

-Given tree s:
      3
     / \
@@ -47,13 +47,15 @@ Given tree s:
    0
 
Given tree t: +
    4
   / \
  1   2
 
Return false. -

+ +

 

### Related Topics [[Tree](../../tag/tree/README.md)] diff --git a/readme/1-300.md b/readme/1-300.md index a9af4d825..afacbba39 100644 --- a/readme/1-300.md +++ b/readme/1-300.md @@ -65,7 +65,7 @@ LeetCode Problems' Solutions | 1 | [Two Sum](https://leetcode.com/problems/two-sum "两数之和") | [Go](../problems/two-sum) | Easy | | 2 | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers "两数相加") | [Go](../problems/add-two-numbers) | Medium | | 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters "无重复字符的最长子串") | [Go](../problems/longest-substring-without-repeating-characters) | Medium | -| 4 | [Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays "寻找两个有序数组的中位数") | [Go](../problems/median-of-two-sorted-arrays) | Hard | +| 4 | [Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays "寻找两个正序数组的中位数") | [Go](../problems/median-of-two-sorted-arrays) | Hard | | 5 | [Longest Palindromic Substring](https://leetcode.com/problems/longest-palindromic-substring "最长回文子串") | [Go](../problems/longest-palindromic-substring) | Medium | | 6 | [ZigZag Conversion](https://leetcode.com/problems/zigzag-conversion "Z 字形变换") | [Go](../problems/zigzag-conversion) | Medium | | 7 | [Reverse Integer](https://leetcode.com/problems/reverse-integer "整数反转") | [Go](../problems/reverse-integer) | Easy | diff --git a/tag/array/README.md b/tag/array/README.md index 78bfe62c8..17127a0a3 100644 --- a/tag/array/README.md +++ b/tag/array/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1442 | [形成两个异或相等数组的三元组数目](../../problems/count-triplets-that-can-form-two-arrays-of-equal-xor) | [[位运算](../bit-manipulation/README.md)] [[数组](../array/README.md)] [[数学](../math/README.md)] | Medium | | 1438 | [绝对差不超过限制的最长连续子数组](../../problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit) | [[数组](../array/README.md)] [[Sliding Window](../sliding-window/README.md)] | Medium | | 1437 | [是否所有 1 都至少相隔 k 个元素](../../problems/check-if-all-1s-are-at-least-length-k-places-away) | [[数组](../array/README.md)] | Medium | | 1431 | [拥有最多糖果的孩子](../../problems/kids-with-the-greatest-number-of-candies) | [[数组](../array/README.md)] | Easy | @@ -254,5 +255,5 @@ | 16 | [最接近的三数之和](../../problems/3sum-closest) | [[数组](../array/README.md)] [[双指针](../two-pointers/README.md)] | Medium | | 15 | [三数之和](../../problems/3sum) | [[数组](../array/README.md)] [[双指针](../two-pointers/README.md)] | Medium | | 11 | [盛最多水的容器](../../problems/container-with-most-water) | [[数组](../array/README.md)] [[双指针](../two-pointers/README.md)] | Medium | -| 4 | [寻找两个有序数组的中位数](../../problems/median-of-two-sorted-arrays) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] [[分治算法](../divide-and-conquer/README.md)] | Hard | +| 4 | [寻找两个正序数组的中位数](../../problems/median-of-two-sorted-arrays) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] [[分治算法](../divide-and-conquer/README.md)] | Hard | | 1 | [两数之和](../../problems/two-sum) | [[数组](../array/README.md)] [[哈希表](../hash-table/README.md)] | Easy | diff --git a/tag/binary-search/README.md b/tag/binary-search/README.md index fd809ab4a..d94a9fe77 100644 --- a/tag/binary-search/README.md +++ b/tag/binary-search/README.md @@ -92,4 +92,4 @@ | 34 | [在排序数组中查找元素的第一个和最后一个位置](../../problems/find-first-and-last-position-of-element-in-sorted-array) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] | Medium | | 33 | [搜索旋转排序数组](../../problems/search-in-rotated-sorted-array) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] | Medium | | 29 | [两数相除](../../problems/divide-two-integers) | [[数学](../math/README.md)] [[二分查找](../binary-search/README.md)] | Medium | -| 4 | [寻找两个有序数组的中位数](../../problems/median-of-two-sorted-arrays) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] [[分治算法](../divide-and-conquer/README.md)] | Hard | +| 4 | [寻找两个正序数组的中位数](../../problems/median-of-two-sorted-arrays) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] [[分治算法](../divide-and-conquer/README.md)] | Hard | diff --git a/tag/bit-manipulation/README.md b/tag/bit-manipulation/README.md index f3b48ab7c..31bd7b3f2 100644 --- a/tag/bit-manipulation/README.md +++ b/tag/bit-manipulation/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1442 | [形成两个异或相等数组的三元组数目](../../problems/count-triplets-that-can-form-two-arrays-of-equal-xor) | [[位运算](../bit-manipulation/README.md)] [[数组](../array/README.md)] [[数学](../math/README.md)] | Medium | | 1434 | [每个人戴不同帽子的方案数](../../problems/number-of-ways-to-wear-different-hats-to-each-other) | [[位运算](../bit-manipulation/README.md)] [[动态规划](../dynamic-programming/README.md)] | Hard | | 1404 | [将二进制表示减到 1 的步骤数](../../problems/number-of-steps-to-reduce-a-number-in-binary-representation-to-one) | [[位运算](../bit-manipulation/README.md)] [[字符串](../string/README.md)] | Medium | | 1356 | [根据数字二进制下 1 的数目排序](../../problems/sort-integers-by-the-number-of-1-bits) | [[排序](../sort/README.md)] [[位运算](../bit-manipulation/README.md)] | Easy | diff --git a/tag/depth-first-search/README.md b/tag/depth-first-search/README.md index c58017106..ff73b6891 100644 --- a/tag/depth-first-search/README.md +++ b/tag/depth-first-search/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1443 | [收集树上所有苹果的最少时间](../../problems/minimum-time-to-collect-all-apples-in-a-tree) | [[树](../tree/README.md)] [[深度优先搜索](../depth-first-search/README.md)] | Medium | | 1391 | [检查网格中是否存在有效路径](../../problems/check-if-there-is-a-valid-path-in-a-grid) | [[深度优先搜索](../depth-first-search/README.md)] [[广度优先搜索](../breadth-first-search/README.md)] | Medium | | 1377 | [T 秒后青蛙的位置](../../problems/frog-position-after-t-seconds) | [[深度优先搜索](../depth-first-search/README.md)] | Hard | | 1376 | [通知所有员工所需的时间](../../problems/time-needed-to-inform-all-employees) | [[深度优先搜索](../depth-first-search/README.md)] | Medium | diff --git a/tag/divide-and-conquer/README.md b/tag/divide-and-conquer/README.md index e761314cd..b0960f7f8 100644 --- a/tag/divide-and-conquer/README.md +++ b/tag/divide-and-conquer/README.md @@ -27,4 +27,4 @@ | 169 | [多数元素](../../problems/majority-element) | [[位运算](../bit-manipulation/README.md)] [[数组](../array/README.md)] [[分治算法](../divide-and-conquer/README.md)] | Easy | | 53 | [最大子序和](../../problems/maximum-subarray) | [[数组](../array/README.md)] [[分治算法](../divide-and-conquer/README.md)] [[动态规划](../dynamic-programming/README.md)] | Easy | | 23 | [合并K个排序链表](../../problems/merge-k-sorted-lists) | [[堆](../heap/README.md)] [[链表](../linked-list/README.md)] [[分治算法](../divide-and-conquer/README.md)] | Hard | -| 4 | [寻找两个有序数组的中位数](../../problems/median-of-two-sorted-arrays) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] [[分治算法](../divide-and-conquer/README.md)] | Hard | +| 4 | [寻找两个正序数组的中位数](../../problems/median-of-two-sorted-arrays) | [[数组](../array/README.md)] [[二分查找](../binary-search/README.md)] [[分治算法](../divide-and-conquer/README.md)] | Hard | diff --git a/tag/dynamic-programming/README.md b/tag/dynamic-programming/README.md index 322c4233d..89963f0ca 100644 --- a/tag/dynamic-programming/README.md +++ b/tag/dynamic-programming/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1444 | [切披萨的方案数](../../problems/number-of-ways-of-cutting-a-pizza) | [[动态规划](../dynamic-programming/README.md)] | Hard | | 1434 | [每个人戴不同帽子的方案数](../../problems/number-of-ways-to-wear-different-hats-to-each-other) | [[位运算](../bit-manipulation/README.md)] [[动态规划](../dynamic-programming/README.md)] | Hard | | 1425 | [带限制的子序列和](../../problems/constrained-subsequence-sum) | [[动态规划](../dynamic-programming/README.md)] | Hard | | 1423 | [可获得的最大点数](../../problems/maximum-points-you-can-obtain-from-cards) | [[数组](../array/README.md)] [[动态规划](../dynamic-programming/README.md)] [[Sliding Window](../sliding-window/README.md)] | Medium | diff --git a/tag/math/README.md b/tag/math/README.md index 9d25bf985..8e62d08dc 100644 --- a/tag/math/README.md +++ b/tag/math/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1442 | [形成两个异或相等数组的三元组数目](../../problems/count-triplets-that-can-form-two-arrays-of-equal-xor) | [[位运算](../bit-manipulation/README.md)] [[数组](../array/README.md)] [[数学](../math/README.md)] | Medium | | 1427 | [Perform String Shifts](../../problems/perform-string-shifts) 🔒 | [[数组](../array/README.md)] [[数学](../math/README.md)] | Easy | | 1390 | [四因数](../../problems/four-divisors) | [[数学](../math/README.md)] | Medium | | 1363 | [形成三的最大倍数](../../problems/largest-multiple-of-three) | [[数学](../math/README.md)] [[动态规划](../dynamic-programming/README.md)] | Hard | diff --git a/tag/stack/README.md b/tag/stack/README.md index d274a91de..bbfe948a8 100644 --- a/tag/stack/README.md +++ b/tag/stack/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1441 | [用栈操作构建数组](../../problems/build-an-array-with-stack-operations) | [[栈](../stack/README.md)] | Easy | | 1410 | [HTML 实体解析器](../../problems/html-entity-parser) | [[栈](../stack/README.md)] [[字符串](../string/README.md)] | Medium | | 1381 | [设计一个支持增量操作的栈](../../problems/design-a-stack-with-increment-operation) | [[栈](../stack/README.md)] [[设计](../design/README.md)] | Medium | | 1249 | [移除无效的括号](../../problems/minimum-remove-to-make-valid-parentheses) | [[栈](../stack/README.md)] [[字符串](../string/README.md)] | Medium | diff --git a/tag/tree/README.md b/tag/tree/README.md index 537be7ad1..ec6576c36 100644 --- a/tag/tree/README.md +++ b/tag/tree/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1443 | [收集树上所有苹果的最少时间](../../problems/minimum-time-to-collect-all-apples-in-a-tree) | [[树](../tree/README.md)] [[深度优先搜索](../depth-first-search/README.md)] | Medium | | 1430 | [Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree](../../problems/check-if-a-string-is-a-valid-sequence-from-root-to-leaves-path-in-a-binary-tree) 🔒 | [[树](../tree/README.md)] | Medium | | 1379 | [找出克隆二叉树中的相同节点](../../problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree) | [[树](../tree/README.md)] | Medium | | 1372 | [二叉树中的最长交错路径](../../problems/longest-zigzag-path-in-a-binary-tree) | [[树](../tree/README.md)] [[动态规划](../dynamic-programming/README.md)] | Medium | From d45a97b0a5ad75056510d53c1c28862489d3b7ab Mon Sep 17 00:00:00 2001 From: Shuo Date: Mon, 11 May 2020 13:02:42 +0800 Subject: [PATCH 2/2] A: tidy --- problems/constrained-subset-sum/README.md | 73 ----------------------- 1 file changed, 73 deletions(-) delete mode 100644 problems/constrained-subset-sum/README.md diff --git a/problems/constrained-subset-sum/README.md b/problems/constrained-subset-sum/README.md deleted file mode 100644 index 3a5366d9a..000000000 --- a/problems/constrained-subset-sum/README.md +++ /dev/null @@ -1,73 +0,0 @@ - - - - - - - -[< Previous](../diagonal-traverse-ii "Diagonal Traverse II") -                 -Next > - -## [1425. Constrained Subset Sum (Hard)](https://leetcode.com/problems/constrained-subset-sum "带限制的子序列和") - -

Given an integer array nums and an integer k, return the maximum sum of a non-empty subset of that array such that for every two consecutive integers in the subset, nums[i] and nums[j], where i < j, the condition j - i <= k is satisfied.

- -

subset of an array is obtained by deleting some number of elements (can be zero) from the array, leaving the remaining elements in their original order.

- -

 

-

Example 1:

- -
-Input: nums = [10,2,-10,5,20], k = 2
-Output: 37
-Explanation: The subset is [10, 2, 5, 20].
-
- -

Example 2:

- -
-Input: nums = [-1,-2,-3], k = 1
-Output: -1
-Explanation: The subset must be non-empty, so we choose the largest number.
-
- -

Example 3:

- -
-Input: nums = [10,-2,-10,-5,20], k = 2
-Output: 23
-Explanation: The subset is [10, -2, -5, 20].
-
- -

 

-

Constraints:

- -
    -
  • 1 <= k <= nums.length <= 10^5
  • -
  • -10^4 <= nums[i] <= 10^4
  • -
- -### Related Topics - [[Dynamic Programming](../../tag/dynamic-programming/README.md)] - -### Hints -
-Hint 1 -Use dynamic programming. -
- -
-Hint 2 -Let dp[i] be the solution for the prefix of the array that ends at index i, if the element at index i is in the subset. -
- -
-Hint 3 -dp[i] = nums[i] + max(0, dp[i-k], dp[i-k+1], ..., dp[i-1]) -
- -
-Hint 4 -Use a heap with the sliding window technique to optimize the dp. -