LeetCode 第 208 场周赛(模拟、模拟、模拟多叉树+树的先序遍历、回溯| 状态压缩)

1598. 文件夹操作日志搜集器

class Solution {
    
    
public:
    int minOperations(vector<string>& logs) {
    
    
        int ans = 0;
        for(string & s:logs){
    
    
            if(s[0]=='.' && s[1]=='.'){
    
    
                if(ans) ans--;
            }else if(s[0]!='.'){
    
    
                ans ++;
            }
        }
        return ans;
    }
};

1599. 经营摩天轮的最大利润
看懂题意就好。

class Solution {
    
    
public:
    vector<int> ans;
    int minOperationsMaxProfit(vector<int>& a, int bc, int rc) {
    
    
        ans =  {
    
    -1,100100};
        int cnt = 0, s = 0, n = a.size();
        
        int i = 0;
        // 需要把所有游客都送上去
        while(i<n || cnt>0){
    
    
            if(i<n){
    
    
                cnt += a[i];
                s += min(4,cnt)*bc-rc;
                if(s>ans[0]){
    
    
                    ans = {
    
    s,i+1};
                }
                if(cnt>4) cnt -= 4;   
                else cnt = 0;
                
            }else{
    
    
                if(cnt>4){
    
    
                    cnt -= 4;
                    s += 4*bc-rc;
                }else{
    
    
                    s += cnt*bc-rc;
                    cnt = 0;
                }
                if(s>ans[0]){
    
    
                    ans = {
    
    s,i+1};
                }
            }
            i++;
        }
        return ans[0]<0?-1:ans[1];
    }
};

1600. 皇位继承顺序
不用管那个递归函数,只需要知道所有人构成了一棵树,然后确定继承人的时候是遍历完一整棵子树才去遍历其他分支(实际上就是树的先序遍历)。

然后,用unordered_map<string,vector<string>>模拟树的结构就好(这种存储方式其实更像图的邻接数组存储)。

class ThroneInheritance {
    
    
    unordered_set<string> dead;
    unordered_map<string,vector<string> > mp;
public:
    string root;
    ThroneInheritance(string kingName) {
    
    
        root = kingName;
        mp[kingName] = {
    
    };
    }
    
    void birth(string parentName, string childName) {
    
    
        mp[parentName].push_back(childName);
    }
    
    void death(string name) {
    
    
        dead.insert(name);
    }
    
    vector<string> getInheritanceOrder() {
    
    
        vector<string> ans;
        dfs(root,ans);
        return ans;
    }
    void dfs(const string& s,vector<string>& vs){
    
    
        if(dead.find(s) == dead.end()){
    
    
            vs.push_back(s);
        }
        for(const string& cs:mp[s]){
    
    
            dfs(cs,vs);
        }
    }
};

1601. 最多可达成的换楼请求数目
看到某些大佬用的是网络流去做的。

  • 状态压缩+枚举子集
class Solution {
    
    
public:
    int maximumRequests(int m, vector<vector<int>>& a) {
    
    
        int n = a.size() , ans = 0 , lim = 1<<n;
        for(int s=0;s<lim;s++){
    
    
            int pos = 0,res = 0, ss = s;;
            int f[25] = {
    
    0};
            while(ss){
    
    
                if(ss&1){
    
    
                    res++;
                    f[a[pos][0]]--;
                    f[a[pos][1]]++;
                }
                ss>>=1;
                pos++;
            }
            bool flag = true;
            for(int i=0;i<m;i++){
    
    
                if(f[i]!=0) flag = false;
            }
            if(flag){
    
    
                ans = max(ans,res);
            }
        }
        return ans;
    }   
};
  • 回溯
class Solution {
    
    
public:
    int ans = 0;
    int f[25] = {
    
    0},n,size;
    int maximumRequests(int n, vector<vector<int>>& rs) {
    
    
        this->n = n;
        size = rs.size();
        dfs(0,0,rs);
        return ans;
    }
    
    void dfs(int pos,int cnt,const vector<vector<int>>& rs){
    
    
        if(pos==size){
    
    
            bool fg = true;
            for(int i=0;i<n;i++){
    
    
                if(f[i]!=0) fg = false;
            }
            if(fg) ans = max(ans,cnt);
            return;
        }
        dfs(pos+1,cnt,rs);
        f[rs[pos][0]]--;
        f[rs[pos][1]]++;
        dfs(pos+1,cnt+1,rs);
        f[rs[pos][0]]++;
        f[rs[pos][1]]--;        
    } 
};

猜你喜欢

转载自blog.csdn.net/qq_44846324/article/details/108855977
今日推荐