LeetCodeBinaryWeeklyContest-22

Rank: 609 / 2041 AC 2/4
第一次写双周赛,晚上写双周赛的人比周赛的还是少一些的…

第 22 场双周赛

1385. 两个数组间的距离值

数据范围比较小,暴力
时间复杂度 O ( m n ) O(mn) O(mn)

class Solution {
public:
    int findTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d) {
        int res = 0;
        for(int i=0;i<arr1.size();i++){
            int flag = 0;
            for(int j=0;j<arr2.size();j++){
                if(abs(arr2[j]-arr1[i])<=d) {
                    flag = 1;break;
                }
            }
            if(!flag) res++;
        }
        return res;
    }
};

1386. 安排电影院座位

参考:无脑查找=。=,方法一般般,好歹过了
这个思路很好,当时写的时候爆内存了…,一直没想到很好的方法来存。

class Solution {
public:
    int maxNumberOfFamilies(int n, vector<vector<int>>& reservedSeats) {
        unordered_map<int,unordered_set<int>> s;
        for(vector<int> e: reservedSeats){
            s[e[0]].insert(e[1]);
        }
        int res = 0;
        unordered_map<int,unordered_set<int>>::iterator it = s.begin();
        for(;it!=s.end();it++){
            int f = 0;
            if(!it->second.count(2)&&!it->second.count(3)&&!it->second.count(4)&&!it->second.count(5)) res++,f =1;
            if(!it->second.count(6)&&!it->second.count(7)&&!it->second.count(8)&&!it->second.count(9)) res++,f =1;
            if(!it->second.count(4)&&!it->second.count(5)&&!it->second.count(6)&&!it->second.count(7)&&!f) res++;   
        }
        return res + (n-s.size())*2;
    }
};

1387. 将整数按权重排序

其实如果这个题目不排序的话,那需要那一个map来存一下,然后求topk,时间复杂度下去了,但是空间复杂度会上来。

int fun(int x){
	int res = 0;
    while(x!=1){
    	if(x&1){
    		x = 3*x+1;
    	}
    	else {
    		x /= 2;
    	}
    	res++;
   }
   return res;
}
bool cmp(int a,int b){
    int ka = fun(a),kb = fun(b);
    if(ka!=kb) return ka < kb;
    else return a < b;
}
class Solution {
public:
    int getKth(int lo, int hi, int k) {
        vector<int> res;
        for(int i=lo;i<=hi;i++){
            res.push_back(i);
        }
        sort(res.begin(),res.end(),cmp);
        return res[k-1];        
    }
};

1388. 3n 块披萨

参考:3n 块披萨
可以将问题简化为一个无环的序列,然后计算从3n块披萨挑出互不相连的n块披萨,使和最大
dp[i][j]表示从前i块披萨中选出j块的披萨的最大和
d p [ i ] [ j ] = m a x ( d p [ i − 1 ] [ j ] , d p [ i − 2 ] [ j − 1 ] + s l i c e s [ i ] ) i = 1...3 n dp[i][j] = max(dp[i-1][j],dp[i-2][j-1]+slices[i]) \quad i=1...3n dp[i][j]=max(dp[i1][j],dp[i2][j1]+slices[i])i=1...3n
然后考虑环的问题,可以做两次dp操作,一次要头不要尾,另一个要尾不要头。返回两者的最大值。

class Solution {
public:
    int calc(vector<int>& slices){
        int n = slices.size(), choose = (n+1)/3,x = 0;
        int dp[n+1][choose+1];
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++){
            for(int j=1;j<=choose;j++){
                if(i>2) x = dp[i-2][j-1]; 
                else x = 0;
                dp[i][j] = max(dp[i-1][j],x+slices[i-1]);
            }
        }
        return dp[n][choose];
    }
    int maxSizeSlices(vector<int>& slices) {
        // f[i][j] 表示到从0到i,从中选出j个最大的
        // f[i][j] = max(f[i-1][j],f[i-2][j-1]+slices[i-2])
        vector<int> s1(slices.begin(),slices.end()-1);
        vector<int> s2(slices.begin()+1,slices.end());
        int ans1 = calc(s1), ans2 = calc(s2);
        return max(ans1,ans2);
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值