Tiktok OA Hard SDE

Spend it ALL

There is a budget and an array of n costs. Repeat the following until budget is less than the minimum element in cost.

Process:
Start at element 0 and work to n-1:
At each element cost[i]
if cost[i]<=budget: budget-=cost[i] ; This is a purchase
else move to next index.

The array is circular and continue until there is not enough budget to make a purchase. Determine how many purchases are made?

Constraints:
1<n<=2*10^5
1<=cost[i]<=10^9
1<=budget<=10^15

Example
1 ) cost= [5,8,3], budget=12
Process:
Buy item 0 : budget = 12-5=7
Skep item 1: budget =7
buy item 2 : 7-3=4
skip item 0 : budget =4
skip item 1
buy item 2: 4-3 =1

Total purchases are 3

  1. cost=[2,4,100,2,6] budget = 21
    ans=6

  2. cost =[ 2,2,7,5] and budget=15
    ans=5

Had a really hard time to solve this, can anyone solve this? Im solving from an hour after the OA still not able to get any leads?

Comments (9)