Minimum Operations 4

Sort by

recency

|

10 Discussions

|

  • + 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]
    
  • + 1 comment

    So, this is apparently one of those challenges where you're supposed to be debugging existing code rather than typing up code. I saw the absolute lack of a code for Python 3, tried to build a code that solved the problem (not noticing the debugging part of the question or the "Can't change more than six lines thing"), and didn't understand why my 50-line code was running errors when it was giving the exact numerical values it was looking for. If you're having similar issues... now you know. Hope HackerRank fixes this soon, but if the age of some of these comments are saying anything, I'm not gonna hold my breath

  • + 0 comments

    Java 7 O(n)

    import java.util.*;
    
    class MinimumOperations {
        private static final Scanner scan = new Scanner(System.in);
        int n, r ,g, b;
        int[][] dp = new int[110][1<<3];
    
        Vector<Integer> red = new Vector();
        Vector<Integer> green = new Vector();
        Vector<Integer> blue = new Vector();
    
        public void get() {
             n = scan.nextInt();
    
            for (int i = 0; i < n; i++) {
                r = scan.nextInt();
                g = scan.nextInt();
                b = scan.nextInt();
                red.add(r);
                green.add(g);
                blue.add(b);
            }
        }
    
        public void minOperations() {
            int i, j;
            for (i = 0; i <= n; i++) {
                for (j = 0; j <= 7; j++) {
                    dp[i][j] = (1<<30);
                }
            }
            dp[0][0] = 0;
            for (i = 0; i < n; i++){
                for (j = 0; j <= 7; j++){
                    dp[i + 1][j | 1] = Math.min(dp[i + 1][j | 1], dp[i][j] + green.get(i) + blue.get(i));
                    dp[i + 1][j | 2] = Math.min(dp[i + 1][j | 2], dp[i][j] + red.get(i) + blue.get(i));
                    dp[i + 1][j | 4] = Math.min(dp[i + 1][j | 4], dp[i][j] + red.get(i) + green.get(i));
                }
            }
            j = 0;
            for (i = 0; i < n; i++){
                if (green.get(i) != 0) j |= 1;
                if (red.get(i) != 0) j |= 2;
                if (blue.get(i) != 0) j |= 4;
            }
            if (dp[n][j] >= (1<<30)) dp[n][j] = -1;
                System.out.println(dp[n][j]);
        }
    }
    class Solution {
        public static void main(String[] args) {
            MinimumOperations obj = new MinimumOperations();
            obj.get();
            obj.minOperations();
        }
    }
    
  • + 1 comment

    Python3 code is empty, missing the standard Python script structure, which includes reading input from standard input (stdin), processing it, and then writing the output to standard output (stdout).

    if name == 'main': fptr = open(os.environ['OUTPUT_PATH'], 'w')

    t = int(input().strip())
    
    for t_itr in range(t):
        first_multiple_input = input().rstrip().split()
    
        n = int(first_multiple_input[0])
    
        boxes = []
        for _ in range(n):
            boxes.append(list(map(int, input().rstrip().split())))
    
        result = min_operations(n, boxes)
    
        fptr.write(str(result) + '\n')
    
    fptr.close()
    
  • + 0 comments

    No python 3 code to modify?