Minimum Operations
Problem Statement :
In this challenge, the task is to debug the existing code to successfully execute all provided test files. There are n boxes in front of you. For each i, box i contains r[i] red balls, g[i] green balls, and b[i] blue balls. You want to separate the balls by their color. In each operation, you can pick a single ball from some box and put it into another box. The balls are separated if no box contains balls of more than one color. Debug the given function min_operations and compute the minimal number of operations required to separate the balls. Note: In this problem you can modify at most six lines of code and you cannot add any new lines. To restore the original code, click on the icon to the right of the language selector. Input Format The first line contains a single integer n. The next n lines i contain three space-separated integers, r[i], g[i], and b[i], respectively. Constraints 1 <= n <= 100 0 <= r[i], g[i], b[i] <= 105 Output Format Print the minimal number of operations required to separate the balls. If this is impossible, return -1.
Solution :
Solution in C :
In C++ :
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <iostream>
#include <map>
using namespace std;
int dp[110][1<<3];
int min_operations(vector <int> red, vector <int> green, vector <int> blue) {
int n = (int)red.size(), 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] = 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]);
}
}
j = 0;
for (i = 0; i < n; i++){
if (red[i]) j |= 1;
if (green[i]) j |= 2;
if (blue[i]) j |= 4;
}
if (dp[n][j] >= (1<<30))
dp[n][j] = -1;
return dp[n][j];
}
int main() {
int n, r, g, b;
cin >> n;
vector<int> red, blue, green;
for(int i = 0; i < n; i++){
cin >> r >> g >> b;
red.push_back(r);
green.push_back(g);
blue.push_back(b);
}
cout << min_operations(red, green, blue) << "\n";
return 0;
}
In Java :
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();
}
}
In Python3 :
int n = (int)red.size(), i, j;
for (i = 0; i <= n; i++) {
for (j = 0; j < 8; j++) {
dp[i][j] = 1<<30;
}
}
dp[0][0] = 0;
for (i = 0; i < n; i++){
for (j = 0; j < 8; j++){
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]);
}
}
j = 0;
for (i = 0; i < n; i++){
if (green[i]) j |= 2;
if (red[i]) j |= 1;
if (blue[i]) j |= 4;
}
View More Similar Problems
Tree: Postorder Traversal
Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the
View Solution →Tree: Inorder Traversal
In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func
View Solution →Tree: Height of a Binary Tree
The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary
View Solution →Tree : Top View
Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.
View Solution →Tree: Level Order Traversal
Given a pointer to the root of a binary tree, you need to print the level order traversal of this tree. In level-order traversal, nodes are visited level by level from left to right. Complete the function levelOrder and print the values in a single line separated by a space. For example: 1 \ 2 \ 5 / \ 3 6 \ 4 F
View Solution →Binary Search Tree : Insertion
You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a function, Node * insert (Node * root ,int data) { } Constraints No. of nodes in the tree <
View Solution →