← Home

1185. Day of the Week

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
118

The trick here is to name the state correctly, then let the implementation follow. For 1185. Day of the Week, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are yearDays, monthDays, dayOfTheWeek.

Guide

Why?

The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 4 ms, faster than 55.47% of C++ online submissions for Day of the Week.
02//Memory Usage: 8.2 MB, less than 100.00% of C++ online submissions for Day of the Week.
03class Solution {
04public:
05    vector<string> days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
06    
07    //1971/1/1 is Friday
08    
09    int yearDays(int year){
10        if(year % 400 == 0){
11            return 366;
12        }else if(year % 100 == 0){
13            return 365;
14        }else if(year % 4 == 0){
15            return 366;
16        }
17        return 365;
18    }
19    
20    int monthDays(int month, int year){
21        if(month == 2){
22            if(year % 400 == 0){
23                return 29;
24            }else if(year % 100 == 0){
25                return 28;
26            }else if(year % 4 == 0){
27                return 29;
28            }
29            return 28;
30        }else if((month <= 7 && month % 2 == 1) || 
31                (month >= 8 && month % 2 == 0)){
32            //1,3,5,7,8,10,12
33            return 31;
34        }
35        //4,6,9,11
36        return 30;
37    }
38    
39    string dayOfTheWeek(int day, int month, int year) {
40        int ans = 0;
41        
42        for(int y = 1971; y < year; y++){
43            ans += yearDays(y);
44        }
45        
46        for(int m = 1; m < month; m++){
47            ans += monthDays(m, year);
48        }
49        
50        ans += day;
51        //the difference with 1971/1/1
52        return days[(ans + 5 - 1) % 7];
53    }
54};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.