코딩 테스트 (Java)/코딩 기초 트레이닝 (프로그래머스)

181838. 날짜 비교하기

가지코딩 2025. 4. 23. 09:28

https://school.programmers.co.kr/learn/courses/30/lessons/181838

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr


문제

정수 배열 date1date2가 주어집니다. 두 배열은 각각 날짜를 나타내며 [year, month, day] 꼴로 주어집니다. 각 배열에서 year는 연도를, month는 월을, day는 날짜를 나타냅니다.

만약 date1date2보다 앞서는 날짜라면 1을, 아니면 0을 return 하는 solution 함수를 완성해 주세요.

 

제한사항

  • date1의 길이 = date2의 길이 = 3
    • 0 ≤ year ≤ 10,000
    • 1 ≤ month ≤ 12
    • daymonth에 따라 가능한 날짜로 주어집니다.

 

입출력 예

date1 date2 result
[2021, 12, 28] [2021, 12, 29] 1
[1024, 10, 24] [1024, 10, 24] 0

 

입출력 예 #1

  • date1date2보다 하루 앞서기 때문에 1을 return 합니다.

입출력 예 #2

  • date1date2는 날짜가 서로 같으므로 date1이 더 앞서는 날짜가 아닙니다. 따라서 0을 return 합니다.

풀이

class Solution {
    public int solution(int[] date1, int[] date2) {
        int d1 = date1[0]*10000 + date1[1]*100 + date1[2];
        int d2 = date2[0]*10000 + date2[1]*100 + date2[2];
        return d1 < d2 ? 1 : 0;
    }
}

 

 

 


GitHub: https://github.com/gajicoding/coding_test/edit/main/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4/0/181838.%E2%80%85%EB%82%A0%EC%A7%9C%E2%80%85%EB%B9%84%EA%B5%90%ED%95%98%EA%B8%B0

 

coding_test/프로그래머스/0/181838. 날짜 비교하기 at main · gajicoding/coding_test

This is an auto push repository for Baekjoon Online Judge created with [BaekjoonHub](https://github.com/BaekjoonHub/BaekjoonHub). - gajicoding/coding_test

github.com


출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges

 

코딩테스트 연습 | 프로그래머스 스쿨

개발자 취업의 필수 관문 코딩테스트를 철저하게 연습하고 대비할 수 있는 문제를 총망라! 프로그래머스에서 선발한 문제로 유형을 파악하고 실력을 업그레이드해 보세요!

school.programmers.co.kr