力扣之603.连续空余座位

文章目录

  • [1. 603.连续空余座位](#1. 603.连续空余座位)
    • [1.1 题干](#1.1 题干)
    • [1.2 准备数据](#1.2 准备数据)
    • [1.3 思路分析](#1.3 思路分析)
    • [1.4 解法](#1.4 解法)
    • [1.5 结果截图](#1.5 结果截图)

1. 603.连续空余座位

1.1 题干

表: Cinema

±------------±-----+

| Column Name | Type |

±------------±-----+

| seat_id | int |

| free | bool |

±------------±-----+

Seat_id 是该表的自动递增主键列。

在 PostgreSQL 中,free 存储为整数。请使用 ::boolean 将其转换为布尔格式。

该表的每一行表示第 i 个座位是否空闲。1 表示空闲,0 表示被占用。

查找电影院所有连续可用的座位。

返回按 seat_id 升序排序 的结果表。

测试用例的生成使得两个以上的座位连续可用。

结果表格式如下所示。

示例 1:

输入:

Cinema 表:

±--------±-----+

| seat_id | free |

±--------±-----+

| 1 | 1 |

| 2 | 0 |

| 3 | 1 |

| 4 | 1 |

| 5 | 1 |

±--------±-----+

输出:

±--------+

| seat_id |

±--------+

| 3 |

| 4 |

| 5 |

±--------+

1.2 准备数据

sql 复制代码
Create table If Not Exists Cinema (seat_id int primary key auto_increment, free bool)
Truncate table Cinema
insert into Cinema (seat_id, free) values ('1', '1')
insert into Cinema (seat_id, free) values ('2', '0')
insert into Cinema (seat_id, free) values ('3', '1')
insert into Cinema (seat_id, free) values ('4', '1')
insert into Cinema (seat_id, free) values ('5', '1')

1.3 思路分析

1.4 解法

sql 复制代码
select distinct c2.seat_id from Cinema c1,Cinema c2 where abs(c1.seat_id-c2.seat_id)=1 and c1.free=1 and c2.free=1;

1.5 结果截图

相关推荐
做一个码农都是奢望1 分钟前
计算机控制系统:最小拍控制系统设计入门
数据结构·算法
腾视科技TENSORTEC4 分钟前
腾视科技重磅发布AD03行车记录仪DashCam!全维守护,智驭出行新生态
大数据·网络·人工智能·科技·ai·车载系统·车载监控
PNP Robotics4 分钟前
PNP机器人分享Frankal机器人等具身案例开发和实践
大数据·python·学习·机器人·开源
米粒15 分钟前
力扣算法刷题 Day 16
算法·leetcode·职场和发展
重生之后端学习6 分钟前
31. 下一个排列
数据结构·算法·leetcode·职场和发展·排序算法·深度优先
Frostnova丶6 分钟前
LeetCode 3212. 统计X和Y出现次数相等的子矩阵数量
算法·leetcode·矩阵
We་ct8 分钟前
LeetCode 53. 最大子数组和:两种高效解法(动态规划+分治)
前端·算法·leetcode·typescript·动态规划·分治
sin°θ_陈8 分钟前
CVPR 2026的3DGS卷到什么地步?工程语义上探:BrepGaussian如何打通图像到CAD的最后一公里?(Part III 1-3)
python·深度学习·算法·机器学习·3d·webgl
岁岁种桃花儿8 分钟前
Flink从入门到上天系列第二十四篇:Flink中的保存点
大数据·flink
shehuiyuelaiyuehao9 分钟前
算法9,滑动窗口,长度最小的子数组
数据结构·算法·leetcode