Algorithm/leetCode

[세무민의 코딩일기] LeetCode Rising Temperature문제풀이

세기루민 2022. 1. 13. 23:12
728x90

오늘 포스팅 할 내용은 LeetCode 문제 중 하나인 Rising Temperature문제를 풀어봤습니다.


1. 문제 설명 


2. 문제 예시 


3. 문제 풀이 

이번 문제는 전일 대비 온도가 높은 ID를 출력해주면 됩니다. 

그렇다면 해당 문제를 접근한는 방법은 현재일과 현재일에서 DATE_ADD를 통해 1일을 추가한 일자와 비교하여 

온도를 비교해주면 된다. 

또 다른 방법은 DATEDIFF를 이용하여 날짜의 차이가 1일인 경우 온도를 비교해주면 된다. 

/**
* LeetCode : Rising Temperature
* URL : https://leetcode.com/problems/rising-temperature/
*/

# DATEDIFF를 통해 날짜의 차이를 구하는 방식
select w1.id
from Weather w1, Weather w2
where DATEDIFF(w1.recordDate, w2.recordDate) = 1
and w1.temperature > w2.temperature;

# DATE_ADD를 통해 1일 추가하는 방식
select w1.id
from Weather w1
join Weather w2
on w1.recordDate = DATE_ADD(w2.recordDate, INTERVAL 1 DAY)
where w1.temperature > w2.temperature;

4. 결과


5. GitHub Code

 

GitHub - sg-moomin/algorithmStudy_CodingTest

Contribute to sg-moomin/algorithmStudy_CodingTest development by creating an account on GitHub.

github.com


이번 포스팅도 LeetCode에 있는 문제를 풀어봤습니다.

오늘은 DATE_ADD에 대해서 알아봤는데 추후에 DB에 대해서 좀 더 다뤄보겠습니다. 

 

728x90