일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
Tags
- 코딩
- Growth hacking
- LeetCode
- counter
- Stack
- 코테
- lv4
- 코딩테스트
- hackerrank
- lambda
- 완전탐색
- programmers
- mysql
- 프로그래머스
- AARRR
- level4
- coding
- Level2
- 조합
- sql
- itertools
- import re
- tableau
- 파이썬
- coding test
- Queue
- join
- cte
- time complexity
- python
Archives
- Today
- Total
ror_coding
[LeetCode] 626. Exchange Seats 본문
728x90
row_number() 로 홀짝을 구별하여 id에서 빼거나 더한 값을 id로 부여합니다.
따라서 이름은 고정, id만 바뀜.
Question
Write a solution to swap the seat id of every two consecutive students. If the number of students is odd, the id of the last student is not swapped.
Return the result table ordered by id in ascending order.
The result format is in the following example.
Code
select
row_number() over(order by if(id % 2 = 0, id - 1, id + 1)) as id
, student
from seat
order by 1;
Code (My Code)
select
case
when (id%2 = 1) and (id+1 in (select id from seat)) then id + 1
when (id%2 = 0) then id - 1
else id
end as id
, student
from seat
order by 1
;
My Code
728x90
'Coding Test > SQL' 카테고리의 다른 글
[LeetCode] 1321. Restaurant Growth (0) | 2025.07.19 |
---|---|
[LeetCode] 1341. Movie Rating (0) | 2025.07.19 |
[LeetCode] 1907. Count Salary Categories (1) | 2025.07.19 |
[LeetCode] 1934. Confirmation Rate (0) | 2025.07.13 |
[LeetCode] 176. Second Highest Salary (0) | 2025.07.13 |