|  | 
| 1 | 1 | package g1101_1200.s1185_day_of_the_week; | 
| 2 | 2 | 
 | 
| 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%) | 
| 4 | 4 | 
 | 
| 5 | 5 | public class Solution { | 
| 6 | 6 |     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 | +            } | 
| 14 | 14 |         } | 
| 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); | 
| 29 | 17 |         } | 
| 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"; | 
| 34 | 27 |             case 2: | 
|  | 28 | +                return "Saturday"; | 
| 35 | 29 |             case 3: | 
| 36 |  | -                return 3; | 
|  | 30 | +                return "Sunday"; | 
| 37 | 31 |             case 4: | 
| 38 |  | -                return 6; | 
|  | 32 | +                return "Monday"; | 
| 39 | 33 |             case 5: | 
| 40 |  | -                return 8; | 
|  | 34 | +                return "Tuesday"; | 
| 41 | 35 |             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"; | 
| 55 | 37 |             default: | 
| 56 |  | -                return 0; | 
|  | 38 | +                return "Thursday"; | 
| 57 | 39 |         } | 
| 58 | 40 |     } | 
| 59 | 41 | 
 | 
| 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) { | 
| 64 | 48 |             case 1: | 
| 65 |  | -                return "Monday"; | 
| 66 |  | -            case 2: | 
| 67 |  | -                return "Tuesday"; | 
| 68 | 49 |             case 3: | 
| 69 |  | -                return "Wednesday"; | 
| 70 |  | -            case 4: | 
| 71 |  | -                return "Thursday"; | 
| 72 | 50 |             case 5: | 
| 73 |  | -                return "Friday"; | 
|  | 51 | +            case 7: | 
|  | 52 | +            case 8: | 
|  | 53 | +            case 10: | 
|  | 54 | +            case 12: | 
|  | 55 | +                return 31; | 
|  | 56 | +            case 4: | 
| 74 | 57 |             case 6: | 
| 75 |  | -                return "Saturday"; | 
|  | 58 | +            case 9: | 
|  | 59 | +            case 11: | 
|  | 60 | +                return 30; | 
| 76 | 61 |             default: | 
| 77 |  | -                return ""; | 
|  | 62 | +                return 28; | 
| 78 | 63 |         } | 
| 79 | 64 |     } | 
| 80 | 65 | } | 
0 commit comments