find the closest sum to a target value

problem: given an array of integers including positive and negative, a target value. find 2 numbers in the array such that the sum of the 2 numbers is the closest to the target value.

Solution: this can be solved in O(nlogn).

First, invest some time to sort the array, O(nlogn).

Then,

int min= Integer.MAX;
while(i< j){
   if(A[i]+ A[j]== T) return (i,j);
   (A[i]+ A[j]- T> 0)? j--: i++;
}
时间: 2024-08-04 14:22:46

find the closest sum to a target value的相关文章

[LeetCode][Python]16: 3Sum Closest

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 16: 3Sum Closesthttps://oj.leetcode.com/problems/3sum-closest/ 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

Snapchat - give sum target list&lt;Integer&gt; first who hits target wins

// DP 从 1-N 不重复取数 加到sum 上 第一个超过target赢 先手可以赢吗? 开始想错了,以为和climbing stairs和combination sum iv一个类型,是一个dfs 1 dfs(list, sum, target) 2 3 1. 如果list长度是0,那么就说明当前玩家没机会赢了,返回false 4 5 2. 如果list长度是1,那么说明该玩家赢了,因为后面一个玩家已经没机会抽了 6 7 3. 对于list中的数依次尝试: 8 9 如果当前数字加上sum能

No.016 3Sum Closest

16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium 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

leetcode 3Sum 3Sum Closest 4Sum

这几个题很典型也是国外一些知名公司经常会问到的题 3Sum: 排序,避免重复,时间复杂度O(n^2) class Solution { public: vector<vector<int> > threeSum(vector<int> &num) { int len=num.size(); sort(num.begin(),num.begin()+len); vector<vector<int> > ret; ret.clear(); i

[LeetCode] 3Sum Closest 最近三数之和

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

LeetCode 16 3Sum Closest(最接近的3个数的和)

翻译 给定一个有n个整数的数组S,找出S中3个数,使其和等于一个给定的数,target. 返回这3个数的和,你可以假定每个输入都有且只有一个结果. 例如,给定S = {-1 2 1 -4},和target = 1. 那么最接近target的和是2.(-1 + 2 + 1 = 2). 原文 Given an array S of n integers, find three integers in S such that the sum is closest to a given number,

LeetCode(16)3Sum Closest

题目 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

leetcode_16题——3Sum Closest(两个指针)

3Sum Closest Total Accepted: 38536 Total Submissions: 143223My Submissions Question Solution 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 ma

16.3Sum Closest (Two-Pointers)

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