LeetCode周赛159

1232. Check If It Is a Straight Line

检查是否为直线,暴力算斜率,但要注意分母为0的情况

class Solution {
public:
    bool checkStraightLine(vector<vector<int>>& coordinates) {
        double n1 = coordinates[1][0] - coordinates[0][0];
        double n2 = coordinates[1][1] - coordinates[0][1];
        if (n2 == 0) {
            for (auto v : coordinates) {
                if (v[1] != coordinates[0][1])
                    return false;
            }
            return true;
        }
        double p = n1 / n2;
        for (int i = 1; i < coordinates.size(); i++) {
            double p1 = coordinates[i][0] - coordinates[i - 1][0];
            double p2 = coordinates[i][1] - coordinates[i - 1][1];
            double m = p1 / p2;
            if (abs(p - m) > 0.000005)
                return false;
        }
        return true;
    }
};

1233. Remove Sub-Folders from the Filesystem

删除子文件,即找出子串,删除对应串

class Solution {
public:
    vector<string> removeSubfolders(vector<string>& folder) 
    {
        sort(folder.begin(),folder.end());
        vector<string> result;
        for(string &s:folder)
            if(result.empty()||result.back().compare(0,result.back().length(),s,0,result.back().length())!=0||s[result.back().length()]!='/') 
        return result;
    }
};

1234. Replace the Substring for Balanced String

找出最小替代数可以使其平衡

 int balancedString(string s) {
        unordered_map<int, int> count;
        int n = s.length(), res = n, i = 0;
        for (int j = 0; j < n; ++j) {
            count[s[j]]++;
        }
        for (int j = 0; j < n; ++j) {
            count[s[j]]--;
            while (i < n && count['Q'] <= n / 4 && count['W'] <= n / 4 && count['E'] <= n / 4 && count['R'] <= n / 4) {
                res = min(res, j - i + 1);
                count[s[i++]] += 1;
            }
        }
        return res;
    }

1235. Maximum Profit in Job Scheduling

最大区间和

class Solution {
public:
    
   int jobScheduling(vector<int>& startTime, vector<int>& endTime, vector<int>& profit) {
       int vsize = startTime.size();
       vector<vector<int>> v(vsize, vector<int>(3));
  
       for (int i = 0; i < vsize; i++) {
           v[i][1] = startTime[i];
           v[i][0] = endTime[i];
           v[i][2] = profit[i];
          
       }
       sort(v.begin(), v.end());
       map<int,int> dp;
       dp[0] = 0;
       for (auto ve : v) {
           auto it = dp.upper_bound(ve[1]);
           it--;
           int cur = it->second + ve[2];
           if (cur > dp.rbegin()->second)
               dp[ve[0]] = cur;
       }
       return dp.rbegin()->second;
   }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值