力扣之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 小时前
Electron 系列文章封面图
算法·架构·前端框架
得物技术1 小时前
从埋点需求到规则资产:Hermes Agent 重构得物数仓工作流
大数据·llm·ai编程
久美子2 小时前
AI驱动数仓建设的Harness工程实践——本体建模、知识分层与上下文工程
大数据
云技纵横2 小时前
Gap Lock 死锁实战:5 秒在本地复现 MySQL 间隙锁死锁
后端·mysql
无响应de神3 小时前
三、用户与权限管理
数据库·mysql
徐小夕3 小时前
JitWord 3.0 正式发布,高精度Word异构解析+复杂组件兼容,打造web端协同Word编辑器
前端·vue.js·算法
大树8818 小时前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠18 小时前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
通信小呆呆18 小时前
当算法有了“五感”:多模态数据融合如何向人体感官协同学习?
人工智能·学习·算法·机器学习·机器人