Simple Fun #74: Growing Plant

Task

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 10.

After day 1 --> 100
After night 1 --> 90
After day 2 --> 190
After night 2 --> 180
After day 3 --> 280
After night 3 --> 270
After day 4 --> 370
After night 4 --> 360
After day 5 --> 460
After night 5 --> 450
After day 6 --> 550
After night 6 --> 540
After day 7 --> 640
After night 7 --> 630
After day 8 --> 730
After night 8 --> 720
After day 9 --> 820
After night 9 --> 810
After day 10 --> 910
After day 1 --> 10

Input/Output

Input

-----------------------------------------
| Table | Column | Type |
|---------------+----------------+------|
| growing_plant | down_speed | int |
| | up_speed | int |
| | desired_height | int |
-----------------------------------------

Columns

  • up_speed: A positive integer representing the daily growth. Constraints: 5 ≤ up_speed ≤ 100.
  • down_speed: A positive integer representing the nightly decline. Constraints: 2 ≤ down_speed < up_speed.
  • desired_height: A positive integer representing the threshold. Constraints: 4 ≤ desired_height ≤ 1000.

Output

-------------------
| Column | Type |
|----------+------|
| id | int |
| num_days | int |
-------------------
SELECT id, 
CASE WHEN desired_height <= up_speed THEN 1
ELSE CEIL((desired_height - up_speed)::decimal / (up_speed - down_speed))::int + 1 END AS num_days
FROM growing_plant;

--

--

--

My homepage to record my thought processes for solving SQL and Algorithm questions

Recommended from Medium

Yours is about the worst code I’ve ever seen. But it runs.

The Future of Mobile Application Development Industry in the Coming Years.

POSIX basics

Docker explained in simple terms!!

Business Law

business law

Ubuntu Setup FCM

Defining a Digital Twin Platform

Revise the whole theory of C programming in one go

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Isabelle

Isabelle

In love with telling stories with data

More from Medium

SQL Introduction 1

An internal hard drive

Embedded SQL and Dynamic SQL a

Basic commands for Oracle SQL

SQL Query optimization using Statistics in OutSystems