Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
567b9f0
Create 2301.Match-Substring-After-Replacement.cpp
wisdompeak Jun 11, 2022
d45c8b5
Update Readme.md
wisdompeak Jun 11, 2022
39d80e8
Delete 2301.Match-Substring-After-Replacement.cpp
wisdompeak Jun 11, 2022
3e5a965
Create 2301.Match-Substring-After-Replacement_KMP.cpp
wisdompeak Jun 11, 2022
bac9e06
Create 2301.Match-Substring-After-Replacement_Brute.cpp
wisdompeak Jun 11, 2022
75a1e52
Update Readme.md
wisdompeak Jun 11, 2022
cd2262c
Create Readme.md
wisdompeak Jun 11, 2022
421a39d
Update 028.Implement-strStr-KMP.cpp
wisdompeak Jun 11, 2022
2dbafb8
Update 028.Implement-strStr-KMP.cpp
wisdompeak Jun 12, 2022
170bf9b
Create 2302.Count-Subarrays-With-Score-Less-Than-K.cpp
wisdompeak Jun 12, 2022
01cf68e
Update Readme.md
wisdompeak Jun 12, 2022
033d237
Create Readme.md
wisdompeak Jun 12, 2022
d241782
Update Readme.md
wisdompeak Jun 12, 2022
851cfe3
Update Readme.md
wisdompeak Jun 12, 2022
4399e23
Create 2306.Naming-a-Company.cpp
wisdompeak Jun 12, 2022
6dd8991
Update Readme.md
wisdompeak Jun 12, 2022
41c78b8
Create Readme.md
wisdompeak Jun 12, 2022
96b1600
Update 2301.Match-Substring-After-Replacement_KMP.cpp
wisdompeak Jun 12, 2022
8001bc6
Update 2301.Match-Substring-After-Replacement_KMP.cpp
wisdompeak Jun 12, 2022
8ab1883
Update Readme.md
wisdompeak Jun 12, 2022
c8a49b6
Create 2305.Fair-Distribution-of-Cookies_v1.cpp
wisdompeak Jun 12, 2022
82c00f4
Update Readme.md
wisdompeak Jun 12, 2022
ca84944
Create 2305.Fair-Distribution-of-Cookies_v2.cpp
wisdompeak Jun 13, 2022
f9d6452
Update 2305.Fair-Distribution-of-Cookies_v2.cpp
wisdompeak Jun 13, 2022
fea9223
Create 2305.Fair-Distribution-of-Cookies_v3.cpp
wisdompeak Jun 13, 2022
d1c6f83
Update 2305.Fair-Distribution-of-Cookies_v1.cpp
wisdompeak Jun 13, 2022
dad302f
Create Readme.md
wisdompeak Jun 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
int ret = INT_MAX;
int plan[8];
public:
int distributeCookies(vector<int>& cookies, int k)
{
dfs(cookies, k, 0);
return ret;
}

void dfs(vector<int>& cookies, int k, int curCookie)
{
if (curCookie == cookies.size())
{
int mx = 0;
for (int i=0; i<k; i++)
mx = max(mx, plan[i]);
ret = min(ret, mx);
return;
}

for (int i=0; i<k; i++)
{
plan[i]+=cookies[curCookie];
dfs(cookies, k, curCookie+1);
plan[i]-=cookies[curCookie];
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Solution {
int plan[8];
public:
int distributeCookies(vector<int>& cookies, int k)
{
sort(cookies.rbegin(), cookies.rend());

int left = 1, right = INT_MAX;
while (left < right)
{
for (int i=0; i<k; i++)
plan[i] = 0;

int mid = left+(right-left)/2;
if (dfs(cookies, mid, k, 0))
right = mid;
else
left = mid+1;
}
return left;
}

bool dfs(vector<int>& cookies, int limit, int k, int curCookie)
{
if (curCookie == cookies.size()) return true;

int flag = 0;
for (int i=0; i<k; i++)
{
if (plan[i]+cookies[curCookie] > limit) continue;
if (plan[i]==0)
{
if (flag==1) continue;
flag = 1;
}

plan[i] += cookies[curCookie];
if (dfs(cookies, limit, k, curCookie+1))
return true;
plan[i] -= cookies[curCookie];
}
return false;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class Solution {
int plan[8];
public:
int distributeCookies(vector<int>& 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<k; i++)
plan[i] = 0;

int mid = left+(right-left)/2;
if (dfs(cookies, mid, k, 0, (1<<n)-1))
right = mid;
else
left = mid+1;
}
return left;
}

bool dfs(vector<int>& 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<int>& cookies, int state)
{
int ret = 0;
for (int i=0; i<cookies.size(); i++)
{
if ((state>>i)&1)
ret += cookies[i];
}
return ret;
}
};
17 changes: 17 additions & 0 deletions DFS/2305.Fair-Distribution-of-Cookies/Readme.md
Original file line number Diff line number Diff line change
@@ -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,就终止这个探索。
25 changes: 25 additions & 0 deletions Greedy/2306.Naming-a-Company/2306.Naming-a-Company.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using LL = long long;
class Solution {
public:
long long distinctNames(vector<string>& ideas)
{
vector<unordered_set<string>>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;
}
};
9 changes: 9 additions & 0 deletions Greedy/2306.Naming-a-Company/Readme.md
Original file line number Diff line number Diff line change
@@ -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层循环得到的结果相加。
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using LL = long long;
class Solution {
public:
long long countSubarrays(vector<int>& nums, long long k)
{
int n = nums.size();
nums.insert(nums.begin(), 0);
vector<LL>presum(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;
}
};
5 changes: 5 additions & 0 deletions Others/2302.Count-Subarrays-With-Score-Less-Than-K/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### 2302.Count-Subarrays-With-Score-Less-Than-K

根据```Count Subarrays by Element```的套路,我们不会用o(N^2)遍历数组。我们会尝试用o(N)遍历每个元素,考察它unique地对应了哪些数组。

因为这道题里的subarray并没有任何代表其特征的最大值、最小值之类的,所以我们可以考虑将每种subarray的最后一个元素作为代表。具体的说,如果nums[i]是符合条件的subarray的最后一个元素,那么这个subarray的起点可以在哪里?显然,长度越长,起点越靠前,权重和就越大,直至可能超过k。利用单调性,我们就能用二分搜索来确定该subarray的最大长度,即对应了有多少个符合条件的subarray。
8 changes: 6 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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+)
Expand Down Expand Up @@ -855,6 +856,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)
Expand Down Expand Up @@ -1094,6 +1096,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-)
Expand Down Expand Up @@ -1221,14 +1224,15 @@
* ``结论转移``
[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-)
[1856.Maximum-Subarray-Min-Product](https://github.com/wisdompeak/LeetCode/tree/master/Stack/1856.Maximum-Subarray-Min-Product) (M+)
[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+)
Expand Down
6 changes: 3 additions & 3 deletions String/028.Implement-strStr/028.Implement-strStr-KMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ class Solution {
vector<int> suf = preprocess(needle);

vector<int>dp(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; i<n; i++)
{
int j = dp[i-1];
while (j>0 && 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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
bool table[256][256];
public:
bool matchReplacement(string s, string sub, vector<vector<char>>& mappings)
{
int m = s.size();

for (auto x: mappings)
{
table[x[0]][x[1]] = 1;
}

for (int i=0; i<s.size(); i++)
{
int flag = true;
for (int j=0; j<sub.size(); j++)
{
if (i+j<m && (sub[j]==s[i+j] || table[sub[j]][s[i+j]]))
continue;

flag = false;
break;
}
if (flag) return true;
}

return false;

}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
class Solution {
// unordered_map<char, unordered_set<char>>>Map; // 't'-> {'7','8'}
bool table[128][128];

public:
bool equal(char x, char y)
{
return (x==y || table[y][x]);
}

bool matchReplacement(string s, string sub, vector<vector<char>>& mappings)
{
for (auto x: mappings)
table[x[0]][x[1]] = 1;

return strStr(s, sub)!= -1;
}

int strStr(string haystack, string needle)
{
int n = haystack.size();
int m = needle.size();
if (m==0) return 0;
if (n==0) return -1;

vector<int> suf = preprocess(needle);

vector<int>dp(n,0);
dp[0] = equal(haystack[0], needle[0]);
if (m==1 && dp[0]==1)
return 0;

for (int i=1; i<n; i++)
{
int j = dp[i-1];
while (j>0 && !equal(haystack[i], needle[j]))
j = suf[j-1];
dp[i] = j + equal(haystack[i], needle[j]);
if (dp[i]==needle.size())
return i-needle.size()+1;
}
return -1;
}

bool equal2(char x, char y)
{
if (x==y) return true;
for (int i=0; i<128; i++)
if (table[x][i]==table[y][i])
return true;
return false;
}

vector<int> preprocess(string s)
{
int n = s.size();
vector<int>dp(n,0);
for (int i=1; i<n; i++)
{
int j = dp[i-1];
while (j>=1 && !equal2(s[j],s[i]))
{
j = dp[j-1];
}
dp[i] = j + equal2(s[j],s[i]);
}
return dp;
}
};
Loading