12
返回列表 发新帖
楼主: newkid

[每日一题] 趣味SQL题:力扣84. 柱状图中最大的矩形

[复制链接]
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
11#
发表于 2024-8-30 12:27 | 只看该作者
本帖最后由 〇〇 于 2024-8-30 14:59 编辑
newkid 发表于 2024-8-28 22:06
你这个写法确实能拓展思路,可是最后那个CONNECT BY 不就局限于整数了吗?我给你改一下:select   lv as he ...

这个在鸭子里不行
Binder Error: correlated columns in window functions not supported
LINE 7:          select height ,sum(case when height>=lv then 0 else 1 end) over (order by id ) as grp


Binder Error: aggregate function calls cannot be nested
LINE 6: select max(count(*))

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
12#
发表于 2024-8-30 19:56 来自手机 | 只看该作者
postgresql也可以


select
   lv as height_lv
  ,(
    select max(group_cnt) from
        (
        select count(case when height>=lv then 1 end) over (partition by grp) group_cnt
        from
        (
         select height ,sum(case when height>=lv then 0 else 1 end) over (order by id ) as grp
         from rectangle a  
        )b
        )c
   )*lv as max_rectangle
from
(
select lv  
from generate_series(1,10,1) t(lv)
)d;

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
13#
发表于 2024-8-30 20:11 来自手机 | 只看该作者

select
   lv as height
  ,(
    select max(count(*))
    from
        (
         select height ,sum(case when height>=lv then 0 else 1 end) over (order by id ) as grp
         from rectangle a  
        )b
    where height>=lv
    group by grp
   )
   *lv as max_rectangle
from
(
select distinct height as lv  
from rectangle
)d;
ERROR:  aggregate function calls cannot be nested
LINE 4:     select max(count(*))
                       ^

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
14#
发表于 2024-8-31 09:16 | 只看该作者
不用分析函数的版本
with t as
(
select left1,width1,min(height)min_height
from
rectangle a,range(1,7)t(left1),range(1,7)t1(width1)
where left1+width1-1<=6
and id between left1 and left1+width1-1
group by left1,width1
)
select *,min_height*width1 from t where min_height*width1 = (select max(min_height*width1)from t);

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
15#
发表于 2024-8-31 16:48 | 只看该作者
用下面的表测试,2位的sql比我的效率高多了
create table rectangle as select i id, (random()*99)::int+1 height from range(1,1001)t(i);

使用道具 举报

回复
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
16#
发表于 2024-9-1 09:13 | 只看该作者
newkid 发表于 2024-8-28 22:06
你这个写法确实能拓展思路,可是最后那个CONNECT BY 不就局限于整数了吗?我给你改一下:select   lv as he ...

改成了duckdb能用的版本
select lv,max(cnt)*lv max_rectangle
from(select lv,grp,count(*) cnt
from(
select lv,height ,sum(case when height>=lv then 0 else 1 end) over (partition by lv order by id ) as grp
from rectangle ,(select distinct height as lv from rectangle)b
)a
where height>=lv
group by lv,grp
)c
group by lv
order by max_rectangle desc
;

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表