← 返回 goldmansachs 的题目列表State Design in Dynamic Programming
类型:online_judge
Given an array nums with length n, where each element represents the stock price on the ith day, you can complete at most two transactions (buy and sell twice). Find the maximum profit. You cannot engage in multiple transactions simultaneously; i.e., you must sell the stock before you buy again.
Input:
int[] nums: Array of integers with length n (0 ≤ n ≤ 10^5, 0 ≤ nums[i] ≤ 10^5)
Output:
int: Maximum profit achievable
Examples:
Input: nums = [3,3,5,0,0,3,1,4] Output: 6
Input: nums = [1,2,3,4,5] Output: 4
Input: nums = [7,6,4,3,1] Output: 0
Input: nums = [1] Output: 0
Example
Input
3 3 5 0 0 3 1 4