【问题标题】:Efficiently solving coding puzzle with long array?用长数组有效解决编码难题?
【发布时间】:2018-02-11 00:24:12
【问题描述】:

我一直在思考解决此问题并使其适用于长数组的有效方法时遇到了一些困难。我很确定我不知道解决它所需的编程技术。如果您能帮助我,我将不胜感激!

问题:杰克是个小商人。他找到了一种赚钱的方法,在电价便宜的时候买电,在电价高得多的时候卖电。他将电力储存在自己制造的电池中。给你 N,Jack 知道电费的天数,以及 X,Jack 可用于投资电力的金额,在下一行中,给你 N 天的价值(买/卖价值) 的电力。你的工作是确定杰克什么时候应该买电,什么时候应该卖电,以便尽可能多地赚钱,并简单地打印出他能赚到的最大金额。电的价值总是一个整数,但根据杰克拥有的钱的数量,他拥有的电和钱的数量可能是浮点数。对于如何解决这个问题,我有一些想法,但是对于长数组,它们都非常低效。

Example: 
Input: 
4 10
4 10 5 20
Output:100, because he buys electricity on the 1st day and the sells it on the 2nd and buys it on the 3rd and sells it on the 4th day.

Example num. 2: 
Input: 
3 21
10 8 3
Output:21, because it's better if he doesn't buy/sell any electricity.


Example num. 3: 
Input:
3 10
8 10 14
Output:17.5, because he buys electricity on the 1st day, but he sells it on the 3rd day.

【问题讨论】:

    标签: arrays


    【解决方案1】:

    在任何一天,杰克都有现金或电。

    除了最后一天,他有现金就买电,第二天的电价更高。

    任何一天,他有电就卖电,要么是最后一天,要么是第二天的价格更低。

    【讨论】:

      猜你喜欢
      • 2017-02-28
      • 2012-02-29
      • 1970-01-01
      • 2021-03-02
      • 2011-01-09
      • 1970-01-01
      • 2012-05-10
      • 2023-02-02
      • 1970-01-01
      相关资源
      最近更新 更多