Conduct Dot Product of two large Vectors

1. two pointers

2. hashmap

3. 如果没有额外空间,如果一个很大,一个很小,适合scan小的,并且在大的里面做binary search

 package fb;

 public class DotProduct {

     public int dotPro(int[][] v1, int[][] v2) {
int[][] shortV;
int[][] longV;
if (v1.length < v2.length) {
shortV = v1;
longV = v2;
}
else {
shortV = v2;
longV = v1;
} int res = 0;
for (int i=0; i<shortV.length; i++) {
int shortIndex = shortV[i][0];
int shortValue = shortV[i][1];
int longSeq = binarySearch(longV, shortIndex);
if (longSeq >= 0) {
res += shortValue * longV[longSeq][1];
}
}
return res;
} public int binarySearch(int[][] arr, int target) {
int l=0, r=arr.length-1;
while (l <= r) {
int m = (l+r)/2;
if (arr[m][0] == target) return m;
else if (arr[m][0] < target) l = m + 1;
else r = m - 1;
}
return -1;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DotProduct sol = new DotProduct();
int[][] v2 = new int[][]{{0,2},{1,3},{5,2},{7,1},{10,1}};
int[][] v1 = new int[][]{{1,6},{7,2}};
int res = sol.dotPro(v1, v2);
System.out.println(res);
} }

FB面经Prepare: Dot Product的更多相关文章

  1. [UCSD白板题] Minimum Dot Product

    Problem Introduction The dot product of two sequences \(a_1,a_2,\cdots,a_n\) and \(b_1,b_2,\cdots,b_ ...

  2. Dot Product

    These are vectors: They can be multiplied using the "Dot Product" (also see Cross Product) ...

  3. CUDA Samples: dot product(使用零拷贝内存)

    以下CUDA sample是分别用C++和CUDA实现的点积运算code,CUDA包括普通实现和采用零拷贝内存实现两种,并对其中使用到的CUDA函数进行了解说,code参考了<GPU高性能编程C ...

  4. 向量点积(Dot Product),向量叉积(Cross Product)

    参考的是<游戏和图形学的3D数学入门教程>,非常不错的书,推荐阅读,老外很喜欢把一个东西解释的很详细. 1.向量点积(Dot Product) 向量点积的结果有什么意义?事实上,向量的点积 ...

  5. FB面经 Prepare: All Palindromic Substrings

    Given a string, calculate how many substring is palindrome. Ignore non-char characters. Ignore case; ...

  6. FB面经 Prepare: Task Schedule

    tasks has cooldown time, give an input task id array, output finish time input: AABCA A--ABCA output ...

  7. FB面经 Prepare: Make Parentheses valid

    给一组括号,remove最少的括号使得它valid 从左从右各scan一次 package fb; public class removeParen { public static String fi ...

  8. FB面经Prepare: Friends Recommendation

    有个getFriend() API, 让你推荐你的朋友的朋友做你的朋友,当然这个新朋友不能是你原来的老朋友 package fb; import java.util.*; public class R ...

  9. FB面经prepare: Count the number of Vector

    给一个超级大的排好序的vector [abbcccdddeeee]比如,要求返回[{,a}, {,b}, {,c}, {,d}, {,e}......]复杂度要优于O(N) 分析: 如果是binary ...

随机推荐

  1. matplotlib 3D数据-【老鱼学matplotlib】

    直接上代码: import numpy as np import matplotlib.pyplot as plt # 导入显示3D的库 from mpl_toolkits.mplot3d impor ...

  2. Servlet(四):request和response对象

    Request对象:问题: 浏览器发起请求到服务器,会遵循HTTP协议将请求数据发送给服务器. 那么服务器接受到请求的数据改怎么存储呢?不但要存,而且要保证完成性. 解决: 使用对象进行存储,服务器每 ...

  3. 第六篇 flask中session

    Flask中的Session非常的奇怪,他会将你的SessionID存放在客户端的Cookie中,使用起来也非常的奇怪 Flask 中 session 的使用 1. Flask 中 session 是 ...

  4. vue学习:安装及创建项目

    1.先安装npm 参考链接:https://www.cnblogs.com/Hao-Killer/p/7235398.html 查看npm版本:在终端输入:npm -v 2.在安装vue # 安装vu ...

  5. react_app 项目开发

    react_app 项目开发 npm install -g create-react-app npm root -g        // 查看安装包位置 创建项目 create-react-app m ...

  6. vue_源码 原理 剖析

    相关基础知识点 // 可以让 任意函数/方法 成功临时指定成对象的方法进行调用 - call/apply // 1. 根据伪数组生成 真数组 const lis = document.getEleme ...

  7. Ubuntu 安装 Redis和phpredis扩展

    服务器Ubuntu16.04 环境php7.0+Apache /****************************开始安装Redis****************************/ 1 ...

  8. scrapy流程

  9. Source Insight4

    创建工程: File->open                        打开创建的工程 同步文件: 方便跟踪 Project->Synchronize   Files 打开小窗口 ...

  10. PAT甲级1103 Integer Factorization【dfs】【剪枝】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805364711604224 题意: 给定一个数n,要求从1~n中找 ...