由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - CS algorithm question
相关主题
请教一道L的题问两道微软题
一FG家常见题facebook telephone interview from careercup
问一个面试题[a9面经] print x,y,z
FB phone interview问个G的题目
一道google 经典题LC的3sum谁有简洁代码?
find sum of three number in array以前能过的leetcode 3sum, 现在fail了, 求助(时间超出了)
问求array中3个数和最接近k的解法请教一道面试题
amazon版上面试问题请教问一道老题
相关话题的讨论汇总
话题: array话题: triplet话题: find话题: cs话题: add
进入JobHunting版参与讨论
1 (共1页)
m*****y
发帖数: 120
1
Given 3 array of numbers (can be positive, or negative), get one from each
array to form a triplet:
1) find whether there is a triplet that add to 0.
2) find all the triplets that add to 0.
The 3 array have the same size (length).
Of course, N^3 will be the default answer.
Any better idea?
for example:
A: 3, -5, 9
B: -2, 4, 1
C: 2, -1, 7
Then 3(from A) + -2 (from B) + -1 (From C) is 0.
r*******y
发帖数: 1081
2
O(n^2)

【在 m*****y 的大作中提到】
: Given 3 array of numbers (can be positive, or negative), get one from each
: array to form a triplet:
: 1) find whether there is a triplet that add to 0.
: 2) find all the triplets that add to 0.
: The 3 array have the same size (length).
: Of course, N^3 will be the default answer.
: Any better idea?
: for example:
: A: 3, -5, 9
: B: -2, 4, 1

j*******r
发帖数: 52
3
将BC中所有可能的加和hash -> O(n^2)
foreach v in A:
Hash.get(0-A) -> O(n)
c*******n
发帖数: 63
4
1.sort three array first --> nlgn
2.1 form the first two elements --> n^2
2.2 find the third element of triplet --> lgn (binary search)
or as justcoder said, hash the third array, then the searching will be
O(1)
==> O(n^2 * lgn) or O(n^2)
F**r
发帖数: 84
5
this is a variation of 3-sum problem, so complexity O(N^2)

【在 m*****y 的大作中提到】
: Given 3 array of numbers (can be positive, or negative), get one from each
: array to form a triplet:
: 1) find whether there is a triplet that add to 0.
: 2) find all the triplets that add to 0.
: The 3 array have the same size (length).
: Of course, N^3 will be the default answer.
: Any better idea?
: for example:
: A: 3, -5, 9
: B: -2, 4, 1

1 (共1页)
进入JobHunting版参与讨论
相关主题
问一道老题一道google 经典题
请教一道题find sum of three number in array
Given an int array and an int value. Find all pairs in arr问求array中3个数和最接近k的解法
Google电话面试题目amazon版上面试问题请教
请教一道L的题问两道微软题
一FG家常见题facebook telephone interview from careercup
问一个面试题[a9面经] print x,y,z
FB phone interview问个G的题目
相关话题的讨论汇总
话题: array话题: triplet话题: find话题: cs话题: add