Created
March 31, 2020 04:05
-
-
Save Nicknyr/aab98ad6dcb39b77e92a279831f0f7ae to your computer and use it in GitHub Desktop.
CodeSignal - Growing Plant
This file contains hidden or 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
| /* | |
| Caring for a plant can be hard work, but since you tend to it regularly, you have a plant that grows consistently. Each day, its height increases by a fixed amount represented by the integer upSpeed. But due to lack of sunlight, the plant decreases in height every night, by an amount represented by downSpeed. | |
| Since you grew the plant from a seed, it started at height 0 initially. Given an integer desiredHeight, your task is to find how many days it'll take for the plant to reach this height. | |
| Example | |
| For upSpeed = 100, downSpeed = 10, and desiredHeight = 910, the output should be | |
| growingPlant(upSpeed, downSpeed, desiredHeight) = 10. | |
| # Day Night | |
| 1 100 90 | |
| 2 190 180 | |
| 3 280 270 | |
| 4 370 360 | |
| 5 460 450 | |
| 6 550 540 | |
| 7 640 630 | |
| 8 730 720 | |
| 9 820 810 | |
| 10 910 900 | |
| The plant first reaches a height of 910 on day 10. | |
| */ | |
| function growingPlant(upSpeed, downSpeed, desiredHeight) { | |
| let currentHeight = 0; | |
| let days = 0; | |
| if(upSpeed > desiredHeight) { | |
| return 1; | |
| } | |
| while(currentHeight < desiredHeight - downSpeed) { | |
| days++; | |
| currentHeight += upSpeed - downSpeed; | |
| } | |
| return days; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment