8. 贪心


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

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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 案例描述: Gekko教授想横穿NorthDarkota州, 教授在起点带着两公升水, 在喝光水之前能滑行m英里....
    陈码工阅读 5,408评论 0 0
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,356评论 0 33
  • 分治方法 将问题划分成互不相交的子问题 递归地求解子问题 将子问题的解组合起来 动态规划(两个要素:最优子结构、子...
    superlj666阅读 3,490评论 0 0
  • 一 云朵常常盘旋在冰湖上空。 湖边坐着一只火红色的小狐狸,天天映着湖水看天上的云。 春去秋来,日夜交替。 小狐狸总...
    阿依嫫诗的月光阅读 2,187评论 0 1
  • 就这样吧
    浅鑶阅读 1,499评论 0 0

友情链接更多精彩内容