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 ...
随机推荐
- ionic2中使用极光IM的WebSDK实现即时聊天
本文主要介绍如何在ionic项目中集成极光IM的WebSDK,详细文档可参考官方介绍. 一.准备 1. 注册激光账号,进入开发者服务页面创建应用. 2. 创建应用后须完成对应平台的推送设置,进行应用或 ...
- 通过linux版本的lr agent提示找不到web_reg_save_param_ex函数
Action.c(5): Error: C interpreter run time error: /tmp/brr_TSBgR2/netdir/E/lrscript/aaa/Action.c (5) ...
- css结构选择器组合使用,选择父元素中多个子元素中某一段元素
nth-of-type()和nth-child()写法一样,这里只用nth-of-type()演示,习惯type 直接上代码 /* 从前向后选择,第6个开始 */ li:nth-of-type(n+6 ...
- Beta(1/7)
鐵鍋燉腯鱻 项目:小鱼记账 团队成员 项目燃尽图 冲刺情况描述 站立式会议照片 各成员情况 团队成员 学号 姓名 git地址 博客地址 031602240 许郁杨 (组长) https://githu ...
- leetcode算法题整理
一.线性表,如数组,单链表,双向链表 线性表.数组 U1.有序数组去重,返回新数组长度 A = [1,1,2] -> [1,2] 返回2 分析:其实一般数组的问题都可以用两个指针解决,一个指 ...
- js 类数组对象arguments
function Add() { for (var i = 0; i < arguments.length; i++) { console.log(arguments[i]); } } Add( ...
- 转UI么?不想
最近一直在弄UI 对于UI实在是不想弄了 很痛苦...我一开始都比较热衷后台开发 但是前端UI 我只是有点兴趣而已,但是还一直要做...太累了 或许要学的东西还是有很多的!一直不知道 ...
- Git服务器安装详解及安装遇到问题解决方案
git是一个不错的版本管理的工具.现在自己在搞一个简单的应用程序开发,想使用git来进行管理.在Google了配置文档后,还是受了N多的挫折.某些文档质量不高,浪费了好多时间...... 好,切入正题 ...
- mysql完整性约束
第一:完整性约束介绍 为了防止不符合规范的数据进入数据库,在用户对数据进行插入.修改.删除等操作时,DBMS自动按照一定的约束条件对数据进行监测,使不符合规范的数据不能写入数据库,以确保数据库中存储的 ...
- 顺序表的原理与python中的list类型
数据是如何在内存中存储的? 在32位的计算机上,1个字节有8位,内存寻址的最小单位就是字节.假设我们有一个int类型的值,它从0x10开始,一个int占据4个字节,则其结束于0x13. 那么数据类型有 ...