Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed meters due to the lack of sun heat. Initially, plant is 0 meters tall. We plant the seed at the beginning of a day. We want to know when the height of the plant will reach a certain level. Example For upSpeed = 100, downSpeed = 10 and desiredHeight = 910, the output should be growingPlant(upSpeed, downSpeed, desiredHeight) = 10. |
code>>
int growingPlant(int upSpeed, int downSpeed, int desiredHeight) {
int dis = upSpeed;
if (dis >= desiredHeight) return 1;
return desiredHeight / (upSpeed - downSpeed);
}
int growingPlant(int upSpeed, int downSpeed, int desiredHeight) {
int grows = upSpeed;
int day = 1;
while (grows < desiredHeight)
{
grows -= downSpeed;
day++;
grows += upSpeed;
}
return day;
}
'알고리즘 > codefights' 카테고리의 다른 글
40>longestDigitsPrefix (0) | 2017.08.08 |
---|---|
39>knapsackLight (0) | 2017.08.08 |
37>arrayMaxConsecutiveSum (0) | 2017.08.07 |
36>differentSymbolsNaive (0) | 2017.07.31 |
35>firstDigit (0) | 2017.07.31 |