Skip to content

1048-longest-string-chain problem #665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 22 additions & 0 deletions 1048-longest-string-chain/1048-longest-string-chain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
bool cmp(string &a, string &b){
return a.length() < b.length();
}
class Solution {
public:
int longestStrChain(vector<string>& words) {
sort(words.begin(), words.end(), cmp);
map<string, int> mp;
int ans = 0;
for(auto word: words){
int cur = 0;
for(int i=0;i<word.length(); i++){
string prev = word.substr(0,i)+word.substr(i+1);
cur = max(cur, mp[prev]+1);
}
mp[word] = cur;
ans = max(ans, cur);
}

return ans;
}
};
18 changes: 18 additions & 0 deletions Dynamic Programming/buy_and_sell_stock_iv.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {

int n = prices.size();
vector<int> profit_after_buying(k + 1, -1e9), profit_after_selling(k + 1, 0);

for (int i = 0; i < prices.size(); ++i) {
int cur_price = prices[i];
for (int j = k; j >= 1; --j) {
profit_after_buying[j] = max(profit_after_buying[j], profit_after_selling[j-1] - cur_price);
profit_after_selling[j] = max(profit_after_selling[j], profit_after_buying[j] + cur_price);
}
}
return profit_after_selling[k];
}

};