Question:
Suppose you have a million integer numbers.
Return all possible values of a,b and c such thata+b+c<=d.
d will be provided to you.
ex: if the numbers are 1,2,3,4,5,6,7
and d=7
[1,2,3]
[1,2,4]
[1,2,3] will be same as [1,3,2] and [3,2,1]...
follow up:
Return all such parts that satisfy the above condition if d is not provided.
Solution:
k-sum的問題一般有兩種解法.
第一種:1)排序2)固定k-2個數位置3)兩個指針分別從前往後和從後往前移動.
第二種:緩存k/2的數的和(和爲key,數字爲值),然後對剩餘k/2個數遍歷.
這題說到million,不知道有沒有額外的技巧.望高人回答.