📜  完成N公里旅程的笔芯数量

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

完成N公里旅程的笔芯数量

给定一个数字 N,它代表汽车在单条道路上行驶的总距离,以公里为单位。有 N 个汽油泵,每个距离 1 公里(1、2、3、..N)。汽车的油箱容量是这样的,在满油箱的情况下,它可以行驶到 K 公里的距离。汽车必须在 M 个油箱处强制停车,该油箱距起始位置的距离为 M 个整数。任务是找出汽车必须加满油箱的次数,包括强制停车以完成 N 公里的行程。
例子 :

方法:由于总行程为 N 公里,所以在一个变量中记录到目前为止所覆盖的距离,比如distCovered ,它将由 0 初始化。增量distCovered K km 直到distCovered小于 N,因为 K 是自上次加油后车辆可以行驶的距离。此外,对于每个增量,检查是否有强制汽油泵停止在distCovereddistCovered + K 之间,如果是,则distCovered将是 K 加上最后一个强制汽油泵停止在distCovereddistCovered + K 之间。另外,保持计算到达 N 公里目的地所需的重新填充次数。
下面是上述方法的实现:

C++
// CPP program for finding the total
// number of stops for refilling to
// reach destination of N km
#include 
using namespace std;
 
// Function that returns the total number of
// refills made to reach the destination of N km
int countRefill(int N, int K, int M, int compulsory[])
{
    int count = 0;
    int i = 0;
    int distCovered = 0;
 
    // While we complete the whole journey.
    while (distCovered < N) {
        // If must visited petrol pump lie
        // between distCovered and distCovered+K.
        if (i < M && compulsory[i] <= (distCovered + K)) {
            // make last mustVisited as distCovered
            distCovered = compulsory[i];
 
            // increment the index of compulsory visited.
            i++;
        }
 
        // if no such must visited pump is
        // there then increment distCovered by K.
        else
            distCovered += K;
 
        // Counting the number of refill.
        if (distCovered < N)
            count++;
    }
 
    return count;
}
 
// Driver Code
int main()
{
    int N = 10;
    int K = 2;
    int M = 3;
    // compulsory petrol pumps to refill at
    int compulsory[] = { 6, 7, 8 };
 
    // function call that returns the answer to the problem
    cout << countRefill(N, K, M, compulsory) << endl;
    return 0;
}


Java
// Java program for finding the
// total number of stops for
// refilling to reach
// destination of N km
import java.io.*;
 
class GFG
{
     
;
 
// Function that returns the
// total number of refills made
// to reach the destination of N km
static int countRefill(int N, int K,
                       int M, int compulsory[])
{
    int count = 0;
    int i = 0;
    int distCovered = 0;
 
    // While we complete
    // the whole journey.
    while (distCovered < N)
    {
        // If must visited petrol pump lie
        // between distCovered and distCovered+K.
        if (i < M && compulsory[i] <=
                                (distCovered + K))
        {
            // make last mustVisited
            // as distCovered
            distCovered = compulsory[i];
 
            // increment the index
            // of compulsory visited.
            i++;
        }
 
        // if no such must visited
        // pump is there then
        // increment distCovered by K.
        else
            distCovered += K;
 
        // Counting the number of refill.
        if (distCovered < N)
            count++;
    }
 
    return count;
}
 
// Driver Code
public static void main (String[] args)
{
    int N = 10;
    int K = 2;
    int M = 3;
    // compulsory petrol
    // pumps to refill at
    int compulsory[] = { 6, 7, 8 };
     
    // function call that returns
    // the answer to the problem
    System.out.println(countRefill(N, K,
                        M, compulsory));
}
}
 
// This code is contributed by anuj_67.


Python3
# Python 3 program for finding the total
# number of stops for refilling to reach
# destination of N km
 
# Function that returns the total number of
# refills made to reach the destination of N km
def countRefill(N, K, M, compulsory):
    count = 0
    i = 0
    distCovered = 0
 
    # While we complete the whole journey.
    while (distCovered < N):
         
        # If must visited petrol pump lie
        # between distCovered and distCovered+K.
        if (i < M and compulsory[i] <= (distCovered + K)):
             
            # make last mustVisited as distCovered
            distCovered = compulsory[i]
 
            # increment the index of
            # compulsory visited.
            i += 1
 
        # if no such must visited pump is
        # there then increment distCovered by K.
        else:
            distCovered += K
 
        # Counting the number of refill.
        if (distCovered < N):
            count += 1
 
    return count
 
# Driver Code
if __name__ == '__main__':
    N = 10
    K = 2
    M = 3
     
    # compulsory petrol pumps to refill at
    compulsory = [6, 7, 8]
 
    # function call that returns the
    # answer to the problem
    print(countRefill(N, K, M, compulsory))
     
# This code is contributed by
# Sanjit_Prasad


C#
// C# program for finding the
// total number of stops for
// refilling to reach
// destination of N km
using System;
 
class GFG
{
     
// Function that returns
// the total number of
// refills made to reach
// the destination of N km
static int countRefill(int N, int K,
                       int M, int []compulsory)
{
    int count = 0;
    int i = 0;
    int distCovered = 0;
 
    // While we complete
    // the whole journey.
    while (distCovered < N)
    {
        // If must visited petrol pump
        // lie between distCovered and
        // distCovered+K.
        if (i < M && compulsory[i] <=
                (distCovered + K))
        {
            // make last mustVisited
            // as distCovered
            distCovered = compulsory[i];
 
            // increment the index
            // of compulsory visited.
            i++;
        }
 
        // if no such must visited
        // pump is there then
        // increment distCovered by K.
        else
            distCovered += K;
 
        // Counting the number of refill.
        if (distCovered < N)
            count++;
    }
 
    return count;
}
 
// Driver Code
public static void Main ()
{
    int N = 10;
    int K = 2;
    int M = 3;
     
    // compulsory petrol
    // pumps to refill at
    int []compulsory = {6, 7, 8};
     
    // function call that returns
    // the answer to the problem
    Console.WriteLine(countRefill(N, K,
                       M, compulsory));
}
}
 
// This code is contributed by anuj_67.


PHP


Javascript


输出 :
5

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