📜  哥伦布序列 | 2套

📅  最后修改于: 2021-09-17 07:04:46             🧑  作者: Mango

给定一个数字N 。任务是找出哥伦布数列的前N项。哥伦布序列是一个非递减的整数序列,其中第 n 项等于 n 在序列中出现的次数。

方法:

  1. 用 [1] 初始化数组arr[] ,因为 Golomb 序列从 1 开始。
  2. 将最后一个索引的值存储在映射 M 中
  3. 对于 Golomb 序列直到N
    • 初始化 cnt = 0 并映射 .
    • 如果 cnt 不等于 0,则 Golomb 序列中的当前元素是序列中的前一个元素并减少 cnt。
    • 否则 Golomb 序列中的当前元素等于1 +序列中的前一个元素,并且 cnt 更新为 Golomb 序列中当前元素的 map 值并减少 cnt。
    • 用 Golomb 序列的当前值映射当前索引。
  4. 打印存储在数组arr[] 中的 Golomb 序列的所有元素。

下面是上述方法的实现:

C++
// C++ program to find the first
// N terms of Golomb Sequence
#include "bits/stdc++.h"
#define MAX 100001
using namespace std;
 
// Function to print the Golomb
// Sequence
void printGolombSequence(int N)
{
 
    // Initialise the array
    int arr[MAX];
 
    // Initialise the cnt to 0
    int cnt = 0;
 
    // First and second element
    // of Golomb Sequence is 0, 1
    arr[0] = 0;
    arr[1] = 1;
 
    // Map to store the count of
    // current element in Golomb
    // Sequence
    map M;
 
    // Store the count of 2
    M[2] = 2;
 
    // Iterate over 2 to N
    for (int i = 2; i <= N; i++) {
 
        // If cnt is equals to 0
        // then we have new number
        // for Golomb Sequence
        // which is 1 + previous
        // element
        if (cnt == 0) {
            arr[i] = 1 + arr[i - 1];
            cnt = M[arr[i]];
            cnt--;
        }
 
        // Else the current element
        // is the previous element
        // in this Sequence
        else {
            arr[i] = arr[i - 1];
            cnt--;
        }
 
        // Map the current index to
        // current value in arr[]
        M[i] = arr[i];
    }
 
    // Print the Golomb Sequence
    for (int i = 1; i <= N; i++) {
        cout << arr[i] << ' ';
    }
}
 
// Driver Code
int main()
{
    int N = 11;
    printGolombSequence(N);
    return 0;
}


Java
// Java program to find the first
// N terms of Golomb Sequence
import java.util.*;
 
class GFG{
 
static int MAX = 1000;
 
// Function to print the Golomb
// Sequence
static void printGolombSequence(int N)
{
 
    // Initialise the array
    int []arr = new int[MAX];
    for(int i = 0; i < MAX; i++)
    arr[i] = 0;
     
    // Initialise the cnt to 0
    int cnt = 0;
 
    // First and second element
    // of Golomb Sequence is 0, 1
    arr[0] = 0;
    arr[1] = 1;
 
    // Map to store the count of
    // current element in Golomb
    // Sequence
    Map M=new HashMap();
     
    // Store the count of 2
    M.put(2,2);
 
    // Iterate over 2 to N
    for (int i = 2; i <= N; i++) {
 
        // If cnt is equals to 0
        // then we have new number
        // for Golomb Sequence 1 2 2 3 3 4 4 4 5 5 5
        // which is 1 + previous
        // element
        if (cnt == 0) {
            arr[i] = 1 + arr[i - 1];
            cnt = M.get(arr[i]);
            cnt--;
        }
 
        // Else the current element
        // is the previous element
        // in this Sequence
        else {
            arr[i] = arr[i - 1];
            cnt--;
        }
 
        // Map the current index to
        // current value in arr[]
            M.put(i, arr[i]);
    }
 
    // Print the Golomb Sequence
    for (int i = 1; i <= N; i++)
    {
        System.out.print(arr[i]+" ");
    }
}
 
// Driver Code
public static void main(String args[])
{
    int N = 11;
    printGolombSequence(N);
}
}
 
// This code is contributed by Surendra_Gangwar


Python3
# Python3 program to find the first
# N terms of Golomb Sequence
MAX = 100001
 
# Function to print the Golomb
# Sequence
def printGolombSequence(N):
 
    # Initialise the array
    arr = [0] * MAX
 
    # Initialise the cnt to 0
    cnt = 0
 
    # First and second element
    # of Golomb Sequence is 0, 1
    arr[0] = 0
    arr[1] = 1
 
    # Map to store the count of
    # current element in Golomb
    # Sequence
    M = dict()
 
    # Store the count of 2
    M[2] = 2
 
    # Iterate over 2 to N
    for i in range(2, N + 1):
 
        # If cnt is equals to 0
        # then we have new number
        # for Golomb Sequence
        # which is 1 + previous
        # element
        if (cnt == 0):
            arr[i] = 1 + arr[i - 1]
            cnt = M[arr[i]]
            cnt -= 1
 
        # Else the current element
        # is the previous element
        # in this Sequence
        else:
            arr[i] = arr[i - 1]
            cnt -= 1
 
        # Map the current index to
        # current value in arr[]
        M[i] = arr[i]
 
    # Print the Golomb Sequence
    for i in range(1, N + 1):
        print(arr[i], end=" ")
 
# Driver Code
N = 11
printGolombSequence(N)
 
# This code is contributed by mohit kumar 29


C#
// C# program to find the first
// N terms of Golomb Sequence
using System;
using System.Collections.Generic;
 
class GFG{
     
static int MAX = 1000;
 
// Function to print the Golomb
// Sequence
static void printGolombSequence(int N)
{
     
    // Initialise the array
    int[] arr = new int[MAX];
    for(int i = 0; i < MAX; i++)
        arr[i] = 0;
       
    // Initialise the cnt to 0
    int cnt = 0;
   
    // First and second element
    // of Golomb Sequence is 0, 1
    arr[0] = 0;
    arr[1] = 1;
   
    // Map to store the count of
    // current element in Golomb
    // Sequence
    Dictionary M = new Dictionary(); 
       
    // Store the count of 2
    M.Add(2, 2);
   
    // Iterate over 2 to N
    for(int i = 2; i <= N; i++)
    {
         
        // If cnt is equals to 0
        // then we have new number
        // for Golomb Sequence 1 2 2 3 3 4 4 4 5 5 5
        // which is 1 + previous
        // element
        if (cnt == 0)
        {
            arr[i] = 1 + arr[i - 1];
            cnt = M[arr[i]];
            cnt--;
        }
   
        // Else the current element
        // is the previous element
        // in this Sequence
        else
        {
            arr[i] = arr[i - 1];
            cnt--;
        }
   
        // Map the current index to
        // current value in arr[]
        if(M.ContainsKey(i))
        {
            M[i] = arr[i];
        }
        else
        {
            M.Add(i, arr[i]);
        }
    }
   
    // Print the Golomb Sequence
    for(int i = 1; i <= N; i++)
    {
        Console.Write(arr[i] + " ");
    }
}
 
// Driver Code
static void Main()
{
    int N = 11;
     
    printGolombSequence(N);
}
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出:
1 2 2 3 3 4 4 4 5 5 5

时间复杂度: O(N)
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程