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. Mapreduce的序列化和流量统计程序开发

    一.Hadoop数据序列化的数据类型 Java数据类型 => Hadoop数据类型 int IntWritable float FloatWritable long LongWritable d ...

  2. 理解上下文Context

    --摘自<Android进阶解密> 知识点: 1.Context的使用场景 1)使用Context调用方法,比如启动Activity.访问资源.调用系统级服务等 2)调用方法时传入Cont ...

  3. css实现连续数字和英文的自动换行的方法

    1.(IE浏览器)连续的英文字符和阿拉伯数字,使用word-wrap : break-word ;或者word-break:break-all;实现强制断行 #wrap{word-break:brea ...

  4. jquery提示sucess

    这是学习笔记. 今天做东西的时候,想把体验做好,于是打算再ajax success字段中添加函数实现提示sucess. 用了jquery的fadeIn 跟fadeOut,再fadeIn的callbac ...

  5. Go九九乘法表

    package main import "fmt" func main(){ ; i < ; i ++ { k ++ ; j ++ { { fmt.Printf(" ...

  6. java知识库

    完善自己的成长之路,建立一套属于自己的系统知识体系,温故而知新. --HBX 1.java特性 2.java程序设计环境 3.java基本的设计结构 4.类与对象 参考资料:<java核心技术( ...

  7. Linux系统安装tomcat

    1.首先下载tomcat:http://tomcat.apache.org/download-60.cgi 2.解压缩tar.gz文件: tar -xzvf xxxxxxx/apache-tomcat ...

  8. 手写AVL 树(下)

    上一篇 手写AVL树上实现了AVL树的插入和查询 上代码: 头文件:AVL.h #include <iostream> template<typename T1,typename T ...

  9. linux 软链接和硬链接的区别 和 inode 的理解

    软连接和硬连接的区别:1.创建的方式不同软:ln -s 源文件 连接名硬:ln 源文件 连接名 2.原理不同,和删除源文件对其的影响.硬连接的inode节点和源文件的inode节点一样.也就是同一个i ...

  10. java.util中,util是什么意思

    Util是utiliy的缩写,是一个多功能.基于工具的包. java.util是包含集合框架.遗留的 collection 类.事件模型.日期和时间设施.国际化和各种实用工具类(字符串标记生成器.随机 ...