【LeetCode Contest 142.】1093 - 1096

48 篇文章 1 订阅
18 篇文章 0 订阅

…周赛一道题都没出,先是晚上了半个点。然后1093读错题,mode疯狂猜想是取模,原来是众数。然后1094以为是树状数组或者并查集,原来只是一个模拟。后两道没看,比赛就过去了。过去了。去了。了。
卒。

1093. Statistics from a Large Sample

对0 to 256的数进行采样,每个数字可能采样 0 to 1 0 9 10^{9} 109次,求出采样数据的最小值,最大值,平均数,中位数,众数。
关键在中位数。先求出总的采样数,然后再遍历一次采样数组,找到中位数位置的数。

1094. Car Pooling

顺风车,只能朝当前方向往前开。中途站点有上车和下车,问能否完成给出的全部trip。
用map记录下车地点和人数,每次有人上车的时候,先把map里的小于当前地点的人下车,然后承载。

1095. Find in Mountain Array

给了一个 Mounatain Array,求出给定值的最小索引。array只有两个函数getIndex() 和getLength(),注意如果getIndex()次数超过100次也会判 WA。
三个二分。先求出峰值的索引,然后在第一段和第二段数组里分别二分找目标值。
用map记录每次找过的值。

/**
 * // This is the MountainArray's API interface.
 * // You should not implement it, or speculate about its implementation
 * class MountainArray {
 *   public:
 *     int get(int index);
 *     int length();
 * };
 */

class Solution {
public:
    int findInMountainArray(int target, MountainArray &mountainArr) {
        int n = mountainArr.length();
        int l = 0;
        int r = n-1;
        int m = 0;
        
        map<int, int> mp;
        
        while(l <= r) {
            int mid = (l + r) / 2;
            int b = (mp[mid] ? mp[mid] : mountainArr.get(mid));
            int a = -1;
            if (mid > 0) {
                a = (mp[mid-1] ? mp[mid-1] : mountainArr.get(mid-1));
                mp[mid-1] = a;
            }
            
            int c = -1;
            if (mid != n-1) {
                c = (mp[mid+1] ? mp[mid+1] : mountainArr.get(mid+1));
                mp[mid+1] = c;
            }
            
            mp[mid] = b;
            
            if ((mid == 0 || b > a) && (mid == n || b > c)) {
                m = mid;
                break;
            }
            else if ((mid == 0 || b > a) && (mid == n || b < c)) {
                l = mid + 1;
            }
            else if ((mid == n || b > c) && (mid == 0 || b < a)) {
                r = mid - 1;
            }
        }
        
        int ll = 0, lr = m;
       
        while(ll <= lr) {
            int mid = (ll + lr) / 2;
            int b = (mp[mid] ? mp[mid] : mountainArr.get(mid));
            mp[mid] = b;
            
            if (b == target) {
                return mid;
            }
            if (b > target) {
                lr = mid - 1;
            }
            if (b < target) {
                ll = mid + 1;
            }
        }
        
        int rl = m, rr = n-1;
        while(rl <= rr) {
            int mid = (rl + rr) / 2;
            int b = (mp[mid] ? mp[mid] : mountainArr.get(mid));
            mp[mid] = b;
            if (b == target) {
                return mid;
            }
            if (b > target) {
                rl = mid + 1;
            }
            if (b < target) {
                rr = mid - 1;
            }
        }
        return -1;
    }
};

1096. Brace Expansion II

一个模拟运算,我觉得好难。
看别人的代码都看了好久。还是有点迷糊。一上午就做了这一个题。
代码:

class Solution {
public:
    vector<string> braceExpansionII(string expression) {
        stack<pair<unordered_set<string>, unordered_set<string>>> stk;
        stk.push({unordered_set<string>(), unordered_set<string>({""})});
        
        for (auto c: expression) {
            if (c == '{') {
                stk.push({unordered_set<string>(), unordered_set<string>({""})});
            }
            else if (c == '}') {
                auto tmp = stk.top();
                stk.pop();
                for (auto s: tmp.second) tmp.first.insert(s);
                auto news = unordered_set<string>();
                for (auto s1: stk.top().second) {
                    for (auto s2: tmp.first) {
                        news.insert(s1 + s2);
                    }
                }
                stk.top().second = news;
            }
            else if (c == ',') {
                for (auto s: stk.top().second) {
                    stk.top().first.insert(s);
                }
                stk.top().second = unordered_set<string>({""});
            }
            else {
                auto news = unordered_set<string>();
                for (auto s: stk.top().second) {
                    news.insert(s + c);
                }
                stk.top().second = news;
            }
        }
        for (auto s: stk.top().second) stk.top().first.insert(s);
        vector<string> ans;
        for (auto s: stk.top().first) ans.push_back(s);
        sort(ans.begin(), ans.end());
        return ans;
    }
};

感觉自己像一个老年人。反应迟钝,思考缓慢。枯了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值