Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- 가상환경
- GPT
- 코딩테스트
- MySQL
- 백준
- NLP
- 연구
- join
- ChatGPT
- SQL
- 프로그래머스
- Aspect
- 자연어처리
- SQL 첫걸음
- outer join
- 논문리뷰
- paper review
- 머신러닝
- leetcode
- 파이썬
- Bert
- deepseek
- LLM
- 분산
- transformer
- gpt1
- dfs
- 알고리즘
- ABAE
- 그래프
Archives
- Today
- Total
huginn muninn
[leetcode] 626. Exchange Seats 본문
https://leetcode.com/problems/exchange-seats/description/?envType=study-plan-v2&envId=top-sql-50

- 연속된 두 학생 마다 좌석 id를 바꾸는 솔루션을 작성, 학생수가 홀수인 경우 마지막 학생의 id는 교환되지 않는다.
- 정렬된 결과를 id 오름차순
- 학생 수가 홀수인 경우 마지막 학생의 Id는 교환되지 않음
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| student | varchar |
+-------------+---------+
id is the primary key (unique value) column for this table.
Each row of this table indicates the name and the ID of a student.
id is a continuous increment.
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.
# Write your MySQL query statement below
-- 연속되 두 학생 마다 좌석 id를 바꾸는 솔루션을 작성, 학생수가 홀수인 경우 마지막 학생의 id는 교환되지 않는다.
-- 정렬된 결과를 id 오름차순
-- 학생 수가 홀수인 경우 마지막 학생의 Id는 교환되지 않음
select
case
when s.id%2!=0 and s.id=(select count(s.id) from seat as s) then s.id
when s.id%2=0 then s.id-1 else s.id+1
end as id,
student
from seat as s
order by id
어떻게 풀지 고민을 많이 했는데 홀수이면서 마지막 id이면 바꾸지 않기, 홀수기만 하면 다음 id랑 바꾸고, 짝수면 전 id랑 바꾸는 방법을 떠올렸다. 그래서 위의 코드처럼 구현...
'코딩테스트' 카테고리의 다른 글
| [프로그래머스] 노선별 평균 역 사이 거리 조회하기, 정렬 시 주의할 점 (2) | 2024.07.16 |
|---|---|
| [Leetcode] SQL Medium 1174, 550, 1070, 1045 (0) | 2024.06.29 |
| [leetcode] 185. Department Top Three Salaries SQL (0) | 2024.06.21 |
| [프로그래머스] 보호소에서 중성화한 동물 SQL (0) | 2024.06.21 |
| [프로그래머스] 년, 월, 성별 별 상품 구매 회원 수 구하기 SQL (0) | 2024.06.21 |