zoukankan      html  css  js  c++  java
  • leetcode 第 195 场周赛

    map记录一波到过的点,不过应该用哈希表更快


    class Solution {
    public:
        bool isPathCrossing(string path) {
            map<pair<int, int>, int> mmp;
            int x=0,y=0;
            mmp[make_pair(0,0)] =1;
            for(auto e:path){
                if(e=='N')
                    y -= 1;
                if(e=='E')
                    x+=1;
                if(e=='S')
                    y+=1;
                if(e=='W')
                    x-=1;
                if(mmp[make_pair(x,y)])
                    return true;
                mmp[make_pair(x,y)]=1;
            }
            
            return false;
        }
    };
    

    这个和以前做过的一道题很像,通过取模把数值映射到0到k-1之前,直接配对,快速解决


    class Solution {
    public:
        bool canArrange(vector<int>& arr, int k) {
            vector<int> cnt(k,0);
            for(auto &a:arr){
                cnt[(a%k+k)%k]++;
            }
            if(cnt[0]%2)
                return false;
            for(int i=1; i<k; i++){
                if(cnt[i]!=cnt[k-i])
                    return false;
            }
            return true;
        }
    };
    

    这波血亏先是卡了直接吃桃去了,然后看错了题以为是连续的
    最后用快速幂加双指针,结果写错了判断条件,血亏,结束后用二分加快速幂做的
    还一直用lower_bound,今天真的是有点二

    typedef long long ll;
    ll m = 1e9+7;
    class Solution {
    public:
        ll quick_pow(ll x,ll n,ll m){
    	ll res = 1;
    	while(n > 0){
    		if(n & 1)	res = res * x % m;
    		x = x * x % m;
    		n >>= 1;
    	}
    	return res%m;
    }
    
        int numSubseq(vector<int>& nums, int target) {
            sort(nums.begin(), nums.end());
            ll ans = 0;
            for(int r = nums.size()-1; r>=0; r--){
                if(nums[r]+nums[0]>target)
                    continue;
                if(2*nums[r]<=target){
                    ans += quick_pow(2, r+1, m)-1;
                    break;
                }
                // 不能用lower_bound 因为值是会有重复的,用lower_bound会丢失重复的值
                auto pos = upper_bound(nums.begin(), nums.end(), target-nums[r]);
                int p  = distance(nums.begin(), pos);
                if(nums[r]+nums[p]>target) p-=1;
                ans += ((((quick_pow(2, p+1, m)-1)%m +m)%m)*(quick_pow(2, r-p-1, m)))%m;
                
            }
            return ans%m;
        }
    };
    

    第四题主要是看出来xj一定大于xi,然后把目标式子变换过来
    大佬都用的单调队列单调栈,我太菜了只会用multiset做,但是
    multiset竟然不是默认end是最大值,还得指定multiset<int,greater< int >>
    这样begin才是最大值,最近学的东西有点少,还是要努努力把荒废的时间补起来


    class Solution {
    public:
        int findMaxValueOfEquation(vector<vector<int>>& points,int k) {
            int ans = -2147483648;
            multiset<int, greater<int>> se;
            int i = 0;
            int l = 0;
            se.insert(points[0][1]-points[0][0]);
            for(i=1; i<points.size(); i++){
                while(l<i&&!se.empty()&&(points[i][0]-points[l][0])>k){
                    se.erase(se.find(points[l][1]-points[l][0]));
                    l++;
                }
                if(se.size()>0)
                    ans = max(ans, points[i][1]+points[i][0]+ (*se.begin()));
                se.insert(points[i][1]-points[i][0]);
            }
    
            return ans;
        }
    };
    
  • 相关阅读:
    排列组合
    动态规划-上台阶
    砝码称重3
    砝码称重2
    砝码称重1
    砝码称重
    TestDirector其他
    TestDirector域或工程用户的管理
    TestDirector创建域或工程
    LoadRunner8 安装步骤
  • 原文地址:https://www.cnblogs.com/Crossea/p/13202804.html
Copyright © 2011-2022 走看看