• 【刷题篇】贪心算法(一)


    分割平衡字符串

    在这里插入图片描述

    class Solution {
    public:
        int balancedStringSplit(string s) {
            int len=s.size();
            int cnt=0;
            int balance=0;
            for(int i=0;i<len;i++)
            {
                if(s[i]=='R')
                {
                    balance--;
                }
                else
                {
                    balance++;
                }
                if(balance==0)
                {
                    cnt++;
                }
            }
            return cnt;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    买卖股票的最佳时机Ⅱ

    在这里插入图片描述

    class Solution {
    public:
        int maxProfit(vector<int>& prices) {
            int maxprofit=0;
            int day=prices.size();
            for(int i=1;i<day;i++)
            {
                if(prices[i-1]<prices[i])
                {
                    maxprofit+=prices[i]-prices[i-1];
                }
            }
            return maxprofit;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    跳跃游戏

    在这里插入图片描述

    class Solution {
    public:
        bool canJump(vector<int>& nums) {
            int maxlen=0;
            int len=nums.size();
            for(int i=0;i<len;i++)
            {   //判断是否能到当前位置
                if(maxlen>=i)
                {
                    maxlen=max(maxlen,i+nums[i]);
                }
                else
                {   //到不了当前位置就说明也就到不了最后的位置
                    return false;
                }
                //当最大路径大于总里程时就可以返回了
                if(maxlen>len-1)
                {
                    return true;
                }
            }
            return true;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    钱币找零

    假设1元、2元、5元、10元、20元、50元、100元的纸币分别由c0,c1,c2,c3,c4,c5,c6张。现在要用这些钱来支付K元,至少要用多少张纸币?

    #include
    #include
    #include
    
    using namespace std;
    
    struct moneycmp
    {
    	bool operator()(vector<int>& array1, vector<int>& array2)
    	{
    		return array1[0] > array2[0];
    	}
    }cmp;
    //0:是面值  1:是数量
    int MoneyMat(vector<vector<int>>& moneymat, int money)
    {
    	int cnt = 0;
    	sort(moneymat.begin(),moneymat.end(),cmp);
    	//遍历面值
    	for (auto& array : moneymat)
    	{
    		int c = 0;
    		c = money / array[0];
    		//确保取得是最小值,保证张数不会超
    		c=min(c, array[1]);
    		money -= c * array[0];
    		cnt += c;
    	}
    	if (money != 0)
    	{
    		return -1;
    	}
    	return cnt;
    }
    
    int main()
    {                              //面值,数量
    	vector<vector<int>> mat = { {100,5} ,{50,3},{20,4},{5,5},{2,5},{1,10} };
    	int money=0;
    	cin >> money;
    	int count=MoneyMat(mat,money);
    	cout << count << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
  • 相关阅读:
    go语言 go-redis watch实例
    .net core 和 WPF 开发升讯威在线客服系统:调用百度翻译接口实现实时自动翻译
    eclipse启动一个Springboot项目
    动态RDLC报表(五)
    E. Count Seconds(DAG/拓扑排序/树形dp)
    Edge浏览器做web自动化测试(selenium)
    centos7 安装docker 步骤整理
    前端vue正则表达式-隐私脱敏处理
    如何加快Chrome谷歌浏览器下载速度?
    设计模式---代理模式(结构型)
  • 原文地址:https://blog.csdn.net/m0_66599463/article/details/132741635