← Home

1360. Number of Days Between Two Dates

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

This is one of those problems where the clean idea matters more than the amount of code. For 1360. Number of Days Between Two Dates, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. 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 pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are string_split, isLeap, monthDays, yearDays, daysBetweenDates.

Guide

Why?

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

  • 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:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. 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

solution.cpp
01//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Number of Days Between Two Dates.
02//Memory Usage: 8.7 MB, less than 100.00% of C++ online submissions for Number of Days Between Two Dates.
03class Solution {
04public:
05    std::vector<std::string> string_split(std::string str, std::string delimiter){
06        size_t pos = 0;
07        std::string token;
08        std::vector<std::string> result;
09        while ((pos = str.find(delimiter)) != std::string::npos) {
10            token = str.substr(0, pos);
11            result.push_back(token);
12            str.erase(0, pos + delimiter.length());
13        }
14        result.push_back(str);
15        return result;
16    }
17    
18    bool isLeap(int y){
19        if(y % 400 == 0){
20            return true;
21        }else if(y % 100 == 0){
22            return false;
23        }else if(y % 4 == 0){
24            return true;
25        }
26        return false;
27    }
28    
29    int monthDays(int m, int y){
30        if(m <= 7){
31            if(m == 2){
32                if(isLeap(y)) return 29;
33                return 28;
34            }else if(m % 2 == 1){
35                //1,3,5,7
36                return 31;
37            }else{
38                //4,6
39                return 30;
40            }
41        }else{
42            if(m%2 == 0){
43                //8,10,12
44                return 31;
45            }else{
46                //9,11
47                return 30;
48            }
49        }
50    }
51    
52    int yearDays(int y){
53        return isLeap(y) ? 366 : 365;
54    }
55    
56    int daysBetweenDates(string date1, string date2) {
57        vector<string> sdate1 = string_split(date1, "-");
58        vector<string> sdate2 = string_split(date2, "-");
59        int ans = 0;
60        
61        if(sdate1 > sdate2){
62            vector<string> tmp = sdate1;
63            sdate1 = sdate2;
64            sdate2 = tmp;
65        }
66        vector<int> idate1, idate2;
67        
68        std::transform(sdate1.begin(), sdate1.end(), 
69            std::back_inserter(idate1),
70            [](const std::string& s) { return std::stoi(s); }
71        );
72        
73        std::transform(sdate2.begin(), sdate2.end(), 
74            std::back_inserter(idate2),
75            [](const std::string& s) { return std::stoi(s); }
76        );
77        
78        if(idate1[0] < idate2[0]){
79            //idate1 to the end of the year
80            ans += monthDays(idate1[1], idate1[0]) - idate1[2];
81            for(int m = idate1[1]+1; m <= 12; m++){
82                ans += monthDays(m, idate1[0]);
83            }
84            
85            //the years btw them
86            for(int y = idate1[0]+1; y <= idate2[0]-1; y++){
87                ans += yearDays(y);
88            }
89            
90            //the start of next year to idate2
91            for(int m = 1; m <= idate2[1]-1; m++){
92                ans += monthDays(m, idate2[0]);
93            }
94            ans += idate2[2];
95        }else{
96            //same year
97            if(idate1[1] < idate2[1]){
98                ans += monthDays(idate1[1], idate1[0]) - idate1[2];
99                for(int m = idate1[1]+1; m <= idate2[1]-1; m++){
100                    ans += monthDays(m, idate1[0]);
101                }
102                ans += idate2[2];
103            }else{
104                //same month
105                ans += idate2[2] - idate1[2];
106            }   
107        }
108        
109        return ans;
110    }
111};

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.