Minimum Operations 4

Sort by

recency

|

9 Discussions

|

  • + 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?

  • + 0 comments

    Could someone explain the logic behind this solution?