Skip to content

Commit 1302a66

Browse files
committed
Sync LeetCode submission Runtime - 1320 ms (39.86%), Memory - 21.7 MB (19.73%)
1 parent e075aae commit 1302a66

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<p>Given two sparse vectors, compute their dot product.</p>
2+
3+
<p>Implement class <code>SparseVector</code>:</p>
4+
5+
<ul data-indent="0" data-stringify-type="unordered-list">
6+
<li><code>SparseVector(nums)</code>&nbsp;Initializes the object with the vector <code>nums</code></li>
7+
<li><code>dotProduct(vec)</code>&nbsp;Compute the dot product between the instance of <em>SparseVector</em> and <code>vec</code></li>
8+
</ul>
9+
10+
<p>A <strong>sparse vector</strong> is a vector that has mostly zero values, you should store the sparse vector&nbsp;<strong>efficiently </strong>and compute the dot product between two <em>SparseVector</em>.</p>
11+
12+
<p><strong>Follow up:&nbsp;</strong>What if only one of the vectors is sparse?</p>
13+
14+
<p>&nbsp;</p>
15+
<p><strong class="example">Example 1:</strong></p>
16+
17+
<pre>
18+
<strong>Input:</strong> nums1 = [1,0,0,2,3], nums2 = [0,3,0,4,0]
19+
<strong>Output:</strong> 8
20+
<strong>Explanation:</strong> v1 = SparseVector(nums1) , v2 = SparseVector(nums2)
21+
v1.dotProduct(v2) = 1*0 + 0*3 + 0*0 + 2*4 + 3*0 = 8
22+
</pre>
23+
24+
<p><strong class="example">Example 2:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> nums1 = [0,1,0,0,0], nums2 = [0,0,0,0,2]
28+
<strong>Output:</strong> 0
29+
<strong>Explanation:</strong> v1 = SparseVector(nums1) , v2 = SparseVector(nums2)
30+
v1.dotProduct(v2) = 0*0 + 1*0 + 0*0 + 0*0 + 0*2 = 0
31+
</pre>
32+
33+
<p><strong class="example">Example 3:</strong></p>
34+
35+
<pre>
36+
<strong>Input:</strong> nums1 = [0,1,0,0,2,0,0], nums2 = [1,0,0,0,3,0,4]
37+
<strong>Output:</strong> 6
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
<p><strong>Constraints:</strong></p>
42+
43+
<ul>
44+
<li><code>n == nums1.length == nums2.length</code></li>
45+
<li><code>1 &lt;= n &lt;= 10^5</code></li>
46+
<li><code>0 &lt;= nums1[i], nums2[i]&nbsp;&lt;= 100</code></li>
47+
</ul>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Approach 2: Hash Table
2+
3+
# n = length of input array, L = no. of non-zeros elements
4+
# Time: O(n) for creating hash table, O(L) for calculating dot product
5+
# Space: O(L) for creating hash map, O(1) for calculating dot product
6+
7+
class SparseVector:
8+
def __init__(self, nums: List[int]):
9+
self.nonzeros = {}
10+
for i, num in enumerate(nums):
11+
if num != 0:
12+
self.nonzeros[i] = num
13+
14+
15+
# Return the dotProduct of two sparse vectors
16+
def dotProduct(self, vec: 'SparseVector') -> int:
17+
result = 0
18+
for i, num in self.nonzeros.items():
19+
if i in vec.nonzeros:
20+
result += num * vec.nonzeros[i]
21+
22+
return result
23+
24+
25+
# Your SparseVector object will be instantiated and called as such:
26+
# v1 = SparseVector(nums1)
27+
# v2 = SparseVector(nums2)
28+
# ans = v1.dotProduct(v2)

0 commit comments

Comments
 (0)