Lag window function

Write codeMedium24 minSnowflake and Databricks style

Your task

Implement the LAG window function to compute per-row deltas: value - LAG(value) OVER (PARTITION BY sensor ORDER BY ts). Given rows [sensor, ts, value] (value int, may arrive unsorted), for each sensor order by ts ascending and compute the difference between consecutive readings. The first reading of each sensor has no predecessor, so its delta is 0. Assume (sensor, ts) is unique. Return [sensor, ts, delta] rows sorted by sensor ascending then ts ascending. Up to 5000 rows.

Examples

lag_delta(rows: list[list]) -> list[list]
Example 1
Inputlag_delta([["s", 1, 10], ["s", 2, 15], ["s", 3, 12]])
Output[["s", 1, 0], ["s", 2, 5], ["s", 3, -3]]