算法-贪心-做项目获得最大收益
package com.jiucaiyuan.net.question;
import java.util.Comparator;
import java.util.PriorityQueue;
/**
 * 【问题】做一个项目,有成本,也有利润,一堆项目,如何选择做,实现收益最大?
 * 【思路】把所有项目,按照成本放入小根堆,从小根堆中拿出可以做的项目,然后按照利润放入大跟堆,开始先做大根堆的对顶项目
 *
 * @Author jiucaiyuan  2022/4/18 23:10
 * @mail services@jiucaiyuan.net
 */
public class MaxProfits {
    public static class Node {
        public int cost;
        public int profit;
        public Node(int cost, int profit) {
            this.cost = cost;
            this.profit = profit;
        }
    }
    public static class MinCostComparator implements Comparator<Node> {
        @Override
        public int compare(Node node1, Node node2) {
            return node1.cost - node2.cost;
        }
    }
    public static class MaxProfitComparator implements Comparator<Node> {
        @Override
        public int compare(Node node1, Node node2) {
            return node2.profit - node1.profit;
        }
    }
    /**
     * 没做完一个项目,马上获得收益,可以支持继续做下一个项目,输出最后获得的最大钱数
     *
     * @param k       只能串行,最多做k个项目
     * @param w       初始资金
     * @param profits profits[i]是i号项目扣除成本后还能剩下的钱(利润)
     * @param costs   costs[i]是i号项目的花费
     * @return
     */
    public static int findMaximizedCapital(int k, int w, int[] profits, int[] costs) {
        PriorityQueue<Node> minCostQueue = new PriorityQueue<>(new MinCostComparator());
        PriorityQueue<Node> maxProfitQueue = new PriorityQueue<>(new MaxProfitComparator());
        //所有项目扔到锁定池中,按照花费组织的小根堆
        for (int i = 0; i < profits.length; i++) {
            minCostQueue.add(new Node(costs[i], profits[i]));
        }
        for (int i = 0; i < k; i++) {  //进行k轮
            //能力所及的项目,全部解锁
            while (!minCostQueue.isEmpty() && minCostQueue.peek().cost < w) {
                maxProfitQueue.add(minCostQueue.poll());
            }
            if (maxProfitQueue.isEmpty()) {
                return w;
            }
            w += maxProfitQueue.poll().profit;
        }
        return w;
    }
    public static void main(String[] args) {
        int[] profits = {1, 2, 3, 5, 3, 2, 1, 4, 54, 5};
        int[] costs = {1, 1, 1, 1, 1, 12, 2, 1, 2, 1};
        System.out.println(findMaximizedCapital(10, 5, profits, costs));
    }
}
	
		
		    
		日历
个人资料
 
		diaba 寻求合作请留言或联系mail: services@jiucaiyuan.net
链接
最新文章
存档
- 2025年4月(17)
- 2025年3月(25)
- 2025年2月(20)
- 2025年1月(2)
- 2024年10月(1)
- 2024年8月(2)
- 2024年6月(4)
- 2024年5月(1)
- 2023年7月(1)
- 2022年10月(1)
- 2022年8月(1)
- 2022年6月(11)
- 2022年5月(6)
- 2022年4月(33)
- 2022年3月(26)
- 2021年3月(1)
- 2020年9月(2)
- 2018年8月(1)
- 2018年3月(1)
- 2017年3月(3)
- 2017年2月(6)
- 2016年12月(3)
- 2016年11月(2)
- 2016年10月(1)
- 2016年9月(3)
- 2016年8月(4)
- 2016年7月(3)
- 2016年6月(4)
- 2016年5月(7)
- 2016年4月(9)
- 2016年3月(4)
- 2016年2月(5)
- 2016年1月(17)
- 2015年12月(15)
- 2015年11月(11)
- 2015年10月(6)
- 2015年9月(11)
- 2015年8月(8)
分类
热门文章
- SpringMVC:Null ModelAndView returned to DispatcherServlet with name 'applicationContext': assuming HandlerAdapter completed request handling
- Mac-删除卸载GlobalProtect
- java.lang.SecurityException: JCE cannot authenticate the provider BC
- MyBatis-Improper inline parameter map format. Should be: #{propName,attr1=val1,attr2=val2}
- Idea之支持lombok编译
标签
最新评论
- logisqykyk	
 Javassist分析、编辑和创建jav...
- xxedgtb	
 Redis—常见参数配置 - 韭菜园 ...
- wdgpjxydo	
 SpringMVC:Null Model...
- rllzzwocp	
 Mysql存储引擎MyISAM和Inno...
- dpkgmbfjh	
 SpringMVC:Null Model...
- tzklbzpj	
 SpringMVC:Null Model...
- bqwrhszmo	
 MyBatis-Improper inl...
- 乐谱吧	
 good非常好
- diaba	
 @diaba:应该说是“时间的度量依据”...
- diaba	
 如果速度增加接近光速、等于光速、甚至大于...
最新微语
- 在每件事情上花费的东西,就是生命的一部分,而我们花费的这些东西要求立即得到回报,或者在一个长时间以后得到回报。2025-01-23 15:46 
- 诺曼·文森特说:“并不是你认为自己是什么样的人,你就是什么样的人。但是你的思想是什么样,你就是什么样的人。”2025-01-23 15:44 
- 从今天起,做一个幸福的人。喂马,砍柴,(思想)周游世界2022-03-21 23:31 
- 2022.03.02 23:37:592022-03-02 23:38 
- 几近崩溃后,找到解决方法,总是那么豁然开朗!所以遇到问题要坚持!2018-07-18 10:49 

发表评论: