ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

dot product

2022-06-19 01:31:06  阅读:158  来源: 互联网

标签:product nums int SparseVector 复杂度 list nums1 dot


1570. Dot Product of Two Sparse Vectors Medium

Given two sparse vectors, compute their dot product.

Implement class SparseVector:

  • SparseVector(nums) Initializes the object with the vector nums
  • dotProduct(vec) Compute the dot product between the instance of SparseVector and vec

A sparse vector is a vector that has mostly zero values, you should store the sparse vector efficiently and compute the dot product between two SparseVector.

Follow up: What if only one of the vectors is sparse?

 

Example 1:

Input: nums1 = [1,0,0,2,3], nums2 = [0,3,0,4,0]
Output: 8
Explanation: v1 = SparseVector(nums1) , v2 = SparseVector(nums2)
v1.dotProduct(v2) = 1*0 + 0*3 + 0*0 + 2*4 + 3*0 = 8

Example 2:

Input: nums1 = [0,1,0,0,0], nums2 = [0,0,0,0,2]
Output: 0
Explanation: v1 = SparseVector(nums1) , v2 = SparseVector(nums2)
v1.dotProduct(v2) = 0*0 + 1*0 + 0*0 + 0*0 + 0*2 = 0

Example 3:

Input: nums1 = [0,1,0,0,2,0,0], nums2 = [1,0,0,0,3,0,4]
Output: 6

 

Constraints:

  • n == nums1.length == nums2.length
  • 1 <= n <= 10^5
  • 0 <= nums1[i], nums2[i] <= 100

解法一:直截了当,对应位置相乘,得到结果. 但是对于稀疏向量并且多次重复调用的话 不是非常efficient.

时间复杂度: O(N)

空间复杂度:O(1)

class SparseVector {
    int[] nums;
    SparseVector(int[] nums) {
        this.nums = nums;
    }
    
    // Return the dotProduct of two sparse vectors
    public int dotProduct(SparseVector vec) {
        int sum=0;
        for(int i=0;i<vec.nums.length;i++){
            sum+=nums[i]*vec.nums[i];
        }
        return sum;
    }
}

解法二:当我们引入hashmap,只保存向量的非零部分,那么这样对于稀疏向量的多次调用会节省大量的计算

时间复杂度: O(N)

空间复杂度:O(L)  L为非零部分的size

class SparseVector {
    Map<Integer,Integer> map;
    SparseVector(int[] nums) {
        map = new HashMap();
        for(int i=0;i<nums.length;i++){
            if(nums[i]!=0) map.put(i,nums[i]);
        }
    }
    
    // Return the dotProduct of two sparse vectors
    public int dotProduct(SparseVector vec) {
        int sum=0;
        for(int key:map.keySet()){
            if(vec.map.containsKey(key)){
                sum += map.get(key)*vec.map.get(key);
            }
        }
        return sum;
    }
}

followup:如果两个向量,只有一个是稀疏的,并且两个向量会非常非常大,无法使用hash,怎么办?

解法三:可以尝试只把它们放入list而不是hashmap,然后使用二分搜索进行匹配对应位置。

时间复杂度: O(N logL)

空间复杂度:O(L)  L为非零部分的size

class SparseVector {
    public List<Pair> list;
    SparseVector(int[] nums) {
        list = new ArrayList();
        for(int i=0;i<nums.length;i++){
            if(nums[i]!=0) list.add(new Pair(i,nums[i]));
        }
    }
    // Return the dotProduct of two sparse vectors
    public int dotProduct(SparseVector vec) {
        //针对更稀疏的那个向量进行遍历,对另一个进行二分
        if(list.size()>vec.list.size())
            return dotResult(vec.list,list);
        else
            return dotResult(list,vec.list);
    }
    private int dotResult(List<Pair> list1, List<Pair> list2){
        int result = 0;
        for(Pair pair1:list1){
            //list1中的pair去list2中进行二分搜索匹配
            result += pair1.value*binarySearch(pair1.key, list2);
        }
        return result;
    }
    private int binarySearch(int pos, List<Pair> list){
        int left = 0,right = list.size();
        while(left<right){
            int mid = left+(right-left)/2;
            System.out.println(pos+" "+left+" "+right+" "+mid);
            Pair curr = list.get(mid);
            if(curr.key > pos) right = mid;
            else if(curr.key < pos) left = mid+1;
            else return curr.value;
        }
        return 0;
    }
}
class Pair{
    public int key;
    public int value;
    Pair(int key,int value){
        this.key = key;
        this.value = value;
    }
}

 

标签:product,nums,int,SparseVector,复杂度,list,nums1,dot
来源: https://www.cnblogs.com/cynrjy/p/16389769.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有