📜  遍历 [1, N] 范围内的所有整数的最小跳转次数,使得整数 i 可以跳转 i 步

📅  最后修改于: 2022-05-13 01:56:09.830000             🧑  作者: Mango

遍历 [1, N] 范围内的所有整数的最小跳转次数,使得整数 i 可以跳转 i 步

给定一个整数N,任务是通过选择任何整数并在每第 i跳转时跳转i步,找到访问[1, N]范围内所有整数的最小步数。

注意:可以多次重新访问一个整数。

例子:

朴素方法:可以根据以下观察解决给定的问题:

请按照以下步骤解决问题:

  • 初始化两个变量,比如count = 1res = 0。
  • 遍历范围[1, N]并将i递增count并将res更新为res =max(res, count)并将count递增1
  • 完成上述步骤后打印res。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Utility function to find minimum steps
int minSteps(int N)
{
    // Initialize count and result
    int count = 1, res = 0;
 
    // Traverse over the range [1, N]
    for (int i = 1; i <= N; i += count) {
        // Update res
        res = max(res, count);
        // Increment count
        count++;
    }
 
    // Return res
    return res;
}
 
// Driver Code
int main()
{
    // Input
    int N = 6;
 
    // Function call
    cout << minSteps(N) << "\n";
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG
{
 
  // Utility function to find minimum steps
  static int minSteps(int N)
  {
 
    // Initialize count and result
    int count = 1, res = 0;
 
    // Traverse over the range [1, N]
    for (int i = 1; i <= N; i += count)
    {
 
      // Update res
      res = Math.max(res, count);
 
      // Increment count
      count++;
    }
 
    // Return res
    return res;
  }
 
  // Driver Code
  public static void main (String[] args)
  {
 
    // Input
    int N = 6;
 
    // Function call
 
    System.out.println(minSteps(N) );
  }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python 3 implementation of the above approach
 
# Utility function to find minimum steps
def minSteps(N):
   
    # Initialize count and result
    count = 1
    res = 0
 
    # Traverse over the range [1, N]
    for i in range(1, N + 1, count):
       
        # Update res
        res = max(res, count)
         
        # Increment count
        count += 1
 
    # Return res
    return res
 
# Driver Code
if __name__ == '__main__':
   
    # Input
    N = 6
 
    # Function call
    print(minSteps(N))
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
 
class GFG{
 
  // Utility function to find minimum steps
  static int minSteps(int N)
  {
 
    // Initialize count and result
    int count = 1, res = 0;
 
    // Traverse over the range [1, N]
    for (int i = 1; i <= N; i += count)
    {
 
      // Update res
      res = Math.Max(res, count);
 
      // Increment count
      count++;
    }
 
    // Return res
    return res;
  }
 
 
// Driver Code
public static void Main()
{
    // Input
    int N = 6;
 
    // Function call
 
    Console.Write(minSteps(N) );
}
}


Javascript


C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Utility function to find minimum steps
int minSteps(int N)
{
    int res = (sqrt(1 + 8 * N) - 1) / 2;
    return res;
}
 
// Driver code
int main()
{
    // Input integer
    int N = 6;
 
    // Function call
    cout << minSteps(N) << "\n";
}


Java
// Java implementation of the above approach
import java.io.*;
import java.lang.Math;
 
class GFG{
     
// Utility function to find minimum steps
static int minSteps(int N)
{
    int res = ((int)Math.sqrt(1 + 8 * N) - 1) / 2;
    return res;
}
 
// Driver code
public static void main (String[] args)
{
     
    // Input integer
    int N = 6;
     
    // Function call
    System.out.println(minSteps(N) + "\n");
}
}
 
// This code is contributed by shivanisinghss2110


Python3
# Python 3 implementation of the above approach
 
from math import sqrt
# Utility function to find minimum steps
def minSteps(N):
    res = int((sqrt(1 + 8 * N) - 1) // 2)
    return res
 
# Driver code
if __name__ == '__main__':
    # Input integer
    N = 6
 
    # Function call
    print(minSteps(N))


C#
// Java implementation of the above approach
using System;
class GFG
{
     
// Utility function to find minimum steps
static int minSteps(int N)
{
    int res = ((int)Math.Sqrt(1 + 8 * N) - 1) / 2;
    return res;
}
 
// Driver code
public static void Main (String[] args)
{
     
    // Input integer
    int N = 6;
     
    // Function call
    Console.Write(minSteps(N) + "\n");
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出
3

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

有效方法:可以根据以下观察优化上述步骤:

请按照以下步骤解决问题:

  • 打印值(-1 + √(1 + 8 *N)) / 2

下面是上述方法的实现:

C++

// C++ implementation of the above approach
#include 
using namespace std;
 
// Utility function to find minimum steps
int minSteps(int N)
{
    int res = (sqrt(1 + 8 * N) - 1) / 2;
    return res;
}
 
// Driver code
int main()
{
    // Input integer
    int N = 6;
 
    // Function call
    cout << minSteps(N) << "\n";
}

Java

// Java implementation of the above approach
import java.io.*;
import java.lang.Math;
 
class GFG{
     
// Utility function to find minimum steps
static int minSteps(int N)
{
    int res = ((int)Math.sqrt(1 + 8 * N) - 1) / 2;
    return res;
}
 
// Driver code
public static void main (String[] args)
{
     
    // Input integer
    int N = 6;
     
    // Function call
    System.out.println(minSteps(N) + "\n");
}
}
 
// This code is contributed by shivanisinghss2110

Python3

# Python 3 implementation of the above approach
 
from math import sqrt
# Utility function to find minimum steps
def minSteps(N):
    res = int((sqrt(1 + 8 * N) - 1) // 2)
    return res
 
# Driver code
if __name__ == '__main__':
    # Input integer
    N = 6
 
    # Function call
    print(minSteps(N))

C#

// Java implementation of the above approach
using System;
class GFG
{
     
// Utility function to find minimum steps
static int minSteps(int N)
{
    int res = ((int)Math.Sqrt(1 + 8 * N) - 1) / 2;
    return res;
}
 
// Driver code
public static void Main (String[] args)
{
     
    // Input integer
    int N = 6;
     
    // Function call
    Console.Write(minSteps(N) + "\n");
}
}
 
// This code is contributed by shivanisinghss2110

Javascript


输出
3

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