日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HackerRank - Stock Maximize

發布時間:2023/12/20 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HackerRank - Stock Maximize 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目: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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。