Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7427ab3

Browse files
committed
Add a question
1 parent c6c7ec1 commit 7427ab3

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

‎README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,3 +914,37 @@ ORDER BY location DESC, id;
914914
---
915915

916916
**[⬆ Back to Top](#sql-coding-challenges-for-beginners)**
917+
918+
## 31. Growing Plant
919+
920+
Each day a plant is growing by `up_speed` meters. Each night that plant's height declines by `down_speed` meters due to the lack of sun heat. Initially, the plant is `0` meters tall. We plant the seed at the beginning of a day. We want to know the number of days that it will take for the plant to reach or pass a desired height (including the last day in the total count). For example,
921+
- For `up_speed = 100`, `down_speed = 10` and `desired_height = 910`, the output should be `10` days.
922+
- For `up_speed = 10`, `down_speed = 9` and `desired_height = 4`, the output should be `1` day, because the plant already reaches the desired height on the first day.
923+
924+
```
925+
growing_plant output
926+
------------- ------
927+
id id
928+
down_speed num_days
929+
up_speed
930+
desired_height
931+
```
932+
933+
<details><summary>Solution</summary>
934+
935+
```sql
936+
SELECT
937+
id,
938+
CASE
939+
WHEN up_speed >= desired_height THEN 1
940+
ELSE CEIL((desired_height - up_speed)::decimal / (up_speed - down_speed))::int + 1
941+
END
942+
AS num_days
943+
FROM growing_plant;
944+
```
945+
946+
</details>
947+
948+
---
949+
950+
**[⬆ Back to Top](#sql-coding-challenges-for-beginners)**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /