数据结构与算法-贪心算法

贪心算法的典型特征:针对一个问题,有限制值和期望值。

解决步骤:针对给定数据,选择数据在限定值同等贡献的情况下,对期望值贡献最大的数据。每个状态下都找到当前状态下的最优解。

两个小练习代码如下:

package com.freshbin.dataStructAndAlgo.chapter37.mycode;

import java.util.HashMap;

/**
 * leetcode 1221题
 *
 * 描述:在一个「平衡字符串」中,'L' 和 'R' 字符的数量是相同的。
 * 给出一个平衡字符串 s,请你将它分割成尽可能多的平衡字符串。
 * 返回可以通过分割得到的平衡字符串的最大数量。
 *
 * 思路:使用一个hashMap存储"L"和"R"两个key,value都初始为0
 * 遍历字符串,将当前字符在hashMap中的value+1,当L和R的value相等的时候,将两个value清空,同时最大数量+1。
 *
 * @author freshbin
 * @date 2020/4/24 15:08
 */
public class Solution1221 {
    public int balancedStringSplit(String s) {
        if(s == null || s.length() < 1) {
            return 0;
        }

        HashMap<Character, Integer> tempMap = new HashMap<>();
        tempMap.put('L', 0);
        tempMap.put('R', 0);
        int maxCount = 0;
        for(int i = 0; i < s.length(); i++) {
            int LCount = tempMap.get('L');
            int RCount = tempMap.get('R');
            char currentChar = s.charAt(i);
            if(currentChar == 'L') {
                LCount = LCount+1;
                tempMap.put('L', LCount);
            } else {
                RCount = RCount+1;
                tempMap.put('R', RCount);
            }

            if(LCount == RCount) {
                tempMap.put('L', 0);
                tempMap.put('R', 0);
                maxCount++;
            }

        }
        return maxCount;
    }

    /**
     * leetcode大佬们的代码
     * @param s
     * @return
     */
    public int balancedStringSplit01(String s) {
        int num = 0;
        int res = 0;
        for(int i=0;i<s.length();i++){
            if(s.charAt(i) == 'L')
                num++;
            else
                num--;
            if(num == 0)
                res++;
        }
        return res;
    }

    public static void main(String[] arg) {
        String s = "RLRRRLLRLL";
        Solution1221 solution1221 = new Solution1221();
        System.out.println(solution1221.balancedStringSplit(s));
    }
}
package com.freshbin.dataStructAndAlgo.chapter37.mycode;

/**
 * leetcode122
 * 思路:剽窃答案之后的思路,一开始竟然想歪了,
 * 哎,其实很简单,只要每天都买卖,后一天的比前一天价格高就卖掉就是赚的最多的
 * 可惜实际股市股价跌了之后就再也不升回去了。。。
 *
 * @author freshbin
 * @date 2020/4/24 22:38
 */
public class Solution122 {
    public int maxProfit(int[] prices) {
        int maxProfit = 0;
        for(int i = 1; i < prices.length; i++) {
            int currentProfit = prices[i] - prices[i - 1];
            if(currentProfit > 0) {
                maxProfit += currentProfit;
            }
        }
        return maxProfit;
    }

    public static void main(String[] arg) {
        int[] prices = {1,2,3,4,5};
        Solution122 solution122 = new Solution122();
        System.out.println(solution122.maxProfit(prices));
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值