Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 找到要购买的股票的价格以实现利润最大化?_Java_Algorithm_Data Structures - Fatal编程技术网

Java 找到要购买的股票的价格以实现利润最大化?

Java 找到要购买的股票的价格以实现利润最大化?,java,algorithm,data-structures,Java,Algorithm,Data Structures,我最近遇到了一个问题 因为我们得到了一个输入,它包含了所有的时间跨度和特定时间跨度内的股票价格 我们的目标是找到并展示所有时间跨度中的最佳股票价格,以实现利润最大化 例如: 输入: 上午9:00-上午10:00-80卢比 上午9:00-上午11:00-50卢比 上午10:00-上午11:00-30卢比 上午10:00-13:00-90卢比 上午11:00-14:00-70卢比 中午12:00-上午14:00-40卢比 上午14:00-上午15:00-45卢比 输出: 上午9:00-上午10:00

我最近遇到了一个问题

因为我们得到了一个输入,它包含了所有的时间跨度和特定时间跨度内的股票价格

我们的目标是找到并展示所有时间跨度中的最佳股票价格,以实现利润最大化

例如:

输入:

上午9:00-上午10:00-80卢比

上午9:00-上午11:00-50卢比

上午10:00-上午11:00-30卢比

上午10:00-13:00-90卢比

上午11:00-14:00-70卢比

中午12:00-上午14:00-40卢比

上午14:00-上午15:00-45卢比

输出:

上午9:00-上午10:00-50卢比

上午10:00-上午11:00-30卢比

上午11:00-上午12:00-70卢比

上午12:00-14:00-40卢比

上午14:00-上午15:00-45卢比

我尝试了几种方法来解决这个问题,比如使用DS。只考虑开始时间和其他方式


您能给我一些建议吗?

您可以将输入转换为对象

Input.java

public class Input {
    // translate time period to list, like "9", "10", "11"
    private ArrayList<String> times = new ArrayList<>(); 
    private Double value;

    public ArrayList<String> getTimes() {

        return times;
    }

    public void setTimes(ArrayList<String> times) {

        this.times = times;
    }

    public Double getValue() {

        return value;
    }

    public void setValue(Double value) {

        this.value = value;
    }
}
使用StockPrice存储值。并将最小值放入每个元素。 StockPrice.java

import java.util.HashMap;
import java.util.Map;

public class StockPrice extends HashMap<String, Double> {

    Map<String, Double> values = new HashMap<>();

    @Override
    public Double put(String key, Double value) {
        Double var = values.get(key);
        if ( var == null || var > value ) {
            values.put(key, value);
            return value;
        }

        return var;
    }

    public void put(Input input) {
        for (String time : input.getTimes()) {
            put(time, input.getValue());
        }
    }
}

键类似于9,10,11…,值是最小的股票价格。

您的代码在哪里?