FB面经Prepare: Dot Product
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的更多相关文章
- [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_ ...
- Dot Product
These are vectors: They can be multiplied using the "Dot Product" (also see Cross Product) ...
- CUDA Samples: dot product(使用零拷贝内存)
以下CUDA sample是分别用C++和CUDA实现的点积运算code,CUDA包括普通实现和采用零拷贝内存实现两种,并对其中使用到的CUDA函数进行了解说,code参考了<GPU高性能编程C ...
- 向量点积(Dot Product),向量叉积(Cross Product)
参考的是<游戏和图形学的3D数学入门教程>,非常不错的书,推荐阅读,老外很喜欢把一个东西解释的很详细. 1.向量点积(Dot Product) 向量点积的结果有什么意义?事实上,向量的点积 ...
- FB面经 Prepare: All Palindromic Substrings
Given a string, calculate how many substring is palindrome. Ignore non-char characters. Ignore case; ...
- FB面经 Prepare: Task Schedule
tasks has cooldown time, give an input task id array, output finish time input: AABCA A--ABCA output ...
- FB面经 Prepare: Make Parentheses valid
给一组括号,remove最少的括号使得它valid 从左从右各scan一次 package fb; public class removeParen { public static String fi ...
- FB面经Prepare: Friends Recommendation
有个getFriend() API, 让你推荐你的朋友的朋友做你的朋友,当然这个新朋友不能是你原来的老朋友 package fb; import java.util.*; public class R ...
- FB面经prepare: Count the number of Vector
给一个超级大的排好序的vector [abbcccdddeeee]比如,要求返回[{,a}, {,b}, {,c}, {,d}, {,e}......]复杂度要优于O(N) 分析: 如果是binary ...
随机推荐
- ubantu中执行docker免sudo方法
1.添加用户组,如果已存在则不用设置. sudo groupadd docker 2.将用户加入该 group (docker)内 sudo gpasswd -a ${USER} docker 3.重 ...
- iframe ios中h5页面 样式变大
实际项目开发中,iframe在移动设备中使用问题还是很大的,说一说我的那些iframe坑 做过的这个后台管理框架,最开始的需求是PC,但随着业务需要,需要将项目兼容到ipad,后台的框架也是使用的开源 ...
- JS对象2
1.Date对象 创建对象 //方法1:不指定参数 var nowd1=new Date(); alert(nowd1.toLocaleString( )); //方法2:参数为日期字符串 var n ...
- yield学习笔记
参考:http://www.dabeaz.com/finalgenerator/ from concurrent.futures import ThreadPoolExecutor import ti ...
- [LeetCode] Soup Servings 供应汤
There are two types of soup: type A and type B. Initially we have N ml of each type of soup. There a ...
- TP5在前端时间戳转换为时间格式
value="{:date('Y-m-d H:i:s',$data['add_date'])}" 例如: <td>{:date('Y-m-d H:i:s',$d[' ...
- 变量类型-Dict
教程:一:字典的创建 1:字典的介绍------>d = {key1:value1, key2:values2} (1)dictionary(字典) 是 Python 中最有用的数 ...
- 解决Tomcatt下连接数据库的classNoFount问题
在数据库连接单独使用的时候.即作为一个独立类建立在mian方法中,可以正确的使用.例:连接MySql数据库 import java.sql.*; public class SQLtest { // J ...
- python语法_列表list_列表内置方法
list 列表 a = ['kevin','alxe','gm','dql','gyx'] a[1] = 'alxe' #按照索引获取数据,索引按照0开始,第一个数据的索引为0 切片: a[1:3] ...
- dynamic 类型不能访问属性
x //解决方案ProjectTest.项目A里面public object r(){ ,name = "zf"}; } //解决方案ProjectTest.项目B里面 publi ...