-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
coding_interviews/leetcode/medium/watering-plants/tests/watering-plants.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { wateringPlants } from '../watering-plants'; | ||
|
||
describe('wateringPlants', () => { | ||
it('', () => { | ||
expect(wateringPlants([2, 2, 3, 3], 5)).toEqual(14); | ||
}); | ||
|
||
it('', () => { | ||
expect(wateringPlants([1, 1, 1, 4, 2, 3], 4)).toEqual(30); | ||
}); | ||
|
||
it('', () => { | ||
expect(wateringPlants([7, 7, 7, 7, 7, 7, 7], 8)).toEqual(49); | ||
}); | ||
|
||
it('', () => { | ||
expect(wateringPlants([2, 1], 2)).toEqual(4); | ||
}); | ||
|
||
it('', () => { | ||
expect(wateringPlants([3, 2, 4, 2, 1], 6)).toEqual(17); | ||
}); | ||
}); |
46 changes: 46 additions & 0 deletions
46
coding_interviews/leetcode/medium/watering-plants/watering-plants.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
You want to water n plants in your garden with a watering can. The plants are arranged in a row and are labeled from 0 to n - 1 from left to right where the ith plant is located at x = i. There is a river at x = -1 that you can refill your watering can at. | ||
Each plant needs a specific amount of water. You will water the plants in the following way: | ||
Water the plants in order from left to right. | ||
After watering the current plant, if you do not have enough water to completely water the next plant, return to the river to fully refill the watering can. | ||
You cannot refill the watering can early. | ||
You are initially at the river (i.e., x = -1). It takes one step to move one unit on the x-axis. | ||
Given a 0-indexed integer array plants of n integers, where plants[i] is the amount of water the ith plant needs, and an integer capacity representing the watering can capacity, return the number of steps needed to water all the plants. | ||
Example 1: | ||
Input: plants = [2,2,3,3], capacity = 5 | ||
Output: 14 | ||
Example 2: | ||
Input: plants = [1,1,1,4,2,3], capacity = 4 | ||
Output: 30 | ||
Example 3: | ||
Input: plants = [7,7,7,7,7,7,7], capacity = 8 | ||
Output: 49 | ||
*/ | ||
|
||
// [2, 2, 3, 3] | ||
// capacity: 5 -> 3 -> 1 | ||
// steps: 1 -> 2 -> 4 | ||
|
||
export function wateringPlants(plants, capacity) { | ||
let steps = 0; | ||
const CAPACITY = capacity; | ||
|
||
for (let i = 0; i < plants.length; i++) { | ||
steps++; | ||
capacity -= plants[i]; | ||
|
||
if (i < plants.length - 1 && capacity < plants[i + 1]) { | ||
steps += i + 1; | ||
steps += i + 1; | ||
capacity = CAPACITY; | ||
} | ||
} | ||
|
||
return steps; | ||
} |