From e22e8f5a8fee3a7ad8033617117cd53bd7ea0f56 Mon Sep 17 00:00:00 2001 From: TK Date: Wed, 9 Feb 2022 23:20:22 -0300 Subject: [PATCH] watering-plants --- .../tests/watering-plants.test.js | 24 ++++++++++ .../medium/watering-plants/watering-plants.js | 46 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 coding_interviews/leetcode/medium/watering-plants/tests/watering-plants.test.js create mode 100644 coding_interviews/leetcode/medium/watering-plants/watering-plants.js diff --git a/coding_interviews/leetcode/medium/watering-plants/tests/watering-plants.test.js b/coding_interviews/leetcode/medium/watering-plants/tests/watering-plants.test.js new file mode 100644 index 00000000..c50ab565 --- /dev/null +++ b/coding_interviews/leetcode/medium/watering-plants/tests/watering-plants.test.js @@ -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); + }); +}); diff --git a/coding_interviews/leetcode/medium/watering-plants/watering-plants.js b/coding_interviews/leetcode/medium/watering-plants/watering-plants.js new file mode 100644 index 00000000..167b06af --- /dev/null +++ b/coding_interviews/leetcode/medium/watering-plants/watering-plants.js @@ -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; +}