function getProfit(list: number[]): number { const maxProfitForEach: number[] = []; list.forEach((cost: number, index: number) => { const nextDays = list.slice(index); const maxCost = Math.max.apply(null, nextDays); maxProfitForEach.push(maxCost - cost); }); return Math.max.apply(null, maxProfitForEach); }