【LC刷题】DAY24:122 55 45 1005

122. 买卖股票的最佳时机 II

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int result = 0;
        for(int i = 1; i < prices.size(); i ++){
            result += max(prices[i] - prices[ i - 1], 0);
        }
        return result;
    }
};

55. 跳跃游戏 link

class Solution {
public:
    bool canJump(vector<int>& nums) {
        int cover = 0;
        if(nums.size() == 1) return true;
        for(int i = 0; i <= cover; i ++){
            cover = max(i + nums[i], cover);
            if(cover >= nums.size() - 1)return true;
        }
        return false;
    }
};

45. 跳跃游戏 II link

class Solution {
public:
    int jump(vector<int>& nums) {
        if(nums.size() == 1) return 0;
        int curDistance = 0;
        int ans = 0;
        int nextDistance = 0;
        for(int i = 0; i < nums.size(); i ++){
            nextDistance = max(nums[i] + i,  nextDistance);
            if(i == curDistance){
                ans ++;
                curDistance = nextDistance;
                if(nextDistance >= nums.size() - 1)  break;
            }
        }
        return ans;
    }
};

1005. K 次取反后最大化的数组和 link

class Solution {
public:
    static bool cmp(int a, int b){
        return abs(a) > abs(b);
    }
    int largestSumAfterKNegations(vector<int>& nums, int k) {
        sort(nums.begin(), nums.end(), cmp);
        for(int i = 0; i < nums.size(); i ++){
            if(nums[i] < 0 && k > 0){
                nums[i] *= -1;
                k --;
            }
        }
        if(k % 2 == 1) nums[nums.size() -1] *= -1;
        int result = 0;
        for(int a: nums) result += a;
        return result;
    }
};

相关推荐

  1. LCDAY02:24 19 142

    2024-07-14 05:36:02       22 阅读
  2. LCDAY03:242 349 202 1

    2024-07-14 05:36:02       30 阅读
  3. LCDAY07:344 541 54

    2024-07-14 05:36:02       30 阅读
  4. LCDAY08:151 55 28 459

    2024-07-14 05:36:02       34 阅读
  5. LCDAY24:122 55 45 1005

    2024-07-14 05:36:02       30 阅读
  6. 二分查找算法记录 -LC34

    2024-07-14 05:36:02       34 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-14 05:36:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-14 05:36:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-14 05:36:02       58 阅读
  4. Python语言-面向对象

    2024-07-14 05:36:02       69 阅读

热门阅读

  1. Qt/QML学习-BusyIndicator

    2024-07-14 05:36:02       22 阅读
  2. 算法热门面试题二

    2024-07-14 05:36:02       29 阅读
  3. pyinstaller系列教程(一)-基础介绍

    2024-07-14 05:36:02       20 阅读
  4. 大语言模型系列-Transformer

    2024-07-14 05:36:02       25 阅读
  5. Layer2是什么?为什么需要Layer2?

    2024-07-14 05:36:02       25 阅读
  6. SpinalHDL之实用工具(上篇)

    2024-07-14 05:36:02       22 阅读