hyezdata 님의 블로그

Exchange Seats 본문

코딩테스트

Exchange Seats

hyezdata 2026. 5. 30. 14:15
# 연속된 id 둘씩 순서 바꾸기
# 햑생 수 홀수면 마지막 학생은 바꾸기x
# id 오름차순

select  case when id%2=0 then id-1
             when (id%2!=0 and id=(select max(id) from seat)) then id
             when id%2!=0 then id+1
        end id,
    student
from seat
order by id

 

 

댓글에 적어놓은 힌트 참고 함

1] You can use a CASE statement to handle swapping between adjacent seats. The key is
to check whether the id is odd or even:
2] If the id is odd, swap the student with the one in the next seat (id + 1).
3] If the id is even, swap the student with the one in the previous seat (id - 1).

 

 

 

Exchange Seats - LeetCode

Can you solve this real interview question? Exchange Seats - Table: Seat +-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | student | varchar | +-------------+---------+ id is the primary key (unique value) column for

leetcode.com

 

728x90
반응형

'코딩테스트' 카테고리의 다른 글

Swap Sex of Employees  (0) 2026.05.30
Biggest Single Number  (0) 2026.05.25
Triangle Judgement  (0) 2026.05.25
Sales Person  (0) 2026.05.25
Customer Placing the Largest Number of Orders  (0) 2026.05.22