遍历 [1, N] 范围内的所有整数的最小跳转次数,使得整数 i 可以跳转 i 步
给定一个整数N,任务是通过选择任何整数并在每第 i次跳转时跳转i步,找到访问[1, N]范围内所有整数的最小步数。
注意:可以多次重新访问一个整数。
例子:
Input: N = 6
Output: 3
Explanation:
One of the following way is:
First start at first number and visit the integers {1, 2, 4}.
Now start at 2nd number and visit the integers as {2, 3, 5}
Now start at the last number and visit it.
Therefore, in total of 3 steps one can visit all the numbers in the range [1, 6]. And also it is the minimum number of steps needed.
Input: N = 4
Output: 2
朴素方法:可以根据以下观察解决给定的问题:
- In each step the sizes of jumps increases therefore some numbers remains unvisited in a step.
- Starting from the first number and performing the jumps it can be observed that the maximum size of the jump is the total number of steps needed to visit every number. As in one move, one cannot visit each number between two unvisited numbers.
请按照以下步骤解决问题:
- 初始化两个变量,比如count = 1和res = 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)
有效方法:可以根据以下观察优化上述步骤:
- Suppose an array arr[] as arr[] ={1, 1, 2, 2, 2, 3, 3, 3, ….]. Now the idea is to find the number K which is the maximum size of jump taken in a step.
- In above taken array, Since, 1 occurs twice, 2 occurs thrice, 3 occurs four times and so on. (K – 1) would occur K times.
- Now let K occurs c times, then count of total occurrence must be equal to N.
- 2 + 3 + 4 +….+ K + c = N
- c = N – 2 – 3 – 4 -….- K …..(i)
- Then one need to find the greatest K satisfying equation (i) also c≥1, then
- K2 + K – 2 × N ≤ 0.
- Therefore, K = (-1 + √(1 + 8 ×N)) / 2
请按照以下步骤解决问题:
- 打印值(-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)