【 数据结构 & 算法 】—— 贪心算法

< 思维导图 >

预备知识:钞票支付问题(贪心法)

  • 预备知识_钞票支付.cpp
#include <stdio.h>

int main(){
    const int RMB[] = {100, 50, 20, 10, 5, 2, 1};
    const int NUM = 7;
    int X = 628;
    int count = 0;
    // 面值从大到小遍历 
    for (int i = 0; i < NUM; i++){
        int use = X / RMB[i];
        count += use;
        X = X - RMB[i] * use;
        printf("需要面额为 %d 的 %d 张, ", RMB[i], use);
        printf("还需要支付 RMB %d.\n", X);
    }
    printf("总共需要 %d 张 RMB\n", count);
    return 0;
}

分糖果(排序、贪心)(★)

  • LeetCode 455.Assign Cookies.cpp
#include <stdio.h>
#include <vector>
#include <algorithm>

class Solution {
public:
    // 需求因子数组 g,糖果大小数组 s 
    int findContentChildren(std::vector<int>& g, std::vector<int>& s) {
        // 对数组 g 排序 
        std::sort(g.begin(), g.end());
        // 对数组 s 排序 
        std::sort(s.begin(), s.end());
        int child = 0; // 满足孩子的个数 
        int cookie = 0; // 尝试糖果的个数 
        // 若两数组均未尝试完 
        while(child < g.size() && cookie < s.size()){
            // 若糖果满足孩子,指针 child 后移 
            if (g[child] <= s[cookie]){ 
                child++;
            }
            // 糖果只尝试一次,指针 cookie 后移 
            cookie++;
        }
        // 返回满足的孩子个数 
        return child;
    }
};

int main(){
    Solution solve;
    std::vector<int> g;
    std::vector<int> s;
    g.push_back(5);
    g.push_back(10);
    g.push_back(2);
    g.push_back(9);
    g.push_back(15);
    g.push_back(9);
    s.push_back(6);
    s.push_back(1);
    s.push_back(20);
    s.push_back(3);
    s.push_back(8); 
    printf("需求因子:");
    for(int i=0; i<g.size(); i++){
        printf("%d  ", g[i]);
    }
    printf("\n糖果大小:"); 
    for(int j=0; j<s.size(); j++){
        printf("%d  ", s[j]);
    }
    printf("\n最多能满足孩子个数:%d\n", solve.findContentChildren(g, s));
    return 0;
}

摇摆序列(贪心)(★★)

  • LeetCode 376.Wiggle Subsequence.cpp
#include <stdio.h>
#include <vector>

class Solution {
public:
    int wiggleMaxLength(std::vector<int>& nums) {
        // 序列个数小于 2 时,直接为摇摆序列 
        if (nums.size() < 2){
            return nums.size();
        }
        // 描述序列的三种状态 
        static const int BEGIN = 0;
        static const int UP = 1;
        static const int DOWN = 2;
        int STATE = BEGIN;
        // 序列个数至少为 1 
        int max_length = 1;
        for (int i = 1; i < nums.size(); i++){
            switch(STATE){ 
            case BEGIN:
                // 初始化状态   
                if (nums[i-1] < nums[i]){
                    STATE = UP;
                    max_length++;
                }
                // 初始化状态
                else if (nums[i-1] > nums[i]){
                    STATE = DOWN;
                    max_length++;
                }
                break;
            case UP:
                // 若状态切换,max_length++ ,否则不变 
                if (nums[i-1] > nums[i]){
                    STATE = DOWN;
                    max_length++;
                }
                break;
            case DOWN:
                // 若状态切换,max_length++ ,否则不变 
                if (nums[i-1] < nums[i]){
                    STATE = UP;
                    max_length++;
                }
                break;
            }
        }
        // 返回最长摇摆序列个数 
        return max_length;
    }
};

int main(){
    std::vector<int> nums;
    nums.push_back(1);
    nums.push_back(17);
    nums.push_back(5);
    nums.push_back(10);
    nums.push_back(13);
    nums.push_back(15);
    nums.push_back(10);
    nums.push_back(5);
    nums.push_back(16);
    nums.push_back(8);
    printf("原序列:");
    for (int i = 1; i < nums.size(); i++){
        printf("%d, ", nums[i]);
    } 
    Solution solve;
    printf("\n最长摇摆序列个数:%d\n", solve.wiggleMaxLength(nums));
    return 0;
}

移除 K 个数字(栈、贪心)(★★)

  • LeetCode 402.Remove K Digits.cpp
#include <stdio.h>
#include <string>
#include <vector>

class Solution {
public:
    std::string removeKdigits(std::string num, int k) {
        // 使用 vector 当作栈   
        std::vector<int> S;
        // 存储结果的字符串 result 
        std::string result = "";
        // 从高位开始遍历 num 
        for (int i = 0; i < num.length(); i++){
            // 将字符串转为整型 
            int number = num[i] - '0';
            // 当栈不为空,栈顶数字大于 number,还可移除数字时 
            while(S.size() != 0 && S[S.size()-1] > number && k > 0){
                // 弹出栈顶数字 
                S.pop_back();
                k--;
            }
            // 栈顶数字变为 number 
            if (number != 0 || S.size() != 0){
                S.push_back(number);
            }
        }
        // 当遍历到末尾,还可移除数字时 
        while(S.size() != 0 && k > 0){
            // 弹出栈顶数字 
            S.pop_back();
            k--;
        }
        // 遍历栈中整型数字 
        for (int i = 0; i < S.size(); i++){
            // 存储至字符串 result 
            result.append(1, '0' + S[i]);
        }
        return result;
    }
};ING
int main(){
    Solution solve;
    std::string result = solve.removeKdigits("1432219", 3);
    printf("num = 1432219, k = 3,"); 
    printf("移除 k 个数字后最小为:%s\n", result.c_str());
    std::string result2 = solve.removeKdigits("100200", 1);
    printf("num = 100200, k = 1,"); 
    printf("移除 k 个数字后最小为:%s\n", result2.c_str());
    return 0;
}

跳跃游戏(贪心)(★★)

  • LeetCode 55.Jump Game.cpp
#include <stdio.h>
#include <vector>

class Solution {
public:
    bool canJump(std::vector<int>& nums) {
        // 最远可跳至的位置 
        std::vector<int> index;
        // 计算 index 数组 
        for (int i = 0; i < nums.size(); i++){
            index.push_back(i + nums[i]);
        }
        // 初始化 jump 
        int jump = 0;
        // 初始化 max_index
        int max_index = index[0];
        // 若满足条件,继续 jump ,同时更新 max_index 
        while(jump < nums.size() && jump <= max_index){
            if (max_index < index[jump]){
                max_index = index[jump];
            }
            jump++;
        }
        // 若 jump 到了最后,返回成功 
        if (jump == nums.size()){
            return true;
        }
        // 若 jump 没到最后,返回失败 
        return false;
    }
};

int main(){
    std::vector<int> nums;
    nums.push_back(2);
    nums.push_back(3);
    nums.push_back(1);
    nums.push_back(1);
    nums.push_back(4);
    Solution solve;
    for (int i = 0; i < nums.size(); i++){
        printf("%d  ", nums[i]);
    }
    if (solve.canJump(nums)){
        printf("\n可以跳到最后");
    }
    else{
        printf("\n不可以跳到最后");
    } 
    return 0;
}

跳跃游戏2(贪心)(★★★)

  • LeetCode 45.Jump Game II.cpp
#include <stdio.h>
#include <vector>

class Solution {
public:
    int jump(std::vector<int>& nums) {
        // 当前位置可到达的最远位置 
        int current_max_index = nums[0];
        // 遍历各个位置可到达的最远位置 
        int pre_max_max_index = nums[0];
        //  最小跳跃次数
        int jump_min = 1;
        for (int i = 1; i < nums.size(); i++){
            // 如果无法再向前移动,就跳跃 
            if (i > current_max_index){
                // 更新跳跃次数 
                jump_min++;
                // 更新当前可到达的最远位置 
                current_max_index = pre_max_max_index;
            } 
            if ( nums[i] + i > pre_max_max_index){
                pre_max_max_index = nums[i] + i;
            }
        } 
        return jump_min;
    }
};
int main(){
    std::vector<int> nums;
    nums.push_back(2);
    nums.push_back(3); 
    nums.push_back(1);
    nums.push_back(1);
    nums.push_back(4);
    Solution solve;
    printf("最少跳跃次数:%d\n", solve.jump(nums));
    return 0;
}

射击气球(排序、贪心)(★★)

  • LeetCode 452.Minimum Number of Arrows to Burst Balloons.cpp
#include <stdio.h>
#include <algorithm>
#include <vector>

// 比较左端点大小 
bool cmp(const std::pair<int, int> &a, const std::pair<int ,int> &b) {
    return a.first < b.first;
}
class Solution {
public:
    int findMinArrowShots(std::vector< std::pair<int,int> > & points) {
        // 对气球按照左端点从小到大排序 
        std::sort(points.begin(), points.end(), cmp);
        // 初始化弓箭手为 1 
        int shoot_num = 1;
        // 初始化射击区间为第一个气球区间 
        int shoot_begin = points[0].first;
        int shoot_end = points[0].second;
        // 按照左端点从小到大遍历气球 
        for (int i = 1; i < points.size(); i++){
            // 若气球区间左端点 <= 射击区间右端点,即有交集  
            if (points[i].first <= shoot_end){
                // 更新射击区间左端点 
                shoot_begin = points[i].first;
                // 若气球区间右端点 <= 射击区间右端点
                if (points[i].second <= shoot_end){
                    // 更新射击区间右端点
                    shoot_end = points[i].second;
                }
            }
            // 若气球区间左端点 > 射击区间右端点,即无交集 
            else{
                // 增加弓箭手 
                shoot_num++;
                // 更新射击区间 
                shoot_begin = points[i].first;
                shoot_end = points[i].second;
            }
        }
        return shoot_num;
    }
};
int main(){
    std::vector< std::pair<int,int> > points;
    // 随机输入各个气球区间 
    points.push_back(std::make_pair(10, 16));
    points.push_back(std::make_pair(2, 8));
    points.push_back(std::make_pair(1, 6));
    points.push_back(std::make_pair(7, 12));    
    Solution solve;
    printf("至少需要弓箭手:%d\n", solve.findMinArrowShots(points));
    return 0;
}

最优加油法(堆、贪心)(★★★)

  • poj2431 Expedition.cpp
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <queue>

// 根据 d 从大到小排序  
bool cmp(const std::pair<int, int> &a, const std::pair<int ,int> &b) {
    return a.first > b.first;
}
// L 为起点距终点距离,P 为起点汽油量
int get_minimum_stop(int L, int P, std::vector< std::pair<int,int> > & stop){
    // Q 为存储油量的最大堆 
    std::priority_queue<int> Q;
    // result 为加油次数 
    int result = 0;
    // 将终点 (0,0) 作为停靠点输入 
    stop.push_back(std::make_pair(0, 0));
    // 以停靠点至终点距离从大到小排序
    std::sort(stop.begin(), stop.end(), cmp);
    // 遍历各个停靠点 
    for (int i = 0; i < stop.size(); i++){
        // d 为到下一站距离,d(这一站) = L(这一站) - d(下一站) 
        int d = L - stop[i].first;
        // 若当前汽油量到不了下一站 
        while (!Q.empty() && P < d){
            // 更新加油次数 
            result++;
            // 更新当前油量 
            P += Q.top();
            // 更新存储油量的最大堆 
            Q.pop();
        }
        // 若当前汽油量到得了下一站
        if (Q.empty() && P < d){
            return -1;
        }
        // 更新当前汽油量 
        P = P - d;
        // 更新距终点距离 d,即 stop[i].first 
        L = stop[i].first;
        // 更新堆里油量 fuel, 即 stop[i].second
        Q.push(stop[i].second);
    }
    return result;
}
int main(){
    // 存储各个站距终点距离,各个站汽油量的堆 stop 
    std::vector<std::pair<int, int> > stop;
    int N, L, P, d, fuel;  
    printf("  请输入加油站数量 (起点不算):\n"); 
    scanf("%d", &N);
    printf("  请输入各个站距终点距离,各个站汽油量:\n"); 
    for (int i = 0; i < N; i++){
        scanf("%d %d", &d, &fuel);
        stop.push_back(std::make_pair(d, fuel));
    }
    printf("请输入起点距终点距离,起点汽油量:\n"); 
    scanf("%d %d", &L, &P); 
    printf("从起点到终点最少加油次数:%d", get_minimum_stop(L, P, stop));
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269