给定长度为N的整数数组A []和整数K。任务是找到在给定数组中不存在的K个不同积分点,以使它们与A []中最近点的距离之和最小。
An integral point is defined as the point with both the coordinates as integers. Also, in the x-axis, we don’t need to consider y-coordinate because y-coordinated of all the points is equal to zero.
例子:
Input: A[] = { -1, 4, 6 }, K = 3
Output: -2, 0, 3
Explanation:
Nearest point for -2 in A[] is -1 -> distance = 1
Nearest point for 0 in A[] is -1 -> distance = 1
Nearest point for 3 in A[] is 4 -> distance = 1
Total distance = 1 + 1 + 1 = 3 which is minimum possible distance.
Other results are also possible with the same minimum distance.
Input: A[] = { 0, 1, 3, 4 }, K = 5
Output: -1, 2, 5, -2, 6
Explanation:
Nearest point for -1 in A[] is 0 -> distance = 1
Nearest point for 2 in A[] is 3 -> distance = 1
Nearest point for 5 in A[] is 4 -> distance = 1
Nearest point for -2 in A[] is 0 -> distance = 2
Nearest point for 6 in A[] is 4 -> distance = 2
Total distance = 2 + 1 + 1 + 1 + 2 = 7 which is minimum possible distance.
方法:我们将使用广度优先搜索的概念来解决此问题。
- 我们首先将给定的整数集作为根元素,并将其推入Queue和Hash中。
- 然后对于任何说X的元素,我们将仅使用哈希图检查是否遇到(X-1)或(X + 1)。如果到目前为止还没有遇到任何问题,那么我们将该元素推送到答案数组中,并同时进行队列和哈希处理。
- 重复此过程,直到遇到K个新元素。
下面是上述方法的实现。
C++
// C++ implementation of above approach
#include
using namespace std;
// Function to find points at
// minimum distance
void minDistancePoints(int A[],
int K,
int n)
{
// Hash to store points
// that are encountered
map m;
// Queue to store initial
// set of points
queue q;
for (int i = 0; i < n; ++i) {
m[A[i]] = 1;
q.push(A[i]);
}
// Vector to store integral
// points
vector ans;
// Using bfs to visit nearest
// points from already
// visited points
while (K > 0) {
// Get first element from
// queue
int x = q.front();
q.pop();
// Check if (x-1) is not
// encountered so far
if (!m[x - 1] && K > 0) {
// Update hash with
// this new element
m[x - 1] = 1;
// Insert (x-1) into
// queue
q.push(x - 1);
// Push (x-1) as
// new element
ans.push_back(x - 1);
// Decrement counter
// by 1
K--;
}
// Check if (x+1) is not
// encountered so far
if (!m[x + 1] && K > 0) {
// Update hash with
// this new element
m[x + 1] = 1;
// Insert (x+1) into
// queue
q.push(x + 1);
// Push (x+1) as
// new element
ans.push_back(x + 1);
// Decrement counter
// by 1
K--;
}
}
// Print result array
for (auto i : ans)
cout << i << " ";
}
// Driver code
int main()
{
int A[] = { -1, 4, 6 };
int K = 3;
int n = sizeof(A) / sizeof(A[0]);
minDistancePoints(A, K, n);
return 0;
}
Java
// Java implementation of above approach
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
class Geeks{
// Function to find points at
// minimum distance
static void minDistancePoints(int A[],
int K, int n)
{
// Hash to store points
// that are encountered
Map m = new HashMap();
// Queue to store initial
// set of points
Queue q = new LinkedList();
for(int i = 0; i < n; ++i)
{
m.put(A[i], true);
q.add(A[i]);
}
// List to store integral
// points
LinkedList ans = new LinkedList();
// Using bfs to visit nearest
// points from already
// visited points
while (K > 0)
{
// Get first element from
// queue
int x = q.poll();
// Check if (x-1) is not
// encountered so far
if (!m.containsKey(x - 1) && K > 0)
{
// Update hash with
// this new element
m.put(x - 1, true);
// Insert (x-1) into
// queue
q.add(x - 1);
// Push (x-1) as
// new element
ans.add(x - 1);
// Decrement counter
// by 1
K--;
}
// Check if (x+1) is not
// encountered so far
if (!m.containsKey(x + 1) && K > 0)
{
// Update hash with
// this new element
m.put(x + 1, true);
// Insert (x+1) into
// queue
q.add(x + 1);
// Push (x+1) as
// new element
ans.add(x + 1);
// Decrement counter
// by 1
K--;
}
}
// Print result array
for(Integer i : ans)
System.out.print(i + " ");
}
// Driver code
public static void main(String[] args)
{
int A[] = new int[] { -1, 4, 6 };
int K = 3;
int n = A.length;
minDistancePoints(A, K, n);
}
}
// This code is contributed by Rajnis09
Python3
# Python 3 implementation
# of above approach
# Function to find points
# at minimum distance
def minDistancePoints(A, K, n):
# Hash to store points
# that are encountered
m = {}
# Queue to store initial
# set of points
q = []
for i in range(n):
m[A[i]] = 1
q.append(A[i])
# Vector to store
# integral points
ans = []
# Using bfs to visit nearest
# points from already
# visited points
while (K > 0):
# Get first element from
# queue
x = q[0]
q = q[1::]
# Check if (x-1) is not
# encountered so far
if ((x - 1) not in m and
K > 0):
# Update hash with
# this new element
m[x - 1] = m.get(x - 1, 0) + 1
# Insert (x-1) into
# queue
q.append(x - 1)
# Push (x-1) as
# new element
ans.append(x - 1)
# Decrement counter
# by 1
K -= 1
# Check if (x+1) is not
# encountered so far
if ((x + 1) not in m and
K > 0):
# Update hash with
# this new element
m[x + 1] = m.get(x + 1, 0) + 1
# Insert (x+1) into
# queue
q.append(x + 1)
# Push (x+1) as
# new element
ans.append(x + 1)
# Decrement counter
# by 1
K -= 1
# Print result array
for i in ans:
print(i, end = " ")
# Driver code
if __name__ == '__main__':
A = [-1, 4, 6]
K = 3
n = len(A)
minDistancePoints(A, K, n)
# This code is contributed by bgangwar59
C#
// C# implementation of above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find points at
// minimum distance
static void minDistancePoints(int []A,
int K, int n)
{
// Hash to store points
// that are encountered
Dictionary m = new Dictionary();
// Queue to store initial
// set of points
Queue q = new Queue();
for(int i = 0; i < n; ++i)
{
m.Add(A[i], true);
q.Enqueue(A[i]);
}
// List to store integral
// points
List ans = new List();
// Using bfs to visit nearest
// points from already
// visited points
while (K > 0)
{
// Get first element from
// queue
int x = q.Dequeue();
// Check if (x-1) is not
// encountered so far
if (!m.ContainsKey(x - 1) && K > 0)
{
// Update hash with
// this new element
m.Add(x - 1, true);
// Insert (x-1) into
// queue
q.Enqueue(x - 1);
// Push (x-1) as
// new element
ans.Add(x - 1);
// Decrement counter
// by 1
K--;
}
// Check if (x+1) is not
// encountered so far
if (!m.ContainsKey(x + 1) && K > 0)
{
// Update hash with
// this new element
m.Add(x + 1, true);
// Insert (x+1) into
// queue
q.Enqueue(x + 1);
// Push (x+1) as
// new element
ans.Add(x + 1);
// Decrement counter
// by 1
K--;
}
}
// Print result array
foreach(int i in ans)
Console.Write(i + " ");
}
// Driver code
public static void Main(String[] args)
{
int []A = new int[] { -1, 4, 6 };
int K = 3;
int n = A.Length;
minDistancePoints(A, K, n);
}
}
// This code is contributed by Amit Katiyar
-2 0 3
时间复杂度: O(M * log(M)) ,其中M = N +K。