This problem looks busy at first, but the accepted solution is built around one steady invariant. For 1154. Day of the Year, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. 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?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are string_split, dayOfYear.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- Substring checks are convenient but not free, so they are part of the real complexity story.
- 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:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
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
01//Runtime: 4 ms, faster than 57.01% of C++ online submissions for Day of the Year.
02//Memory Usage: 8.5 MB, less than 100.00% of C++ online submissions for Day of the Year.
03
04class Solution {
05public:
06 std::vector<std::string> string_split(std::string str, std::string delimiter){
07 size_t pos = 0;
08 std::string token;
09 std::vector<std::string> result;
10 while ((pos = str.find(delimiter)) != std::string::npos) {
11 token = str.substr(0, pos);
12 result.push_back(token);
13 str.erase(0, pos + delimiter.length());
14 }
15 result.push_back(str);
16 return result;
17 }
18
19 int dayOfYear(string date) {
20 vector<string> vs = string_split(date, "-");
21 vector<int> ymd;
22 int ans;
23
24 std::transform(vs.begin(), vs.end(), std::back_inserter(ymd),
25 [](const std::string& s) { return std::stoi(s); });
26
27 ans = ymd[2]; //d
28
29 for(int m = 1; m < ymd[1]; m++){
30 if(m == 2){
31 ans += 28;
32 if(ymd[0] % 400 == 0){
33 ans += 1;
34 }else if(ymd[0] % 100 == 0){
35 //100's multiple, not 400's multiple
36 //is not leap year
37 }else if(ymd[0] % 4 == 0){
38 ans += 1;
39 }
40 }else if(m < 8){
41 if(m % 2 == 1) ans += 31;
42 else ans += 30;
43 }else{
44 if(m % 2 == 1) ans += 30;
45 else ans += 31;
46 }
47 }
48
49 return ans;
50 }
51};
Cost