dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
98 lines
2.4 KiB
Markdown
98 lines
2.4 KiB
Markdown
---
|
|
id: 6a19dd69388e5a6e59e4f2ef
|
|
title: "Challenge 326: Max Profit"
|
|
challengeType: 29
|
|
dashedName: challenge-326
|
|
---
|
|
|
|
# --description--
|
|
|
|
Given an array of daily stock prices and a budget (in dollars), calculate the maximum profit you could make by buying and selling the stock over the given period.
|
|
|
|
- You may only sell after you buy.
|
|
- You may perform at most one buy and one sell transaction. Once you sell, you cannot buy again.
|
|
- You can only buy whole shares.
|
|
- Return the maximum possible profit as a string, rounded down to the nearest cent and formatted to two decimal places.
|
|
|
|
# --hints--
|
|
|
|
`get_max_profit([5, 6], 50)` should return `"10.00"`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(get_max_profit([5, 6], 50), "10.00")`)
|
|
}})
|
|
```
|
|
|
|
`get_max_profit([8, 2, 5, 10], 20)` should return `"80.00"`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(get_max_profit([8, 2, 5, 10], 20), "80.00")`)
|
|
}})
|
|
```
|
|
|
|
`get_max_profit([4, 5, 3, 6], 20)` should return `"18.00"`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(get_max_profit([4, 5, 3, 6], 20), "18.00")`)
|
|
}})
|
|
```
|
|
|
|
`get_max_profit([54.40, 51.22, 53.99, 50.28, 53.01, 52.84], 200)` should return `"8.31"`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(get_max_profit([54.40, 51.22, 53.99, 50.28, 53.01, 52.84], 200), "8.31")`)
|
|
}})
|
|
```
|
|
|
|
`get_max_profit([15.38, 15.01, 14.99, 14.62, 14.28], 80)` should return `"0.00"`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(get_max_profit([15.38, 15.01, 14.99, 14.62, 14.28], 80), "0.00")`)
|
|
}})
|
|
```
|
|
|
|
`get_max_profit([121.45, 126.82, 122.91, 124.65, 128.83, 128.83, 127.33], 1230.25)` should return `"73.80"`.
|
|
|
|
```js
|
|
({test: () => { runPython(`
|
|
from unittest import TestCase
|
|
TestCase().assertEqual(get_max_profit([121.45, 126.82, 122.91, 124.65, 128.83, 128.83, 127.33], 1230.25), "73.80")`)
|
|
}})
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```py
|
|
def get_max_profit(prices, budget):
|
|
|
|
return prices
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```py
|
|
def get_max_profit(prices, budget):
|
|
max_profit = 0
|
|
|
|
for buy in range(len(prices) - 1):
|
|
for sell in range(buy + 1, len(prices)):
|
|
shares = int(budget / prices[buy])
|
|
profit = shares * (prices[sell] - prices[buy])
|
|
if profit > max_profit:
|
|
max_profit = profit
|
|
|
|
return f"{int(max_profit * 100) / 100:.2f}"
|
|
```
|