LeetCode 218 天际线问题 -revisit

I guess the demo provides a hint on how to efficiently solve the problem.  When only points are needed in final expression, why convert points to ranges and then convert back to points?  Totally waste time.  After play a little bit using Oracle, it proves the above thinking (though hardly see it coming without playing).  Below show the SQL script run in Oracle 12 with input data and output results.  The logic flow should be simple enough for self-explaining.

--one comment here:

the id part in the sql script is only for sql operation and not needed in procedural program (a single loop is enough to reconstruct the output array for desired expression)

with t (sp, ep, ht) as (
select 2, 9, 10 from dual
union all
select 3, 7, 15 from dual
union all
select 5, 12, 12 from dual
union all
select 15, 20, 10 from dual
union all
select 19, 24, 8 from dual
), t1 as (select sp p from t union select ep p from t
), t2 as (select p, max(ht) ht,
row_number() over (order by p) - row_number() over (partition by max(ht) order by p) id
from t right join t1 on p>=sp and p<ep group by p
), t3 as (select min(p) p, ht from t2 group by id, ht
)
select p, nvl(ht,0) ht from t3 order by p
/

         P         HT
---------- ----------
         2         10
         3         15
         7         12
        12          0
        15         10
        20          8
        24          0

7 rows selected.


with t (sp, ep, ht) as (
select 0, 2, 3 from dual
union all
select 2, 5, 3 from dual
), t1 as (select sp p from t union select ep p from t
), t2 as (select p, max(ht) ht,
row_number() over (order by p) - row_number() over (partition by max(ht) order by p) id
from t right join t1 on p>=sp and p<ep group by p
), t3 as (select min(p) p, ht from t2 group by id, ht
)
select p, nvl(ht,0) ht from t3 order by p
/

         P         HT
---------- ----------
         0          3
         5          0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值