将 N 表示为唯一正整数的最大数量之和
给定一个数N,任务是将 N 表示为最大唯一正整数数之和。
例子:
Input: N = 12
Output: 1+ 2 + 3 + 6
Explanation:
Possible splits are:
2 + 10
4 + 8
2 + 4 + 6
1 + 2 + 3 + 6
Among them, (1 + 2 + 3 + 6) contains the maximum number of unique integers.
Input: N = 6
Output: 1 + 2 + 3
方法:考虑以下观察来解决问题:
- 我们知道任何数N都可以表示为 N 1s的总和。
- 但是在这个问题中要求没有重复值。
- 因此,我们将尝试将 N 个 1 分组为唯一的组合,这肯定会给出总和 N。
插图:
Consider, N = 12 = 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
Since the numbers have to be unique, lets try to group 1s in a continuous manner.
- Cannot group 1’s in this manner since second grouping and last grouping result in same number.
12 = (1) + (1 + 1) + (1 + 1 + 1) + (1 + 1 + 1 + 1) + (1 + 1)
= 1 + 2 + 3 + 4 + 2 - To avoid this, combine the last two groupings.
12 = (1) + (1 + 1) + (1 + 1 + 1) + (1 + 1 + 1 + 1 + 1 + 1)
= 1 + 2 + 3 + 6
Therefore, To make groupings unique, the number of 1’s in any two groups should not be the same.
下面是上述方法的实现:
C++
// C++ program to find maximum number of
// splits into unique positive integers.
#include
#include
using namespace std;
vector maximumSplit(int N)
{
vector answer;
int groupSize = 1;
while (N >= groupSize) {
answer.push_back(groupSize);
N -= groupSize;
groupSize++;
}
// In case N != 0, then it is not
// possible to group remaining 1's
if (N != 0) {
// Get the last unique integer
int last = answer.back();
answer.pop_back();
// Add the remaining 1's to it
last += N;
// Add the new number to list
answer.push_back(last);
}
return answer;
}
// Driver Code
int main()
{
int N = 12;
vector answer = maximumSplit(N);
for (int n : answer)
cout << n << " ";
}
Java
// Java program to find maximum number of
// splits into unique positive integers.
import java.util.*;
class GFG {
public static List maximumSplit(int N)
{
List answer
= new ArrayList<>();
int groupSize = 1;
while (N >= groupSize) {
answer.add(groupSize);
N -= groupSize;
groupSize++;
}
// In case N != 0,
// then we were not able
// to group remaining 1's
if (N != 0) {
// get the last unique integer
int last
= answer.remove(
answer.size() - 1);
// add the remaining 1's to it
last += N;
// add the new number to list
answer.add(last);
}
return answer;
}
// Driver code
public static void main(String[] args)
{
int N = 12;
List answer = maximumSplit(N);
for (int n : answer)
System.out.print(n + " ");
}
}
Python3
# Python code for the above approach
def maximumSplit(N):
answer = [];
groupSize = 1;
while (N >= groupSize):
answer.append(groupSize);
N -= groupSize;
groupSize= groupSize + 1;
# In case N != 0, then it is not
# possible to group remaining 1's
if (N != 0):
# Get the last unique integer
last = answer[len(answer) - 1];
answer.pop();
# Add the remaining 1's to it
last += N;
# Add the new number to list
answer.append(last);
return answer;
# Driver Code
N = 12;
answer = maximumSplit(N);
for i in range(len(answer)):
print(answer[i], end = " ");
# This code is contributed by Potta Lokesh
C#
// C# program to find maximum number of
// splits into unique positive integers.
using System;
using System.Collections;
class GFG
{
static ArrayList maximumSplit(int N)
{
ArrayList answer = new ArrayList();
int groupSize = 1;
while (N >= groupSize) {
answer.Add(groupSize);
N -= groupSize;
groupSize++;
}
// In case N != 0, then it is not
// possible to group remaining 1's
if (N != 0) {
// Get the last unique integer
int last = (int)answer[answer.Count - 1];
answer.RemoveAt(answer.Count - 1);
// Add the remaining 1's to it
last += N;
// Add the new number to list
answer.Add(last);
}
return answer;
}
// Driver Code
public static void Main()
{
int N = 12;
ArrayList answer = maximumSplit(N);
foreach (int n in answer)
Console.Write(n + " ");
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
1 2 3 6
时间复杂度: O(N)
辅助空间:O(N)