原始问题:
生成股票价格上涨等待天数列表的Java实现方法
import java.util.ArrayList;
import java.util.List;
public class StockPricePrediction {
public static void main(String[] args) {
int[] stockPrices = {100, 105, 110, 120, 130, 140, 150};
List<Integer> prediction = predictStockPrice(stockPrices);
System.out.println(prediction);
}
public static List<Integer> predictStockPrice(int[] stockPrices) {
List<Integer> prediction = new ArrayList<>();
for (int i = 0; i < stockPrices.length; i++) {
if (i == 0) {
prediction.add(0);
} else {
int daysToWait = findDaysToWait(stockPrices, i);
prediction.add(daysToWait);
}
}
return prediction;
}
private static int findDaysToWait(int[] stockPrices, int currentIndex) {
int daysToWait = 0;
boolean isUpwardTrend = true;
for (int i = currentIndex - 1; i >= 0 && isUpwardTrend; i--) {
if (stockPrices[i] > stockPrices[currentIndex]) {
daysToWait++;
} else {
isUpwardTrend = false;
}
}
return daysToWait;
}
}
Prev:小红定义“好二叉树”:节点子数为偶数,求不同形态数量(结果对109+7取模)的JS实现方法