Minimum Operations 4

  • + 0 comments

    I am wondering if the Python 3 solution is correct. I changed 5 lines and at least for the first 6 tests, I got the same solution as the correct solution. However, HackerRank always told me that I am wrong. I mean at least for the first two sample tests, I am correct, right? Please help. I don't think the answer for Python 3 is right.

    def min_operations(red, green, blue): dp = [[(1<<30) for x in range(8)] for y in range(101)] # 7 to 8 n = len(red) dp[0][0] = 0 for i in range(0, n): for j in range(0, 8): # 7 to 8 dp[i + 1][j | 1] = min(dp[i + 1][j | 1], dp[i][j] + green[i] + blue[i]) dp[i + 1][j | 2] = min(dp[i + 1][j | 2], dp[i][j] + red[i] + blue[i]) dp[i + 1][j | 4] = min(dp[i + 1][j | 4], dp[i][j] + red[i] + green[i]) # blue to red

    j = 0
    for i in range(0, n):
        if red[i]: # changed to red
            j |= 1 
        if green[i]: # changed to green
            j |= 2 
        if blue[i]:
            j |= 4
    
    if dp[n][j] >= (1<<30):
        dp[n][j] = -1
    
    return dp[n][j]