Skip to content

Commit a7dcb6c

Browse files
committed
Daily and other
1 parent 3f24d43 commit a7dcb6c

11 files changed

+600
-43
lines changed

README.md

+45-43
Large diffs are not rendered by default.
+218
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# 350. [Intersection of Two Arrays II](<https://leetcode.com/problems/intersection-of-two-arrays-ii>)
2+
3+
*First completed: July 01, 2024*
4+
5+
*Last updated: July 01, 2024*
6+
7+
8+
> *To see the question prompt, click the title.*
9+
10+
**Topics:** Array, Hash Table, Two Pointers, Binary Search, Sorting
11+
12+
**AC %:** 56.566
13+
14+
15+
## Solutions
16+
17+
- [e350 Daily v1.java](<../my-submissions/e350 Daily v1.java>)
18+
- [e350 Daily v2.java](<../my-submissions/e350 Daily v2.java>)
19+
- [e350.py](<../my-submissions/e350.py>)
20+
### Java
21+
#### [e350 Daily v1.java](<../my-submissions/e350 Daily v1.java>)
22+
```Java
23+
class Solution {
24+
public int[] intersect(int[] nums1, int[] nums2) {
25+
HashMap<Integer, Integer> cnt1 = new HashMap<>();
26+
HashMap<Integer, Integer> cnt2 = new HashMap<>();
27+
28+
for (int i : nums1) {
29+
cnt1.put(i, cnt1.getOrDefault(i, 0) + 1);
30+
}
31+
32+
for (int i : nums2) {
33+
cnt2.put(i, cnt2.getOrDefault(i, 0) + 1);
34+
}
35+
36+
ArrayList<Integer> output = new ArrayList<>();
37+
for (Integer i : cnt1.keySet()) {
38+
for (int j = 0; j < Integer.min(cnt1.get(i), cnt2.getOrDefault(i, 0)); j++) {
39+
output.add(i);
40+
}
41+
}
42+
43+
int[] outputArr = new int[output.size()];
44+
for (int i = 0; i < output.size(); i++) {
45+
outputArr[i] = output.get(i);
46+
}
47+
48+
return outputArr;
49+
}
50+
}
51+
```
52+
53+
#### [e350 Daily v2.java](<../my-submissions/e350 Daily v2.java>)
54+
```Java
55+
class Solution {
56+
public int[] intersect(int[] nums1, int[] nums2) {
57+
HashMap<Integer, Integer> cnt = new HashMap<>();
58+
59+
for (int i : nums1) {
60+
cnt.put(i, cnt.getOrDefault(i, 0) + 1);
61+
}
62+
63+
ArrayList<Integer> output = new ArrayList<>();
64+
for (int i : nums2) {
65+
if (cnt.getOrDefault(i, 0) > 0) {
66+
cnt.put(i, cnt.get(i) - 1);
67+
output.add(i);
68+
}
69+
}
70+
71+
int[] outputArr = new int[output.size()];
72+
for (int i = 0; i < output.size(); i++) {
73+
outputArr[i] = output.get(i);
74+
}
75+
76+
return outputArr;
77+
}
78+
}
79+
```
80+
81+
### Python
82+
#### [e350.py](<../my-submissions/e350.py>)
83+
```Python
84+
class Solution:
85+
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
86+
cnt = Counter(nums1) & Counter(nums2)
87+
output = []
88+
for i, val in cnt.items() :
89+
output.extend([i] * val)
90+
91+
return output
92+
```
93+

my-submissions/e350 Daily v1.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int[] intersect(int[] nums1, int[] nums2) {
3+
HashMap<Integer, Integer> cnt1 = new HashMap<>();
4+
HashMap<Integer, Integer> cnt2 = new HashMap<>();
5+
6+
for (int i : nums1) {
7+
cnt1.put(i, cnt1.getOrDefault(i, 0) + 1);
8+
}
9+
10+
for (int i : nums2) {
11+
cnt2.put(i, cnt2.getOrDefault(i, 0) + 1);
12+
}
13+
14+
ArrayList<Integer> output = new ArrayList<>();
15+
for (Integer i : cnt1.keySet()) {
16+
for (int j = 0; j < Integer.min(cnt1.get(i), cnt2.getOrDefault(i, 0)); j++) {
17+
output.add(i);
18+
}
19+
}
20+
21+
int[] outputArr = new int[output.size()];
22+
for (int i = 0; i < output.size(); i++) {
23+
outputArr[i] = output.get(i);
24+
}
25+
26+
return outputArr;
27+
}
28+
}

my-submissions/e350 Daily v2.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public int[] intersect(int[] nums1, int[] nums2) {
3+
HashMap<Integer, Integer> cnt = new HashMap<>();
4+
5+
for (int i : nums1) {
6+
cnt.put(i, cnt.getOrDefault(i, 0) + 1);
7+
}
8+
9+
ArrayList<Integer> output = new ArrayList<>();
10+
for (int i : nums2) {
11+
if (cnt.getOrDefault(i, 0) > 0) {
12+
cnt.put(i, cnt.get(i) - 1);
13+
output.add(i);
14+
}
15+
}
16+
17+
int[] outputArr = new int[output.size()];
18+
for (int i = 0; i < output.size(); i++) {
19+
outputArr[i] = output.get(i);
20+
}
21+
22+
return outputArr;
23+
}
24+
}

my-submissions/e350.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
3+
cnt = Counter(nums1) & Counter(nums2)
4+
output = []
5+
for i, val in cnt.items() :
6+
output.extend([i] * val)
7+
8+
return output
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def compareVersion(self, version1: str, version2: str) -> int:
3+
v1 = [int(x) for x in version1.split('.')]
4+
v2 = [int(x) for x in version2.split('.')]
5+
6+
if len(v1) < len(v2) :
7+
v1.extend([0 for _ in range(len(v2) - len(v1))])
8+
elif len(v1) > len(v2) :
9+
v2.extend([0 for _ in range(len(v1) - len(v2))])
10+
11+
for one, two in zip(v1, v2) :
12+
if one < two :
13+
return -1
14+
15+
if two < one :
16+
return 1
17+
18+
return 0

0 commit comments

Comments
 (0)