Best Time to Buy and Sell Stock II Problem Statement
Given the stock prices for a certain number of days, represented as an array, determine the maximum profit you can achieve. You may perform as many transactions as you like, but you must sell the stock before you buy again.
Input:
The input starts with an integer 't', the number of test cases that follow.
For each test case:
The first line contains an integer 'N' indicating the number of days.
The second line contains 'N' space-separated integers, where each integer denotes the price of the stock on that respective day.
Output:
For each test case, output the maximum profit possible on a new line.
Example:
Input:
t = 2
N = 6
prices = 7 1 5 3 6 4
N = 5
prices = 1 2 3 4 5
Output:
7
4
Explanation:
For the first test case, buying on day 2 and selling on day 3, then buying on day 4 and selling on day 5 yields a maximum profit of 7. For the second test case, buying on day 1 and selling on day 5 yields a maximum profit of 4.
Constraints:
1 ≤ t ≤ 10^2
0 ≤ N ≤ 10^5
- Time Limit: 1 sec
Note:
Implement the function but do not write explicit printing logic, as it is handled separately.
Be the first one to answer
Add answer anonymously...
Top Clarivate Associate Software Engineer interview questions & answers
Popular interview questions of Associate Software Engineer
Top HR questions asked in Clarivate Associate Software Engineer
>
Clarivate Associate Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app