From f9b69bc9f2671cae9236d81cee106cbda02e998a Mon Sep 17 00:00:00 2001 From: Shuo Date: Tue, 26 May 2020 13:05:06 +0800 Subject: [PATCH] A: new --- README.md | 9 +- problems/active-users/README.md | 14 +++ problems/active-users/mysql_schemas.sql | 15 ++++ problems/apples-oranges/README.md | 2 +- .../README.md | 90 +++++++++++++++++++ .../README.md | 2 +- problems/find-peak-element/README.md | 4 +- problems/find-pivot-index/README.md | 28 +++--- .../README.md | 2 +- .../kth-smallest-element-in-a-bst/README.md | 11 ++- .../README.md | 2 +- .../README.md | 60 +++++++++++++ .../README.md | 2 +- .../README.md | 82 +++++++++++++++++ problems/mini-parser/README.md | 29 +++--- problems/odd-even-linked-list/README.md | 4 +- problems/permutation-in-string/README.md | 7 +- .../README.md | 68 ++++++++++++++ readme/601-900.md | 2 +- tag/array/README.md | 2 +- tag/bit-manipulation/README.md | 1 + tag/depth-first-search/README.md | 1 + tag/dynamic-programming/README.md | 3 +- tag/sliding-window/README.md | 1 + tag/string/README.md | 4 +- tag/tree/README.md | 1 + 26 files changed, 396 insertions(+), 50 deletions(-) create mode 100644 problems/active-users/README.md create mode 100644 problems/active-users/mysql_schemas.sql create mode 100644 problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/README.md create mode 100644 problems/max-dot-product-of-two-subsequences/README.md create mode 100644 problems/maximum-number-of-vowels-in-a-substring-of-given-length/README.md create mode 100644 problems/pseudo-palindromic-paths-in-a-binary-tree/README.md diff --git a/README.md b/README.md index 02b82d890..ccbc2ec36 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,11 @@ LeetCode Problems' Solutions | # | Title | Solution | Difficulty | | :-: | - | - | :-: | +| 1458 | [Max Dot Product of Two Subsequences](https://leetcode.com/problems/max-dot-product-of-two-subsequences "两个子序列的最大点积") | [Go](problems/max-dot-product-of-two-subsequences) | Hard | +| 1457 | [Pseudo-Palindromic Paths in a Binary Tree](https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree "二叉树中的伪回文路径") | [Go](problems/pseudo-palindromic-paths-in-a-binary-tree) | Medium | +| 1456 | [Maximum Number of Vowels in a Substring of Given Length](https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length "定长子串中元音的最大数目") | [Go](problems/maximum-number-of-vowels-in-a-substring-of-given-length) | Medium | +| 1455 | [Check If a Word Occurs As a Prefix of Any Word in a Sentence](https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence "检查单词是否为句中其他单词的前缀") | [Go](problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence) | Easy | +| 1454 | [Active Users](https://leetcode.com/problems/active-users) 🔒 | [MySQL](problems/active-users) | Medium | | 1453 | [Maximum Number of Darts Inside of a Circular Dartboard](https://leetcode.com/problems/maximum-number-of-darts-inside-of-a-circular-dartboard "圆形靶内的最大飞镖数量") | [Go](problems/maximum-number-of-darts-inside-of-a-circular-dartboard) | Hard | | 1452 | [People Whose List of Favorite Companies Is Not a Subset of Another List](https://leetcode.com/problems/people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list "收藏清单") | [Go](problems/people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list) | Medium | | 1451 | [Rearrange Words in a Sentence](https://leetcode.com/problems/rearrange-words-in-a-sentence "重新排列句子中的单词") | [Go](problems/rearrange-words-in-a-sentence) | Medium | @@ -70,7 +75,7 @@ LeetCode Problems' Solutions | 1448 | [Count Good Nodes in Binary Tree](https://leetcode.com/problems/count-good-nodes-in-binary-tree "统计二叉树中好节点的数目") | [Go](problems/count-good-nodes-in-binary-tree) | Medium | | 1447 | [Simplified Fractions](https://leetcode.com/problems/simplified-fractions "最简分数") | [Go](problems/simplified-fractions) | Medium | | 1446 | [Consecutive Characters](https://leetcode.com/problems/consecutive-characters "连续字符") | [Go](problems/consecutive-characters) | Easy | -| 1445 | [Apples & Oranges](https://leetcode.com/problems/apples-oranges) 🔒 | [MySQL](problems/apples-oranges) | Medium | +| 1445 | [Apples & Oranges](https://leetcode.com/problems/apples-oranges "苹果和桔子") 🔒 | [MySQL](problems/apples-oranges) | Medium | | 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 | @@ -87,7 +92,7 @@ LeetCode Problems' Solutions | 1431 | [Kids With the Greatest Number of Candies](https://leetcode.com/problems/kids-with-the-greatest-number-of-candies "拥有最多糖果的孩子") | [Go](problems/kids-with-the-greatest-number-of-candies) | Easy | | 1430 | [Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree](https://leetcode.com/problems/check-if-a-string-is-a-valid-sequence-from-root-to-leaves-path-in-a-binary-tree) 🔒 | [Go](problems/check-if-a-string-is-a-valid-sequence-from-root-to-leaves-path-in-a-binary-tree) | Medium | | 1429 | [First Unique Number](https://leetcode.com/problems/first-unique-number) 🔒 | [Go](problems/first-unique-number) | Medium | -| 1428 | [Leftmost Column with at Least a One](https://leetcode.com/problems/leftmost-column-with-at-least-a-one) 🔒 | [Go](problems/leftmost-column-with-at-least-a-one) | Medium | +| 1428 | [Leftmost Column with at Least a One](https://leetcode.com/problems/leftmost-column-with-at-least-a-one "至少有一个 1 的最左端列") 🔒 | [Go](problems/leftmost-column-with-at-least-a-one) | Medium | | 1427 | [Perform String Shifts](https://leetcode.com/problems/perform-string-shifts) 🔒 | [Go](problems/perform-string-shifts) | Easy | | 1426 | [Counting Elements](https://leetcode.com/problems/counting-elements "数元素") 🔒 | [Go](problems/counting-elements) | Easy | | 1425 | [Constrained Subsequence Sum](https://leetcode.com/problems/constrained-subsequence-sum "带限制的子序列和") | [Go](problems/constrained-subsequence-sum) | Hard | diff --git a/problems/active-users/README.md b/problems/active-users/README.md new file mode 100644 index 000000000..05d3c0032 --- /dev/null +++ b/problems/active-users/README.md @@ -0,0 +1,14 @@ + + + + + + + +[< Previous](../maximum-number-of-darts-inside-of-a-circular-dartboard "Maximum Number of Darts Inside of a Circular Dartboard") +                 +[Next >](../check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence "Check If a Word Occurs As a Prefix of Any Word in a Sentence") + +## [1454. Active Users (Medium)](https://leetcode.com/problems/active-users "") + + diff --git a/problems/active-users/mysql_schemas.sql b/problems/active-users/mysql_schemas.sql new file mode 100644 index 000000000..256bf8be8 --- /dev/null +++ b/problems/active-users/mysql_schemas.sql @@ -0,0 +1,15 @@ +Create table If Not Exists Accounts (id int, name varchar(10)); +Create table If Not Exists Logins (id int, login_date date); +Truncate table Accounts; +insert into Accounts (id, name) values ('1', 'Winston'); +insert into Accounts (id, name) values ('7', 'Jonathan'); +Truncate table Logins; +insert into Logins (id, login_date) values ('7', '2020-05-30'); +insert into Logins (id, login_date) values ('1', '2020-05-30'); +insert into Logins (id, login_date) values ('7', '2020-05-31'); +insert into Logins (id, login_date) values ('7', '2020-06-01'); +insert into Logins (id, login_date) values ('7', '2020-06-02'); +insert into Logins (id, login_date) values ('7', '2020-06-02'); +insert into Logins (id, login_date) values ('7', '2020-06-03'); +insert into Logins (id, login_date) values ('1', '2020-06-07'); +insert into Logins (id, login_date) values ('7', '2020-06-10'); diff --git a/problems/apples-oranges/README.md b/problems/apples-oranges/README.md index b9963c64d..6d592e273 100644 --- a/problems/apples-oranges/README.md +++ b/problems/apples-oranges/README.md @@ -9,6 +9,6 @@                  [Next >](../consecutive-characters "Consecutive Characters") -## [1445. Apples & Oranges (Medium)](https://leetcode.com/problems/apples-oranges "") +## [1445. Apples & Oranges (Medium)](https://leetcode.com/problems/apples-oranges "苹果和桔子") diff --git a/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/README.md b/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/README.md new file mode 100644 index 000000000..9d216a1e5 --- /dev/null +++ b/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/README.md @@ -0,0 +1,90 @@ + + + + + + + +[< Previous](../active-users "Active Users") +                 +[Next >](../maximum-number-of-vowels-in-a-substring-of-given-length "Maximum Number of Vowels in a Substring of Given Length") + +## [1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence (Easy)](https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence "检查单词是否为句中其他单词的前缀") + +

Given a sentence that consists of some words separated by a single space, and a searchWord.

+ +

You have to check if searchWord is a prefix of any word in sentence.

+ +

Return the index of the word in sentence where searchWord is a prefix of this word (1-indexed).

+ +

If searchWord is a prefix of more than one word, return the index of the first word (minimum index). If there is no such word return -1.

+ +

A prefix of a string S is any leading contiguous substring of S.

+ +

 

+

Example 1:

+ +
+Input: sentence = "i love eating burger", searchWord = "burg"
+Output: 4
+Explanation: "burg" is prefix of "burger" which is the 4th word in the sentence.
+
+ +

Example 2:

+ +
+Input: sentence = "this problem is an easy problem", searchWord = "pro"
+Output: 2
+Explanation: "pro" is prefix of "problem" which is the 2nd and the 6th word in the sentence, but we return 2 as it's the minimal index.
+
+ +

Example 3:

+ +
+Input: sentence = "i am tired", searchWord = "you"
+Output: -1
+Explanation: "you" is not a prefix of any word in the sentence.
+
+ +

Example 4:

+ +
+Input: sentence = "i use triple pillow", searchWord = "pill"
+Output: 4
+
+ +

Example 5:

+ +
+Input: sentence = "hello from the other side", searchWord = "they"
+Output: -1
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[String](../../tag/string/README.md)] + +### Hints +
+Hint 1 +First extract the words of the sentence. +
+ +
+Hint 2 +Check for each word if searchWord occurs at index 0, if so return the index of this word (1-indexed) +
+ +
+Hint 3 +If searchWord doesn't exist as a prefix of any word return the default value (-1). +
diff --git a/problems/count-different-palindromic-subsequences/README.md b/problems/count-different-palindromic-subsequences/README.md index b8d590c65..4ca9e550b 100644 --- a/problems/count-different-palindromic-subsequences/README.md +++ b/problems/count-different-palindromic-subsequences/README.md @@ -9,7 +9,7 @@                  [Next >](../my-calendar-ii "My Calendar II") -## [730. Count Different Palindromic Subsequences (Hard)](https://leetcode.com/problems/count-different-palindromic-subsequences "统计不同回文子字符串") +## [730. Count Different Palindromic Subsequences (Hard)](https://leetcode.com/problems/count-different-palindromic-subsequences "统计不同回文子序列")

Given a string S, find the number of different non-empty palindromic subsequences in S, and return that number modulo 10^9 + 7. diff --git a/problems/find-peak-element/README.md b/problems/find-peak-element/README.md index 92bf14323..0e6e3069d 100644 --- a/problems/find-peak-element/README.md +++ b/problems/find-peak-element/README.md @@ -35,9 +35,7 @@   or index number 5 where the peak element is 6. -

Note:

- -

Your solution should be in logarithmic complexity.

+

Follow up: Your solution should be in logarithmic complexity.

### Related Topics [[Array](../../tag/array/README.md)] diff --git a/problems/find-pivot-index/README.md b/problems/find-pivot-index/README.md index 7e653f866..03aa0c5b9 100644 --- a/problems/find-pivot-index/README.md +++ b/problems/find-pivot-index/README.md @@ -13,44 +13,38 @@

Given an array of integers nums, write a method that returns the "pivot" index of this array.

-

We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.

+

We define the pivot index as the index where the sum of all the numbers to the left of the index is equal to the sum of all the numbers to the right of the index.

If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.

-

Example 1:

+

 

+

Example 1:

-Input: 
-nums = [1, 7, 3, 6, 5, 6]
-Output: 3
-Explanation: 
+Input: nums = [1,7,3,6,5,6]
+Output: 3
+Explanation:
 The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.
 Also, 3 is the first index where this occurs.
 
-

 

- -

Example 2:

+

Example 2:

-Input: 
-nums = [1, 2, 3]
-Output: -1
-Explanation: 
+Input: nums = [1,2,3]
+Output: -1
+Explanation:
 There is no index that satisfies the conditions in the problem statement.
 

 

- -

Note:

+

Constraints:

-

 

- ### Related Topics [[Array](../../tag/array/README.md)] diff --git a/problems/form-largest-integer-with-digits-that-add-up-to-target/README.md b/problems/form-largest-integer-with-digits-that-add-up-to-target/README.md index 448b78012..957fe3cef 100644 --- a/problems/form-largest-integer-with-digits-that-add-up-to-target/README.md +++ b/problems/form-largest-integer-with-digits-that-add-up-to-target/README.md @@ -29,7 +29,7 @@
 Input: cost = [4,3,2,5,6,7,2,5,5], target = 9
 Output: "7772"
-Explanation:  The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "997", but "7772" is the largest number.
+Explanation:  The cost to paint the digit '7' is 2, and the digit '2' is 3. Then cost("7772") = 2*3+ 3*1 = 9. You could also paint "977", but "7772" is the largest number.
 Digit    cost
   1  ->   4
   2  ->   3
diff --git a/problems/kth-smallest-element-in-a-bst/README.md b/problems/kth-smallest-element-in-a-bst/README.md
index a071458bd..ccb774a3b 100644
--- a/problems/kth-smallest-element-in-a-bst/README.md
+++ b/problems/kth-smallest-element-in-a-bst/README.md
@@ -13,8 +13,7 @@
 
 

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.

-

Note:
-You may assume k is always valid, 1 ≤ k ≤ BST's total elements.

+

 

Example 1:

@@ -44,6 +43,14 @@ You may assume k is always valid, 1 ≤ k ≤ BST's total elements.

Follow up:
What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?

+

 

+

Constraints:

+ + + ### Related Topics [[Tree](../../tag/tree/README.md)] [[Binary Search](../../tag/binary-search/README.md)] diff --git a/problems/leftmost-column-with-at-least-a-one/README.md b/problems/leftmost-column-with-at-least-a-one/README.md index bcf077e40..75c48d336 100644 --- a/problems/leftmost-column-with-at-least-a-one/README.md +++ b/problems/leftmost-column-with-at-least-a-one/README.md @@ -9,7 +9,7 @@                  [Next >](../first-unique-number "First Unique Number") -## [1428. Leftmost Column with at Least a One (Medium)](https://leetcode.com/problems/leftmost-column-with-at-least-a-one "") +## [1428. Leftmost Column with at Least a One (Medium)](https://leetcode.com/problems/leftmost-column-with-at-least-a-one "至少有一个 1 的最左端列") diff --git a/problems/max-dot-product-of-two-subsequences/README.md b/problems/max-dot-product-of-two-subsequences/README.md new file mode 100644 index 000000000..ee6d8c975 --- /dev/null +++ b/problems/max-dot-product-of-two-subsequences/README.md @@ -0,0 +1,60 @@ + + + + + + + +[< Previous](../pseudo-palindromic-paths-in-a-binary-tree "Pseudo-Palindromic Paths in a Binary Tree") +                 +Next > + +## [1458. Max Dot Product of Two Subsequences (Hard)](https://leetcode.com/problems/max-dot-product-of-two-subsequences "两个子序列的最大点积") + +

Given two arrays nums1 and nums2.

+ +

Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the same length.

+ +

A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).

+ +

 

+

Example 1:

+ +
+Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6]
+Output: 18
+Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2.
+Their dot product is (2*3 + (-2)*(-6)) = 18.
+ +

Example 2:

+ +
+Input: nums1 = [3,-2], nums2 = [2,-6,7]
+Output: 21
+Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2.
+Their dot product is (3*7) = 21.
+ +

Example 3:

+ +
+Input: nums1 = [-1,-1], nums2 = [1,1]
+Output: -1
+Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2.
+Their dot product is -1.
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Dynamic Programming](../../tag/dynamic-programming/README.md)] + +### Hints +
+Hint 1 +Use dynamic programming, define DP[i][j] as the maximum dot product of two subsequences starting in the position i of nums1 and position j of nums2. +
diff --git a/problems/maximum-number-of-darts-inside-of-a-circular-dartboard/README.md b/problems/maximum-number-of-darts-inside-of-a-circular-dartboard/README.md index a6bd74524..972f9f534 100644 --- a/problems/maximum-number-of-darts-inside-of-a-circular-dartboard/README.md +++ b/problems/maximum-number-of-darts-inside-of-a-circular-dartboard/README.md @@ -7,7 +7,7 @@ [< Previous](../people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list "People Whose List of Favorite Companies Is Not a Subset of Another List")                  -Next > +[Next >](../active-users "Active Users") ## [1453. Maximum Number of Darts Inside of a Circular Dartboard (Hard)](https://leetcode.com/problems/maximum-number-of-darts-inside-of-a-circular-dartboard "圆形靶内的最大飞镖数量") diff --git a/problems/maximum-number-of-vowels-in-a-substring-of-given-length/README.md b/problems/maximum-number-of-vowels-in-a-substring-of-given-length/README.md new file mode 100644 index 000000000..ff2dbd569 --- /dev/null +++ b/problems/maximum-number-of-vowels-in-a-substring-of-given-length/README.md @@ -0,0 +1,82 @@ + + + + + + + +[< Previous](../check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence "Check If a Word Occurs As a Prefix of Any Word in a Sentence") +                 +[Next >](../pseudo-palindromic-paths-in-a-binary-tree "Pseudo-Palindromic Paths in a Binary Tree") + +## [1456. Maximum Number of Vowels in a Substring of Given Length (Medium)](https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length "定长子串中元音的最大数目") + +

Given a string s and an integer k.

+ +

Return the maximum number of vowel letters in any substring of s with length k.

+ +

Vowel letters in English are (a, e, i, o, u).

+ +

 

+

Example 1:

+ +
+Input: s = "abciiidef", k = 3
+Output: 3
+Explanation: The substring "iii" contains 3 vowel letters.
+
+ +

Example 2:

+ +
+Input: s = "aeiou", k = 2
+Output: 2
+Explanation: Any substring of length 2 contains 2 vowels.
+
+ +

Example 3:

+ +
+Input: s = "leetcode", k = 3
+Output: 2
+Explanation: "lee", "eet" and "ode" contain 2 vowels.
+
+ +

Example 4:

+ +
+Input: s = "rhythms", k = 4
+Output: 0
+Explanation: We can see that s doesn't have any vowel letters.
+
+ +

Example 5:

+ +
+Input: s = "tryhard", k = 4
+Output: 1
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[String](../../tag/string/README.md)] + [[Sliding Window](../../tag/sliding-window/README.md)] + +### Hints +
+Hint 1 +Keep a window of size k and maintain the number of vowels in it. +
+ +
+Hint 2 +Keep moving the window and update the number of vowels while moving. Answer is max number of vowels of any window. +
diff --git a/problems/mini-parser/README.md b/problems/mini-parser/README.md index 8f3720ae7..711e765b7 100644 --- a/problems/mini-parser/README.md +++ b/problems/mini-parser/README.md @@ -15,26 +15,30 @@

Each element is either an integer, or a list -- whose elements may also be integers or other lists.

-

Note: -You may assume that the string is well-formed: +

Note: You may assume that the string is well-formed:

+ -

-

Example 1: +

 

+ +

Example 1:

+
-Given s = "324",
+Given s = "324",
 
 You should return a NestedInteger object which contains a single integer 324.
 
-

-

Example 2: +

 

+ +

Example 2:

+
-Given s = "[123,[456,[789]]]",
+Given s = "[123,[456,[789]]]",
 
 Return a NestedInteger object containing a nested list with 2 elements:
 
@@ -44,7 +48,8 @@ Return a NestedInteger object containing a nested list with 2 elements:
     ii. A nested list with one element:
          a. An integer containing value 789.
 
-

+ +

 

### Related Topics [[Stack](../../tag/stack/README.md)] diff --git a/problems/odd-even-linked-list/README.md b/problems/odd-even-linked-list/README.md index 45f2449b2..4abbe37ec 100644 --- a/problems/odd-even-linked-list/README.md +++ b/problems/odd-even-linked-list/README.md @@ -29,11 +29,13 @@ Output: 2->3->6->7->1->5->4->NULL
-

Note:

+

 

+

Constraints:

### Related Topics diff --git a/problems/permutation-in-string/README.md b/problems/permutation-in-string/README.md index 3c3b8fb52..59878437b 100644 --- a/problems/permutation-in-string/README.md +++ b/problems/permutation-in-string/README.md @@ -31,13 +31,12 @@

 

+

Constraints:

-

Note:

- -
    +
+ ### Related Topics [[Two Pointers](../../tag/two-pointers/README.md)] diff --git a/problems/pseudo-palindromic-paths-in-a-binary-tree/README.md b/problems/pseudo-palindromic-paths-in-a-binary-tree/README.md new file mode 100644 index 000000000..b673de543 --- /dev/null +++ b/problems/pseudo-palindromic-paths-in-a-binary-tree/README.md @@ -0,0 +1,68 @@ + + + + + + + +[< Previous](../maximum-number-of-vowels-in-a-substring-of-given-length "Maximum Number of Vowels in a Substring of Given Length") +                 +[Next >](../max-dot-product-of-two-subsequences "Max Dot Product of Two Subsequences") + +## [1457. Pseudo-Palindromic Paths in a Binary Tree (Medium)](https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree "二叉树中的伪回文路径") + +

Given a binary tree where node values are digits from 1 to 9. A path in the binary tree is said to be pseudo-palindromic if at least one permutation of the node values in the path is a palindrome.

+ +

Return the number of pseudo-palindromic paths going from the root node to leaf nodes.

+ +

 

+

Example 1:

+ +

+ +
+Input: root = [2,3,1,3,1,null,1]
+Output: 2 
+Explanation: The figure above represents the given binary tree. There are three paths going from the root node to leaf nodes: the red path [2,3,3], the green path [2,1,1], and the path [2,3,1]. Among these paths only red path and green path are pseudo-palindromic paths since the red path [2,3,3] can be rearranged in [3,2,3] (palindrome) and the green path [2,1,1] can be rearranged in [1,2,1] (palindrome).
+
+ +

Example 2:

+ +

+ +
+Input: root = [2,1,1,1,3,null,null,null,null,null,1]
+Output: 1 
+Explanation: The figure above represents the given binary tree. There are three paths going from the root node to leaf nodes: the green path [2,1,1], the path [2,1,3,1], and the path [2,1]. Among these paths only the green path is pseudo-palindromic since [2,1,1] can be rearranged in [1,2,1] (palindrome).
+
+ +

Example 3:

+ +
+Input: root = [9]
+Output: 1
+
+ +

 

+

Constraints:

+ + + +### Related Topics + [[Bit Manipulation](../../tag/bit-manipulation/README.md)] + [[Tree](../../tag/tree/README.md)] + [[Depth-first Search](../../tag/depth-first-search/README.md)] + +### Hints +
+Hint 1 +Note that the node values of a path form a palindrome if at most one digit has an odd frequency (parity). +
+ +
+Hint 2 +Use a Depth First Search (DFS) keeping the frequency (parity) of the digits. Once you are in a leaf node check if at most one digit has an odd frequency (parity). +
diff --git a/readme/601-900.md b/readme/601-900.md index fbce6aef4..8e761e7fb 100644 --- a/readme/601-900.md +++ b/readme/601-900.md @@ -191,7 +191,7 @@ LeetCode Problems' Solutions | 727 | [Minimum Window Subsequence](https://leetcode.com/problems/minimum-window-subsequence "最小窗口子序列") 🔒 | [Go](../problems/minimum-window-subsequence) | Hard | | 728 | [Self Dividing Numbers](https://leetcode.com/problems/self-dividing-numbers "自除数") | [Go](../problems/self-dividing-numbers) | Easy | | 729 | [My Calendar I](https://leetcode.com/problems/my-calendar-i "我的日程安排表 I") | [Go](../problems/my-calendar-i) | Medium | -| 730 | [Count Different Palindromic Subsequences](https://leetcode.com/problems/count-different-palindromic-subsequences "统计不同回文子字符串") | [Go](../problems/count-different-palindromic-subsequences) | Hard | +| 730 | [Count Different Palindromic Subsequences](https://leetcode.com/problems/count-different-palindromic-subsequences "统计不同回文子序列") | [Go](../problems/count-different-palindromic-subsequences) | Hard | | 731 | [My Calendar II](https://leetcode.com/problems/my-calendar-ii "我的日程安排表 II") | [Go](../problems/my-calendar-ii) | Medium | | 732 | [My Calendar III](https://leetcode.com/problems/my-calendar-iii "我的日程安排表 III") | [Go](../problems/my-calendar-iii) | Hard | | 733 | [Flood Fill](https://leetcode.com/problems/flood-fill "图像渲染") | [Go](../problems/flood-fill) | Easy | diff --git a/tag/array/README.md b/tag/array/README.md index b311be9fb..ed9e02fc0 100644 --- a/tag/array/README.md +++ b/tag/array/README.md @@ -14,7 +14,7 @@ | 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 | -| 1428 | [Leftmost Column with at Least a One](../../problems/leftmost-column-with-at-least-a-one) 🔒 | [[数组](../array/README.md)] | Medium | +| 1428 | [至少有一个 1 的最左端列](../../problems/leftmost-column-with-at-least-a-one) 🔒 | [[数组](../array/README.md)] | Medium | | 1427 | [Perform String Shifts](../../problems/perform-string-shifts) 🔒 | [[数组](../array/README.md)] [[数学](../math/README.md)] | Easy | | 1426 | [数元素](../../problems/counting-elements) 🔒 | [[数组](../array/README.md)] | Easy | | 1424 | [对角线遍历 II](../../problems/diagonal-traverse-ii) | [[排序](../sort/README.md)] [[数组](../array/README.md)] | Medium | diff --git a/tag/bit-manipulation/README.md b/tag/bit-manipulation/README.md index 31bd7b3f2..108b7ff40 100644 --- a/tag/bit-manipulation/README.md +++ b/tag/bit-manipulation/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1457 | [二叉树中的伪回文路径](../../problems/pseudo-palindromic-paths-in-a-binary-tree) | [[位运算](../bit-manipulation/README.md)] [[树](../tree/README.md)] [[深度优先搜索](../depth-first-search/README.md)] | Medium | | 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 | diff --git a/tag/depth-first-search/README.md b/tag/depth-first-search/README.md index e025264a9..936794801 100644 --- a/tag/depth-first-search/README.md +++ b/tag/depth-first-search/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1457 | [二叉树中的伪回文路径](../../problems/pseudo-palindromic-paths-in-a-binary-tree) | [[位运算](../bit-manipulation/README.md)] [[树](../tree/README.md)] [[深度优先搜索](../depth-first-search/README.md)] | Medium | | 1448 | [统计二叉树中好节点的数目](../../problems/count-good-nodes-in-binary-tree) | [[树](../tree/README.md)] [[深度优先搜索](../depth-first-search/README.md)] | Medium | | 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 | diff --git a/tag/dynamic-programming/README.md b/tag/dynamic-programming/README.md index db2bd32a3..7f5c30011 100644 --- a/tag/dynamic-programming/README.md +++ b/tag/dynamic-programming/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1458 | [两个子序列的最大点积](../../problems/max-dot-product-of-two-subsequences) | [[动态规划](../dynamic-programming/README.md)] | Hard | | 1449 | [数位成本和为目标值的最大数字](../../problems/form-largest-integer-with-digits-that-add-up-to-target) | [[字符串](../string/README.md)] [[动态规划](../dynamic-programming/README.md)] | Hard | | 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 | @@ -115,7 +116,7 @@ | 746 | [使用最小花费爬楼梯](../../problems/min-cost-climbing-stairs) | [[数组](../array/README.md)] [[动态规划](../dynamic-programming/README.md)] | Easy | | 741 | [摘樱桃](../../problems/cherry-pickup) | [[动态规划](../dynamic-programming/README.md)] | Hard | | 740 | [删除与获得点数](../../problems/delete-and-earn) | [[动态规划](../dynamic-programming/README.md)] | Medium | -| 730 | [统计不同回文子字符串](../../problems/count-different-palindromic-subsequences) | [[字符串](../string/README.md)] [[动态规划](../dynamic-programming/README.md)] | Hard | +| 730 | [统计不同回文子序列](../../problems/count-different-palindromic-subsequences) | [[字符串](../string/README.md)] [[动态规划](../dynamic-programming/README.md)] | Hard | | 727 | [最小窗口子序列](../../problems/minimum-window-subsequence) 🔒 | [[动态规划](../dynamic-programming/README.md)] [[Sliding Window](../sliding-window/README.md)] | Hard | | 718 | [最长重复子数组](../../problems/maximum-length-of-repeated-subarray) | [[数组](../array/README.md)] [[哈希表](../hash-table/README.md)] [[二分查找](../binary-search/README.md)] [[动态规划](../dynamic-programming/README.md)] | Medium | | 714 | [买卖股票的最佳时机含手续费](../../problems/best-time-to-buy-and-sell-stock-with-transaction-fee) | [[贪心算法](../greedy/README.md)] [[数组](../array/README.md)] [[动态规划](../dynamic-programming/README.md)] | Medium | diff --git a/tag/sliding-window/README.md b/tag/sliding-window/README.md index 5ad662cb4..a602f91fc 100644 --- a/tag/sliding-window/README.md +++ b/tag/sliding-window/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1456 | [定长子串中元音的最大数目](../../problems/maximum-number-of-vowels-in-a-substring-of-given-length) | [[字符串](../string/README.md)] [[Sliding Window](../sliding-window/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 | | 1423 | [可获得的最大点数](../../problems/maximum-points-you-can-obtain-from-cards) | [[数组](../array/README.md)] [[动态规划](../dynamic-programming/README.md)] [[Sliding Window](../sliding-window/README.md)] | Medium | | 1208 | [尽可能使字符串相等](../../problems/get-equal-substrings-within-budget) | [[数组](../array/README.md)] [[Sliding Window](../sliding-window/README.md)] | Medium | diff --git a/tag/string/README.md b/tag/string/README.md index 04c8d34cd..23117a1b0 100644 --- a/tag/string/README.md +++ b/tag/string/README.md @@ -9,6 +9,8 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1456 | [定长子串中元音的最大数目](../../problems/maximum-number-of-vowels-in-a-substring-of-given-length) | [[字符串](../string/README.md)] [[Sliding Window](../sliding-window/README.md)] | Medium | +| 1455 | [检查单词是否为句中其他单词的前缀](../../problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence) | [[字符串](../string/README.md)] | Easy | | 1452 | [收藏清单](../../problems/people-whose-list-of-favorite-companies-is-not-a-subset-of-another-list) | [[排序](../sort/README.md)] [[字符串](../string/README.md)] | Medium | | 1451 | [重新排列句子中的单词](../../problems/rearrange-words-in-a-sentence) | [[排序](../sort/README.md)] [[字符串](../string/README.md)] | Medium | | 1449 | [数位成本和为目标值的最大数字](../../problems/form-largest-integer-with-digits-that-add-up-to-target) | [[字符串](../string/README.md)] [[动态规划](../dynamic-programming/README.md)] | Hard | @@ -95,7 +97,7 @@ | 761 | [特殊的二进制序列](../../problems/special-binary-string) | [[递归](../recursion/README.md)] [[字符串](../string/README.md)] | Hard | | 758 | [字符串中的加粗单词](../../problems/bold-words-in-string) 🔒 | [[字符串](../string/README.md)] | Easy | | 736 | [Lisp 语法解析](../../problems/parse-lisp-expression) | [[字符串](../string/README.md)] | Hard | -| 730 | [统计不同回文子字符串](../../problems/count-different-palindromic-subsequences) | [[字符串](../string/README.md)] [[动态规划](../dynamic-programming/README.md)] | Hard | +| 730 | [统计不同回文子序列](../../problems/count-different-palindromic-subsequences) | [[字符串](../string/README.md)] [[动态规划](../dynamic-programming/README.md)] | Hard | | 722 | [删除注释](../../problems/remove-comments) | [[字符串](../string/README.md)] | Medium | | 709 | [转换成小写字母](../../problems/to-lower-case) | [[字符串](../string/README.md)] | Easy | | 696 | [计数二进制子串](../../problems/count-binary-substrings) | [[字符串](../string/README.md)] | Easy | diff --git a/tag/tree/README.md b/tag/tree/README.md index ee49bf21c..99f713b4e 100644 --- a/tag/tree/README.md +++ b/tag/tree/README.md @@ -9,6 +9,7 @@ | # | 题目 | 标签 | 难度 | | :-: | - | - | :-: | +| 1457 | [二叉树中的伪回文路径](../../problems/pseudo-palindromic-paths-in-a-binary-tree) | [[位运算](../bit-manipulation/README.md)] [[树](../tree/README.md)] [[深度优先搜索](../depth-first-search/README.md)] | Medium | | 1448 | [统计二叉树中好节点的数目](../../problems/count-good-nodes-in-binary-tree) | [[树](../tree/README.md)] [[深度优先搜索](../depth-first-search/README.md)] | Medium | | 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 |