8. 贪心

作者: superlj666 | 来源:发表于2017-04-15 11:30 被阅读0次

动态规划: 每一步都进行选择,依赖于子问题的解。通常使用自底向上求解,先求较小的子问题,然后是较大的子问题。
贪心: 每次找出局部最优解。

122. Best Time to Buy and Sell Stock II
可以无限次买入卖出
最优子结构:res += max(0, prices[i] - prices[i-1])
class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int res = 0;
        for (auto i = 1; i < prices.size(); ++i) 
            res += max(0, prices[i] - prices[i-1]);
        return res;
    }
};

55. Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
For example:
A = [2,3,1,1,4], return true.
A = [3,2,1,0,4], return false.
求是否能到达数组的最后位置。
class Solution {
public:
    bool canJump(vector<int>& nums) {
        int reach = 0;
        for (auto i = 0; i != nums.size() && i <= reach; ++i) {
            reach = max(reach, nums[i] + i);
        }
        
        return reach >= nums.size() - 1;
    }
};

45. Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A = [2,3,1,1,4]
The minimum number of jumps to reach the last index is 2. (Jump 1 step from index 0 to 1, then 3 steps to the last index.)
Note:
You can assume that you can always reach the last index.
与44题相似,不过要求到达最后一个位置的最小跳数。
局部最优解:求出当前点能到达的最远距离,在该范围内count不更新。
class Solution {
public:
    int jump(vector<int>& nums) {
        int reach = 0, count = 0, next = 0;
        for (auto i = 0; i < nums.size() - 1; ++i) {
            reach = max(reach, i + nums[i]);
            if (i == next) {
                count++;
                next = reach;
            }
        }
        return count;
    }
}; 

134. Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station's index if you can travel around the circuit once, otherwise return -1.
Note:
The solution is guaranteed to be unique.
如果所有gas >= 所有cost,则结果存在;且起始点为剩余油量最少的下一个。
最优子结构:total = min(total, total + gas[i] - cost[i])。找出剩余油量最少的下一个。
int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
    int n = gas.size();
    int total = 0, subsum = INT_MAX, start = 0;
    for (int i = 0; i < n; ++i) {
        total += (gas[i] - cost[i]);
        if (total < subsum) {
            subsum = total;
            start = i + 1;
        }
    }
    
    return (total >= 0) ? start%n : -1;
}

135. Candy
There are N children standing in a line. Each child is assigned a rating value.
You are giving candies to these children subjected to the following requirements:
Each child must have at least one candy.
Children with a higher rating get more candies than their neighbors.
What is the minimum candies you must give?
分为如下步骤进行
  • 初始化为每个小孩都有1块糖
  • 从前往后遍历,保证如果后一个的rate大,则分的糖也比前一个多1个
  • 从后往前遍历,保证如果前一个的rate大,则分的糖取max(当前, 后一个+1)
最优子结构:左边比当前大,则左边+1。右边比当前大,则右边+1。
int candy(vector<int>& ratings) {
    if (ratings.size() <= 1) return ratings.size();
    
    int res = 0;
    vector<int> counts(ratings.size(), 1);
    for (auto i = 0; i < ratings.size() - 1; ++i) {
        if (ratings[i + 1] > ratings[i])
            counts[i + 1] = counts[i] + 1;
    }
    
    for (auto i = ratings.size() - 1; i > 0; --i) {
        if (ratings[i - 1] > ratings[i])
            counts[i - 1] = max(counts[i - 1], counts[i] + 1);
    }
    
    for (auto n:counts) 
        res += n;
    return res;
}

相关文章

  • 8. 贪心

    动态规划: 每一步都进行选择,依赖于子问题的解。通常使用自底向上求解,先求较小的子问题,然后是较大的子问题。 贪心...

  • 舍得

    贪心VS舍得,目标太多—贪心,什么都想得到—贪心,什么都想要美好—贪心,什么都想要舒服—贪心,没有付出就想要得到—...

  • 做人不能太贪心

    做人不能太贪心 嗯,做人不能太贪心 是的,做人不能太贪心 我发现自己有些贪心 我想要得到更多却又不想失去 别人的,...

  • 【算法打卡60天】Day29贪心算法:如何用贪心算法实现Huff

    Day29学习内容 :贪心算法:如何用贪心算法实现Huffman压缩编码? 1.如何理解贪心算法?贪心算法解决问题...

  • 【日更】一种喜欢

    看见,一种喜欢,莫名的贪恋。贪心遇见,贪心靠近,贪心嬉戏,贪心一切与她有关。恋,是心中物,是尘世花,是她眼中华,是...

  • 贪-赌

    贪心就会赌,赌就会因贪而输,不贪心就不会输,然不贪心就不会赌 ...

  • 我们无法阻止贪心生起,但我们可以觉知贪心 | 维安小参笔记@20

    我们无法阻止贪心,但是我们可以觉知贪心。只要我们有觉知,贪心就不可能完全占据我们的心。如果我们没有觉知,贪心将完全...

  • 红尘梦醒●贪心一念

    红尘梦醒●贪心一念 (2009.06.28) “贪心”,是的,我真的太过贪心了。 希望永远留住人世间美好的事物...

  • 贪心

    像我这样的人 活该孤独 因为太贪心 贪图一切爱我的 毁坏一切爱我的

  • 贪心

    纵使你有七分坏三分好 落我眼中三分已是全包括 不敢相信吧 理智被绑架 局促不安 是常态吗 越是喜欢就越不敢抬头看...

网友评论

    本文标题:8. 贪心

    本文链接:https://www.haomeiwen.com/subject/otynattx.html