From 567b9f0e2fbf5e31a27acc447680295fa8a14bf9 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年6月11日 12:18:12 -0700 Subject: [PATCH 01/27] Create 2301.Match-Substring-After-Replacement.cpp --- ...2301.Match-Substring-After-Replacement.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Dynamic_Programming/2301.Match-Substring-After-Replacement/2301.Match-Substring-After-Replacement.cpp diff --git a/Dynamic_Programming/2301.Match-Substring-After-Replacement/2301.Match-Substring-After-Replacement.cpp b/Dynamic_Programming/2301.Match-Substring-After-Replacement/2301.Match-Substring-After-Replacement.cpp new file mode 100644 index 000000000..ef8ed6dc7 --- /dev/null +++ b/Dynamic_Programming/2301.Match-Substring-After-Replacement/2301.Match-Substring-After-Replacement.cpp @@ -0,0 +1,35 @@ +class Solution { + bool dp[5001][5001]; + bool table[256][256]; + unordered_map>Map; +public: + bool matchReplacement(string s, string sub, vector>& mappings) + { + int m = s.size(); + int n = sub.size(); + s = "#"+s; + sub = "#"+sub; + + for (auto x: mappings) + { + table[x[0]][x[1]] = 1; + } + + for (int i=0; i<=m; i++) + dp[i][0] = true; + + for (int i=1; i<=m; i++) + for (int j=1; j<=n; j++) + { + if (dp[i-1][j-1]==0) continue; + + if (s[i]==sub[j] || table[sub[j]][s[i]]) + dp[i][j] = dp[i-1][j-1]; + + if (j==n && dp[i][j]==true) + return true; + } + return false; + + } +}; From d45c8b550940a018a33d311bdecd9b7696c92fd4 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年6月11日 12:18:42 -0700 Subject: [PATCH 02/27] Update Readme.md --- Readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Readme.md b/Readme.md index cf54fcceb..aef27a575 100644 --- a/Readme.md +++ b/Readme.md @@ -734,6 +734,7 @@ [1312.Minimum-Insertion-Steps-to-Make-a-String-Palindrome](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1312.Minimum-Insertion-Steps-to-Make-a-String-Palindrome) (M+) [1458.Max-Dot-Product-of-Two-Subsequences](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1458.Max-Dot-Product-of-Two-Subsequences) (M) [1771.Maximize-Palindrome-Length-From-Subsequences](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1771.Maximize-Palindrome-Length-From-Subsequences) (H) +[2301.Match-Substring-After-Replacement](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/2301.Match-Substring-After-Replacement) (M+) * ``状态压缩DP`` [465.Optimal-Account-Balancing](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/465.Optimal-Account-Balancing) (H) [691.Stickers-to-Spell-Word](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/691.Stickers-to-Spell-Word) (H) From 39d80e85321c382000e3f02cbee3284237710a30 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年6月11日 12:28:27 -0700 Subject: [PATCH 03/27] Delete 2301.Match-Substring-After-Replacement.cpp --- ...2301.Match-Substring-After-Replacement.cpp | 35 ------------------- 1 file changed, 35 deletions(-) delete mode 100644 Dynamic_Programming/2301.Match-Substring-After-Replacement/2301.Match-Substring-After-Replacement.cpp diff --git a/Dynamic_Programming/2301.Match-Substring-After-Replacement/2301.Match-Substring-After-Replacement.cpp b/Dynamic_Programming/2301.Match-Substring-After-Replacement/2301.Match-Substring-After-Replacement.cpp deleted file mode 100644 index ef8ed6dc7..000000000 --- a/Dynamic_Programming/2301.Match-Substring-After-Replacement/2301.Match-Substring-After-Replacement.cpp +++ /dev/null @@ -1,35 +0,0 @@ -class Solution { - bool dp[5001][5001]; - bool table[256][256]; - unordered_map>Map; -public: - bool matchReplacement(string s, string sub, vector>& mappings) - { - int m = s.size(); - int n = sub.size(); - s = "#"+s; - sub = "#"+sub; - - for (auto x: mappings) - { - table[x[0]][x[1]] = 1; - } - - for (int i=0; i<=m; i++) - dp[i][0] = true; - - for (int i=1; i<=m; i++) - for (int j=1; j<=n; j++) - { - if (dp[i-1][j-1]==0) continue; - - if (s[i]==sub[j] || table[sub[j]][s[i]]) - dp[i][j] = dp[i-1][j-1]; - - if (j==n && dp[i][j]==true) - return true; - } - return false; - - } -}; From 3e5a9650fc8b7ea046db1c83e2e7171d25262c41 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年6月11日 15:29:06 -0700 Subject: [PATCH 04/27] Create 2301.Match-Substring-After-Replacement_KMP.cpp --- ....Match-Substring-After-Replacement_KMP.cpp | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 String/2301.Match-Substring-After-Replacement/2301.Match-Substring-After-Replacement_KMP.cpp diff --git a/String/2301.Match-Substring-After-Replacement/2301.Match-Substring-After-Replacement_KMP.cpp b/String/2301.Match-Substring-After-Replacement/2301.Match-Substring-After-Replacement_KMP.cpp new file mode 100644 index 000000000..52c8348e7 --- /dev/null +++ b/String/2301.Match-Substring-After-Replacement/2301.Match-Substring-After-Replacement_KMP.cpp @@ -0,0 +1,72 @@ +class Solution { + bool table[256][256]; +public: + bool matchReplacement(string s, string sub, vector>& mappings) + { + int m = s.size(); + + for (auto x: mappings) + { + table[x[0]][x[1]] = 1; + } + + return strStr(s, sub)!=-1; + } + + bool equal(char a, char b) + { + return (a==b || table[b][a]); + } + + int strStr(string haystack, string needle) + { + string s = haystack; + string p = needle; + int n = s.size(); + int m = p.size(); + + if (m==0) return 0; + if (n==0) return -1; + + vectordp(n,0); + dp[0] = equal(s[0], p[0]); + if (m==1 && dp[0]==1) + return 0; + + vectorsuffix = preprocess(p); + + for (int i=1; i0 && !equal(s[i], p[j])) + j = suffix[j-1]; + dp[i] = j + equal(s[i], p[j]); + + if (dp[i]==p.size()) + return i - p.size() + 1; + } + + return -1; + + } + + vector preprocess(string s) + { + int n = s.size(); + vectordp(n); + dp[0] = 0; + + for (int i=1; i=1 && !equal(s[j],s[i])) + { + j = dp[j-1]; + } + dp[i] = j + equal(s[j], s[i]); + } + + return dp; + } +}; From bac9e06e3e39d5973294c413999b4b9a521ab8ee Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年6月11日 15:30:02 -0700 Subject: [PATCH 05/27] Create 2301.Match-Substring-After-Replacement_Brute.cpp --- ...atch-Substring-After-Replacement_Brute.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 String/2301.Match-Substring-After-Replacement/2301.Match-Substring-After-Replacement_Brute.cpp diff --git a/String/2301.Match-Substring-After-Replacement/2301.Match-Substring-After-Replacement_Brute.cpp b/String/2301.Match-Substring-After-Replacement/2301.Match-Substring-After-Replacement_Brute.cpp new file mode 100644 index 000000000..ed30a7934 --- /dev/null +++ b/String/2301.Match-Substring-After-Replacement/2301.Match-Substring-After-Replacement_Brute.cpp @@ -0,0 +1,30 @@ +class Solution { + bool table[256][256]; +public: + bool matchReplacement(string s, string sub, vector>& mappings) + { + int m = s.size(); + + for (auto x: mappings) + { + table[x[0]][x[1]] = 1; + } + + for (int i=0; i Date: 2022年6月11日 15:30:30 -0700 Subject: [PATCH 06/27] Update Readme.md --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index aef27a575..11ff53c83 100644 --- a/Readme.md +++ b/Readme.md @@ -734,7 +734,6 @@ [1312.Minimum-Insertion-Steps-to-Make-a-String-Palindrome](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1312.Minimum-Insertion-Steps-to-Make-a-String-Palindrome) (M+) [1458.Max-Dot-Product-of-Two-Subsequences](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1458.Max-Dot-Product-of-Two-Subsequences) (M) [1771.Maximize-Palindrome-Length-From-Subsequences](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1771.Maximize-Palindrome-Length-From-Subsequences) (H) -[2301.Match-Substring-After-Replacement](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/2301.Match-Substring-After-Replacement) (M+) * ``状态压缩DP`` [465.Optimal-Account-Balancing](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/465.Optimal-Account-Balancing) (H) [691.Stickers-to-Spell-Word](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/691.Stickers-to-Spell-Word) (H) @@ -856,6 +855,7 @@ [1367.Linked-List-in-Binary-Tree](https://github.com/wisdompeak/LeetCode/tree/master/String/1367.Linked-List-in-Binary-Tree) (H) 1397.Find All Good Strings (TBD) [1764.Form-Array-by-Concatenating-Subarrays-of-Another-Array](https://github.com/wisdompeak/LeetCode/tree/master/String/1764.Form-Array-by-Concatenating-Subarrays-of-Another-Array) (H) +[2301.Match-Substring-After-Replacement](https://github.com/wisdompeak/LeetCode/tree/master/String/2301.Match-Substring-After-Replacement) (H-) * ``Manacher`` [005.Longest-Palindromic-Substring](https://github.com/wisdompeak/LeetCode/tree/master/String/005.Longest-Palindromic-Substring) (H) [214.Shortest-Palindrome](https://github.com/wisdompeak/LeetCode/blob/master/String/214.Shortest-Palindrome) (H) From cd2262ce4e266a95ec26750cc93fc60a7e8e27c9 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年6月11日 15:41:39 -0700 Subject: [PATCH 07/27] Create Readme.md --- String/2301.Match-Substring-After-Replacement/Readme.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 String/2301.Match-Substring-After-Replacement/Readme.md diff --git a/String/2301.Match-Substring-After-Replacement/Readme.md b/String/2301.Match-Substring-After-Replacement/Readme.md new file mode 100644 index 000000000..50da0a046 --- /dev/null +++ b/String/2301.Match-Substring-After-Replacement/Readme.md @@ -0,0 +1,7 @@ +### 2301.Match-Substring-After-Replacement + +#### 解法1:暴力 +本题暴力查验字符串匹配,时间是o(N^2),有AC的可能。 + +#### 解法2:KMP +本题的本质其实就是在一个字符串中查找匹配的子串。最直观的高效解法就是KMP。我们只需要略微修改KMP算法中关于"两个字符相等"的定义。定义一个新的```equal(char a, char b)```. 当两个字符相等,或者sub的字符可以映射到s的字符中时,就返回true。这样KMP就可以直接用于此题。 From 421a39d84ebd3d2515cfc293cdc8d5051dbab19e Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年6月11日 16:42:48 -0700 Subject: [PATCH 08/27] Update 028.Implement-strStr-KMP.cpp --- String/028.Implement-strStr/028.Implement-strStr-KMP.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/String/028.Implement-strStr/028.Implement-strStr-KMP.cpp b/String/028.Implement-strStr/028.Implement-strStr-KMP.cpp index e381ee686..2bc5ab088 100644 --- a/String/028.Implement-strStr/028.Implement-strStr-KMP.cpp +++ b/String/028.Implement-strStr/028.Implement-strStr-KMP.cpp @@ -11,16 +11,16 @@ class Solution { vector suf = preprocess(needle); vectordp(n,0); - dp[0] = (needle[0]==haystack[0]); + dp[0] = (haystack[0]==needle[0]); if (m==1 && dp[0]==1) return 0; for (int i=1; i0 && needle[j]!=haystack[i]) + while (j>0 && haystack[i]!=needle[j]) j = suf[j-1]; - dp[i] = j + (needle[j]==haystack[i]); + dp[i] = j + (haystack[i]!=needle[j]); if (dp[i]==needle.size()) return i-needle.size()+1; } From 2dbafb8c6a29d7d2b44e334f7c43c6a26b094e07 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年6月11日 17:00:42 -0700 Subject: [PATCH 09/27] Update 028.Implement-strStr-KMP.cpp --- String/028.Implement-strStr/028.Implement-strStr-KMP.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/String/028.Implement-strStr/028.Implement-strStr-KMP.cpp b/String/028.Implement-strStr/028.Implement-strStr-KMP.cpp index 2bc5ab088..39ba57963 100644 --- a/String/028.Implement-strStr/028.Implement-strStr-KMP.cpp +++ b/String/028.Implement-strStr/028.Implement-strStr-KMP.cpp @@ -20,7 +20,7 @@ class Solution { int j = dp[i-1]; while (j>0 && haystack[i]!=needle[j]) j = suf[j-1]; - dp[i] = j + (haystack[i]!=needle[j]); + dp[i] = j + (haystack[i]==needle[j]); if (dp[i]==needle.size()) return i-needle.size()+1; } From 170bf9b859f24133a59976b5c5349b02854f354f Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年6月11日 17:18:44 -0700 Subject: [PATCH 10/27] Create 2302.Count-Subarrays-With-Score-Less-Than-K.cpp --- ...Count-Subarrays-With-Score-Less-Than-K.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Others/2302.Count-Subarrays-With-Score-Less-Than-K/2302.Count-Subarrays-With-Score-Less-Than-K.cpp diff --git a/Others/2302.Count-Subarrays-With-Score-Less-Than-K/2302.Count-Subarrays-With-Score-Less-Than-K.cpp b/Others/2302.Count-Subarrays-With-Score-Less-Than-K/2302.Count-Subarrays-With-Score-Less-Than-K.cpp new file mode 100644 index 000000000..6320f46ac --- /dev/null +++ b/Others/2302.Count-Subarrays-With-Score-Less-Than-K/2302.Count-Subarrays-With-Score-Less-Than-K.cpp @@ -0,0 +1,30 @@ +using LL = long long; +class Solution { +public: + long long countSubarrays(vector& nums, long long k) + { + int n = nums.size(); + nums.insert(nums.begin(), 0); + vectorpresum(n+1); + presum[0] = nums[0]; + for (int i=1; i<=n; i++) + presum[i] = presum[i-1]+nums[i]; + + LL ret = 0; + for (int i=1; i<=n; i++) + { + if (nums[i]>= k) continue; + LL left = 1, right = i; + while (left < right) + { + int mid = right-(right-left)/2; + if ((presum[i]-presum[i-mid])*(mid) < k) + left = mid; + else + right = mid-1; + } + ret += left; + } + return ret; + } +}; From 01cf68e39f18fa8bf92eed43cb9b7529b57bb0f4 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年6月11日 17:19:10 -0700 Subject: [PATCH 11/27] Update Readme.md --- Readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Readme.md b/Readme.md index 11ff53c83..9ba85d0fb 100644 --- a/Readme.md +++ b/Readme.md @@ -1230,6 +1230,7 @@ [2104.Sum-of-Subarray-Ranges](https://github.com/wisdompeak/LeetCode/tree/master/Stack/2104.Sum-of-Subarray-Ranges) (H-) [2262.Total-Appeal-of-A-String](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/2262.Total-Appeal-of-A-String) (M+) [2281.Sum-of-Total-Strength-of-Wizards](https://github.com/wisdompeak/LeetCode/tree/master/Others/2281.Sum-of-Total-Strength-of-Wizards) (H) +[2302.Count-Subarrays-With-Score-Less-Than-K](https://github.com/wisdompeak/LeetCode/tree/master/Others/2302.Count-Subarrays-With-Score-Less-Than-K) (H-) * ``扫描线 / 差分数组`` [252.Meeting-Rooms](https://github.com/wisdompeak/LeetCode/tree/master/Others/252.Meeting-Rooms) (M) [253.Meeting-Rooms-II](https://github.com/wisdompeak/LeetCode/tree/master/Others/253.Meeting-Rooms-II) (M+) From 033d2370efe18ef2a26623372c782d452617b20c Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年6月11日 17:35:50 -0700 Subject: [PATCH 12/27] Create Readme.md --- Others/2302.Count-Subarrays-With-Score-Less-Than-K/Readme.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 Others/2302.Count-Subarrays-With-Score-Less-Than-K/Readme.md diff --git a/Others/2302.Count-Subarrays-With-Score-Less-Than-K/Readme.md b/Others/2302.Count-Subarrays-With-Score-Less-Than-K/Readme.md new file mode 100644 index 000000000..038d2f3e9 --- /dev/null +++ b/Others/2302.Count-Subarrays-With-Score-Less-Than-K/Readme.md @@ -0,0 +1,5 @@ +### 2302.Count-Subarrays-With-Score-Less-Than-K + +根据```Count Subarrays by Element```的套路,我们不会用o(N^2)遍历数组。我们会尝试用o(N)遍历每个元素,考察它对应了哪些数组。 + +因为这道题里的subarray并没有任何代表其特征的最大值、最小值之类的,所以我们可以考虑将每种subarray的最后一个元素作为代表。具体的说,如果nums[i]是符合条件的subarray的最后一个元素,那么这个subarray的起点可以在哪里?显然,长度越长,起点越靠前,权重和就越大,直至可能超过k。利用单调性,我们就能用二分搜索来确定该subarray的最大长度,即对应了有多少个符合条件的subarray。 From d2417825e4363fb42a5a370fb8669f2d7fe5d653 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年6月11日 17:36:11 -0700 Subject: [PATCH 13/27] Update Readme.md --- Others/2302.Count-Subarrays-With-Score-Less-Than-K/Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Others/2302.Count-Subarrays-With-Score-Less-Than-K/Readme.md b/Others/2302.Count-Subarrays-With-Score-Less-Than-K/Readme.md index 038d2f3e9..b4fe19759 100644 --- a/Others/2302.Count-Subarrays-With-Score-Less-Than-K/Readme.md +++ b/Others/2302.Count-Subarrays-With-Score-Less-Than-K/Readme.md @@ -1,5 +1,5 @@ ### 2302.Count-Subarrays-With-Score-Less-Than-K -根据```Count Subarrays by Element```的套路,我们不会用o(N^2)遍历数组。我们会尝试用o(N)遍历每个元素,考察它对应了哪些数组。 +根据```Count Subarrays by Element```的套路,我们不会用o(N^2)遍历数组。我们会尝试用o(N)遍历每个元素,考察它unique地对应了哪些数组。 因为这道题里的subarray并没有任何代表其特征的最大值、最小值之类的,所以我们可以考虑将每种subarray的最后一个元素作为代表。具体的说,如果nums[i]是符合条件的subarray的最后一个元素,那么这个subarray的起点可以在哪里?显然,长度越长,起点越靠前,权重和就越大,直至可能超过k。利用单调性,我们就能用二分搜索来确定该subarray的最大长度,即对应了有多少个符合条件的subarray。 From 851cfe381e433d2e248efd47448694a090c2d649 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年6月11日 17:40:46 -0700 Subject: [PATCH 14/27] Update Readme.md --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 9ba85d0fb..bbc6990ef 100644 --- a/Readme.md +++ b/Readme.md @@ -1222,7 +1222,7 @@ * ``结论转移`` [1685.Sum-of-Absolute-Differences-in-a-Sorted-Array](https://github.com/wisdompeak/LeetCode/tree/master/Others/1685.Sum-of-Absolute-Differences-in-a-Sorted-Array) (M) [2121.Intervals-Between-Identical-Elements](https://github.com/wisdompeak/LeetCode/tree/master/Others/2121.Intervals-Between-Identical-Elements) (M) -* ``Aggregate Subarray by element`` +* ``Count Subarray by Element`` [828.Count-Unique-Characters-of-All-Substrings-of-a-Given-String](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/828.Count-Unique-Characters-of-All-Substrings-of-a-Given-String) (H-) [907.Sum-of-Subarray-Minimums](https://github.com/wisdompeak/LeetCode/tree/master/Stack/907.Sum-of-Subarray-Minimums) (H-) [1498.Number-of-Subsequences-That-Satisfy-the-Given-Sum-Condition](https://github.com/wisdompeak/LeetCode/tree/master/Two_Pointers/1498.Number-of-Subsequences-That-Satisfy-the-Given-Sum-Condition) (H-) From 4399e23b87390b6460d16a8d9648143a6d1c98c8 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年6月12日 00:42:05 -0700 Subject: [PATCH 15/27] Create 2306.Naming-a-Company.cpp --- .../2306.Naming-a-Company.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Greedy/2306.Naming-a-Company/2306.Naming-a-Company.cpp diff --git a/Greedy/2306.Naming-a-Company/2306.Naming-a-Company.cpp b/Greedy/2306.Naming-a-Company/2306.Naming-a-Company.cpp new file mode 100644 index 000000000..181739b70 --- /dev/null +++ b/Greedy/2306.Naming-a-Company/2306.Naming-a-Company.cpp @@ -0,0 +1,25 @@ +using LL = long long; +class Solution { +public: + long long distinctNames(vector& ideas) + { + vector>head2str(26); + for (string& idea: ideas) + head2str[idea[0]-'a'].insert(idea.substr(1)); + + LL ret = 0; + for (int i=0; i<26; i++) + for (int j=i+1; j<26; j++) + { + int dup = 0; + for (string x: head2str[i]) + if (head2str[j].find(x)!=head2str[j].end()) + dup++; + LL a = head2str[i].size() - dup; + LL b = head2str[j].size() - dup; + ret += a*b*2; + } + + return ret; + } +}; From 6dd8991a08da927b9dc43588ac6079c3cba66991 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年6月12日 00:42:35 -0700 Subject: [PATCH 16/27] Update Readme.md --- Readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Readme.md b/Readme.md index bbc6990ef..e8e83482e 100644 --- a/Readme.md +++ b/Readme.md @@ -1095,6 +1095,7 @@ [2257.Count-Unguarded-Cells-in-the-Grid](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/2257.Count-Unguarded-Cells-in-the-Grid) (M+) [2271.Maximum-White-Tiles-Covered-by-a-Carpet](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/2271.Maximum-White-Tiles-Covered-by-a-Carpet) (M+) [2275.Largest-Combination-With-Bitwise-AND-Greater-Than-Zero](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/2275.Largest-Combination-With-Bitwise-AND-Greater-Than-Zero) (M+) +[2306.Naming-a-Company](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/2306.Naming-a-Company) (H-) * ``LIS`` [300.Longest-Increasing-Subsequence](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/300.Longest-Increasing-Subsequence) (M+) [354.Russian-Doll-Envelopes](https://github.com/wisdompeak/LeetCode/tree/master/Greedy/354.Russian-Doll-Envelopes) (H-) From 41c78b875a9536d53283b8d4587ffd1a89590413 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年6月12日 01:16:32 -0700 Subject: [PATCH 17/27] Create Readme.md --- Greedy/2306.Naming-a-Company/Readme.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Greedy/2306.Naming-a-Company/Readme.md diff --git a/Greedy/2306.Naming-a-Company/Readme.md b/Greedy/2306.Naming-a-Company/Readme.md new file mode 100644 index 000000000..2eb5a1429 --- /dev/null +++ b/Greedy/2306.Naming-a-Company/Readme.md @@ -0,0 +1,9 @@ +### 2306.Naming-a-Company + +我们令{a}表示以字母a为首字母的后缀字符串的集合。同理有{b},{c}, ... + +根据题意,我们会将任意一个名字分成两部分看待:aA。前者是首字母,后者是除首字母外的后缀字符串。我们考虑任意两个名字aA和bB是否能配对呢?根据规则,aA + bB => aB + bA。 + +为了符合条件,aB不能出现在原始字符串中。也就是说,B不能出现在{a}里。类似的,bA不能出现在元素字符串中,即A不能出现在{b}里。所以想要aA和bB配对成功,{a}集合与{b}集合里面的相同元素都不能出现。而将这些元素从两个集合中都拿走后,{a}与{b}的元素就可以任意选取,都能保证 aA + bB => aB + bA 符合规则。 + +综上,我们用二层循环,考察不同的首字母组合,假设分别是x和y,且{x}有m个元素,{y}有n个元素,两个集合的共同元素是k个。那么就有```(m-k)*(n-k)*2```种符合规则的配对。最终将26x26层循环得到的结果相加。 From 96b1600466863efdb7625e407e8676a8414a0abf Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年6月12日 01:51:46 -0700 Subject: [PATCH 18/27] Update 2301.Match-Substring-After-Replacement_KMP.cpp --- ....Match-Substring-After-Replacement_KMP.cpp | 73 +++++++++---------- 1 file changed, 35 insertions(+), 38 deletions(-) diff --git a/String/2301.Match-Substring-After-Replacement/2301.Match-Substring-After-Replacement_KMP.cpp b/String/2301.Match-Substring-After-Replacement/2301.Match-Substring-After-Replacement_KMP.cpp index 52c8348e7..b97918b32 100644 --- a/String/2301.Match-Substring-After-Replacement/2301.Match-Substring-After-Replacement_KMP.cpp +++ b/String/2301.Match-Substring-After-Replacement/2301.Match-Substring-After-Replacement_KMP.cpp @@ -1,72 +1,69 @@ class Solution { - bool table[256][256]; + // unordered_map>>Map; // 't'-> {'7','8'} + bool table[128][128]; + public: + bool match(char x, char y) + { + return (x==y || table[y][x]); + } + bool matchReplacement(string s, string sub, vector>& mappings) { - int m = s.size(); - for (auto x: mappings) - { table[x[0]][x[1]] = 1; - } - return strStr(s, sub)!=-1; + return strStr(s, sub)!= -1; } - - bool equal(char a, char b) - { - return (a==b || table[b][a]); - } - + int strStr(string haystack, string needle) { - string s = haystack; - string p = needle; - int n = s.size(); - int m = p.size(); - + int n = haystack.size(); + int m = needle.size(); if (m==0) return 0; - if (n==0) return -1; + if (n==0) return -1; + + vector suf = preprocess(needle); vectordp(n,0); - dp[0] = equal(s[0], p[0]); + dp[0] = match(haystack[0], needle[0]); if (m==1 && dp[0]==1) return 0; - - vectorsuffix = preprocess(p); - + for (int i=1; i0 && !equal(s[i], p[j])) - j = suffix[j-1]; - dp[i] = j + equal(s[i], p[j]); - - if (dp[i]==p.size()) - return i - p.size() + 1; + while (j>0 && !match(haystack[i], needle[j])) + j = suf[j-1]; + dp[i] = j + match(haystack[i], needle[j]); + if (dp[i]==needle.size()) + return i-needle.size()+1; } - return -1; - } - vector preprocess(string s) + bool equal2(char x, char y) { - int n = s.size(); - vectordp(n); - dp[0] = 0; + if (x==y) return true; + for (int i=0; i<128; i++) + if (table[x][i]==table[y][i]) + return true; + return false; + } + vector preprocess(string s) + { + int n = s.size(); + vectordp(n,0); for (int i=1; i=1 && !equal(s[j],s[i])) + while (j>=1 && !equal2(s[j],s[i])) { j = dp[j-1]; } - dp[i] = j + equal(s[j], s[i]); + dp[i] = j + equal2(s[j],s[i]); } - return dp; } }; From 8001bc69c15ed5543fb578cfc837f786f9c5b7c8 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年6月12日 02:15:31 -0700 Subject: [PATCH 19/27] Update 2301.Match-Substring-After-Replacement_KMP.cpp --- .../2301.Match-Substring-After-Replacement_KMP.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/String/2301.Match-Substring-After-Replacement/2301.Match-Substring-After-Replacement_KMP.cpp b/String/2301.Match-Substring-After-Replacement/2301.Match-Substring-After-Replacement_KMP.cpp index b97918b32..988f77bf5 100644 --- a/String/2301.Match-Substring-After-Replacement/2301.Match-Substring-After-Replacement_KMP.cpp +++ b/String/2301.Match-Substring-After-Replacement/2301.Match-Substring-After-Replacement_KMP.cpp @@ -3,7 +3,7 @@ class Solution { bool table[128][128]; public: - bool match(char x, char y) + bool equal(char x, char y) { return (x==y || table[y][x]); } @@ -26,16 +26,16 @@ class Solution { vector suf = preprocess(needle); vectordp(n,0); - dp[0] = match(haystack[0], needle[0]); + dp[0] = equal(haystack[0], needle[0]); if (m==1 && dp[0]==1) return 0; for (int i=1; i0 && !match(haystack[i], needle[j])) + while (j>0 && !equal(haystack[i], needle[j])) j = suf[j-1]; - dp[i] = j + match(haystack[i], needle[j]); + dp[i] = j + equal(haystack[i], needle[j]); if (dp[i]==needle.size()) return i-needle.size()+1; } From 8ab1883e7963faf3a701c2ede86f33a35047a577 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年6月12日 02:16:46 -0700 Subject: [PATCH 20/27] Update Readme.md --- String/2301.Match-Substring-After-Replacement/Readme.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/String/2301.Match-Substring-After-Replacement/Readme.md b/String/2301.Match-Substring-After-Replacement/Readme.md index 50da0a046..8fb5b111f 100644 --- a/String/2301.Match-Substring-After-Replacement/Readme.md +++ b/String/2301.Match-Substring-After-Replacement/Readme.md @@ -4,4 +4,8 @@ 本题暴力查验字符串匹配,时间是o(N^2),有AC的可能。 #### 解法2:KMP -本题的本质其实就是在一个字符串中查找匹配的子串。最直观的高效解法就是KMP。我们只需要略微修改KMP算法中关于"两个字符相等"的定义。定义一个新的```equal(char a, char b)```. 当两个字符相等,或者sub的字符可以映射到s的字符中时,就返回true。这样KMP就可以直接用于此题。 +本题的本质其实就是在一个字符串中查找匹配的子串。最直观的高效解法就是KMP。我们只需要略微修改KMP算法中关于"两个字符相等"的定义。 + +在KMP的主函数中,定义一个新的```equal(char a, char b)```. 当两个字符相等,或者sub的字符可以映射到s的字符中时,就返回true。 + +在KMP的preprocessing函数中,定义一个新的```equal2(char a, char b)```. 当两个字符相等,或者这两个字符都可以映射到同一个字符时,就返回true。 From c8a49b6ef6a59b185d004ccccba2c94114b6a920 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年6月12日 16:38:17 -0700 Subject: [PATCH 21/27] Create 2305.Fair-Distribution-of-Cookies_v1.cpp --- .../2305.Fair-Distribution-of-Cookies_v1.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 DFS/2305.Fair-Distribution-of-Cookies/2305.Fair-Distribution-of-Cookies_v1.cpp diff --git a/DFS/2305.Fair-Distribution-of-Cookies/2305.Fair-Distribution-of-Cookies_v1.cpp b/DFS/2305.Fair-Distribution-of-Cookies/2305.Fair-Distribution-of-Cookies_v1.cpp new file mode 100644 index 000000000..fb48bad53 --- /dev/null +++ b/DFS/2305.Fair-Distribution-of-Cookies/2305.Fair-Distribution-of-Cookies_v1.cpp @@ -0,0 +1,29 @@ +class Solution { + int ret = INT_MAX; + int plan[8]; +public: + int distributeCookies(vector& cookies, int k) + { + dfs(cookies, k, 0, 0); + return ret; + } + + void dfs(vector& cookies, int k, int idx, int count) + { + if (idx==cookies.size()) + { + int mx = 0; + for (int i=0; i Date: 2022年6月12日 16:38:38 -0700 Subject: [PATCH 22/27] Update Readme.md --- Readme.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index e8e83482e..3d70073d8 100644 --- a/Readme.md +++ b/Readme.md @@ -432,7 +432,6 @@ [959.Regions-Cut-By-Slashes](https://github.com/wisdompeak/LeetCode/tree/master/DFS/959.Regions-Cut-By-Slashes) (M+) [1306.Jump-Game-III](https://github.com/wisdompeak/LeetCode/tree/master/DFS/1306.Jump-Game-III) (M) [1718.Construct-the-Lexicographically-Largest-Valid-Sequence](https://github.com/wisdompeak/LeetCode/tree/master/DFS/1718.Construct-the-Lexicographically-Largest-Valid-Sequence) (H-) -[1723.Find-Minimum-Time-to-Finish-All-Jobs](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1723.Find-Minimum-Time-to-Finish-All-Jobs) (H-) [1766.Tree-of-Coprimes](https://github.com/wisdompeak/LeetCode/tree/master/DFS/1766.Tree-of-Coprimes) (H-) [2014.Longest-Subsequence-Repeated-k-Times](https://github.com/wisdompeak/LeetCode/tree/master/DFS/2014.Longest-Subsequence-Repeated-k-Times) (H) [2056.Number-of-Valid-Move-Combinations-On-Chessboard](https://github.com/wisdompeak/LeetCode/tree/master/DFS/2056.Number-of-Valid-Move-Combinations-On-Chessboard) (H) @@ -447,6 +446,8 @@ [1307.Verbal-Arithmetic-Puzzle](https://github.com/wisdompeak/LeetCode/tree/master/DFS/1307.Verbal-Arithmetic-Puzzle) (H) [1593.Split-a-String-Into-the-Max-Number-of-Unique-Substrings](https://github.com/wisdompeak/LeetCode/tree/master/DFS/1593.Split-a-String-Into-the-Max-Number-of-Unique-Substrings) (M) [1681.Minimum-Incompatibility](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1681.Minimum-Incompatibility) (H) +[1723.Find-Minimum-Time-to-Finish-All-Jobs](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1723.Find-Minimum-Time-to-Finish-All-Jobs) (H-) +[2305.Fair-Distribution-of-Cookies](https://github.com/wisdompeak/LeetCode/tree/master/DFS/2305.Fair-Distribution-of-Cookies) (H-) * ``memorization`` [329.Longest-Increasing-Path-in-a-Matrix](https://github.com/wisdompeak/LeetCode/tree/master/DFS/329.Longest-Increasing-Path-in-a-Matrix) (M) [638.Shopping-Offers](https://github.com/wisdompeak/LeetCode/tree/master/DFS/638.Shopping-Offers) (M+) From ca84944c2acb468cbdc19bd63c7a9292d114d9d2 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年6月12日 17:05:28 -0700 Subject: [PATCH 23/27] Create 2305.Fair-Distribution-of-Cookies_v2.cpp --- .../2305.Fair-Distribution-of-Cookies_v2.cpp | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 DFS/2305.Fair-Distribution-of-Cookies/2305.Fair-Distribution-of-Cookies_v2.cpp diff --git a/DFS/2305.Fair-Distribution-of-Cookies/2305.Fair-Distribution-of-Cookies_v2.cpp b/DFS/2305.Fair-Distribution-of-Cookies/2305.Fair-Distribution-of-Cookies_v2.cpp new file mode 100644 index 000000000..513e8c912 --- /dev/null +++ b/DFS/2305.Fair-Distribution-of-Cookies/2305.Fair-Distribution-of-Cookies_v2.cpp @@ -0,0 +1,44 @@ +class Solution { + int plan[8]; +public: + int distributeCookies(vector& cookies, int k) + { + sort(cookies.rbegin(), cookies.rend()); + + int left = 1, right = INT_MAX; + while (left < right) + { + for (int i=0; i& cookies, int limit, int k, int idx) + { + if (idx == cookies.size()) return true; + + int flag = 0; + for (int i=0; i limit) continue; + if (plan[i]==0) + { + if (flag==1) continue; + flag = 1; + } + + plan[i] += cookies[idx]; + if (dfs(cookies, limit, k, idx+1)) + return true; + plan[i] -= cookies[idx]; + } + return false; + } +}; From f9d645211d5d263aeb1f476c097b63fc0525f361 Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年6月12日 17:39:01 -0700 Subject: [PATCH 24/27] Update 2305.Fair-Distribution-of-Cookies_v2.cpp --- .../2305.Fair-Distribution-of-Cookies_v2.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/DFS/2305.Fair-Distribution-of-Cookies/2305.Fair-Distribution-of-Cookies_v2.cpp b/DFS/2305.Fair-Distribution-of-Cookies/2305.Fair-Distribution-of-Cookies_v2.cpp index 513e8c912..66b32448a 100644 --- a/DFS/2305.Fair-Distribution-of-Cookies/2305.Fair-Distribution-of-Cookies_v2.cpp +++ b/DFS/2305.Fair-Distribution-of-Cookies/2305.Fair-Distribution-of-Cookies_v2.cpp @@ -20,24 +20,24 @@ class Solution { return left; } - bool dfs(vector& cookies, int limit, int k, int idx) + bool dfs(vector& cookies, int limit, int k, int curCookie) { - if (idx == cookies.size()) return true; + if (curCookie == cookies.size()) return true; int flag = 0; for (int i=0; i limit) continue; + if (plan[i]+cookies[curCookie]> limit) continue; if (plan[i]==0) { if (flag==1) continue; flag = 1; } - plan[i] += cookies[idx]; - if (dfs(cookies, limit, k, idx+1)) + plan[i] += cookies[curCookie]; + if (dfs(cookies, limit, k, curCookie+1)) return true; - plan[i] -= cookies[idx]; + plan[i] -= cookies[curCookie]; } return false; } From fea92233afb28326bb996a30084f0ec642c7618a Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年6月12日 17:43:35 -0700 Subject: [PATCH 25/27] Create 2305.Fair-Distribution-of-Cookies_v3.cpp --- .../2305.Fair-Distribution-of-Cookies_v3.cpp | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 DFS/2305.Fair-Distribution-of-Cookies/2305.Fair-Distribution-of-Cookies_v3.cpp diff --git a/DFS/2305.Fair-Distribution-of-Cookies/2305.Fair-Distribution-of-Cookies_v3.cpp b/DFS/2305.Fair-Distribution-of-Cookies/2305.Fair-Distribution-of-Cookies_v3.cpp new file mode 100644 index 000000000..f8740e76b --- /dev/null +++ b/DFS/2305.Fair-Distribution-of-Cookies/2305.Fair-Distribution-of-Cookies_v3.cpp @@ -0,0 +1,52 @@ +class Solution { + int plan[8]; +public: + int distributeCookies(vector& cookies, int k) + { + sort(cookies.rbegin(), cookies.rend()); + int n = cookies.size(); + + int left = 1, right = INT_MAX; + while (left < right) + { + for (int i=0; i& cookies, int limit, int k, int curPerson, int state) + { + if (curPerson == k) + { + return state == 0; + } + + for (int subset=state; subset>0; subset=(subset-1)&state) + { + int sum = getSum(cookies, subset); + if (sum> limit) continue; + if (dfs(cookies, limit, k, curPerson+1, state-subset)) + return true; + }; + + return false; + } + + int getSum(vector& cookies, int state) + { + int ret = 0; + for (int i=0; i>i)&1) + ret += cookies[i]; + } + return ret; + } +}; From d1c6f833c540033b34ddcbe5f96b476a7d342f5b Mon Sep 17 00:00:00 2001 From: wisdompeak Date: 2022年6月12日 17:47:28 -0700 Subject: [PATCH 26/27] Update 2305.Fair-Distribution-of-Cookies_v1.cpp --- .../2305.Fair-Distribution-of-Cookies_v1.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/DFS/2305.Fair-Distribution-of-Cookies/2305.Fair-Distribution-of-Cookies_v1.cpp b/DFS/2305.Fair-Distribution-of-Cookies/2305.Fair-Distribution-of-Cookies_v1.cpp index fb48bad53..10cb2bbc1 100644 --- a/DFS/2305.Fair-Distribution-of-Cookies/2305.Fair-Distribution-of-Cookies_v1.cpp +++ b/DFS/2305.Fair-Distribution-of-Cookies/2305.Fair-Distribution-of-Cookies_v1.cpp @@ -4,13 +4,13 @@ class Solution { public: int distributeCookies(vector& cookies, int k) { - dfs(cookies, k, 0, 0); + dfs(cookies, k, 0); return ret; } - void dfs(vector& cookies, int k, int idx, int count) + void dfs(vector& cookies, int k, int curCookie) { - if (idx==cookies.size()) + if (curCookie == cookies.size()) { int mx = 0; for (int i=0; i Date: 2022年6月12日 17:59:07 -0700 Subject: [PATCH 27/27] Create Readme.md --- DFS/2305.Fair-Distribution-of-Cookies/Readme.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 DFS/2305.Fair-Distribution-of-Cookies/Readme.md diff --git a/DFS/2305.Fair-Distribution-of-Cookies/Readme.md b/DFS/2305.Fair-Distribution-of-Cookies/Readme.md new file mode 100644 index 000000000..7d4fe9c47 --- /dev/null +++ b/DFS/2305.Fair-Distribution-of-Cookies/Readme.md @@ -0,0 +1,17 @@ +### 2305.Fair-Distribution-of-Cookies + +#### 解法1:常规dfs,遍历cookie +通过DFS遍历所有的分配方案。dfs的每一层处理一块cookie,分支考察分配给每个人的方案。总的时间复杂度就是o(k^N). + +#### 解法2:二分+dfs,遍历cookie +我们先二分搜索猜测一个答案t,然后用dfs来寻找是否存在一种分配方案,使得每个人能分到的饼干数量不超过t。最终二分逼近的答案就是所求的最优解。 + +dfs的原理同解法1. 此时,我们可以有很多剪枝策略: +1. 发现任何一个人的饼干总数已经大于t,就返回false +2. 将cookies从大到小排列,尽早排除那些容易溢出的分支。 +3. 如果某块饼干打算分发给某个没有得到饼干的人,那么就不需要平行地尝试分给其他没有得到饼干的人。 + +#### 解法3:二分+dfs,遍历人 +dfs的原理正好相反:每一层处理一个人,对于该人的饼干选配方案就是当前剩余饼干的子集。显然我们可以通过遍历子集的技巧,进行dfs的分支搜索。 + +同样,如果遍历子集时,发现某种分配方案会导致个人的饼干总数已经大于t,就终止这个探索。

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