package ej2.func; public class HowManySongYouCanPlay { static int opt = 0; public static int getOpt(int[] songs, int remaining, int count, int interval, int maxTime) { opt = 0; if (songs.length == 0) return 0; for (int i = 0; i < songs.length; i++) { if (songs[i] < 0) return 0; } playSongs(songs, remaining, count, interval, maxTime); return opt; } private static void playSongs(int[] songs, int remaining, int count, int interval, int maxTime) { if (count == songs.length && remaining >= 0 || remaining == 0) { if (maxTime - remaining > opt) opt = maxTime - remaining; } else if (remaining > 0) { if (remaining >= songs[count]) { int actualValue = remaining - songs[count]; if (remaining != maxTime) actualValue -= interval; if (remaining >= actualValue) { playSongs(songs, actualValue, count + 1, interval, maxTime); } } playSongs(songs, remaining, count + 1, interval, maxTime); } } }
- package ej2.func;
- public class HowManySongYouCanPlay {
- static int opt = 0;
- public static int getOpt(int[] songs, int remaining, int count, int interval, int maxTime) {
- opt = 0;
- if (songs.length == 0) return 0;
- for (int i = 0; i < songs.length; i++) {
- if (songs[i] < 0) return 0;
- }
- playSongs(songs, remaining, count, interval, maxTime);
- return opt;
- }
- private static void playSongs(int[] songs, int remaining, int count, int interval, int maxTime) {
- if (count == songs.length && remaining >= 0 || remaining == 0) {
- if (maxTime - remaining > opt) opt = maxTime - remaining;
- } else if (remaining > 0) {
- if (remaining >= songs[count]) {
- int actualValue = remaining - songs[count];
- if (remaining != maxTime)
- actualValue -= interval;
- if (remaining >= actualValue) {
- playSongs(songs, actualValue, count + 1, interval, maxTime);
- }
- }
- playSongs(songs, remaining, count + 1, interval, maxTime);
- }
- }
- }
Task:
Write a program that takes as input an array of integers representing the durations in seconds of the songs in a concert, and returns the maximum number of seconds whose total duration approaches without passing a 60 minutes (including an additional minute between each song).
Input:
An array of integers representing the durations in seconds of the songs in the concert.
Output:
An integer representing the maximum number of seconds that can be included in the concert, according to the criteria mentioned above.
Example:
1º Array with song durations: [240, 300, 180, 360, 120, 240, 300, 240, 180, 240]. Maximum duration: 2400
seconds(41 minutes)
2º Array with song durations: [397, 634, 274, 510, 405, 323, 291, 279, 142, 117]. Maximum duration: 3512
seconds (58 minutes y 32 seconds)
3º Array with song durations: [531, 329, 401, 542, 216, 313, 489, 517, 485]. Maximum duration: 3510
seconds (58 minutes y 30 seconds)
Note:
- The input array must not be null or empty.
- The song durations must be positive integers.
- The program must consider the additional minute between each song when calculating the total duration of the concert.
Good luck!
package ej2.func; public class HowManySongYouCanPlay { static int opt = 0; public static int getOpt(int[] songs, int remaining, int contador, int intervalo, int maxTime) { playSongs(songs, remaining, contador, intervalo, maxTime); return opt; } public static void playSongs(int[] songs, int remaining, int contador, int intervalo, int maxTime) { if (contador == songs.length || remaining == 0) { if (maxTime - remaining > opt) opt = maxTime - remaining; } else if (remaining > 0) { if (remaining >= songs[contador] + intervalo) { int actualValue = remaining - songs[contador]; if (remaining != maxTime) actualValue += intervalo; playSongs(songs, actualValue, contador + 1, intervalo, maxTime); } playSongs(songs, remaining, contador + 1, intervalo, maxTime); } } }
- package ej2.func;
- public class HowManySongYouCanPlay {
- static int opt = 0;
- public static int getOpt(int[] songs, int remaining, int contador, int intervalo, int maxTime) {
- playSongs(songs, remaining, contador, intervalo, maxTime);
- return opt;
- }
- public static void playSongs(int[] songs, int remaining, int contador, int intervalo, int maxTime) {
- if (contador == songs.length || remaining == 0) {
- if (maxTime - remaining > opt) opt = maxTime - remaining;
- } else if (remaining > 0) {
- if (remaining >= songs[contador] + intervalo) {
- int actualValue = remaining - songs[contador];
- if (remaining != maxTime) actualValue += intervalo;
- playSongs(songs, actualValue, contador + 1, intervalo, maxTime);
- }
- playSongs(songs, remaining, contador + 1, intervalo, maxTime);
- }
- }
- }
Task:
Write a program that takes as input an array of integers representing the durations in seconds of the songs in a concert, and returns the maximum number of seconds whose total duration approximates or equals a minimum of 50 minutes and a maximum of 60 minutes (including the additional minute between each song).
Input:
An array of integers representing the durations in seconds of the songs in the concert.
Output:
An integer representing the maximum number of songs that can be included in the concert, according to the criteria mentioned above.
Example:
1º Array with song durations: [240, 300, 180, 360, 120, 240, 300, 240, 180, 240]. Total duration: 49 minutes (Does not meet the minimum of 50 minutes)
2º Array with song durations: [420, 180, 360, 240, 120, 240, 300, 180]. Total duration: 59 minutes and 15 seconds (Meets the criteria)
3º Array with song durations: [300, 240, 480, 180, 240, 240, 300, 360]. Total duration: 57 minutes and 40 seconds (Meets the criteria)
Note:
- The input array must not be null or empty.
- The song durations must be positive integers.
- The program must consider the additional minute between each song when calculating the total duration of the concert.
Good luck!
public class HowManySongYouCanPlay { public static String playSongs(Integer[] songDuration) { return ""; } }
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; // TODO: Replace examples and use TDD by writing your own tests class SolutionTest { @Test void testSomething() { // assertEquals("expected", "actual"); } }