|
| 1 | +# 165. [Compare Version Numbers](<https://leetcode.com/problems/compare-version-numbers>) |
| 2 | + |
| 3 | +*First completed: July 02, 2024* |
| 4 | + |
| 5 | +*Last updated: July 02, 2024* |
| 6 | + |
| 7 | + |
| 8 | +> *To see the question prompt, click the title.* |
| 9 | +
|
| 10 | +**Topics:** Two Pointers, String |
| 11 | + |
| 12 | +**AC %:** 40.83 |
| 13 | + |
| 14 | + |
| 15 | +For both, we simply compare chunk by chunk to check if the values are equal integer-wise. If we run out, we simply iterate through the remainder of each pointer and see if it's equal to zero, or if we need to return +/- 1. |
| 16 | + |
| 17 | +### Version 2: |
| 18 | + |
| 19 | +**Runtime:** $O(n)$ |
| 20 | +**Space:** $O(1)$ |
| 21 | + |
| 22 | +Compared to V2, the process is very similar. In this case, it updates it current indices one at a time to avoid having to use $O(n)$ storage to preprocess and avoids having to compute integer conversions when it's unnecessary (i.e. if the answer is solvable early in the strings length). |
| 23 | + |
| 24 | +### Version 1: |
| 25 | + |
| 26 | +**Runtime:** $O(n)$ |
| 27 | +**Space:** $O(n)$ |
| 28 | + |
| 29 | +Splits both strings and parses them into integers first, extending the array itself by the missing zeros if necessary. This is still $O(1)$ but uses much more space than *Version 2* and also can result in unnecessary processing from when the answer is determined early on in the comparisons. |
| 30 | + |
| 31 | +## Solutions |
| 32 | + |
| 33 | +- [m165 v1 Initial Splitting and Processing.py](<../my-submissions/m165 v1 Initial Splitting and Processing.py>) |
| 34 | +- [m165 v2 Progressive Pass.py](<../my-submissions/m165 v2 Progressive Pass.py>) |
| 35 | +- [m165 v2.c](<../my-submissions/m165 v2.c>) |
| 36 | +### Python |
| 37 | +#### [m165 v1 Initial Splitting and Processing.py](<../my-submissions/m165 v1 Initial Splitting and Processing.py>) |
| 38 | +```Python |
| 39 | +class Solution: |
| 40 | + def compareVersion(self, version1: str, version2: str) -> int: |
| 41 | + v1 = [int(x) for x in version1.split('.')] |
| 42 | + v2 = [int(x) for x in version2.split('.')] |
| 43 | + |
| 44 | + if len(v1) < len(v2) : |
| 45 | + v1.extend([0 for _ in range(len(v2) - len(v1))]) |
| 46 | + elif len(v1) > len(v2) : |
| 47 | + v2.extend([0 for _ in range(len(v1) - len(v2))]) |
| 48 | + |
| 49 | + for one, two in zip(v1, v2) : |
| 50 | + if one < two : |
| 51 | + return -1 |
| 52 | + |
| 53 | + if two < one : |
| 54 | + return 1 |
| 55 | + |
| 56 | + return 0 |
| 57 | +``` |
| 58 | + |
| 59 | +#### [m165 v2 Progressive Pass.py](<../my-submissions/m165 v2 Progressive Pass.py>) |
| 60 | +```Python |
| 61 | +class Solution: |
| 62 | + def compareVersion(self, version1: str, version2: str) -> int: |
| 63 | + one, two = 0, 0 |
| 64 | + |
| 65 | + while one < len(version1) and two < len(version2) : |
| 66 | + oneNew = version1.find('.', one) |
| 67 | + twoNew = version2.find('.', two) |
| 68 | + |
| 69 | + if oneNew < 0 : |
| 70 | + oneNew = len(version1) |
| 71 | + if twoNew < 0 : |
| 72 | + twoNew = len(version2) |
| 73 | + |
| 74 | + oneInt = int(version1[one:oneNew]) |
| 75 | + twoInt = int(version2[two:twoNew]) |
| 76 | + |
| 77 | + if oneInt < twoInt : |
| 78 | + return -1 |
| 79 | + if twoInt < oneInt : |
| 80 | + return 1 |
| 81 | + |
| 82 | + one = oneNew + 1 |
| 83 | + two = twoNew + 1 |
| 84 | + |
| 85 | + while one < len(version1) : |
| 86 | + oneNew = version1.find('.', one) |
| 87 | + if oneNew < 0 : |
| 88 | + oneNew = len(version1) |
| 89 | + oneInt = int(version1[one:oneNew]) |
| 90 | + |
| 91 | + if oneInt < 0 : |
| 92 | + return -1 |
| 93 | + if oneInt > 0 : |
| 94 | + return 1 |
| 95 | + |
| 96 | + one = oneNew + 1 |
| 97 | + |
| 98 | + while two < len(version2) : |
| 99 | + twoNew = version2.find('.', two) |
| 100 | + if twoNew < 0 : |
| 101 | + twoNew = len(version2) |
| 102 | + twoInt = int(version2[two:twoNew]) |
| 103 | + |
| 104 | + if twoInt > 0 : |
| 105 | + return -1 |
| 106 | + if twoInt < 0 : |
| 107 | + return 1 |
| 108 | + |
| 109 | + two = twoNew + 1 |
| 110 | + |
| 111 | + return 0 |
| 112 | +``` |
| 113 | + |
| 114 | +### C |
| 115 | +#### [m165 v2.c](<../my-submissions/m165 v2.c>) |
| 116 | +```C |
| 117 | +int compareVersion(char* version1, char* version2) { |
| 118 | + int one = 0; |
| 119 | + int two = 0; |
| 120 | + |
| 121 | + while (true) { |
| 122 | + int oneRight = one + 1; |
| 123 | + int twoRight = two + 1; |
| 124 | + while (version1[oneRight] && version1[oneRight] != '.') { |
| 125 | + oneRight++; |
| 126 | + } |
| 127 | + while (version2[twoRight] && version2[twoRight] != '.') { |
| 128 | + twoRight++; |
| 129 | + } |
| 130 | + |
| 131 | + int oneVal = 0; |
| 132 | + int twoVal = 0; |
| 133 | + for (int i = one; i < oneRight; i++) { |
| 134 | + oneVal = 10 * oneVal + (version1[i] - '0'); |
| 135 | + } |
| 136 | + for (int i = two; i < twoRight; i++) { |
| 137 | + twoVal = 10 * twoVal+ (version2[i] - '0'); |
| 138 | + } |
| 139 | + |
| 140 | + if (oneVal < twoVal) { |
| 141 | + return -1; |
| 142 | + } |
| 143 | + if (oneVal > twoVal) { |
| 144 | + return 1; |
| 145 | + } |
| 146 | + |
| 147 | + one = oneRight; |
| 148 | + two = twoRight; |
| 149 | + |
| 150 | + if (!version1[oneRight] && !version2[twoRight]) { |
| 151 | + break; |
| 152 | + } |
| 153 | + if (!version1[oneRight]) { |
| 154 | + two++; |
| 155 | + break; |
| 156 | + } |
| 157 | + if (!version2[twoRight]) { |
| 158 | + one++; |
| 159 | + break; |
| 160 | + } |
| 161 | + one++; |
| 162 | + two++; |
| 163 | + } |
| 164 | + |
| 165 | + // Finish off one |
| 166 | + while (version1[one]) { |
| 167 | + int oneRight = one + 1; |
| 168 | + while (version1[oneRight] && version1[oneRight] != '.') { |
| 169 | + oneRight++; |
| 170 | + } |
| 171 | + |
| 172 | + int oneVal = 0; |
| 173 | + for (int i = one; i < oneRight; i++) { |
| 174 | + oneVal = 10 * oneVal + (version1[i] - '0'); |
| 175 | + } |
| 176 | + |
| 177 | + if (oneVal < 0) { |
| 178 | + return -1; |
| 179 | + } |
| 180 | + if (oneVal > 0) { |
| 181 | + return 1; |
| 182 | + } |
| 183 | + if (!version1[oneRight]) { |
| 184 | + break; |
| 185 | + } |
| 186 | + |
| 187 | + one = oneRight + 1; |
| 188 | + } |
| 189 | + |
| 190 | + |
| 191 | + // Finish off two |
| 192 | + while (version2[two]) { |
| 193 | + int twoRight = two + 1; |
| 194 | + while (version2[twoRight] && version2[twoRight] != '.') { |
| 195 | + twoRight++; |
| 196 | + } |
| 197 | + int twoVal = 0; |
| 198 | + for (int i = two; i < twoRight; i++) { |
| 199 | + twoVal = 10 * twoVal + (version2[i] - '0'); |
| 200 | + } |
| 201 | + |
| 202 | + if (twoVal > 0) { |
| 203 | + return -1; |
| 204 | + } |
| 205 | + if (twoVal < 0) { |
| 206 | + return 1; |
| 207 | + } |
| 208 | + if (!version2[twoRight]) { |
| 209 | + break; |
| 210 | + } |
| 211 | + |
| 212 | + two = twoRight + 1; |
| 213 | + } |
| 214 | + |
| 215 | + return 0; |
| 216 | +} |
| 217 | +``` |
| 218 | +
|
0 commit comments