题目:https://www.hackerrank.com/challenges/stockmax

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

3
3
5 3 2
3
1 2 100
4
1 3 1 2

Sample Output

0
197
3

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.

分析:

动态规划的思想,从后往前处理,如果后面的最大值大于当前值,则将差值加到结果上,否则更新最大值。

代码:

#include <iostream>
#include <vector>int main() {int T;std::cin >> T;int N;while(T--) {std::cin >> N;std::vector<int> nums(N, 0);for(int i = 0; i < N; ++i) {std::cin >> nums[i];}long long result = 0;  // !int max = nums[N - 1];for(int i = N - 2; i >= 0; --i) {if(max > nums[i]) {result += (max - nums[i]);} else {max = nums[i];}}std::cout << result << std::endl;}return 0;
}

HackerRank - Stock Maximize相关推荐

  1. Introduction to Financial Management

    Recently,i am learning some useful things about financial management by reading <Essentials of Co ...

  2. Stock Market(luogu 2938)

    题目描述 Despite their innate prudence, the cows took a beating in the home mortgage market and now are ...

  3. 【Column Generation思考-02】|从对偶的角度理解Cutting Stock Problem【更新版本】

    [Column Generation思考-01]|从对偶的角度理解 Cutting Stock Problem简介 Cutting Stock Problem的Column generation re ...

  4. 121. Best Time to Buy and Sell Stock买卖股票的最佳时机

    You are given an array prices where prices[i] is the price of a given stock on the ith day. 给定一个数组 p ...

  5. 【73套】Epic Stock Media配乐音效素材合集包

    [73套]Epic Stock Media配乐音效素材合集包 大小:114G丨WAV Epic Stock Media 系列音效合辑 包含了战争电影音效.史诗梦幻音效.日常生活音效.森林环境音效.电影 ...

  6. [Swift]LeetCode901. 股票价格跨度 | Online Stock Span

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  7. Best Time to Buy and Sell Stock II

    题目: Say you have an array for which the i th element is the price of a given stock on day i. Design ...

  8. 121-Best Time to Buy and Sell Stock

    题目: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...

  9. 1578: [Usaco2009 Feb]Stock Market 股票市场

    1578: [Usaco2009 Feb]Stock Market 股票市场 Time Limit: 10 Sec  Memory Limit: 64 MB Submit: 414  Solved:  ...

  10. 大宗商品(Bulk Stock)交易

    大宗商品(Bulk Stock) 是指可进入流通领域,但非零售环节,具有商品属性用于工农业生产与消费使用的大批量买卖的物质商品.在金融投资市场,大宗商品指同质化. 可交易. 被广泛作为工业基础原材料的 ...

最新文章

  1. Java编码技巧之高效代码50例
  2. Deep Learning 26:读论文“Maxout Networks”——ICML 2013
  3. 五种最易被老板开除的人
  4. C++的Android接口---配置NDK
  5. 不定高宽的元素居中的方法
  6. python替换缺失值,处理空值+生成图形+图形标准化
  7. 发现 nios2-elf-gcc 一处 bug
  8. jsf服务_JSF dataTable示例
  9. 实现文字跟随鼠标移动--[转]
  10. 婚恋职场人格-张晓文-武汉理工大学-中国MOOC-亲密关系测试题参考答案
  11. 免费开源PDF复制格式整理神器
  12. 2021-05-19 退役啦
  13. Django下载文件——三种方法和大文件下载
  14. 2020互联网公司中秋礼盒大比拼(22家互联网厂商)
  15. blockquote缩进标签
  16. 如何解决安装佳能LBP2900驱动时总是提示无法识别USB连接(附带驱动程序下载地址)
  17. Python | 输出分数形式(Fraction)
  18. OSS对象存储之阿里云和七牛云
  19. 跨链桥:用零知识证明桥接多链宇宙
  20. 2020 Python中文社区热门文章 Top 10

热门文章

  1. 单片机简易时钟开发(protues)
  2. 【毕设狗】【单片机毕业设计】基于单片机的空气质量检测-仿真设计
  3. 霍尼韦尔:物联网“起跑线”上的巨头转型之路
  4. nsis升级包_NSIS v3.2.0.1-简易封包工具
  5. Windows下使用C++(Win32SDK)编程无需提权读取硬盘序列号
  6. 用ABAP编程破解世界上最难数独游戏
  7. MFC的API函数大全
  8. 数字图像处理:名词解释
  9. 位图图片转换矢量图的工具:Vector Magic for mac
  10. 软考软件设计师中级考试大纲(附题型举例)