Skip to content

Commit b3669ef

Browse files
authored
Improved task 1185.
1 parent e38e30e commit b3669ef

File tree

1 file changed

+41
-56
lines changed

1 file changed

+41
-56
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,65 @@
11
package g1101_1200.s1185_day_of_the_week;
22

3-
// #Easy #Math #2022_03_03_Time_1_ms_(51.16%)_Space_41_MB_(42.25%)
3+
// #Easy #Math #2022_03_30_Time_0_ms_(100.00%)_Space_41.7_MB_(31.89%)
44

55
public class Solution {
66
public String dayOfTheWeek(int day, int month, int year) {
7-
int yr = chooseYear((year - 1) % 400) % 7;
8-
int m = chooseMonth(month) % 7;
9-
int d = day % 7;
10-
int sum = 0;
11-
12-
if ((((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) && month > 2) {
13-
sum = sum + 1;
7+
int counter = 0;
8+
for (int i = 1971; i < year; i++) {
9+
if (isLeapYear(i)) {
10+
counter += 366;
11+
} else {
12+
counter += 365;
13+
}
1414
}
15-
16-
sum = sum + yr + m + d;
17-
return chooseDay(sum % 7);
18-
}
19-
20-
private int chooseYear(int year) {
21-
if (year >= 100 && year < 200) {
22-
return (int) Math.floor((year - 100.0) / 4) + year - 100 + 5;
23-
} else if (year >= 200 && year < 300) {
24-
return (int) Math.floor((year - 200.0) / 4) + year - 200 + 3;
25-
} else if (year >= 300 && year < 400) {
26-
return (int) Math.floor((year - 300.0) / 4) + year - 300 + 1;
27-
} else {
28-
return (int) Math.floor(year * 1.0 / 4) + year;
15+
for (int i = 1; i < month; i++) {
16+
counter += dayOfMonth(i);
2917
}
30-
}
31-
32-
private int chooseMonth(int month) {
33-
switch (month) {
18+
for (int i = 1; i <= day; i++) {
19+
counter += 1;
20+
}
21+
if (isLeapYear(year) && month > 2) {
22+
counter++;
23+
}
24+
switch (counter % 7) {
25+
case 1:
26+
return "Friday";
3427
case 2:
28+
return "Saturday";
3529
case 3:
36-
return 3;
30+
return "Sunday";
3731
case 4:
38-
return 6;
32+
return "Monday";
3933
case 5:
40-
return 8;
34+
return "Tuesday";
4135
case 6:
42-
return 11;
43-
case 7:
44-
return 13;
45-
case 8:
46-
return 16;
47-
case 9:
48-
return 19;
49-
case 10:
50-
return 21;
51-
case 11:
52-
return 24;
53-
case 12:
54-
return 26;
36+
return "Wednesday";
5537
default:
56-
return 0;
38+
return "Thursday";
5739
}
5840
}
5941

60-
private String chooseDay(int day) {
61-
switch (day) {
62-
case 0:
63-
return "Sunday";
42+
private boolean isLeapYear(int year) {
43+
return ((year % 4 == 0) && (year % 100 != 0)) || year % 400 == 0;
44+
}
45+
46+
private int dayOfMonth(int month) {
47+
switch (month) {
6448
case 1:
65-
return "Monday";
66-
case 2:
67-
return "Tuesday";
6849
case 3:
69-
return "Wednesday";
70-
case 4:
71-
return "Thursday";
7250
case 5:
73-
return "Friday";
51+
case 7:
52+
case 8:
53+
case 10:
54+
case 12:
55+
return 31;
56+
case 4:
7457
case 6:
75-
return "Saturday";
58+
case 9:
59+
case 11:
60+
return 30;
7661
default:
77-
return "";
62+
return 28;
7863
}
7964
}
8065
}

0 commit comments

Comments
 (0)