📌  相关文章
📜  检查是否可以通过给定路径从 0 到达 M

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

检查是否可以通过给定路径从 0 到达 M

给定一个由N对整数组成的数组arr[] ,其中每对(a, b)表示从ab的路径,任务是检查是否可以使用数组中的给定路径从0到达M arr[] 。如果可能,则打印“是” 。否则,打印“否”

例子:

方法:给定的问题可以通过使用给定的路径数组作为一对从0找到最右边的点来解决,然后如果最右边的点大于等于M ,则意味着在0M之间存在路径。否则,它不是。请按照以下步骤解决问题:

  • 初始化一个数组,比如rightMost[]dp[] ,分别存储从1点和最远点可以到达的更远点,并用0初始化rightMost[]的每个值。
  • 迭代范围[0, N – 1]并将rightMost[a[i][0]]更新为 rightMost[a[i][0]]arr[i][1]的最大值。
  • 使用变量i迭代范围[M, 0]
    • dp[i]的值更新为i
    • 使用变量j迭代范围[min(m, rightMost[i]), i 1 ]并将dp[i]更新为dp[i]dp[j]的最大值。
  • 如果dp[0]的值至少为 M ,则可以从0到达M ,因此打印“Yes” 。否则,打印“否”

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if it is
// possible to reach M from 0
void canReach0toM(int a[][2], int n,
                  int m)
{
    // Stores the farther point that
    // can reach from 1 point
    int rightMost[m + 1];
 
    // Stores the farthest point it
    // can go for each index i
    int dp[m + 1];
 
    // Initialize rightMost[i] with 0
    for (int i = 0; i <= m; i++) {
        rightMost[i] = 0;
    }
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
        int a1 = a[i][0];
        int b1 = a[i][1];
 
        // Update the rightMost
        // position reached from a1
        rightMost[a1] = max(
            rightMost[a1], b1);
    }
 
    for (int i = m; i >= 0; i--) {
 
        dp[i] = i;
 
        // Find the farthest point
        // it can reach from i
        for (int j = min(m, rightMost[i]);
             j > i; j--) {
            dp[i] = max(dp[i], dp[j]);
        }
    }
 
    // If point < can be reached
    if (dp[0] >= m) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
}
 
// Driver Code
int main()
{
    int arr[][2] = { { 0, 2 }, { 2, 2 },
                     { 2, 5 }, { 4, 5 } };
    int M = 5;
    int N = sizeof(arr) / sizeof(arr[0]);
    canReach0toM(arr, N, M);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG {
 
    // Function to check if it is
    // possible to reach M from 0
    static void canReach0toM(int[][] a, int n, int m)
    {
       
        // Stores the farther point that
        // can reach from 1 point
        int[] rightMost = new int[m + 1];
 
        // Stores the farthest point it
        // can go for each index i
        int[] dp = new int[m + 1];
 
        // Initialize rightMost[i] with 0
        for (int i = 0; i <= m; i++) {
            rightMost[i] = 0;
        }
 
        // Traverse the array
        for (int i = 0; i < n; i++) {
 
            int a1 = a[i][0];
            int b1 = a[i][1];
 
            // Update the rightMost
            // position reached from a1
            rightMost[a1] = Math.max(rightMost[a1], b1);
        }
 
        for (int i = m; i >= 0; i--) {
 
            dp[i] = i;
 
            // Find the farthest point
            // it can reach from i
            for (int j = Math.min(m, rightMost[i]); j > i;
                 j--) {
                dp[i] = Math.max(dp[i], dp[j]);
            }
        }
 
        // If point < can be reached
        if (dp[0] >= m) {
            System.out.print("Yes");
        }
        else {
            System.out.print("No");
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int[][] arr
            = { { 0, 2 }, { 2, 2 }, { 2, 5 }, { 4, 5 } };
        int M = 5;
        int N = arr.length;
        canReach0toM(arr, N, M);
    }
}
 
// This code is contributed by subhammahato348.


Python3
# Python3 program for the above approach
 
# Function to check if it is
# possible to reach M from 0
def canReach0toM(a, n, m):
     
    # Stores the farther point that
    # can reach from 1 point
    rightMost = [0 for i in range(m + 1)]
 
    # Stores the farthest point it
    # can go for each index i
    dp = [0 for i in range(m + 1)]
 
    # Initialize rightMost[i] with 0
    for i in range(m + 1):
        rightMost[i] = 0
 
    # Traverse the array
    for i in range(n):
        a1 = a[i][0]
        b1 = a[i][1]
 
        # Update the rightMost
        # position reached from a1
        rightMost[a1] = max(rightMost[a1], b1)
 
    i = m
     
    while(i >= 0):
        dp[i] = i
 
        # Find the farthest point
        # it can reach from i
        j = min(m, rightMost[i])
        while(j > i):
            dp[i] = max(dp[i], dp[j])
            j -= 1
             
        i -= 1
 
    # If point < can be reached
    if (dp[0] >= m):
        print("Yes")
    else:
        print("No")
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ [ 0, 2 ], [ 2, 2 ],
            [ 2, 5 ], [ 4, 5 ] ]
    M = 5
    N = len(arr)
     
    canReach0toM(arr, N, M)
     
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if it is
// possible to reach M from 0
static void canReach0toM(int[,] a, int n, int m)
{
     
    // Stores the farther point that
    // can reach from 1 point
    int[] rightMost = new int[m + 1];
 
    // Stores the farthest point it
    // can go for each index i
    int[] dp = new int[m + 1];
 
    // Initialize rightMost[i] with 0
    for(int i = 0; i <= m; i++)
    {
        rightMost[i] = 0;
    }
 
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
        int a1 = a[i, 0];
        int b1 = a[i, 1];
 
        // Update the rightMost
        // position reached from a1
        rightMost[a1] = Math.Max(rightMost[a1], b1);
    }
 
    for(int i = m; i >= 0; i--)
    {
        dp[i] = i;
 
        // Find the farthest point
        // it can reach from i
        for(int j = Math.Min(m, rightMost[i]); j > i; j--)
        {
            dp[i] = Math.Max(dp[i], dp[j]);
        }
    }
 
    // If point < can be reached
    if (dp[0] >= m)
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
 
// Driver Code
public static void Main()
{
    int[,] arr = { { 0, 2 }, { 2, 2 },
                   { 2, 5 }, { 4, 5 } };
    int M = 5;
    int N = arr.GetLength(0);
     
    canReach0toM(arr, N, M);
}
}
 
// This code is contributed by code_hunt


Javascript


输出:
Yes

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