给定N个不同整数的列表和N-1个不等号的列表,任务是在不等号之间插入整数,以使形成的最终不等式始终成立。
注意:不等号的顺序不得更改。
例子:
Input: Integers: [ 2, 5, 1, 0 ], Signs: [ <, >, < ]
Output: 0 < 5 > 1 < 2
Explanation:
The inequality formed is consistent and valid.
Input: Integers: [ 8, 34, 25, 1, -5, 10], Signs: [ >, >, <, <, > ]
Output: 34 > 25 > -5 < 1 < 10 > 8
Explanation:
The inequality formed is consistent and valid.
方法:不等式符号列表可以包含任何顺序的符号。因此,为了获得一致的不等式,请将数组中每个<符号前的最小整数和每个>符号前的最大整数放置在数组中。基于此想法,请执行以下步骤:
- 按升序对整数列表进行排序。
- 保持两个变量low和high指向整数列表的第一个和最后一个索引。
- 遍历不等式符号列表。如果当前符号较小,则在<之前加上low所指向的整数,并递增low以指向下一个索引。如果当前符号较大,则在>之前添加由high指向的整数,并递减high指向先前的索引。
- 最后,将剩余的元素添加到最后一个位置。
下面是上述方法的实现:
Java
// Java program for the above approach
import java.util.Arrays;
public class PlacingNumbers {
// Function to place the integers
// in between the inequality signs
static String
formAnInequality(int[] integers,
char[] inequalities)
{
// Sort the integers array and
// set the index of smallest
// and largest element
Arrays.sort(integers);
int lowerIndex = 0;
int higherIndex = integers.length - 1;
StringBuilder sb = new StringBuilder();
// Iterate over the inequalities
for (char ch : inequalities) {
// Append the necessary
// integers per symbol
if (ch == '<') {
sb.append(" "
+ integers[lowerIndex++]
+ " "
+ ch);
}
else {
sb.append(" "
+ integers[higherIndex--]
+ " "
+ ch);
}
}
// Add the final integer
sb.append(" " + integers[lowerIndex]);
// Return the answer
return sb.toString();
}
// Driver Code
public static void main(String[] args)
{
// Given List of Integers
int[] integers = { 2, 5, 1, 0 };
// Given list of inequalities
char[] inequalities = { '<', '>', '<' };
// Function Call
String output
= formAnInequality(integers,
inequalities);
// Print the output
System.out.println(output);
}
}
Python3
# Python3 program for
# the above approach
# Function to place the integers
# in between the inequality signs
def formAnInequality(integers,
inequalities):
# Sort the integers array and
# set the index of smallest
# and largest element
integers.sort()
lowerIndex = 0
higherIndex = len(integers) - 1
sb = ""
# Iterate over the inequalities
for ch in inequalities:
# Append the necessary
# integers per symbol
if (ch == '<'):
sb += (" " + chr(integers[lowerIndex]) +
" " + ch)
lowerIndex += 1
else:
sb += (" " + chr(integers[higherIndex]) +
" " + ch)
higherIndex -= 1
# Add the final integer
sb += (" " + chr(integers[lowerIndex]))
# Return the answer
return sb
# Driver Code
if __name__ == "__main__":
# Given List of Integers
integers = [2, 5, 1, 0]
# Given list of inequalities
inequalities = ['<', '>', '<']
# Function Call
output = formAnInequality(integers,
inequalities)
# Print the output
print(output)
# This code is contributed by Chitranayal
C#
// C# program for the above approach
using System;
using System.Text;
class GFG{
// Function to place the integers
// in between the inequality signs
static String
formAnInequality(int[] integers,
char[] inequalities)
{
// Sort the integers array and
// set the index of smallest
// and largest element
Array.Sort(integers);
int lowerIndex = 0;
int higherIndex = integers.Length - 1;
StringBuilder sb = new StringBuilder();
// Iterate over the inequalities
foreach(char ch in inequalities)
{
// Append the necessary
// integers per symbol
if (ch == '<')
{
sb.Append(" " + integers[lowerIndex++] +
" " + ch);
}
else
{
sb.Append(" " + integers[higherIndex--] +
" " + ch);
}
}
// Add the readonly integer
sb.Append(" " + integers[lowerIndex]);
// Return the answer
return sb.ToString();
}
// Driver Code
public static void Main(String[] args)
{
// Given List of ints
int[] integers = { 2, 5, 1, 0 };
// Given list of inequalities
char[] inequalities = { '<', '>', '<' };
// Function call
String output = formAnInequality(integers,
inequalities);
// Print the output
Console.WriteLine(output);
}
}
// This code is contributed by 29AjayKumar
输出:
0 < 5 > 1 < 2
时间复杂度: O(N * log N)
辅助空间: O(1)