Minimum Penalty Path Discussions | Algorithms | HackerRank
  • + 0 comments

    For python code, at the begining I had a lot of test cases with time limit. I did a lot of tweaking and solved most of them. However, I could not pass test case 2. I changed the way I read input, as a result the prblem was solved.

    if name == 'main': # Read all input at once using sys.stdin.read() input_data = sys.stdin.read().splitlines()

    # Process the first line to extract n and m
    n, m = map(int, input_data[0].split())
    
    # Initialize an empty list for edges
    edges = []
    
    # Process each of the next m lines to extract edges
    for i in range(1, m + 1):
        edges.append(list(map(int, input_data[i].split())))
    
    # Process the second to last line to extract A and B
    A, B = map(int, input_data[m + 1].split())
    
    # Call the function with the parsed data
    result = beautifulPath(n, edges, A, B)
    
    # Write the result to the output file
    with open(os.environ['OUTPUT_PATH'], 'w') as fptr:
        fptr.write(str(result) + '\n')