Stock Maximize
来源:互联网 发布:工业设计制图软件 编辑:程序博客网 时间:2024/04/28 15:33
Your algorithms have become so good at predicting the market that you now know what the share price of Wooden Orange Toothpicks Inc. (WOT) will be for the next N days.
Each day, you can either buy one share of WOT, sell any number of shares of WOT that you own, or not make any transaction at all. What is the maximum profit you can obtain with an optimum trading strategy?
Input
The first line contains the number of test cases T. T test cases follow:
The first line of each test case contains a number N. The next line contains N integers, denoting the predicted price of WOT shares for the next N days.
Output
Output T lines, containing the maximum profit which can be obtained for the corresponding test case.
Constraints
1 <= T <= 10
1 <= N <= 50000
All share prices are between 1 and 100000
Sample Input
335 3 231 2 10041 3 1 2
Sample Output
01973
Explanation
For the first case, you cannot obtain any profit because the share price never rises.
For the second case, you can buy one share on the first two days, and sell both of them on the third day.
For the third case, you can buy one share on day 1, sell one on day 2, buy one share on day 3, and sell one share on day 4.
思路:这题跟Leetcode的买卖II的最大不同就是,LC的股票买卖条件值允许有一只股票在手,但是这个题目的条件是手上可以有任意支条股票。可以放在手里等到N天之后再抛售。这样的条件下求最大。那么就是从后往前的贪心法则,从后往前算,keep住当前后面的最大值,然后更新最大值,每次用后面的最大值减去当前值,就是当前的盈利最大。
这题是 https://www.hackerrank.com/challenges/stockmax的题目。
import java.io.*;import java.util.*;import java.text.*;import java.math.*;import java.util.regex.*;public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner scanner = new Scanner(System.in); int size = scanner.nextInt(); for(int i=0; i<size; i++){ int numOfDays = scanner.nextInt(); // data size; int[] prices = new int[numOfDays]; for(int j=0; j<numOfDays; j++){ prices[j] = scanner.nextInt(); } System.out.println(calcualteMax(prices)); } } public static Long calcualteMax(int [] prices) { if(prices == null || prices.length <=1) return 0L; Long profit = 0L; int maxprice = 0; for(int i=prices.length-1; i>=0; i--){ if(prices[i] > maxprice){ maxprice = prices[i]; } profit += maxprice - prices[i]; } return profit; }}
- Stock Maximize
- hackerrank Stock Maximize
- HackerRank - Stock Maximize
- HackerRank--Stock Maximize
- ZOJ2834 Maximize Game Time
- stock
- stock
- stock
- stock
- stock
- stock
- stock
- stock
- ZOJ 2834 Maximize Game Time
- google :xlib maximize window notify
- B. Maximize Sum of Digits
- swjtu2385(Maximize The Beautiful Value)
- maximize your World of Warcraft demo time
- 直方图均衡化的数学原理
- [美剧赏析] 权力的游戏<Game of Thrones>完全赏析 (51-52)
- LeetCode Self Crossing
- JavaEE环境搭建
- 2242: [SDOI2011]计算器
- Stock Maximize
- C# WinForm ComboBox下拉菜单如何切换鼠标移动到某项的高亮背景色
- hibernate的分页+jsp小计+mysql建表+hql的使用
- 复习Java SE
- MySQL数据类型及sql模型
- jQuery选择器大全
- 解决Qt 5.7自带Creator在Ubuntu 16.04下不能输入中文的问题(fcitx输入框架)
- Android 桌面小组件Timer时钟使用场景
- 读取海量数据到文件并拆分排序