-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm1570 v2.c
56 lines (47 loc) · 1.42 KB
/
m1570 v2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
typedef struct {
int* nums;
int numsSize;
} SparseVector;
SparseVector* sparseVectorCreate(int* nums, int numsSize) {
// number of values to store
int newNumSize = 0;
for (int i = 0; i < numsSize; i++) {
if (nums[i]) {
newNumSize++;
}
}
// storing the values
SparseVector* data = malloc(sizeof(SparseVector));
data->nums = (int*) malloc(sizeof(int) * newNumSize * 2);
data->numsSize = newNumSize;
for (int i = 0, currPointer = 0; i < numsSize; i++) {
if (nums[i]) {
data->nums[currPointer] = i;
data->nums[currPointer + 1] = nums[i];
currPointer += 2;
}
}
return data;
}
// Return the dotProduct of two sparse vectors
int sparseVectordotProduct(SparseVector* obj, SparseVector* vec) {
int output = 0;
for (int i = 0, j = 0; i < 2 * obj->numsSize && j < 2 * vec->numsSize;) {
if (obj->nums[i] < vec->nums[j]) {
i += 2;
} else if (obj->nums[i] > vec->nums[j]) {
j += 2;
} else {
output += obj->nums[i + 1] * vec->nums[j + 1];
i += 2;
j += 2;
}
}
return output;
}
/**
* Your SparseVector struct will be instantiated and called as such:
* SparseVector* v1 = sparseVectorCreate(nums1, nums1Size);
* SparseVector* v2 = sparseVectorCreate(nums2, nums2Size);
* int ans = sparseVectordotProduct(v1, v2);
*/