closest

LeetCode16. 3Sum Closest(思路及python解法)

﹥>﹥吖頭↗ 提交于 2020-01-16 05:16:21
Given an array nums of n integers and an integer target , find three integers in nums such that the sum is closest to target . Return the sum of the three integers. You may assume that each input would have exactly one solution. Example: Given array nums = [-1, 2, 1, -4], and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). 找到三个数的和,使这个和距离target距离最近。 思路和3sum差不多,只不过需要记录一下最短距离即可。 还是先将数组排序,然后利用两个指针去遍历数组。多了一步记录距离的过程,注意使用abs() class Solution: def threeSumClosest(self, nums: List[int], target: int) -> int: nums.sort() closest=float('inf') for i in range(len(nums)): if i!=0

Fastest way to find the closest point to a given point in 3D, in Python

拟墨画扇 提交于 2020-01-12 07:07:29
问题 So lets say I have 10,000 points in A and 10,000 points in B and want to find out the closest point in A for every B point. Currently, I simply loop through every point in B and A to find which one is closest in distance. ie. B = [(.5, 1, 1), (1, .1, 1), (1, 1, .2)] A = [(1, 1, .3), (1, 0, 1), (.4, 1, 1)] C = {} for bp in B: closestDist = -1 for ap in A: dist = sum(((bp[0]-ap[0])**2, (bp[1]-ap[1])**2, (bp[2]-ap[2])**2)) if(closestDist > dist or closestDist == -1): C[bp] = ap closestDist =

using jquery, how would i find the closest match in an array, to a specified number

穿精又带淫゛_ 提交于 2020-01-09 05:08:27
问题 using jquery, how would i find the closest match in an array, to a specified number For example, you've got an array like this: 1, 3, 8, 10, 13, ... What number is closest to 4? 4 would return 3 2 would return 3 5 would return 3 6 would return 8 ive seen this done in many different languages, but not in jquery, is this possible to do simply 回答1: You can use the jQuery.each method to loop the array, other than that it's just plain Javascript. Something like: var theArray = [ 1, 3, 8, 10, 13 ];

Using numpy.argmax() on multidimensional arrays

雨燕双飞 提交于 2019-12-30 04:00:05
问题 I have a 4 dimensional array, i.e., data.shape = (20,30,33,288) . I am finding the index of the closest array to n using index = abs(data - n).argmin(axis = 1), so index.shape = (20,33,288) with the indices varying. I would like to use data[index] = "values" with values.shape = (20,33,288) , but data[index] returns the error "index (8) out of range (0<=index<1) in dimension 0" or this operation takes a relatively long time to compute and returns a matrix with a shape that doesn't seem to make

849. Maximize Distance to Closest Person

喜你入骨 提交于 2019-12-28 11:21:06
1. Question 849. Maximize Distance to Closest Person url https://leetcode.com/problems/maximize-distance-to-closest-person/ In a row of seats , 1 represents a person sitting in that seat, and 0 represents that the seat is empty. There is at least one empty seat, and at least one person sitting. Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized. Return that maximum distance to closest person. Example 1: Input: [1,0,0,0,1,0,1] Output: 2 Explanation: If Alex sits in the second open seat (seats[2]), then the closest person has distance 2.

849. Maximize Distance to Closest Person

依然范特西╮ 提交于 2019-12-28 11:20:48
In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is empty. There is at least one empty seat, and at least one person sitting. Alex wants to sit in the seat such that the distance between him and the closest person to him is maximized. Return that maximum distance to closest person. Example 1: Input: [1,0,0,0,1,0,1] Output: 2 Explanation: If Alex sits in the second open seat (seats[2]), then the closest person has distance 2. If Alex sits in any other open seat, the closest person has distance 1. Thus, the maximum distance to the closest person is 2.

Find nearest values of 2 columns from a larger 'lookup' style data.table

寵の児 提交于 2019-12-24 11:27:19
问题 I have a data.table ( dt_1 ) that contains measured values for 2 columns ( Observed_A and Observed_B ). I need to use the values from those columns and lookup the nearest values to both columns in a second lookup styled data.table ( dt_2 ) referencing columns Modeled_A and Modeled_B , respectively. The ultimate goal is to then, using the rows that have the nearest matches in dt_2 , select the 3 additional modeled column values in dt_2 ( Variable_1 , Variable_2 and Variable_3 ) and add them

Find the closest value in a matrix matlab

╄→尐↘猪︶ㄣ 提交于 2019-12-23 12:47:11
问题 How can I find the closest element in a matrix in matlab? Suppose I have a matrix of the size 300x200 and I want to find the value and the index of the element in the matrix which is the closest to a element given. Does anyone know how this can be done in matlab? I know how to do this for a given array but I can not figure out how this is done for a matrix. 回答1: A smaller case might help you understand - Code %%// Given big matrix, taken as a matrix of random numbers for demo a1 = rand(10,5);

Closest Prime Number in Python

旧巷老猫 提交于 2019-12-23 05:49:54
问题 I need a user to enter a number and enter out the closest prime number to the value they put in. I am struggling on how to check the prime numbers before and after the number they put in. The last part is to print the smaller value of the two prime numbers if they are the same distance away from the inputted number. n = int(input("Enter n: ")) holder1 = n holder2 = n prime = True holder3 = 0 holder4 = 0 for i in range(2,n): if (n % i) == 0: prime = False if(prime == True): print("The prime

Find the index of numerically closest value [duplicate]

寵の児 提交于 2019-12-23 04:14:11
问题 This question already has answers here : Mapping 2 vectors - help to vectorize (6 answers) Closed 6 years ago . Say I have 2 matrices in matlab : A = [1 4 6 9 11 13 15 18 21] B = [2 10 19] Is there a function I can use so that, for every element in B, I am able to find the index of the closest value to that element in A. For instance, in the above example: 2,10 and 19 are numerically closest to 1,9 and 18 in A, and the indices of 1, 9 and 18 are 1,4 and 8, so the function should return [1 4 8