bool canPartition(vector<int>& nums) {
int sum=0;
for(int i=0;i<nums.size();i++) sum+=nums[i];
if(sum%2==1) return false;
int n=nums.size();
int f[sum/2+1];
memset(f,0,sizeof(f));
for(int i=1;i<=n;i++){
for(int j=sum/2;j>=nums[i-1];j--){
// cout<<j<<" "<<j-nums[i-1]<< " "<<sum/2<<endl;
f[j]=max(f[j],f[j-nums[i-1]]+nums[i-1]);
}
}
if(f[sum/2]==sum/2) return true;
else return false;
}
bool canPartition(vector<int>& nums) {
int sum=0;
for(int i=0;i<nums.size();i++) sum+=nums[i];
if(sum%2==1) return false;
int n=nums.size();
int f[sum/2+1];
memset(f,0,sizeof(f));
for(int i=1;i<=n;i++){
for(int j=sum/2;j>=nums[i-1];j--){
// cout<<j<<" "<<j-nums[i-1]<< " "<<sum/2<<endl;
f[j]=max(f[j],f[j-nums[i-1]]+nums[i-1]);
}
}
if(f[sum/2]==sum/2) return true;
else return false;
}
int coinChange(vector<int>& coins, int amount) {
int n=coins.size();
long f[amount+1];
memset(f,0,sizeof(f));
for(int j=0;j<=amount;j++)
f[j]=INT_MAX;
f[0]=0;
for(int i=1;i<=n;i++){
for(int j=coins[i-1];j<=amount;j++){
f[j]=min(f[j],f[j-coins[i-1]]+1);
}
}
if(f[amount]==INT_MAX) return -1;
return f[amount];
}
注意这里如果用二维想法->前i个表示体积前j个表示前j个种类是区别不了组合数的,结果和前i个表示种类前j个表示前j个体积一样!!!如果想表示组合数则要化为一位数组。若要用二维解题可以用--->f[i][j]:所有从前i个位置中选,且总体积恰好是j的所有方案。这种想法解题,但是这种会多出一维出来。
int res=0;
int combinationSum4(vector<int>& nums, int target) {
int n=nums.size();
nums.insert(nums.begin(),0);
int f[target+1];
memset(f,0,sizeof(f));
f[0]=1;
for(int i=1;i<=target;i++){
for(int j=1;j<=n;j++){
if(i>=nums[j]&&f[i] < INT_MAX - f[i - nums[j]]) f[i]+=f[i-nums[j]];
}
}
return f[target];
}
二维:
int combinationSum4(vector<int>& nums, int target) {
int n=nums.size();
nums.insert(nums.begin(),0);
int f[target+1][target+1];
memset(f,0,sizeof(f));
for(int i=0;i<=target;i++) f[i][0] =1;
for(int i=1;i<=target;i++){
for(int j=1;j<=target;j++){
for(int k=1;k<=n;k++){
if(j>=nums[k]&&f[i][j]<INT_MAX-f[i][j-nums[k]]) f[i][j]+=f[i][j-nums[k]];
}
}
}
return f[target][target];
}
int maxAreaOfIsland(vector<vector<int>>& grid) {
int res=0;
int n=grid.size(),m=grid[0].size();
int sum=0;
vector<vector<bool>> st(n,vector<bool>(m));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
sum=0;
if(st[i][j]||grid[i][j]==0) continue;
sum++;
st[i][j]=true;
dfs(i,j,sum,grid,st);
res=max(res,sum);
}
}
return res;
}
int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};
void dfs(int x,int y,int& sum,vector<vector<int>>grid,vector<vector<bool>>& st){
int n=grid.size(),m=grid[0].size();
for(int i=0;i<4;i++){
int X=x+dx[i],Y=y+dy[i];
if(X>=n||X<0||Y>=m||Y<0||st[X][Y]||grid[X][Y]==0) continue;
sum++;
st[X][Y]=true;
dfs(X,Y,sum,grid,st);
}
}
vector<int>color;
bool isBipartite(vector<vector<int>>& graph) {
for(int i=0;i<graph.size();i++) color.push_back(0);
bool flag=true;
for(int i=0;i<graph.size();i++){
if(!color[i]){
if(!dfs(i,1,graph)) return false;
}
}
return true;
}
bool dfs(int u,int c,vector<vector<int>> graph){
color[u]=c;
for(int i=0;i<graph[u].size();i++){
int j=graph[u][i];
if(!color[j]){
if(!dfs(j,3-c,graph)) return false;
}else if(color[j]==c) return false;
}
return true;
}
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
unordered_map<string ,int> dist;
unordered_map<string ,int> map;
wordList.push_back(beginWord);
for(int i=0;i<wordList.size();i++) map[wordList[i]]++;
int res=1;
queue<string> q;
q.push(beginWord);
dist[beginWord]=1;
while(q.size()){
auto cc=q.front();
q.pop();
string ori=cc;
for(int i=0;i<cc.size();i++){
string t=ori;
for(char c='a';c<='z';c++){
t[i]=c;
if(!dist[t]&&map[t]){
dist[t]=dist[ori]+1;
if(t==endWord) return dist[t];
q.push(t);
}
}
}
}
return 0;
}
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
unordered_map<string ,int> dist;
unordered_map<string ,int> map;
wordList.push_back(beginWord);
for(int i=0;i<wordList.size();i++) map[wordList[i]]++;
if(!map[endWord]) return 0;
int res=1;
queue<string> q;
q.push(endWord);
dist[endWord]=1;
while(q.size()){
auto cc=q.front();
q.pop();
string ori=cc;
for(int i=0;i<cc.size();i++){
string t=ori;
for(char c='a';c<='z';c++){
t[i]=c;
if(!dist[t]&&map[t]){
dist[t]=dist[ori]+1;
if(t==beginWord) return dist[t];
q.push(t);
}
}
}
}
return 0;
}
int openLock(vector<string>& deadends, string target) {
int res=0;
unordered_map<string,int> map;
unordered_map<string,int> dist;
for(int i=0;i<deadends.size();i++) map[deadends[i]]++;
if(map["0000"]) return -1;
if(target=="0000") return 0;
queue<string>q;
q.push("0000");
dist["0000"]=1;
while(q.size()){
auto cc=q.front();
q.pop();
string ori=cc;
for(int i=0;i<4;i++){
cc=ori;
if(cc[i]=='9') cc[i]='0';
else cc[i]++;
if(!map[cc]&&!dist[cc]){
dist[cc]=dist[ori]+1;
if(cc==target) return dist[cc]-1;
q.push(cc);
}
cc=ori;
if(cc[i]=='0') cc[i]='9';
else cc[i]--;
if(!map[cc]&&!dist[cc]){
dist[cc]=dist[ori]+1;
if(cc==target) return dist[cc]-1;
q.push(cc);
}
}
}
return -1;
}
vector<vector<int>> res;
int n;
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
if(graph.size()==0||graph[0].size()==0) return res;
n=graph.size()-1;
vector<int>xx;
xx.push_back(0);
dfs(0,graph,xx);
return res;
}
void dfs(int u ,vector<vector<int>> graph,vector<int> xx){
if(xx.back()==n){
res.push_back(xx);
return ;
}
for(int i=0;i<graph[u].size();i++){
xx.push_back(graph[u][i]);
dfs(graph[u][i],graph,xx);
xx.pop_back();
}
}
map<pair<string,string>,double> map;
unordered_map<string,bool> f;
vector<double>res;
vector<double> calcEquation(vector<vector<string>>& equations, vector<double>& values, vector<vector<string>>& queries) {
for(int i=0;i<equations.size();i++){
pair<string,string> pss,pssf;
pss.first=equations[i][0];pssf.first=equations[i][1];
pss.second=equations[i][1];pssf.second=equations[i][0];
map[pss]=values[i];
map[pssf]=1.0/values[i];
f[equations[i][0]]=true;
f[equations[i][1]]=true;
}
for(int i=0;i<queries.size();i++){
unordered_map<string,bool> st;
vector<string> u=queries[i];
string aa=u[0],bb=u[1];
int sz=res.size();
if(!f[aa]||!f[bb]) res.push_back(-1);
else if(aa==bb&&f[aa]) res.push_back(1);
else {
if(i==queries.size()-1) cout<<res.size()<<endl;
dfs(aa,bb,1,st);
if(sz==res.size()) res.push_back(-1);
}
}
return res;
}
int dfs(string aa,string bb,double xx,unordered_map<string,bool>& st){
if(aa==bb) {
res.push_back(xx);
return 1;
}
for(auto cc: map){
if(st[aa]) continue;
if(cc.first.first!=aa) continue;
st[aa]=true;
if(dfs(cc.first.second,bb,xx*cc.second,st)) return 1;
st[aa]=false;
}
return 0;
}
int n,m;
int cnt=0;
int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};
vector<vector<int>> dist;
vector<vector<int>> g;
int longestIncreasingPath(vector<vector<int>>& matrix) {
g=matrix;
if (matrix.empty()) return 0;
n=matrix.size(),m=matrix[0].size();
dist=vector<vector<int>>(n,vector<int>(m,0));
int res=1;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
res=max(res,dfs(i,j));
}
}
return res;
}
int dfs(int X,int Y){
if(!dist[X][Y]) dist[X][Y]=1;
else return dist[X][Y];
for(int i=0;i<4;i++){
int x=dx[i]+X,y=dy[i]+Y;
if(x<0||x>=n||y<0||y>=m||g[x][y]<=g[X][Y]) continue;
dist[X][Y]=max(dist[X][Y],dfs(x,y)+1);
}
return dist[X][Y];
}
vector<int> findOrder(int numCourses, vector<vector<int>>& p) {
unordered_map<int,vector<int>> map;
int n=p.size();
vector<int> d(numCourses);
int head=0;
int tt=-1,hh=0;
for(int i=0;i<n;i++){
int aa=p[i][0],bb=p[i][1];
d[aa]++;
map[bb].push_back(aa);
}
queue<int> q;vector<int> res;
for(int i=0;i<numCourses;i++){
if(!d[i]) q.push(i);
}
while(q.size()){
int cc=q.front();
q.pop();
res.push_back(cc);
for(int i=0;i<map[cc].size();i++){
int u=map[cc][i];
d[u]--;
if(!d[u]) q.push(u);
}
}
if(res.size()<numCourses) return res={};
return res;
}
unordered_map<char,string> map;
string alienOrder(vector<string>& words) {
int cnt=0;
int f[26]={0};
int d[26]={0};
queue<char> q;
string res;
for(int i=0,j=1;j<words.size();i++,j++){
int js=0;
for(int k=0;k<min(words[i].size(),words[j].size());k++){
if(words[i][k]==words[j][k]) {
js++;
continue;
}
char aa=words[i][k],bb=words[j][k];
if(map[bb].find(aa)==-1){
map[bb].push_back(aa),d[aa-'a']++;
break;
}
break;
}
if(js==words[j].size()&&words[i].size()>words[j].size()){
cout<<"xx";
return "";
}
}
for(int i=0;i<words.size();i++)
for(int j=0;j<words[i].size();j++)
f[words[i][j]-'a']++;
for(int i=0;i<26;i++){
if(d[i]==0&&f[i]) q.push(i+'a');
}
while(q.size()){
char cc=q.front();
q.pop();
res+=cc;
for(int i=0;i<map[cc].size();i++){
d[map[cc][i]-'a']--;
if(!d[map[cc][i]-'a']) q.push(map[cc][i]);
}
}
for(int i=0;i<26;i++)
if(f[i]) cnt++;
// cout<<res<<endl;
// cout<<cnt<<endl;
if(res.size()<cnt) return "";
reverse(res.begin(),res.end());
return res;
}
bool sequenceReconstruction(vector<int>& nums, vector<vector<int>>& p) {
unordered_map<int,vector<int>> map;
vector<int> d(nums.size()+1);
for(int i=0;i<p.size();i++){
for(int j=0,k=1;k<p[i].size();j++,k++){
int aa=p[i][j],bb=p[i][k];
map[aa].push_back(bb);
d[bb]++;
}
}
int q[10010];int tt=-1,hh=0;
for(int i=1;i<=nums.size();i++)
if(d[i]==0) q[++tt]=i;
int loc=0;
while(hh<=tt){
if(tt-hh>=1) return false;
int cc=q[hh++];
if(cc!=nums[loc++]) return false;
for(int i=0;i<map[cc].size();i++){
int j=map[cc][i];
d[j]--;
if(!d[j]) q[++tt]=j;
}
}
return true;
}
int res=0;
vector<bool>st;
vector<vector<int>> p;
int n,m;
int findCircleNum(vector<vector<int>>& pp) {
p=pp;
n=p.size(),m=p[0].size();
st=vector<bool>(n);
for(int i=0;i<n;i++){
if(!st[i]) res++, dfs(i);
}
return res;
}
void dfs(int u){
st[u]=true;
for(int i=0;i<n;i++){
if(p[u][i]==1&&u!=i&&!st[i]) dfs(i);
}
}
int n,m;
int res;
vector<int>p;
int find(int u){
if(p[u]!=u) p[u]=find(p[u]);
return p[u];
}
bool check(string aa,string bb){
if(aa==bb) return true;
vector<int> q;
for(int i=0;i<aa.size();i++){
if(aa[i]!=bb[i]) q.push_back(i);
}
if(q.size()!=2) return false;
return aa[q[0]]==bb[q[1]]&&aa[q[1]]==bb[q[0]];
}
int numSimilarGroups(vector<string>& strs) {
int n=strs.size(),m=strs[0].size();
res=n;
p=vector<int>(n);
for(int i=0;i<n;i++) p[i]=i;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(check(strs[i],strs[j])){
if(find(i)!=find(j)){
p[find(i)]=p[find(j)];
res--;
}
}
}
}
return res;
}
vector<int> p;
int find(int u){
if(p[u]!=u) p[u]=find(p[u]);
return p[u];
}
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
int n=edges.size();
p.resize(n+1);
for(int i=1;i<=n;i++) p[i]=i;
for(auto e:edges){
int aa=find(e[0]) ,bb=find(e[1]);
if(aa!=bb) p[aa]=bb;
else return e;
}
return {};
}
int longestConsecutive(vector<int>& p) {
set<int> q(p.begin(),p.end());
vector<int> nums(q.begin(),q.end());
int n=nums.size();
int res=0;
for(int i=0,j=0;i<n;i++){
// cout<<i<<endl;
int cnt=1;
while(i+1<n&&nums[i]+1==nums[i+1]) cnt++,i++;
res=max(res,cnt);
}
return res;
}