给定一个数字数组(值从 0 到 9),找到由该数组的数字组成的两个数字的最小可能总和。必须使用给定数组的所有数字来形成两个数字。
例子:
Input: [6, 8, 4, 5, 2, 3]
Output: 604
The minimum sum is formed by numbers
358 and 246
Input: [5, 3, 0, 7, 4]
Output: 82
The minimum sum is formed by numbers
35 and 047
由于我们要最小化要形成的两个数字之和,我们必须将所有数字分成两半,并为它们分配半个数字。我们还需要确保前导数字更小。
我们用给定数组的元素构建一个最小堆,这需要 O(n) 最坏的时间。现在我们通过从优先级队列轮询来检索数组的最小值(一次 2 个)并将这两个最小值附加到我们的数字中,直到堆变空,即数组的所有元素都被耗尽。我们返回两个组成数字的总和,这是我们需要的答案。整体复杂度为 O(nlogn),因为 push() 操作需要 O(logn) 并且重复 n 次。
C++
// C++ program to find minimum sum of two numbers
// formed from all digits in a given array.
#include
using namespace std;
// Returns sum of two numbers formed
// from all digits in a[]
int minSum(int arr[], int n)
{
// min Heap
priority_queue , greater > pq;
// to store the 2 numbers formed by array elements to
// minimize the required sum
string num1, num2;
// Adding elements in Priority Queue
for(int i=0; i
Java
// Java program to find minimum sum of two numbers
// formed from all digits in a given array.
import java.util.PriorityQueue;
class MinSum
{
// Returns sum of two numbers formed
// from all digits in a[]
public static long solve(int[] a)
{
// min Heap
PriorityQueue pq = new PriorityQueue();
// to store the 2 numbers formed by array elements to
// minimize the required sum
StringBuilder num1 = new StringBuilder();
StringBuilder num2 = new StringBuilder();
// Adding elements in Priority Queue
for (int x : a)
pq.add(x);
// checking if the priority queue is non empty
while (!pq.isEmpty())
{
num1.append(pq.poll()+ "");
if (!pq.isEmpty())
num2.append(pq.poll()+ "");
}
// the required sum calculated
long sum = Long.parseLong(num1.toString()) +
Long.parseLong(num2.toString());
return sum;
}
// Driver code
public static void main (String[] args)
{
int arr[] = {6, 8, 4, 5, 2, 3};
System.out.println("The required sum is "+ solve(arr));
}
}
Python3
# Python3 program to find minimum
# sum of two numbers formed from
# all digits in a given array.
from queue import PriorityQueue
# Returns sum of two numbers formed
# from all digits in a[]
def solve(a):
# min Heap
pq = PriorityQueue()
# To store the 2 numbers
# formed by array elements to
# minimize the required sum
num1 = ""
num2 = ""
# Adding elements in
# Priority Queue
for x in a:
pq.put(x)
# Checking if the priority
# queue is non empty
while not pq.empty():
num1 += str(pq.get())
if not pq.empty():
num2 += str(pq.get())
# The required sum calculated
sum = int(num1) + int(num2)
return sum
# Driver code
if __name__=="__main__":
arr = [ 6, 8, 4, 5, 2, 3 ]
print("The required sum is ", solve(arr))
# This code is contributed by rutvik_56
C#
// C# program to find minimum sum of two numbers
// formed from all digits in a given array.
using System;
using System.Collections.Generic;
class GFG
{
// Returns sum of two numbers formed
// from all digits in a[]
public static long solve(int[] a)
{
// min Heap
List pq = new List();
// to store the 2 numbers formed by array elements to
// minimize the required sum
string num1 = "";
string num2 = "";
// Adding elements in Priority Queue
foreach(int x in a)
pq.Add(x);
pq.Sort();
// checking if the priority queue is non empty
while (pq.Count > 0)
{
num1 = num1 + pq[0];
pq.RemoveAt(0);
if (pq.Count > 0)
{
num2 = num2 + pq[0];
pq.RemoveAt(0);
}
}
// the required sum calculated
int sum = Int32.Parse(num1) + Int32.Parse(num2);
return sum;
}
// Driver code
static void Main()
{
int[] arr = {6, 8, 4, 5, 2, 3};
Console.WriteLine("The required sum is "+ solve(arr));
}
}
// This code is contributed by divyesh072019.
Javascript
C++
// C++ program to find minimum sum of two numbers
// formed from all digits in a given array.
#include
using namespace std;
// Returns sum of two numbers formed
// from all digits in a[]
int minSum(int a[], int n){
// sort the elements
sort(a,a+n);
int num1 = 0;
int num2 = 0;
for(int i = 0;i
Java
import java.util.Arrays;
//Java program to find minimum sum of two numbers
//formed from all digits in a given array.
public class AQRQ {
//Returns sum of two numbers formed
//from all digits in a[]
static int minSum(int a[], int n){
// sort the elements
Arrays.sort(a);
int num1 = 0;
int num2 = 0;
for(int i = 0;i
Python3
# Python 3 program to find minimum
# sum of two numbers formed
# from all digits in an given array
# Returns sum of two numbers formed
# from all digits in a[]
def minSum(a, n):
# sorted the elements
a = sorted(a)
num1, num2 = 0, 0
for i in range(n):
if i % 2 == 0:
num1 = num1 * 10 + a[i]
else:
num2 = num2 * 10 + a[i]
return num2 + num1
# Driver code
arr = [5, 3, 0, 7, 4]
n = len(arr)
print("The required sum is",
minSum(arr, n))
# This code is contributed
# by Mohit kumar 29
C#
// C# a program to find minimum sum of two numbers
//formed from all digits in a given array.
using System;
public class GFG{
//Returns sum of two numbers formed
//from all digits in a[]
static int minSum(int []a, int n){
// sort the elements
Array.Sort(a);
int num1 = 0;
int num2 = 0;
for(int i = 0;i
PHP
Javascript
输出:
The required sum is 604
另一种方法:
我们也可以像这样遵循另一种方法,因为我们需要两个数字,使得它们的总和最小,那么我们也需要两个最小数字。如果我们按升序排列我们的数组,那么我们可以用两个数字组成最小的数字,
例如,2 3 4 5 6 8,现在我们可以得到两个从 2 和 3 开始的数字。第一部分现在完成了。继续前进,我们必须形成这样的形式,即它们将包含小数字,即从数组中选择数字来扩展您的两个数字。
即 246、358。现在如果我们看到分析这个,那么我们可以为 num1 选择偶数索引数,为 num2 选择奇数。
下面是实现:
C++
// C++ program to find minimum sum of two numbers
// formed from all digits in a given array.
#include
using namespace std;
// Returns sum of two numbers formed
// from all digits in a[]
int minSum(int a[], int n){
// sort the elements
sort(a,a+n);
int num1 = 0;
int num2 = 0;
for(int i = 0;i
Java
import java.util.Arrays;
//Java program to find minimum sum of two numbers
//formed from all digits in a given array.
public class AQRQ {
//Returns sum of two numbers formed
//from all digits in a[]
static int minSum(int a[], int n){
// sort the elements
Arrays.sort(a);
int num1 = 0;
int num2 = 0;
for(int i = 0;i
蟒蛇3
# Python 3 program to find minimum
# sum of two numbers formed
# from all digits in an given array
# Returns sum of two numbers formed
# from all digits in a[]
def minSum(a, n):
# sorted the elements
a = sorted(a)
num1, num2 = 0, 0
for i in range(n):
if i % 2 == 0:
num1 = num1 * 10 + a[i]
else:
num2 = num2 * 10 + a[i]
return num2 + num1
# Driver code
arr = [5, 3, 0, 7, 4]
n = len(arr)
print("The required sum is",
minSum(arr, n))
# This code is contributed
# by Mohit kumar 29
C#
// C# a program to find minimum sum of two numbers
//formed from all digits in a given array.
using System;
public class GFG{
//Returns sum of two numbers formed
//from all digits in a[]
static int minSum(int []a, int n){
// sort the elements
Array.Sort(a);
int num1 = 0;
int num2 = 0;
for(int i = 0;i
PHP
Javascript
输出:
The required sum is 82
时间复杂度: O(nLogN)