105 lines
3.1 KiB
C++
Raw Normal View History

2014-10-20 11:23:39 +08:00
// Source : https://oj.leetcode.com/problems/3sum-closest/
// Author : Hao Chen
// Date : 2014-07-03
/**********************************************************************************
*
2014-10-21 11:39:53 +08:00
* Given an array S of n integers, find three integers in S such that the sum is
* closest to a given number, target. Return the sum of the three integers.
* You may assume that each input would have exactly one solution.
*
* For example, given array S = {-1 2 1 -4}, and target = 1.
*
* The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
*
*
**********************************************************************************/
2014-10-20 11:23:39 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
2014-10-20 11:23:39 +08:00
using namespace std;
#define INT_MAX 2147483647
//solution: http://en.wikipedia.org/wiki/3SUM
2014-11-05 00:21:28 +08:00
//the idea as blow:
// 1) sort the array.
// 2) take the element one by one, calculate the two numbers in reset array.
//
//notes: be careful the duplication number.
//
// for example:
// [-4,-1,-1,1,2] target=1
//
// take -4, can cacluate the "two number problem" of the reset array [-1,-1,1,2] while target=5
// [(-4),-1,-1,1,2] target=5 distance=4
// ^ ^
// because the -1+2 = 1 which < 5, then move the `low` pointer(skip the duplication)
// [(-4),-1,-1,1,2] target=5 distance=2
// ^ ^
// take -1(skip the duplication), can cacluate the "two number problem" of the reset array [1,2] while target=2
// [-4,-1,(-1),1,2] target=2 distance=1
// ^ ^
2014-10-20 11:23:39 +08:00
int threeSumClosest(vector<int> &num, int target) {
2014-11-05 00:21:28 +08:00
//sort the array
2014-10-20 11:23:39 +08:00
sort(num.begin(), num.end());
int n = num.size();
2014-11-05 00:21:28 +08:00
int distance = INT_MAX;
2014-10-20 11:23:39 +08:00
int result;
for (int i=0; i<n-2; i++) {
//skip the duplication
if (i > 0 && num[i - 1] == num[i]) continue;
2014-10-20 11:23:39 +08:00
int a = num[i];
int low = i + 1;
int high = n - 1;
2014-11-05 00:21:28 +08:00
//convert the 3sum to 2sum problem
while (low < high) {
2014-10-20 11:23:39 +08:00
int b = num[low];
int c = num[high];
int sum = a + b + c;
2014-10-20 11:23:39 +08:00
if (sum - target == 0) {
//got the final soultion
return target;
2014-11-05 00:21:28 +08:00
} else {
//tracking the minmal distance
if (abs(sum - target) < distance ) {
2014-11-05 00:21:28 +08:00
distance = abs(sum - target);
2014-10-20 11:23:39 +08:00
result = sum;
}
2014-11-05 00:21:28 +08:00
if (sum - target > 0) {
2014-11-05 00:21:28 +08:00
//skip the duplication
while(high > 0 && num[high] == num[high - 1]) high--;
2014-11-05 00:21:28 +08:00
//move the `high` pointer
high--;
} else {
//skip the duplication
while(low < n && num[low] == num[low + 1]) low++;
2014-11-05 00:21:28 +08:00
//move the `low` pointer
low++;
2014-10-20 11:23:39 +08:00
}
2014-11-05 00:21:28 +08:00
}
2014-10-20 11:23:39 +08:00
}
}
2014-11-05 00:21:28 +08:00
return result;
2014-10-20 11:23:39 +08:00
}
int main()
{
int a[] = { -1, 2, 1, -4 };
vector<int> n(a, a + sizeof(a)/sizeof(int));
2014-10-20 11:23:39 +08:00
int target = 1;
cout << threeSumClosest(n, target) << endl;
return 0;
}