You are viewing a single comment's thread. Return to all 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(); } }
Seems like cookies are disabled on this browser, please enable them to open this website
Minimum Operations 4
You are viewing a single comment's thread. Return to all comments →
Java 7 O(n)