File tree 1 file changed +13
-10
lines changed
scripts/algorithms/M/Minimum Common Value
1 file changed +13
-10
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 69 ms (Top 81.31%) | Memory: 50.80 MB (Top 73.94%)
2
+
1
3
class Solution {
2
4
public:
3
5
int getCommon (vector<int >& nums1, vector<int >& nums2) {
4
- int i=0 , j=0 , element;
6
+ int i=0 , j=0 , element;
5
7
while (i<nums1.size () && j<nums2.size ()) {
6
- if (nums1[i]<nums2[j])
7
- {
8
+ if (nums1[i]<nums2[j]) {
9
+ // if the ith element in nums1 is smaller than
10
+ // the jth element in nums2, increment pointer i
8
11
i++;
9
- }
10
- else if ( nums1[i]>nums2[j])
11
- {
12
+ } else if (nums1[i]>nums2[j]) {
13
+ // if the ith element in nums1 is greater than
14
+ // the jth element in nums2, increment pointer j
12
15
j++;
13
- }
14
- else if (nums1[i] == nums2[j])
15
- {
16
+ } else if (nums1[i] == nums2[j]) {
17
+ // if the ith and jth elements are equal, return it
18
+ // this will always return the smallest element
16
19
return nums1[i];
17
20
}
18
21
}
19
22
return -1 ;
20
23
}
21
- };
24
+ };
You can’t perform that action at this time.
0 commit comments