일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
- PYTHON
- 데이콘
- dacon
- Real or Not? NLP with Disaster Tweets
- ChatGPT
- 백준
- 맥북
- 더현대서울 맛집
- 편스토랑
- 금융문자분석경진대회
- 프로그래머스
- Docker
- 캐치카페
- github
- hackerrank
- Baekjoon
- Kaggle
- 편스토랑 우승상품
- SW Expert Academy
- gs25
- programmers
- 프로그래머스 파이썬
- ubuntu
- 우분투
- Git
- 코로나19
- leetcode
- 자연어처리
- AI 경진대회
- 파이썬
- Today
- Total
솜씨좋은장씨
[HackerRank] Stock Maximize (Python) 본문
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 number of 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?
For example, if you know that prices for the next two days are prices = [ 1, 2 ], you should buy one share day one, and sell it day two for a profit of 1. If they are instead prices = [ 2, 1 ], no profit can be made so you don't buy or sell stock those days.
Function Description
Complete the stockmax function in the editor below. It must return an integer that represents the maximum profit achievable.
stockmax has the following parameter(s):
- prices: an array of integers that represent predicted daily stock prices
Input Format
The first line contains the number of test cases .
Each of the next pairs of lines contain:
- The first line contains an integer , the number of predicted prices for WOT.
- The next line contains n space-separated integers , each a predicted stock price for day .
Constraints
- 1 <= t <= 10
- 1 <= n <= 50000
- 1 <= prices [ i ] <= 100000
Output Format
Output t lines, each containing the maximum profit which can be obtained for the corresponding test case.
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.
Solution
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'stockmax' function below.
#
# The function is expected to return a LONG_INTEGER.
# The function accepts INTEGER_ARRAY prices as parameter.
#
def stockmax(price):
my_profit = 0
max_price = price[-1]
for i in range(len(price)-1, -1, -1):
if price[i] >= max_price:
max_price = price[i]
my_profit = my_profit + max_price - price[i]
return my_profit
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
t = int(input().strip())
for t_itr in range(t):
n = int(input().strip())
prices = list(map(int, input().rstrip().split()))
result = stockmax(prices)
fptr.write(str(result) + '\n')
fptr.close()
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[BaeKJoon] 2225번: 합분해 (Python) (0) | 2020.05.30 |
---|---|
[BaeKJoon] 2156번: 포도주 시식 (Python) (0) | 2020.05.30 |
[SW_Expert_Academy] 2072번 홀수만 더하기 (Python) (0) | 2020.05.26 |
[SW_Expert_Academy] 5549번 홀수일까 짝수일까 (Python) (0) | 2020.05.25 |
[SW_Expert_Academy] 4522번 세상의 모든 팰린드롬 (Python) (0) | 2020.05.24 |