Posted on

Description

Submission

class Solution {
    vector<int> nDaysInMonth;
    
    inline int daysInYear(int year) {
        return isLeapYear(year) ? 366 : 365;
    }

    inline int daysInMonth(int month, int year) {
        if(month == 2) {
            return isLeapYear(year) ? 29 : 28;
        }
        return nDaysInMonth[month-1];
    }

    inline bool isLeapYear(int year) {
        return (year % 400 == 0) || (year % 100 != 0 && year % 4 == 0);
    }
public:
    string dayOfTheWeek(int day, int month, int year) {
        int initDay = 1;
        int initMonth = 1;
        int initYear = 1971;
        int initDayOfTheWeek = 5;

        string strDaysOfWeek[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        nDaysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        int nDays = 0;
        for(int i = initYear; i < year; ++i) {
            nDays += daysInYear(i);
        }

        for(int i = initMonth; i < month; ++i) {
            nDays += daysInMonth(i, year);
        }

        nDays += day;
        nDays += initDayOfTheWeek - 1;

        return strDaysOfWeek[nDays % 7];
    }
};

// 1971.01.01 is Friday

Leave a Reply

Your email address will not be published. Required fields are marked *