[LeetCode] 603. Consecutive Available Seats_Easy tag: SQL
Several friends at a cinema ticket office would like to reserve consecutive available seats.
Can you help to query all the consecutive available seats order by the seat_id using the following cinema table?
| seat_id | free |
|---------|------|
| 1 | 1 |
| 2 | 0 |
| 3 | 1 |
| 4 | 1 |
| 5 | 1 |
Your query should return the following result for the sample case above.
| seat_id |
|---------|
| 3 |
| 4 |
| 5 |
Note:
- The seat_id is an auto increment int, and free is bool ('1' means free, and '0' means occupied.).
- Consecutive available seats are more than 2(inclusive) seats consecutively available.
这个题目参考solution, 就是先得到seat_id 和所有seat_id对应的表格, 然后再选两个的差值是1的, 最后Distinct 并且 in order.
Code
SELECT DISTINCT c1.seat_id FROM cinema AS c1 JOIN cinema AS c2 WHERE abs(c1.seat_id - c2.seat_id) = 1 and c1.free = True and c2.free = True
order by c1.seat_id
[LeetCode] 603. Consecutive Available Seats_Easy tag: SQL的更多相关文章
- [LeetCode] 619. Biggest Single Number_Easy tag: SQL
Table number contains many numbers in column num including duplicated ones.Can you write a SQL query ...
- [LeetCode] 620. Not Boring Movies_Easy tag: SQL
X city opened a new cinema, many people would like to go to this cinema. The cinema also gives out a ...
- [LeetCode] 176. Second Highest Salary_Easy tag: SQL
Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...
- [LeetCode] 196. Delete Duplicate Emails_Easy tag: SQL
Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...
- [LeetCode] 584. Find Customer Referee_Easy tag: SQL
Given a table customer holding customers information and the referee. +------+------+-----------+ | ...
- LeetCode Database: Consecutive Numbers
Consecutive Numbers Write a SQL query to find all numbers that appear at least three times consecuti ...
- [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- LeetCode——Longest Consecutive Sequence
LeetCode--Longest Consecutive Sequence Question Given an unsorted array of integers, find the length ...
- LeetCode——Max Consecutive Ones
LeetCode--Max Consecutive Ones Question Given a binary array, find the maximum number of consecutive ...
随机推荐
- js中parseInt和Number
昨天在项目中遇到一个问题,有关字符串准换成数字的问题,具体如下: 页面中<input type="number" id="bankNum" ng-mode ...
- LogisticRegression 和 LogisticRegressionCV
在scikit-learn中,与逻辑回归有关的主要是这3个类.LogisticRegression, LogisticRegressionCV 和logistic_regression_path.其中 ...
- Ubuntu下搭建高匿HTTP代理(亲测可用)
功能用途 我们在生活中见过各种代理,比如我们距离火车站较远,我们可以选择通过距离最近的火车票代售点来购买火车票.又比如商品代理商,我们拿不到厂家的直接或者,可以通过厂家授权的代理经销商来获得产品.代理 ...
- 转载]PhpCms V9调用指定栏目子栏目文章的两种方法
PhpCms V9调用指定栏目子栏目文章的两种方法 第一种.直接写子栏目id ,用cat in {pc:get sql="SELECT * from v9_news where status ...
- Redis 应该是存放的数据超出了范围
使用ServiceStack操作Redis 发生了 System.StackOverflowException HResult=0x800703E9 Source=<无法计算异常源> St ...
- [No0000B3].NET C# 单体模式(Singleton)
单体模式(Singleton)是经常为了保证应用程序操作某一全局对象,让其保持一致而产生的对象,例如对文件的读写操作的锁定,数据库操作的时候的事务回滚,还有任务管理器操作,都是一单体模式读取的.创建一 ...
- sklearn.utils.shuffle-训练数据打乱的最佳方法
在进行模型训练前,我们要将数据打乱,以获得更好的训练效果.可以使用sklearn.utils中的shuffle,获得打乱后的数据索引,最后,迭代生成打乱后的batch数据,一个写好的模块如下. 思路是 ...
- day 0308 编码的进阶 文件操作
一.编码的进阶: 在python3以后,字符串和bytes类型彻底分开,字符串以字符为单位进行处理的,bytes类型是以字节为单位处理的. bytes数据类型在所有的操作和使用与字符串方法基本一样,也 ...
- OC中如何优化代理是否响应某个方法
看以下示例代码: if([_delegate respondsToSelector: @selector(someClassDidSomething:)){ [_delegate someClassD ...
- Python判断字符串是否为字母或者数字
严格解析:有除了数字或者字母外的符号(空格,分号,etc.)都会Falseisalnum()必须是数字和字母的混合isalpha()不区分大小写 str_1 = "123" str ...