给定整数N 、 L和R ,任务是从[L, R]范围内的整数生成长度为N的双调序列,使得第一个元素是最大值。如果无法创建这样的序列,则打印“-1” 。
A Bitonic Sequence is a sequence that must be strictly increasing at first and then strictly decreasing.
例子:
Input: N = 5, L = 3, R = 10
Output: 9, 10, 9, 8, 7
Explanation: The sequence {9, 10, 9, 8, 7} is first strictly increasing and then strictly decreasing.
Input: N = 5, L = 2, R = 5
Output: 4, 5, 4, 3, 2
Explanation:
[ The sequence {4, 5, 4, 3, 2} is first strictly increasing and then strictly decreasing.
方法:想法是使用 Deque 以便可以从结尾和开头添加元素。请按照以下步骤解决问题:
- 初始化双端队列以存储结果双音序列的元素。
- 将变量i初始化为0并开始在结果列表中添加元素,从(R – i)开始,直到i小于(R – L + 1)和(N – 1)的最小值。
- 在上述步骤之后,如果结果列表的大小小于N ,则从列表的开头将(R – 1) 的元素添加到L ,直到结果列表的大小不变为N 。
- 经过上述步骤,如果N大于(R – L)*2 + 1 ,则无法构造这样的序列,然后打印“-1”,否则打印存储在deque 中的序列。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to construct bitonic
// sequence of length N from
// integers in the range [L, R]
void bitonicSequence(int num, int lower,
int upper)
{
// If sequence is not possible
if (num > (upper - lower) * 2 + 1)
{
cout << -1;
return;
}
// Store the resultant list
deque ans;
deque::iterator j = ans.begin();
for(int i = 0;
i < min(upper - lower + 1, num - 1);
i++)
ans.push_back(upper - i);
// If size of deque < n
for(int i = 0;
i < num - ans.size();
i++)
// Add elements from start
ans.push_front(upper - i - 1);
// Print the stored in the list
cout << '[';
for(j = ans.begin(); j != ans.end(); ++j)
cout << ' ' << *j;
cout << ' ' << ']';
}
// Driver Code
int main()
{
int N = 5, L = 3, R = 10;
// Function Call
bitonicSequence(N, L, R);
return 0;
}
// This code is contributed by jana_sayantan
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to construct bitonic
// sequence of length N from
// integers in the range [L, R]
public static void bitonicSequence(
int num, int lower, int upper)
{
// If sequence is not possible
if (num > (upper - lower) * 2 + 1) {
System.out.println(-1);
return;
}
// Store the resultant list
Deque ans
= new ArrayDeque<>();
for (int i = 0;
i < Math.min(upper - lower + 1,
num - 1);
i++)
ans.add(upper - i);
// If size of deque < n
for (int i = 0;
i < num - ans.size(); i++)
// Add elements from start
ans.addFirst(upper - i - 1);
// Print the stored in the list
System.out.println(ans);
}
// Driver Code
public static void main(String[] args)
{
int N = 5, L = 3, R = 10;
// Function Call
bitonicSequence(N, L, R);
}
}
Python3
# Python3 program for the above approach
from collections import deque
# Function to construct bitonic
# sequence of length N from
# integers in the range [L, R]
def bitonicSequence(num, lower, upper):
# If sequence is not possible
if (num > (upper - lower) * 2 + 1):
print(-1)
return
# Store the resultant list
ans = deque()
for i in range(min(upper - lower + 1,
num - 1)):
ans.append(upper - i)
# If size of deque < n
for i in range(num - len(ans)):
# Add elements from start
ans.appendleft(upper - i - 1)
# Print the stored in the list
print(list(ans))
# Driver Code
if __name__ == '__main__':
N = 5
L = 3
R = 10
# Function Call
bitonicSequence(N, L, R)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to construct bitonic
// sequence of length N from
// integers in the range [L, R]
public static void bitonicSequence(int num,
int lower,
int upper)
{
// If sequence is not possible
if (num > (upper - lower) * 2 + 1)
{
Console.WriteLine(-1);
return;
}
// Store the resultant list
List ans = new List();
for(int i = 0;
i < Math.Min(upper - lower + 1,
num - 1); i++)
ans.Add(upper - i);
// If size of deque < n
for(int i = 0;
i < num - ans.Count; i++)
// Add elements from start
ans.Insert(0,upper - i - 1);
// Print the stored in the list
Console.Write("[");
foreach(int x in ans)
Console.Write(x + ", ");
Console.Write("]");
}
// Driver Code
public static void Main(String[] args)
{
int N = 5, L = 3, R = 10;
// Function Call
bitonicSequence(N, L, R);
}
}
// This code is contributed by Rajput-Ji
Javascript
输出:
[9, 10, 9, 8, 7]
时间复杂度:O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。