240323 아날로그 시계

Jongleee·2024년 3월 23일
0

TIL

목록 보기
528/737
public int solution(int h1, int m1, int s1, int h2, int m2, int s2) {
	int startTime = hmsToSec(h1, m1, s1);
	int endTime = hmsToSec(h2, m2, s2);

	return countAlarms(endTime) - countAlarms(startTime) + (isAlarmAtStartTime(startTime) ? 1 : 0);
}

private int hmsToSec(int h, int m, int s) {
	return h * 3600 + m * 60 + s;
}

private int countAlarms(int time) {
	int hourSecondAlarm = time * 719 / 43200;
	int minuteSecondAlarm = time * 59 / 3600;
	int overlap = 43200 <= time ? 2 : 1;

	return hourSecondAlarm + minuteSecondAlarm - overlap;
}

private boolean isAlarmAtStartTime(int time) {
	return time * 719 % 43200 == 0 || time * 59 % 3600 == 0;
}

출처:
https://school.programmers.co.kr/learn/courses/30/lessons/250135

0개의 댓글