[leetcode] 最多可达成的换楼请求数目 暴力水题

Posted PushyTao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[leetcode] 最多可达成的换楼请求数目 暴力水题相关的知识,希望对你有一定的参考价值。

题目链接


二进制暴力枚举就行了:

class Solution 
public:
    int maximumRequests(int n, vector<vector<int>>& requests) 
        int cnt[21];
        int len = requests.size();
        int lim = (1 << len);
        int ans = 0;
        for(int i = 0;i < lim;i ++) 
            int t = i;
            int pos = 0;
            int tans = 0;
            while(t) 
                ++ pos;
                int x = requests[pos - 1][0], y = requests[pos - 1][1];
                if(t & 1) cnt[x] --,cnt[y] ++, tans ++;
                t >>= 1;
            
            int flag = 0;
            for(int i = 0;i < n;i ++) 
                if(cnt[i]) 
                    flag = 1;
                    cnt[i] = 0;
                
            
            if(flag == 0) ans = max(ans, tans);
        
        return ans;
    
;

以上是关于[leetcode] 最多可达成的换楼请求数目 暴力水题的主要内容,如果未能解决你的问题,请参考以下文章