力扣第 93 场双周赛---代码记录

T1 数组中字符串的最大值 ---- 签到

class Solution {
public:
    int maximumValue(vector<string>& vec) {
        int res = 0;
        for(auto s : vec)
        {
            bool f = true;
            for(auto c : s)
            {
                if(!isdigit(c))
                {
                    f = false;
                    break;
                }
            }
            if(f) res = max(res, stoi(s));
            else res = max(res, (int)s.size());
        }
        return res;
    }
};

T2 图中最大星和—签到

class Solution {
public:
    vector<int> g[100010];
    int maxStarSum(vector<int>& val, vector<vector<int>>& ed, int k) {
        int res = INT_MIN;
        int n = val.size();
        for(auto e : ed)
        {
            g[e[0]].push_back(e[1]);
            g[e[1]].push_back(e[0]);
        }
        int sum;
        for(int i = 0; i < n; i ++)
        {
            sum = val[i];
            sort(g[i].begin(), g[i].end(), [&](int a,int b){
                return val[a] > val[b];
            });
            for(int j = 0; j < min(k, (int)g[i].size()); j ++)
            {
                if(val[g[i][j]] < 0) break;
                sum += val[g[i][j]];
            } 
            res = max(res, sum);
        }
        return res;
    }
};

青蛙过河 II—二分/贪心

贪心写法
class Solution {
public:
    int maxJump(vector<int>& a) {
        int n = a.size();
        int res = a[1] - a[0];
        for(int i = 0; i + 2 < n; i ++) 
            res = max(res, a[i + 2] - a[i]);
        return res;
    }
};
二分写法
class Solution {
public:
    int maxJump(vector<int>& a) {
        int n = a.size();
        vector<bool> st(n, 0);
        function<bool(int)> check = [&](int len)->bool
        {
            for(int i = 0; i < n; i ++) st[i] = 0;
            
            int j = 0;
            for(int i = 1; i < n; i ++)
            {
                if(j + 1 < n && a[j + 1] - a[j] > len) return false;
                if(a[i] - a[j] > len)
                {
                    j = i - 1;
                    st[i - 1] = true;
                }
            }
            
            if(a[n - 1] - a[j] > len) return false;
            vector<int> vec;
            
            for(int i = 0; i < n; i ++)
                if(!st[i]) vec.push_back(a[i]);
            j = 0;
            for(int i = 1; i < vec.size(); i ++)
            {
                if(j + 1 < vec.size() && vec[j + 1] - vec[j] > len) return false;
                if(vec[i] - vec[j] > len)
                {
                    j = i - 1;
                    st[i - 1] = true;
                } 
            }
            
            if(vec.back() - vec[j] > len) return false;
            
            return true;
        };
        
        int l = 1, r = 1e9;
        while(l < r)
        {
            int mid = l + r >> 1;
            if(check(mid)) r = mid;
            else l = mid + 1;
        }
        return r;
    }
};

T4 让数组不相等的最小总代价 — 思维

class Solution {
public:
    long long minimumTotalCost(vector<int>& nums1, vector<int>& nums2) {
        int n = nums1.size();
        vector<int> vec;
        for(int i = 0; i < n; i ++)
            if(nums1[i] == nums2[i]) 
                vec.push_back(i);
        
        int maxval = -1, maxcnt = 0;
        vector<int> cnt(n + 1, 0);
        for(auto i : vec)
        {
            cnt[nums1[i]] ++;
            if(cnt[nums1[i]] > maxcnt)
            {
                maxcnt = cnt[nums1[i]];
                maxval = nums1[i];
            }
        }
        for (int i = 0; i < n; i++) 
        {
            if (maxcnt * 2 <= vec.size()) return sum(vec);
            if(nums1[i] == nums2[i]) continue;
            if (nums1[i] != maxval && nums2[i] != maxval) vec.push_back(i);
        }
        if (maxcnt * 2 <= vec.size()) return sum(vec);
        return -1; 

    }
    long long sum(const vector<int> &vec) {
        long long res = 0;
        for(auto x : vec) res += x;
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_WAWA鱼_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值