0 like 0 dislike
2,461 views
| 2,461 views

0 like 0 dislike
1. Climbing steps with 2,4,6
2. Leetcode easy level problem: see below

Traveler Fund

A traveler is traveling form city of zeta to omega. He starts with
X amount of money. Every day he spends some money and also he may
work on some days to earn money. He may find good work some day
and end up earning more than what he spends that day. It also may
happen that he spends more than what he earns on any day.
You are given an array of integers which represents his net savings
(earning -expense) on any day. You need to find out minimum amount
the traveler should begin with to ensure that he always have some
money (>0) at the end of any day.

Constraints:
-200<=a; <=200 , where aj are array elements
O<<=100, where i is the array length
X>=0

For example:

Input:
3 //Array length
4 //Array elements start
2
-3

output:
0

Explanation:
Traveler saves \$4 on first day, \$2 on second day and \$-3 on third day
(expense is more on 3 day than earnings).
End of the first day, he has X + \$4
End of the Second day, he has X + \$(4+2)
End of third day, he has X + \$(4+2-3)

by Expert (113,040 points)
0 like 0 dislike

Please find the solution below for Travel fund
// Just add the sum difference to startSum and plus 1 to keep currentSum > 0

int startSum = 0;
int currentSum = 0;
for(int i=0;i<arr.length;i++){
currentSum = currentSum + arr[i];
if(currentSum <=0){
startSum = startSum + Math.abs(currentSum)+1;
currentSum = currentSum + startSum;
}
}
return startSum;

this sounds like a variation of https://leetcode.com/problems/gas-station/

All you need to do is add the value (sum += arr[i]) and when sum < 0 max_req = max(max_req, sum) then in end output max_req. But the constraints are very low so I think you are missing something in question

by Expert (113,040 points)