1332. 删除回文子序列
1 | class Solution { |
1333. 餐厅过滤器
1 | bool cmp(vector<int>& a, vector<int>& b){ |
1334. 阈值距离内邻居最少的城市
1 | class Solution { |
1335. 工作计划的最低难度
1 | class Solution { |
1 | class Solution { |
1 | bool cmp(vector<int>& a, vector<int>& b){ |
1 | class Solution { |
1 | class Solution { |
1 | class Solution { |
1 | class Solution { |
1 | class TweetCounts { |
1 | class Solution { |
1 | class Solution { |
1 | class Solution { |
1 | /** |
1 | class Solution { |
1 | class Solution { |
1 | class Solution { |
1 | class Solution { |
1 | /** |
1 | class Solution { |
1 | class Solution { |
1 | class Solution { |
1 | class Solution { |
1 | class Solution { |
1 | class Solution { |
dfs莫名不过1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40class Solution {
public:
map<string, int> mp;
vector<int> visited;
void dfs(vector<vector<string>> &watchedVideos, vector<vector<int>> &friends, int id, int level){
if(visited[id] == 1) return;
if(level < 0) return;
if(level == 0){
for(int i=0; i<watchedVideos[id].size(); i++)
mp[watchedVideos[id][i]]++;
}
visited[id] = 1;
for(int i=0; i<friends[id].size(); i++){
dfs(watchedVideos, friends, friends[id][i], level-1);
}
}
vector<string> watchedVideosByFriends(vector<vector<string>>& watchedVideos, vector<vector<int>>& friends, int id, int level) {
visited.resize(friends.size());
visited[id] = 1;
for(int i=0; i<friends[id].size(); i++){
dfs(watchedVideos, friends, friends[id][i], level-1);
}
vector<pair<string, int>> ret1;
for(auto it = mp.begin(); it != mp.end(); it++){
ret1.push_back(*it);
}
sort(ret1.begin(), ret1.end(), cmp);
vector<string> ret;
for(auto it=ret1.begin(); it != ret1.end(); it++)
ret.push_back(it->first);
return ret;
}
static bool cmp(const pair<string, int>& a, const pair<string, int>& b)
{
if (a.second == b.second)
return a.first < b.first;
else
return a.second < b.second;
}
};
1 | class Solution { |
动态规划 用字符串长度减去最长回文子序列的长度就是最少插入次数了1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20class Solution {
public:
int minInsertions(string s) {
int n=s.size();
int dp[n][n];
memset(dp,0,sizeof(dp));
for(int i=n-1;i>=0;--i){
dp[i][i]=1;
for(int j=i+1;j<n;++j){
if(s[i]==s[j]){
dp[i][j]=dp[i+1][j-1]+2;
}
else{
dp[i][j]=max(dp[i+1][j],dp[i][j-1]);
}
}
}
return n-dp[0][n-1];
}
};
1 | class Solution { |
1 | /** |
1 | class Solution { |
1 | class Solution { |
1 | class Solution { |
1 | class Solution { |
1 | /** |
1 | typedef long long ll; |
1 | class Solution { |
1 | class Solution { |
1 | class Solution { |
1 | class Solution { |
Implement FreqStack, a class which simulates the operation of a stack-like data structure.
FreqStack has two functions:
push(int x), which pushes an integer x onto the stack.
pop(), which removes and returns the most frequent element in the stack.
If there is a tie for most frequent element, the element closest to the top of the stack is removed and returned.
1 | class FreqStack { |