코딩테스트 SQL 코드카타 (Consecutive Numbers)

2024. 12. 12. 11:20·Daily 코드카타/SQL

문제

Consecutive Numbers - LeetCode

Find all numbers that appear at least three times consecutively.

Return the result table in any order.

The result format is in the following example.

 

Example 1:

Input: 
Logs table:
+----+-----+
| id | num |
+----+-----+
| 1  | 1   |
| 2  | 1   |
| 3  | 1   |
| 4  | 2   |
| 5  | 1   |
| 6  | 2   |
| 7  | 2   |
+----+-----+
Output: 
+-----------------+
| ConsecutiveNums |
+-----------------+
| 1               |
+-----------------+
Explanation: 1 is the only number that appears consecutively for at least three times.

 


풀이

  1. 첫번째 숫자와 두번째, 세번째 구분하기
  2. 만약 같다면 반환

 


코드

select distinct num as ConsecutiveNums
from 
(select num, lead(num, 1) over (order by id)as next, lead(num,2) over (order by id) as next2
 from Logs) as nums
 where num = next and next = next2

 


링크

LeetCode/0180-consecutive-numbers at main · K-MarkLee/LeetCode

 

LeetCode/0180-consecutive-numbers at main · K-MarkLee/LeetCode

Collection of LeetCode questions to ace the coding interview! - Created using [LeetHub](https://github.com/QasimWani/LeetHub) - K-MarkLee/LeetCode

github.com

 

 

저작자표시 비영리 변경금지 (새창열림)

'Daily 코드카타 > SQL' 카테고리의 다른 글

코딩테스트 SQL 코드카타 (Exchange Seats)  (0) 2024.12.17
코딩테스트 SQL 코드카타 (Last person)  (0) 2024.12.16
코딩테스트 SQL 코드카타 (Primary Department)  (1) 2024.12.10
코딩테스트 SQL 코드카타 (Number of Employees)  (1) 2024.12.09
코딩테스트 SQL 코드카타 (Followers count)  (1) 2024.12.06
'Daily 코드카타/SQL' 카테고리의 다른 글
  • 코딩테스트 SQL 코드카타 (Exchange Seats)
  • 코딩테스트 SQL 코드카타 (Last person)
  • 코딩테스트 SQL 코드카타 (Primary Department)
  • 코딩테스트 SQL 코드카타 (Number of Employees)
코드 유랑자 승열
코드 유랑자 승열
코드 유랑자 승열의 프로그래밍 일지를 남기는 공간입니다.
  • 코드 유랑자 승열
    승열의 프로그래밍 시네마
    코드 유랑자 승열
  • 전체
    오늘
    어제
  • 링크

    • 깃허브 보러가기
    • 링크드인 보러가기
    • 인스타그램 보러가기
    • 카테고리
      • 코딩테스트
        • BaekJoon
      • TIL and WIL
        • TIL
        • WIL
      • 주말스터디
      • 내일배움캠프
        • 사전캠프 강의 (SQL)
      • 용어정리
        • Python
        • Python-Library
        • Machine-Learning
        • Deep-Learning
        • AI 활용
        • LLM & RAG
        • Docker
        • Django
        • SQL
        • Java Script
        • etc
      • Daily 코드카타
        • SQL
        • Python 알고리즘
      • 임시저장
      • 보류
  • 태그

    View
    llm
    vector db
    RAG
    word2vec
    django
    티스토리챌린지
    template
    langchain
    오블완
  • 인기 글

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
코드 유랑자 승열
코딩테스트 SQL 코드카타 (Consecutive Numbers)
상단으로

티스토리툴바