通过递减 arr[0] 并反复移动到 end 将 arr[K] 更改为 0 的最小步骤
给定一个大小为N的数组arr[]和一个表示索引K 的整数,任务是找到arr[K]变为0的最小操作次数。在一次操作中,第一个数组元素的值减1并走到数组的末尾。如果在任何时候, arr[i]变为0,则将其从数组中删除,并对剩余元素执行操作。
例子:
Input: arr[] = {2, 3, 2}, K = 2
Output: 6
Explanation: For the first input,
After iteration-1, the array changes to [1, 2, 1]. Steps taken = 3 steps
After iteration-2, the array changes to [0, 1, 0]. Steps taken = 3 steps
Hence, for the element at index 2, it took 6 steps to become 0.
Input: arr[] = {5, 1, 1, 1}, K = 0
Output: 8
方法:思路是不断遍历数组,当arr[i]大于0时,减小arr[i]的值,计算答案。请按照以下步骤解决问题:
- 将变量time初始化为0以存储答案。
- 在while循环中遍历直到arr[k]不为0并执行以下任务:
- 使用变量i遍历范围[0, N)并执行以下任务:
- 如果arr[i]大于0,则将arr[i]的值减1,然后将time的值加1。
- 如果arr[k]变为0,则中断。
- 使用变量i遍历范围[0, N)并执行以下任务:
- 执行上述步骤后,打印时间值作为答案。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum number
// of steps
void findMinimumNumberOfSteps(vector arr,
int K)
{
// Variable to store the answer
int time = 0;
// Traverse in the while loop
while (arr[K] != 0) {
// Iterate over the loop
for (int i = 0; i < arr.size(); i++) {
// Check the condition and
// decrease the value
if (arr[i] > 0) {
arr[i] -= 1;
time++;
}
// Break the loop
if (arr[K] == 0)
break;
}
}
// Print the result
cout << time;
}
// Driver Code
int main()
{
vector arr = { 2, 3, 2 };
int K = 2;
findMinimumNumberOfSteps(arr, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to find the minimum number
// of steps
static void findMinimumNumberOfSteps(int arr[],
int K)
{
// Variable to store the answer
int time = 0;
// Traverse in the while loop
while (arr[K] != 0) {
// Iterate over the loop
for (int i = 0; i < arr.length; i++) {
// Check the condition and
// decrease the value
if (arr[i] > 0) {
arr[i] -= 1;
time++;
}
// Break the loop
if (arr[K] == 0)
break;
}
}
// Print the result
System.out.println(time);
}
// Driver Code
public static void main (String[] args) {
int arr[] = { 2, 3, 2 };
int K = 2;
findMinimumNumberOfSteps(arr, K);
}
}
// This code is contributed by hrithikgarg03188.
Python3
# Python program to implement
# the above approach
# Function to find the minimum number
# of steps
def findMinimumNumberOfSteps(arr, K) :
# Variable to store the answer
time = 0
# Traverse in the while loop
while (arr[K] != 0) :
# Iterate over the loop
for i in range(0, len(arr)) :
# Check the condition and
# decrease the value
if (arr[i] > 0) :
arr[i] -= 1
time += 1
# Break the loop
if (arr[K] == 0):
break
# Print the result
print(time)
# Driver Code
arr = [ 2, 3, 2 ]
K = 2
findMinimumNumberOfSteps(arr, K)
# This code is contributed by sanjoy_62.
C#
// C# program for the above approach
using System;
class GFG {
// Function to find the minimum number
// of steps
static void findMinimumNumberOfSteps(int []arr,
int K)
{
// Variable to store the answer
int time = 0;
// Traverse in the while loop
while (arr[K] != 0) {
// Iterate over the loop
for (int i = 0; i < arr.Length; i++) {
// Check the condition and
// decrease the value
if (arr[i] > 0) {
arr[i] -= 1;
time++;
}
// Break the loop
if (arr[K] == 0)
break;
}
}
// Print the result
Console.WriteLine(time);
}
// Driver Code
public static void Main () {
int []arr = { 2, 3, 2 };
int K = 2;
findMinimumNumberOfSteps(arr, K);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
6
时间复杂度: O(N*X),其中 X 是arr[K]的值
辅助空间: O(1)