Loading...
You are given an integer target, an integer initialUnits, and an array supplies where supplies[i] = [position_i, amount_i].
You move along a number line from position 0 toward position target, starting with initialUnits units of a resource. Moving one unit of distance consumes one unit of the resource. The i-th supply cache sits at position_i and holds amount_i units; when you are at a cache's position, you may collect all of its units, and each collection counts as one pickup. There is no limit on how many units you can carry.
Return the minimum number of pickups needed to reach position target. If it is impossible, return -1.
Note that arriving at a cache with exactly 0 units remaining still lets you collect from it, and arriving at target with 0 units remaining still counts as reaching it.
Input: target = 1, initialUnits = 1, supplies = []
Output: 0
Explanation: The starting units cover the whole distance; no pickups are needed.
Input: target = 100, initialUnits = 1, supplies = [[10,100]]
Output: -1
Explanation: With 1 unit you can only reach position 1. The first cache at position 10 is out of range, so the target cannot be reached.
Input: target = 100, initialUnits = 10, supplies = [[10,60],[20,30],[30,30],[60,40]]
Output: 2
Explanation: Start with 10 units and move to position 10, arriving with 0 units. Collect 60 units there, then move 50 to position 60, arriving with 10 units. Collect 40 more for a total of 50, then move the remaining 40 to reach position 100. That is 2 pickups.
target, initialUnits ≤109supplies.length ≤500position_i < position_{i+1} < targetamount_i <109Click "Run" to test with sample cases or "Submit" to run all tests.