Let's make this one less mysterious. For 165. Compare Version Numbers, 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?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are string_split, compareVersion.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- 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:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
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 23.97% of C++ online submissions for Compare Version Numbers.
02//Memory Usage: 6.4 MB, less than 20.14% of C++ online submissions for Compare Version Numbers.
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
10 while(true){
11 pos = str.find(delimiter);
12 //works even if pos is string::npos
13 token = str.substr(0, pos);
14 result.push_back(token);
15 if(pos == string::npos) break;
16 //pos+1 equals to 0, so the line below can't handle this situation
17 str.erase(0, pos+delimiter.length());
18 }
19 return result;
20 };
21
22 int compareVersion(string version1, string version2) {
23 vector<string> vsv1 = string_split(version1, ".");
24 vector<string> vsv2 = string_split(version2, ".");
25
26 // for(string& s : vsv1){
27 // cout << s << " ";
28 // }
29 // cout << endl;
30
31 // for(string& s : vsv2){
32 // cout << s << " ";
33 // }
34 // cout << endl;
35
36 vector<int> viv1, viv2;
37 transform(vsv1.begin(), vsv1.end(), back_inserter(viv1), [](string& s){return stoi(s);});
38 transform(vsv2.begin(), vsv2.end(), back_inserter(viv2), [](string& s){return stoi(s);});
39
40 int swapped = 1;
41 //viv1 is always longer
42 if(viv1.size() < viv2.size()) {
43 swapped = -1;
44 swap(viv1, viv2);
45 }
46
47 int i;
48
49 for(i = 0; i < viv2.size() && viv1[i] == viv2[i]; ++i){}
50
51 if(i == viv2.size()){
52 //viv1[0... viv2.size()) == viv2[0... viv2.size())
53 //assume viv2[ viv2.size()...viv1.size()) is all 0
54 for(i = viv2.size(); i < viv1.size() && viv1[i] == 0; ++i){}
55 return (i == viv1.size()) ? 0 : (viv1[i] > 0) ? 1*swapped : -1*swapped;
56 }else{
57 return (viv1[i] > viv2[i]) ? 1*swapped : -1*swapped;
58 }
59
60 }
61};
Cost