Maximum Number of Days to Survive
A lender lent money to a borrower, each day a different lender lent the money to the borrower.
The borrower borrowed money on the jth day should payback on the (j+1)th day to maintain good credit and avoid defaulting.
The borrower can payback the jth day loan from the (j+1)th day's borrowed money and can use the leftover money on that day.
Find the maximum number of days the borrower can survive before defaulting.
lender[i] represents the ith lending amount, and payback[i] represents the ith payback amount.
Complete the function maximumNumberOfDaysToSurvive in the editor.
maximumNumberOfDaysToSurvive has the following parameters:
int[] lender: an array of integers representing the lending amountsint[] payback: an array of integers representing the payback amounts
Returns
int: the maximum number of days the borrower can survive before defaulting
1Example 1
1. Choose lender -> 1, so payback is 3.
2. Choose lender -> 4, repay previous payback 3, hence remaining 4-3 = 1 (borrower spends it), the current Payback is 7.
3. Choose lender -> 8, repay previous payback 7, hence remaining 8-7 = 1 (borrower spends it), the current Payback is 9.
4. Left with lender -> 6, cannot repay previous payback which is 9, 9 > 6 hence default.
So the borrower can survive 3 days.
2Example 2
The borrower can pay back each day's loan with the next day's borrowed money without any leftover, thus surviving for all 3 days.
3Example 3
1. Choose lender -> 1, so payback is 2.
2. Choose lender -> 1, repay previous payback 2, hence no remaining (borrower spends it), the current Payback is 2.
3. Left with lender -> 1, cannot repay previous payback which is 2, 2 > 1 hence default.
So the borrower can survive 2 days.