📌  相关文章
📜  从范围 [L, R] 移动任意步数,可以访问给定范围内的数字

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

从范围 [L, R] 移动任意步数,可以访问给定范围内的数字

给定两个整数X , Y和一个范围[L, R] ,任务是计算范围XY包括)中的整数个数,这些整数可以从X开始以任意数量的步骤访问。在每个步骤中,可以将L增加到R

例子:

方法:从索引 i 开始,可以到达 [i+L, i+R] 之间的任何位置,同样对于 [i+L, i+R] 中的每个点 j,人们可以移动到 [j+L, j+R]。对于每个索引 i,可以使用差异数组标记这些可达范围。请按照以下步骤进行操作。

  • 构造一个大尺寸数组diff_arr[]来标记范围。
  • 初始化一个变量说count = 0,以计算可达点。
  • 最初,使diff_arr[x] = 1diff_arr[x+1] = -1将起点X标记为已访问。
  • X迭代到Y并为每个i更新diff_arr[i] += diff_arr[i-1]如果diff_arr[i] >= 1则更新diff_arr[i+L] = 1diff_arr[i + R + 1] = -1计数 = 计数 + 1。
  • 最后,返回count

下面是上述方法的实现:

C++
// C++ code for above approach
#include 
using namespace std;
 
// Function to count points from the range [X, Y]
// that can be reached by moving by L or R steps
int countReachablePoints(int X, int Y,
                         int L, int R)
{
 
    // Initialize difference array
    int diff_arr[100000] = { 0 };
 
    // Initialize Count
    int count = 0;
 
    // Marking starting point
    diff_arr[X] = 1;
    diff_arr[X + 1] = -1;
 
    // Iterating from X to Y
    for (int i = X; i <= Y; i++) {
 
        // Accumulate difference array
        diff_arr[i] += diff_arr[i - 1];
 
        // If diff_arr[i] is greater
        // than 1
        if (diff_arr[i] >= 1) {
            // Updating difference array
            diff_arr[i + L] += 1;
            diff_arr[i + R + 1] -= 1;
 
            // Visited point found
            count++;
        }
    }
    return count;
}
 
// Driver Code
int main()
 
{
    // Given Input
    int X = 3, Y = 12, L = 2, R = 3;
 
    // Function Call
    cout << countReachablePoints(X, Y, L, R);
 
    return 0;
}


Java
// Java code for above approach
 
import java.util.*;
 
class GFG{
 
// Function to count points from the range [X, Y]
// that can be reached by moving by L or R steps
static int countReachablePoints(int X, int Y,
                         int L, int R)
{
 
    // Initialize difference array
    int diff_arr[] = new int[100000];
 
    // Initialize Count
    int count = 0;
 
    // Marking starting point
    diff_arr[X] = 1;
    diff_arr[X + 1] = -1;
 
    // Iterating from X to Y
    for (int i = X; i <= Y; i++) {
 
        // Accumulate difference array
        diff_arr[i] += diff_arr[i - 1];
 
        // If diff_arr[i] is greater
        // than 1
        if (diff_arr[i] >= 1) {
            // Updating difference array
            diff_arr[i + L] += 1;
            diff_arr[i + R + 1] -= 1;
 
            // Visited point found
            count++;
        }
    }
    return count;
}
 
// Driver Code
public static void main(String[] args)
 
{
    // Given Input
    int X = 3, Y = 12, L = 2, R = 3;
 
    // Function Call
    System.out.print(countReachablePoints(X, Y, L, R));
 
}
}
 
// This code contributed by shikhasingrajput


Python3
# Python3 code for above approach
 
# Function to count points from the range [X, Y]
# that can be reached by moving by L or R steps
def countReachablePoints(X, Y, L, R):
     
    # Initialize difference array
    diff_arr = [0 for i in range(100000)]
 
    # Initialize Count
    count = 0
 
    # Marking starting point
    diff_arr[X] = 1
    diff_arr[X + 1] = -1
 
    # Iterating from X to Y
    for i in range(X, Y + 1, 1):
         
        # Accumulate difference array
        diff_arr[i] += diff_arr[i - 1]
 
        # If diff_arr[i] is greater
        # than 1
        if (diff_arr[i] >= 1):
             
            # Updating difference array
            diff_arr[i + L] += 1
            diff_arr[i + R + 1] -= 1
 
            # Visited point found
            count += 1
 
    return count
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    X = 3
    Y = 12
    L = 2
    R = 3
 
    # Function Call
    print(countReachablePoints(X, Y, L, R))
     
# This code is contributed by ipg2016107


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to count points from the range [X, Y]
// that can be reached by moving by L or R steps
static int countReachablePoints(int X, int Y,
                         int L, int R)
{
 
    // Initialize difference array
    int[] diff_arr= new int[100000];
 
    // Initialize Count
    int count = 0;
 
    // Marking starting point
    diff_arr[X] = 1;
    diff_arr[X + 1] = -1;
 
    // Iterating from X to Y
    for (int i = X; i <= Y; i++) {
 
        // Accumulate difference array
        diff_arr[i] += diff_arr[i - 1];
 
        // If diff_arr[i] is greater
        // than 1
        if (diff_arr[i] >= 1)
        {
           
            // Updating difference array
            diff_arr[i + L] += 1;
            diff_arr[i + R + 1] -= 1;
 
            // Visited point found
            count++;
        }
    }
    return count;
}
// Driver Code
public static void Main()
{
        // Given Input
    int X = 3, Y = 12, L = 2, R = 3;
 
    // Function Call
    Console.Write(countReachablePoints(X, Y, L, R));
}
}
 
// This code is contributed by splevel62.


Javascript


输出:
9

时间复杂度: O(Y – X)
辅助空间: O(Y)