Created
December 14, 2023 11:56
-
-
Save youssef3wi/cd17c8d3a8aa0b2b4dc51a968585b80d to your computer and use it in GitHub Desktop.
Pick the packages from the heaviest to the lightest one.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * <p> | |
| * Rules | |
| * <p> | |
| * You work in an automated factory that controls a robotic arm to move packages. | |
| * The arm can pick packages from the conveyor belts to form a stack of packages. | |
| * The packages are sorted from the heaviest to the lightest on each of the conveyor belts. | |
| * Your objective is to pick the heaviest package among the 3 conveyor belts to move it on a stack. | |
| * <p> | |
| * Implement the function solve(weight0, weight1, weight2) that takes 3 integer arguments: weight0, weight1 and weight2. | |
| * These values represent the weight of the packages available on the conveyor belts with respective index 0, 1 and 2. | |
| * When a conveyor belt is empty, the value is 0. | |
| * <p> | |
| * The function must return the index of the conveyor belt that has the heaviest package. | |
| * For example, if the values for weight0, weight1 and weight2 are 85, 100 and 90, then the expected answer is 1. | |
| * In case of equality, any correct answer is accepted. | |
| * <p> | |
| * The function solve(weight0, weight1, weight2) will be called successively until all the conveyor belts are empty. | |
| */ | |
| function solve(weight0: number, weight1: number, weight2: number): number { | |
| if (weight0 >= weight1) { | |
| if (weight1 >= weight2 || weight0 >= weight2) { | |
| return 0; | |
| } else { | |
| return 2; | |
| } | |
| } else if (weight1 >= weight2) { | |
| return 1; | |
| } | |
| return 2; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In this Java version, the function solve takes three double parameters and returns an int, just like in your original TypeScript version. You can call it from the main method for testing.
public class WeightSolver {
public static int solve(double weight0, double weight1, double weight2) {
if (weight0 >= weight1) {
if (weight1 >= weight2 || weight0 >= weight2) {
return 0;
} else {
return 2;
}
} else if (weight1 >= weight2) {
return 1;
}
return 2;
}
}