📌  相关文章
📜  从给定范围内的整数生成长度为 N 的双调序列

📅  最后修改于: 2021-09-06 11:08:57             🧑  作者: Mango

给定整数NLR ,任务是从[L, R]范围内的整数生成长度为N双调序列,使得第一个元素是最大值。如果无法创建这样的序列,则打印“-1”

例子:

方法:想法是使用 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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live