首页 > 技术文章 > 力扣 第 208 场周赛

Crossea 2020-09-27 17:11 原文

用栈简单模拟就可以

class Solution {
public:
    int minOperations(vector<string>& logs) {
        stack<int> st;
        for(auto s:logs){
            if(s[0]=='.'&&s[1]=='.'){
                if(st.size()) st.pop();
            }
            else if(s[0]=='.'){
               
            }else{
                st.push(1);
            }
        }
        return st.size();
    }
};

阅读理解题
直接对转动次数进行模拟即可

class Solution {
public:
    int minOperationsMaxProfit(vector<int>& a, int b, int c) {
        int ans = -1;
        int used = 0;
        int wait = 0;
        int temp = -1;
        int res = -1;
        for(int i=0; i<a.size(); i++){
            used += min(4, a[i]+wait);
            wait += a[i]- min(4, a[i]+wait);
            temp = ans;
            ans = max(ans, used*b-c*(i+1));
            if(ans>temp){
                res = i+1;
                temp = ans;
            }
        }
        int k = a.size();
        while(wait>0){
            used += min(4, wait);
            wait -= min(4, wait);
            temp = ans;
            ans = max(ans, used*b-c*(++k));
            if(ans>temp){
                res = k;
                temp = ans;
            }
        }
        return res;
    }
};

利用两个哈希表分别记录家族成员
以及去世的成员,统计皇位继承顺序
的时候直接dfs即可,因为最多进行
十次继承查询

class ThroneInheritance {
public:
    unordered_map<string, vector<string> > mp;
    unordered_map<string, int> dead;
    string k;
    vector<string> ans;
    ThroneInheritance(string kingName) {
        mp.clear();
        dead.clear();
        k = kingName;
    }
    
    void birth(string a, string b) {
        mp[a].push_back(b);
        
    }
    
    void death(string name) {
        dead[name]=1;
    }
    void dfs(string & name){
       if(!dead[name])
            ans.push_back(name);
        for(string s:mp[name]){
            dfs(s);
        }
    }
    vector<string> getInheritanceOrder() {
        ans.clear();
        dfs(k);
        return ans;
    }
};

/**
 * Your ThroneInheritance object will be instantiated and called as such:
 * ThroneInheritance* obj = new ThroneInheritance(kingName);
 * obj->birth(parentName,childName);
 * obj->death(name);
 * vector<string> param_3 = obj->getInheritanceOrder();
 */


可以发现换楼申请最多才16个
于是直接子集枚举

class Solution {
public:
    int maximumRequests(int n, vector<vector<int>>& a) {
        vector<int> in(n+1,0);
        int ans = 0;
        int cnt = 0;
        bool flag = true;
        for(int i=1; i<=(1<<a.size())-1; i++){
            in = vector<int> (n+1,0);
            flag = true;
            cnt = 0;
            for(int j=0; j<a.size(); j++){
                if(i&(1<<j)){
                    cnt++;
                    in[a[j][1]]++;
                    in[a[j][0]]--;
                }
            }
            for(int j=0; j<n; j++){
                if(in[j]!=0){
                    flag = false;
                        break;
                }
            }
            if(flag)
                ans = max(ans, cnt);
        }
        return ans;
    }
};

推荐阅读