A fruit-growers' cooperative logs each member orchard's annual crop yield, in tonnes, for every season the orchard chooses to report. Because orchards join mid-season, skip a year, or eventually leave the cooperative, one orchard's reported years are not guaranteed to be consecutive. For every orchard-year record whose orchard also has a record for the immediately preceding year (year - 1, exactly — not merely 'the previous year that orchard happened to report'), compute the year-over-year growth rate of yield as a percentage: (currentYield - previousYield) / previousYield * 100. Report every such growth rate you can compute.
n — the number of records (1 <= n <= 100000).n lines: three space-separated integers orchardId year yield — orchardId (1 <= orchardId <= 100000) identifies the orchard, year (1 <= year <= 9999) is the season, and yield (1 <= yield <= 10^9) is that orchard's reported tonnage for that season. No two records share the same (orchardId, year) pair. Records are not given in any particular order.For every record (orchardId, year, yield) such that a record (orchardId, year - 1, previousYield) also exists, print one line: orchardId year growth, where growth is (yield - previousYield) / previousYield * 100 rounded to exactly two decimal places (e.g. 20.00, -25.00). Print the lines ordered by orchardId ascending, then by year ascending. If no record qualifies, print nothing.
(orchardId, year) pairs.Example 1
Input
5 1 2018 100 1 2019 120 1 2020 90 2 2019 50 2 2020 75
Expected
1 2019 20.00 1 2020 -25.00 2 2020 50.00
Explanation
Orchard 1 reports 2018->100, 2019->120, 2020->90. Year 2019 has a 2018 record, so growth = (120-100)/100*100 = 20.00; year 2020 has a 2019 record, so growth = (90-120)/120*100 = -25.00; year 2018 has no 2017 record, so it is skipped. Orchard 2 reports 2019->50 and 2020->75: 2019 has no 2018 record so it is skipped, and 2020 has a 2019 record so growth = (75-50)/50*100 = 50.00. Sorted by orchard then year gives: 1 2019 20.00, then 1 2020 -25.00, then 2 2020 50.00.
Example 2
Input
4 3 2000 200 3 2002 300 4 2005 10 4 2006 20
Expected
4 2006 100.00
Explanation
Orchard 3 reports only 2000 and 2002 -- 2002's preceding year, 2001, was never reported, so despite 2000 being on file the gap means 2002 is skipped entirely (and 2000 itself has no 1999 record). Orchard 4 reports 2005->10 and 2006->20; 2006 has an exact 2005 record, so growth = (20-10)/10*100 = 100.00, while 2005 has no 2004 record and is skipped. The only output line is: 4 2006 100.00.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →