Stock price for day k can be determined using this equation:
p(k) = p + sin(a*k + b) + cos(c*k + d)where p, a, b, c, d are given constants.
Determine largest price drop between day 1 and day n.
Input Format: p, a, b, c, d, n
Test Case:
[
{
input: '42 1 23 4 8 10',
output: '104.855110',
},
]Very similar to this problem: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
Solution:
function stockMaxDecline(input) {
function price(k) {
return p * (Math.sin(a * k + b) + Math.cos(c * k + d) + 2)
}
const [p, a, b, c, d, n] = input.split(' ').map((value) => parseInt(value))
const p1 = price(1)
let max = p1
let index = 2
let globalMaxDecline = 0
while (index <= n) {
const pk = price(index)
const localDecline = max - pk
globalMaxDecline = Math.max(localDecline, globalMaxDecline)
max = Math.max(max, pk)
index++
}
return globalMaxDecline.toFixed(6)
}