502. IPO

这道题必须记录一下,写的好难受。
首先,题目并没有说用过的项目不能再用;
其次并不清楚为什么不能用数组来处理,提示我超时了,这里对于堆排序的复杂度问题我又有了新的疑问,比如之前我得到的知识一直是log1+log2+…+logn的时间复杂度是O(N),结果谷歌后发现这个时间复杂度是nlogn,那么建堆的过程为什么不能理解成log1+log2+…+logn而非是O(N)呢?现在还不知道,暂时放一下;
最后,我在把数据从收益大根堆拿出来后,堆中其它数据还要不要处理?我之前把这些数据又放回了成本小根堆,超时了。其实w的值是一直上涨的,所以原来收益大根堆中存放的项目在后续也一定可用。

struct money{
public:
	int profits;
	int capital;
	money(int _profits, int _capital):profits(_profits),capital(_capital){};
};

    
struct cmp_profits
{
public:
	bool operator()(money a, money b)
	{return a.profits<b.profits;}
};
struct cmp_capital
{
public:
	bool operator()(money a ,money b)
	{return a.capital>b.capital;}
};
int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) {

	priority_queue<money,vector<money>,cmp_profits>prof;///<利润堆
	priority_queue<money,vector<money>,cmp_capital>cap;///<成本小根堆
	
	for(size_t i=0;i<profits.size();++i)
	{
		money x = money(profits[i], capital[i]);
		cap.push(x);
	}
	for(int i=0;i<k;++i)
	{
		while(!cap.empty() && cap.top().capital<=w)
		{
			prof.push(cap.top());
			cap.pop();
		}
		if(!prof.empty())
		{
			w+=prof.top().profits;
			prof.pop();
		}
	}
	return w;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tux~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值