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 contador, int intervalo, int maxTime) {opt = 0;playSongs(songs, remaining, 0, intervalo, remaining);return opt;}- 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);
private 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]) {int actualValue = remaining - songs[contador];if (remaining != maxTime)actualValue -= intervalo;if (remaining >= actualValue) {playSongs(songs, actualValue, contador + 1, intervalo, maxTime);}}playSongs(songs, remaining, contador + 1, intervalo, maxTime);}}- }
- }
- }
import ej2.func.HowManySongYouCanPlay; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import java.util.Random; import java.util.ArrayList; import org.junit.jupiter.api.Timeout; import static org.junit.jupiter.api.Assertions.assertEquals; class SolutionTest { int remaining; int counter; int maxTime; int interval; static int opt; @BeforeEach void setup() { remaining = 3600; counter = 0; maxTime = 3600; interval = 60; } public static int calculateOpt(int[] songs, int remaining, int count, int interval, int maxTime) { opt = 0; playSongs(songs, remaining, count, interval, maxTime); return opt; } public 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); } } @Test @DisplayName("Hidden Test") void hiddenTest() { assertEquals(0, HowManySongYouCanPlay.getOpt(new int[] {}, remaining, counter, interval, maxTime)); assertEquals(3600, HowManySongYouCanPlay.getOpt(new int[] { 1200, 1200, 1080 }, remaining, counter, interval, maxTime)); assertEquals(0, HowManySongYouCanPlay.getOpt(new int[] { 53, 22, -54, 62, 90, 56, -72, 2, 8, 17, 36, 5, 36, 4 }, remaining, counter, interval, maxTime)); assertEquals(3600, HowManySongYouCanPlay.getOpt(new int[] { 3600 }, remaining, counter, interval, maxTime)); } @Test @DisplayName("Sample Test") void sampleTest() { assertEquals(2100, HowManySongYouCanPlay.getOpt(new int[] { 240, 300, 180, 360, 120, 240, 300 }, remaining, counter, interval, maxTime)); assertEquals(3330, HowManySongYouCanPlay.getOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180, 400, 350 }, remaining, counter, interval, maxTime)); assertEquals(3600, HowManySongYouCanPlay.getOpt(new int[] { 1200, 1500, 1080, 1620, 3660, 1200, 2160 }, remaining, counter, interval, maxTime)); } @Test @Timeout(10000) @DisplayName("RandomTest") void randomTest() { for (int i = 0; i < 4; i++) { int[] randomListOne = randomList(new Random().nextInt(40)); assertEquals(calculateOpt(randomListOne, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(randomListOne, remaining, counter, interval, maxTime)); } } public int[] randomList(int n) { ArrayList<Integer> numbersList = new ArrayList<>(); for (int i = 0; i < n; i++) { int numb = new Random().nextInt(1000); numbersList.add(numb); } return numbersList.stream().mapToInt(i -> i).toArray(); } }
- import ej2.func.HowManySongYouCanPlay;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.DisplayName;
- import org.junit.jupiter.api.Tag;
- import org.junit.jupiter.api.Test;
- import java.util.Random;
- import java.util.ArrayList;
- import org.junit.jupiter.api.Timeout;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- class SolutionTest {
- int remaining;
- int counter;
- int maxTime;
- int interval;
- static int opt;
- @BeforeEach
- void setup() {
- remaining = 3600;
- counter = 0;
- maxTime = 3600;
- interval = 60;
- }
public static int calculateOpt(int[] songs, int remaining, int contador, int intervalo, int maxTime) {- public static int calculateOpt(int[] songs, int remaining, int count, int interval, int maxTime) {
- opt = 0;
playSongs(songs, remaining, contador, intervalo, maxTime);- playSongs(songs, remaining, count, interval, maxTime);
- return opt;
- }
public static void playSongs(int[] songs, int remaining, int contador, int intervalo, int maxTime) {if (contador == songs.length || remaining == 0) {- public 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[contador]) {int actualValue = remaining - songs[contador];- if (remaining >= songs[count]) {
- int actualValue = remaining - songs[count];
- if (remaining != maxTime)
actualValue -= intervalo;- actualValue -= interval;
- if (remaining >= actualValue) {
playSongs(songs, actualValue, contador + 1, intervalo, maxTime);- playSongs(songs, actualValue, count + 1, interval, maxTime);
- }
- }
playSongs(songs, remaining, contador + 1, intervalo, maxTime);- playSongs(songs, remaining, count + 1, interval, maxTime);
- }
- }
- @Test
- @DisplayName("Hidden Test")
void testSample() {assertEquals(calculateOpt(new int[] {}, remaining, counter, interval, maxTime),HowManySongYouCanPlay.getOpt(new int[] {}, remaining, counter, interval, maxTime));assertEquals(calculateOpt(new int[] { 20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60 }, remaining,counter, interval, maxTime),HowManySongYouCanPlay.getOpt(new int[] { 20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60 }, remaining, counter,interval, maxTime));assertEquals(calculateOpt(new int[] { 53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4 }, remaining, counter,interval, maxTime),HowManySongYouCanPlay.getOpt(new int[] { 53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4 },remaining, counter, interval, maxTime));assertEquals(calculateOpt(new int[] { 240, 300, 180, 360, 120, 240, 300, 240, 180, 240 }, remaining, counter,interval, maxTime),HowManySongYouCanPlay.getOpt(new int[] { 240, 300, 180, 360, 120, 240, 300, 240, 180, 240 }, remaining,counter, interval, maxTime));assertEquals(calculateOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180 }, remaining, counter, interval,maxTime),HowManySongYouCanPlay.getOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180 }, remaining, counter,interval, maxTime));assertEquals(calculateOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter, interval,maxTime),HowManySongYouCanPlay.getOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter,interval, maxTime));assertEquals(calculateOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 120 }, remaining, counter,interval, maxTime),HowManySongYouCanPlay.getOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 120 }, remaining,counter, interval, maxTime));assertEquals(calculateOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 240 }, remaining, counter,interval, maxTime),HowManySongYouCanPlay.getOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 240 }, remaining,counter, interval, maxTime));- void hiddenTest() {
- assertEquals(0, HowManySongYouCanPlay.getOpt(new int[] {}, remaining, counter, interval, maxTime));
- assertEquals(3600,
- HowManySongYouCanPlay.getOpt(new int[] { 1200, 1200, 1080 }, remaining, counter, interval, maxTime));
- assertEquals(0, HowManySongYouCanPlay.getOpt(new int[] { 53, 22, -54, 62, 90, 56, -72, 2, 8, 17, 36, 5, 36, 4 },
- remaining, counter, interval, maxTime));
- assertEquals(3600, HowManySongYouCanPlay.getOpt(new int[] { 3600 }, remaining, counter, interval, maxTime));
- }
- @Test
- @DisplayName("Sample Test")
void seenTest() {assertEquals(calculateOpt(new int[] { 240, 300, 180, 360, 120, 240, 300 }, remaining, counter, interval, maxTime),HowManySongYouCanPlay.getOpt(new int[] { 240, 300, 180, 360, 120, 240, 300 }, remaining, counter,interval, maxTime));assertEquals(calculateOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180, 400, 350 }, remaining, counter,interval, maxTime),HowManySongYouCanPlay.getOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180, 400, 350 }, remaining,counter, interval, maxTime));assertEquals(calculateOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter, interval,maxTime),HowManySongYouCanPlay.getOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter,interval, maxTime));- void sampleTest() {
- assertEquals(2100, HowManySongYouCanPlay.getOpt(new int[] { 240, 300, 180, 360, 120, 240, 300 }, remaining,
- counter, interval, maxTime));
- assertEquals(3330, HowManySongYouCanPlay.getOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180, 400, 350 },
- remaining, counter, interval, maxTime));
- assertEquals(3600, HowManySongYouCanPlay.getOpt(new int[] { 1200, 1500, 1080, 1620, 3660, 1200, 2160 },
- remaining, counter, interval, maxTime));
- }
- @Test
- @Timeout(10000)
@DisplayName("Random Test")void RandomTest() {- @DisplayName("RandomTest")
- void randomTest() {
- for (int i = 0; i < 4; i++) {
- int[] randomListOne = randomList(new Random().nextInt(40));
- assertEquals(calculateOpt(randomListOne, remaining, counter, interval, maxTime),
- HowManySongYouCanPlay.getOpt(randomListOne, remaining, counter, interval, maxTime));
- }
- }
- public int[] randomList(int n) {
- ArrayList<Integer> numbersList = new ArrayList<>();
- for (int i = 0; i < n; i++) {
- int numb = new Random().nextInt(1000);
- numbersList.add(numb);
- }
- return numbersList.stream().mapToInt(i -> i).toArray();
- }
- }
package ej2.func; public class HowManySongYouCanPlay { static int opt = 0; public static int getOpt(int[] songs, int remaining, int contador, int intervalo, int maxTime) { opt = 0; playSongs(songs, remaining, 0, intervalo, remaining); return opt; } private 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]) { int actualValue = remaining - songs[contador]; if (remaining != maxTime) actualValue -= intervalo; if (remaining >= actualValue) { 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) {opt = 0;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);- public static int getOpt(int[] songs, int remaining, int contador, int intervalo, int maxTime) {
- opt = 0;
- playSongs(songs, remaining, 0, intervalo, remaining);
- return opt;
- }
}}- private 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]) {
- int actualValue = remaining - songs[contador];
- if (remaining != maxTime)
- actualValue -= intervalo;
- if (remaining >= actualValue) {
- playSongs(songs, actualValue, contador + 1, intervalo, maxTime);
- }
- }
- playSongs(songs, remaining, contador + 1, intervalo, maxTime);
- }
- }
- }
import ej2.func.HowManySongYouCanPlay; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import java.util.Random; import java.util.ArrayList; import org.junit.jupiter.api.Timeout; import static org.junit.jupiter.api.Assertions.assertEquals; class SolutionTest { int remaining; int counter; int maxTime; int interval; static int opt; @BeforeEach void setup() { remaining = 3600; counter = 0; maxTime = 3600; interval = 60; } public static int calculateOpt(int[] songs, int remaining, int contador, int intervalo, int maxTime) { opt = 0; 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]) { int actualValue = remaining - songs[contador]; if (remaining != maxTime) actualValue -= intervalo; if (remaining >= actualValue) { playSongs(songs, actualValue, contador + 1, intervalo, maxTime); } } playSongs(songs, remaining, contador + 1, intervalo, maxTime); } } @Test @DisplayName("Hidden Test") void testSample() { assertEquals(calculateOpt(new int[] {}, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] {}, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt( new int[] { 20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60 }, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4 }, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 240, 300, 180, 360, 120, 240, 300, 240, 180, 240 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 240, 300, 180, 360, 120, 240, 300, 240, 180, 240 }, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180 }, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 120 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 120 }, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 240 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 240 }, remaining, counter, interval, maxTime)); } @Test @DisplayName("Sample Test") void seenTest() { assertEquals( calculateOpt(new int[] { 240, 300, 180, 360, 120, 240, 300 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 240, 300, 180, 360, 120, 240, 300 }, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180, 400, 350 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180, 400, 350 }, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter, interval, maxTime)); } @Test @Timeout(10000) @DisplayName("Random Test") void RandomTest() { for (int i = 0; i < 4; i++) { int[] randomListOne = randomList(new Random().nextInt(40)); assertEquals(calculateOpt(randomListOne, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(randomListOne, remaining, counter, interval, maxTime)); } } public int[] randomList(int n) { ArrayList<Integer> numbersList = new ArrayList<>(); for (int i = 0; i < n; i++) { int numb = new Random().nextInt(1000); numbersList.add(numb); } return numbersList.stream().mapToInt(i -> i).toArray(); } }
- import ej2.func.HowManySongYouCanPlay;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.DisplayName;
- import org.junit.jupiter.api.Tag;
- import org.junit.jupiter.api.Test;
- import java.util.Random;
- import java.util.ArrayList;
- import org.junit.jupiter.api.Timeout;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- class SolutionTest {
- int remaining;
- int counter;
- int maxTime;
- int interval;
- static int opt;
- @BeforeEach
- void setup() {
- remaining = 3600;
- counter = 0;
- maxTime = 3600;
- interval = 60;
- }
- public static int calculateOpt(int[] songs, int remaining, int contador, int intervalo, int maxTime) {
- opt = 0;
- 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) {- if (remaining >= songs[contador]) {
- int actualValue = remaining - songs[contador];
- if (remaining != maxTime)
actualValue += intervalo;playSongs(songs, actualValue, contador + 1, intervalo, maxTime);- actualValue -= intervalo;
- if (remaining >= actualValue) {
- playSongs(songs, actualValue, contador + 1, intervalo, maxTime);
- }
- }
- playSongs(songs, remaining, contador + 1, intervalo, maxTime);
- }
- }
- @Test
- @DisplayName("Hidden Test")
- void testSample() {
- assertEquals(calculateOpt(new int[] {}, remaining, counter, interval, maxTime),
- HowManySongYouCanPlay.getOpt(new int[] {}, remaining, counter, interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60 }, remaining,
- counter, interval, maxTime),
- HowManySongYouCanPlay.getOpt(
- new int[] { 20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60 }, remaining, counter,
- interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4 }, remaining, counter,
- interval, maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4 },
- remaining, counter, interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 240, 300, 180, 360, 120, 240, 300, 240, 180, 240 }, remaining, counter,
- interval, maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 240, 300, 180, 360, 120, 240, 300, 240, 180, 240 }, remaining,
- counter, interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180 }, remaining, counter, interval,
- maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180 }, remaining, counter,
- interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter, interval,
- maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter,
- interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 120 }, remaining, counter,
- interval, maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 120 }, remaining,
- counter, interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 240 }, remaining, counter,
- interval, maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 240 }, remaining,
- counter, interval, maxTime));
- }
- @Test
- @DisplayName("Sample Test")
- void seenTest() {
- assertEquals(
- calculateOpt(new int[] { 240, 300, 180, 360, 120, 240, 300 }, remaining, counter, interval, maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 240, 300, 180, 360, 120, 240, 300 }, remaining, counter,
- interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180, 400, 350 }, remaining, counter,
- interval, maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180, 400, 350 }, remaining,
- counter, interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter, interval,
- maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter,
- interval, maxTime));
- }
- @Test
- @Timeout(10000)
- @DisplayName("Random Test")
- void RandomTest() {
- for (int i = 0; i < 4; i++) {
int[] randomListOne = randomList(new Random().nextInt(45));- int[] randomListOne = randomList(new Random().nextInt(40));
- assertEquals(calculateOpt(randomListOne, remaining, counter, interval, maxTime),
- HowManySongYouCanPlay.getOpt(randomListOne, remaining, counter, interval, maxTime));
- }
- }
- public int[] randomList(int n) {
- ArrayList<Integer> numbersList = new ArrayList<>();
- for (int i = 0; i < n; i++) {
- int numb = new Random().nextInt(1000);
- numbersList.add(numb);
- }
- return numbersList.stream().mapToInt(i -> i).toArray();
- }
package ej2.func; public class HowManySongYouCanPlay { static int opt = 0; public static int getOpt(int[] songs, int remaining, int contador, int intervalo, int maxTime) { opt = 0; 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) {
- opt = 0;
- 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);
- }
- }
- }
import ej2.func.HowManySongYouCanPlay; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import java.util.Random; import java.util.ArrayList; import static org.junit.jupiter.api.Assertions.assertEquals; class SolutionTest { int remaining; int counter; int maxTime; int interval; static int opt; @BeforeEach void setup() { remaining = 3600; counter = 0; maxTime = 3600; interval = 60; } public static int calculateOpt(int[] songs, int remaining, int contador, int intervalo, int maxTime) { opt = 0; 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); } } @Test @DisplayName("Hidden Test") void testSample() { assertEquals( calculateOpt(new int[] { 20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt( new int[] { 20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60 }, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4 }, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 240, 300, 180, 360, 120, 240, 300, 240, 180, 240 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 240, 300, 180, 360, 120, 240, 300, 240, 180, 240 }, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180 }, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 120 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 120 }, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 240 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 240 }, remaining, counter, interval, maxTime)); } @Test @DisplayName("Sample Test") void seenTest() { assertEquals( calculateOpt(new int[] { 240, 300, 180, 360, 120, 240, 300 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 240, 300, 180, 360, 120, 240, 300 }, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180, 400, 350 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180, 400, 350 }, remaining, counter, interval, maxTime)); assertEquals( calculateOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter, interval, maxTime)); } @Test @DisplayName("Random Test") void RandomTest() { for (int i = 0; i < 4; i++) { int[] randomListOne = randomList(new Random().nextInt(45)); assertEquals(calculateOpt(randomListOne, remaining, counter, interval, maxTime), HowManySongYouCanPlay.getOpt(randomListOne, remaining, counter, interval, maxTime)); } } public int[] randomList(int n) { ArrayList<Integer> numbersList = new ArrayList<>(); for (int i = 0; i < n; i++) { int numb = new Random().nextInt(1000); numbersList.add(numb); } return numbersList.stream().mapToInt(i -> i).toArray(); } }
- import ej2.func.HowManySongYouCanPlay;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.DisplayName;
- import org.junit.jupiter.api.Tag;
- import org.junit.jupiter.api.Test;
- import java.util.Random;
- import java.util.ArrayList;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- class SolutionTest {
- int remaining;
- int counter;
- int maxTime;
- int interval;
- static int opt;
- @BeforeEach
void setup(){remaining = 3600;counter = 0;maxTime = 3600;interval = 60;- void setup() {
- remaining = 3600;
- counter = 0;
- maxTime = 3600;
- interval = 60;
- }
- public static int calculateOpt(int[] songs, int remaining, int contador, int intervalo, int maxTime) {
opt = 0;playSongs(songs,remaining,contador,intervalo,maxTime);return opt;- opt = 0;
- 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);- 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);
- }
- }
- @Test
@Tag("Hidden Test")- @DisplayName("Hidden Test")
- void testSample() {
assertEquals(calculateOpt(new int[]{20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60}, remaining,counter,interval,maxTime), HowManySongYouCanPlay.getOpt(new int[]{20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60}, remaining,counter,interval,maxTime));assertEquals(calculateOpt(new int[]{53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4}, remaining,counter,interval,maxTime), HowManySongYouCanPlay.getOpt(new int[]{53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4}, remaining,counter,interval,maxTime));assertEquals(calculateOpt(new int[]{240, 300, 180, 360, 120, 240, 300, 240, 180, 240}, remaining,counter,interval,maxTime), HowManySongYouCanPlay.getOpt(new int[]{240, 300, 180, 360, 120, 240, 300, 240, 180, 240}, remaining,counter,interval,maxTime));assertEquals(calculateOpt(new int[]{420, 180, 360, 240, 120, 240, 300, 180}, remaining,counter,interval,maxTime), HowManySongYouCanPlay.getOpt(new int[]{420, 180, 360, 240, 120, 240, 300, 180}, remaining,counter,interval,maxTime));assertEquals(calculateOpt(new int[]{300, 240, 480, 180, 240, 240, 300, 360}, remaining,counter,interval,maxTime), HowManySongYouCanPlay.getOpt(new int[]{300, 240, 480, 180, 240, 240, 300, 360}, remaining,counter,interval,maxTime));assertEquals(calculateOpt(new int[]{240, 180, 360, 240, 300, 240, 240, 240, 240, 120}, remaining,counter,interval,maxTime), HowManySongYouCanPlay.getOpt(new int[]{240, 180, 360, 240, 300, 240, 240, 240, 240, 120}, remaining,counter,interval,maxTime));assertEquals(calculateOpt(new int[]{240, 180, 360, 240, 300, 240, 240, 240, 240, 240}, remaining,counter,interval,maxTime), HowManySongYouCanPlay.getOpt(new int[]{240, 180, 360, 240, 300, 240, 240, 240, 240, 240}, remaining,counter,interval,maxTime));- assertEquals(
- calculateOpt(new int[] { 20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60 }, remaining,
- counter, interval, maxTime),
- HowManySongYouCanPlay.getOpt(
- new int[] { 20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60 }, remaining, counter,
- interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4 }, remaining, counter,
- interval, maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4 },
- remaining, counter, interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 240, 300, 180, 360, 120, 240, 300, 240, 180, 240 }, remaining, counter,
- interval, maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 240, 300, 180, 360, 120, 240, 300, 240, 180, 240 }, remaining,
- counter, interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180 }, remaining, counter, interval,
- maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180 }, remaining, counter,
- interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter, interval,
- maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter,
- interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 120 }, remaining, counter,
- interval, maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 120 }, remaining,
- counter, interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 240 }, remaining, counter,
- interval, maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 240, 180, 360, 240, 300, 240, 240, 240, 240, 240 }, remaining,
- counter, interval, maxTime));
- }
- @Test
@Tag("SampleTest")- @DisplayName("Sample Test")
- void seenTest() {
//los dos primeros funcionan, el ultimo noassertEquals(calculateOpt(new int[]{240, 300, 180, 360, 120, 240, 300}, remaining,counter,interval,maxTime), HowManySongYouCanPlay.getOpt(new int[]{240, 300, 180, 360, 120, 240, 300}, remaining,counter,interval,maxTime));assertEquals(calculateOpt(new int[]{420, 180, 360, 240, 120, 240, 300, 180, 400, 350}, remaining,counter,interval,maxTime), HowManySongYouCanPlay.getOpt(new int[]{420, 180, 360, 240, 120, 240, 300, 180, 400, 350}, remaining,counter,interval,maxTime));assertEquals(calculateOpt(new int[]{300, 240, 480, 180, 240, 240, 300, 360}, remaining,counter,interval,maxTime), HowManySongYouCanPlay.getOpt(new int[]{300, 240, 480, 180, 240, 240, 300, 360}, remaining,counter,interval,maxTime));- assertEquals(
- calculateOpt(new int[] { 240, 300, 180, 360, 120, 240, 300 }, remaining, counter, interval, maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 240, 300, 180, 360, 120, 240, 300 }, remaining, counter,
- interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180, 400, 350 }, remaining, counter,
- interval, maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 420, 180, 360, 240, 120, 240, 300, 180, 400, 350 }, remaining,
- counter, interval, maxTime));
- assertEquals(
- calculateOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter, interval,
- maxTime),
- HowManySongYouCanPlay.getOpt(new int[] { 300, 240, 480, 180, 240, 240, 300, 360 }, remaining, counter,
- interval, maxTime));
- }
- @Test
- @DisplayName("Random Test")
- void RandomTest() {
- for (int i = 0; i < 4; i++) {
- int[] randomListOne = randomList(new Random().nextInt(45));
- assertEquals(calculateOpt(randomListOne, remaining, counter, interval, maxTime),
- HowManySongYouCanPlay.getOpt(randomListOne, remaining, counter, interval, maxTime));
- }
- }
- public int[] randomList(int n) {
- ArrayList<Integer> numbersList = new ArrayList<>();
- for (int i = 0; i < n; i++) {
- int numb = new Random().nextInt(1000);
- numbersList.add(numb);
- }
- return numbersList.stream().mapToInt(i -> i).toArray();
- }
- }
package ej2.func; public class HowManySongYouCanPlay { public static int calc() { opt = 0; playSongs(new int[]{420, 180, 360, 240, 120, 240, 300, 180, 400, 350}, 3600, 0, 60, 3600); return opt; } static int opt = 0; public static void playSongs(int[] songs, int remaining, int count, int interval, int maxTime) { if (count == songs.length || remaining == 0) { if (maxTime - remaining > opt) opt = maxTime - remaining; } else if (remaining > 0) { if (remaining >= songs[count] + interval) { int actualValue = remaining - songs[count]; if (remaining != maxTime) actualValue += interval; playSongs(songs, actualValue, count + 1, interval, maxTime); } playSongs(songs, remaining, count + 1, interval, maxTime); } } }
- package ej2.func;
- public class HowManySongYouCanPlay {
public static void main(String[] args) {calcular();System.out.println(optimo);}public static int calcular() {optimo = 0;- public static int calc() {
- opt = 0;
- playSongs(new int[]{420, 180, 360, 240, 120, 240, 300, 180, 400, 350}, 3600, 0, 60, 3600);
return optimo;- return opt;
- }
static int optimo = 0;- static int opt = 0;
public static void playSongs(int[] songs, int remaining, int contador, int intervalo, int maxTime) {if (contador == songs.length || remaining == 0) {if (maxTime - remaining > optimo) optimo = maxTime - remaining;- public static void playSongs(int[] songs, int remaining, int count, int interval, int maxTime) {
- if (count == 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);- if (remaining >= songs[count] + interval) {
- int actualValue = remaining - songs[count];
- if (remaining != maxTime) actualValue += interval;
- playSongs(songs, actualValue, count + 1, interval, maxTime);
- }
playSongs(songs, remaining, contador + 1, intervalo, maxTime);- playSongs(songs, remaining, count + 1, interval, maxTime);
- }
- }
- }
import ej2.func.HowManySongYouCanPlay; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; class SolutionTest { int remaining; int counter; int maxTime; int interval; static int opt; @BeforeEach void setup(){ remaining = 3600; counter = 0; maxTime = 3600; interval = 60; } public static int calc(int[] songs, int remaining, int count, int interval, int maxTime) { opt = 0; playSongs(songs,remaining,count,interval,maxTime); return opt; } public static void playSongs(int[] songs, int remaining, int count, int interval, int maxTime) { if (count == songs.length || remaining == 0) { if (maxTime - remaining > opt) opt = maxTime - remaining; } else if (remaining > 0) { if (remaining >= songs[count] + interval) { int actualValue = remaining - songs[count]; if (remaining != maxTime) actualValue += interval; playSongs(songs, actualValue, count + 1, interval, maxTime); } playSongs(songs, remaining, count + 1, interval, maxTime); } } @Test @Tag("Hidden Test") @DisplayName("Hidden Test") void testSample() { assertEquals(3, calc(new int[]{20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60}, remaining,counter,interval,maxTime)); assertEquals(0, calc(new int[]{53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4}, remaining,counter,interval,maxTime)); assertEquals(10, calc(new int[]{240, 300, 180, 360, 120, 240, 300, 240, 180, 240}, remaining,counter,interval,maxTime)); assertEquals(0, calc(new int[]{420, 180, 360, 240, 120, 240, 300, 180}, remaining,counter,interval,maxTime)); assertEquals(0, calc(new int[]{300, 240, 480, 180, 240, 240, 300, 360}, remaining,counter,interval,maxTime)); assertEquals(10, calc(new int[]{240, 180, 360, 240, 300, 240, 240, 240, 240, 120}, remaining,counter,interval,maxTime)); assertEquals(10, calc(new int[]{240, 180, 360, 240, 300, 240, 240, 240, 240, 240}, remaining,counter,interval,maxTime)); } @Test @Tag("SampleTest") @DisplayName("Sample Test") void seenTest() { assertEquals(1380, calc(new int[]{240, 300, 180, 360, 120, 240, 300}, remaining,counter,interval,maxTime)); assertEquals(2250, calc(new int[]{420, 180, 360, 240, 120, 240, 300, 180, 400, 350}, remaining,counter,interval,maxTime)); assertEquals(1920, calc(new int[]{300, 240, 480, 180, 240, 240, 300, 360}, remaining,counter,interval,maxTime)); } }
- import ej2.func.HowManySongYouCanPlay;
- import org.junit.jupiter.api.BeforeEach;
- import org.junit.jupiter.api.DisplayName;
- import org.junit.jupiter.api.Tag;
- import org.junit.jupiter.api.Test;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- class SolutionTest {
- int remaining;
- int counter;
- int maxTime;
- int interval;
static int optimo;- static int opt;
- @BeforeEach
- void setup(){
- remaining = 3600;
- counter = 0;
- maxTime = 3600;
- interval = 60;
- }
public static int calcular(int[] songs, int remaining, int contador, int intervalo, int maxTime) {optimo = 0;playSongs(songs,remaining,contador,intervalo,maxTime);return optimo;- public static int calc(int[] songs, int remaining, int count, int interval, int maxTime) {
- opt = 0;
- playSongs(songs,remaining,count,interval,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 > optimo) optimo = maxTime - remaining;- public static void playSongs(int[] songs, int remaining, int count, int interval, int maxTime) {
- if (count == 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);- if (remaining >= songs[count] + interval) {
- int actualValue = remaining - songs[count];
- if (remaining != maxTime) actualValue += interval;
- playSongs(songs, actualValue, count + 1, interval, maxTime);
- }
playSongs(songs, remaining, contador + 1, intervalo, maxTime);- playSongs(songs, remaining, count + 1, interval, maxTime);
- }
- }
- @Test
- @Tag("Hidden Test")
- @DisplayName("Hidden Test")
- void testSample() {
assertEquals(3, calcular(new int[]{20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60}, remaining,counter,interval,maxTime));- assertEquals(3, calc(new int[]{20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60}, remaining,counter,interval,maxTime));
assertEquals(0, calcular(new int[]{53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4}, remaining,counter,interval,maxTime));- assertEquals(0, calc(new int[]{53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4}, remaining,counter,interval,maxTime));
assertEquals(10, calcular(new int[]{240, 300, 180, 360, 120, 240, 300, 240, 180, 240}, remaining,counter,interval,maxTime));- assertEquals(10, calc(new int[]{240, 300, 180, 360, 120, 240, 300, 240, 180, 240}, remaining,counter,interval,maxTime));
assertEquals(0, calcular(new int[]{420, 180, 360, 240, 120, 240, 300, 180}, remaining,counter,interval,maxTime));- assertEquals(0, calc(new int[]{420, 180, 360, 240, 120, 240, 300, 180}, remaining,counter,interval,maxTime));
assertEquals(0, calcular(new int[]{300, 240, 480, 180, 240, 240, 300, 360}, remaining,counter,interval,maxTime));- assertEquals(0, calc(new int[]{300, 240, 480, 180, 240, 240, 300, 360}, remaining,counter,interval,maxTime));
assertEquals(10, calcular(new int[]{240, 180, 360, 240, 300, 240, 240, 240, 240, 120}, remaining,counter,interval,maxTime));- assertEquals(10, calc(new int[]{240, 180, 360, 240, 300, 240, 240, 240, 240, 120}, remaining,counter,interval,maxTime));
assertEquals(10, calcular(new int[]{240, 180, 360, 240, 300, 240, 240, 240, 240, 240}, remaining,counter,interval,maxTime));- assertEquals(10, calc(new int[]{240, 180, 360, 240, 300, 240, 240, 240, 240, 240}, remaining,counter,interval,maxTime));
- }
- @Test
- @Tag("SampleTest")
- @DisplayName("Sample Test")
- void seenTest() {
assertEquals(1380, calcular(new int[]{240, 300, 180, 360, 120, 240, 300}, remaining,counter,interval,maxTime));assertEquals(2250, calcular(new int[]{420, 180, 360, 240, 120, 240, 300, 180, 400, 350}, remaining,counter,interval,maxTime));assertEquals(1920, calcular(new int[]{300, 240, 480, 180, 240, 240, 300, 360}, remaining,counter,interval,maxTime));- assertEquals(1380, calc(new int[]{240, 300, 180, 360, 120, 240, 300}, remaining,counter,interval,maxTime));
- assertEquals(2250, calc(new int[]{420, 180, 360, 240, 120, 240, 300, 180, 400, 350}, remaining,counter,interval,maxTime));
- assertEquals(1920, calc(new int[]{300, 240, 480, 180, 240, 240, 300, 360}, remaining,counter,interval,maxTime));
- }
- }
import java.util.ArrayList; public class HowManySongYouCanPlay { public static void main(String[] args) { ArrayList<Integer> songList = new ArrayList<>(); ArrayList<Integer> songTestedList = new ArrayList<>(); System.out.println(playSongs(new Integer[] { 30, 50, 32, 22, 1, 22, 21 }, songList, songTestedList, false)); } public static int playSongs(Integer[] songDuration, ArrayList<Integer> songList, ArrayList<Integer> songTestedList, boolean status) { int maxTime = 60; if (status) { int optimalDuration = sumDuration(songList); int currentDuration = sumDuration(songTestedList); if (currentDuration > optimalDuration) { ArrayList<Integer> aux = songTestedList; songList.clear(); for (Integer element : aux) { if (element != null) { songList.add(element); } } } } else { for (int i = 0; i < songDuration.length; i++) { if (!songTestedList.contains(songDuration[i])) { if (maxTime > sumDuration(songTestedList) + songDuration[i]) { songTestedList.add(songDuration[i]); playSongs(songDuration, songList, songTestedList, false); songTestedList.remove(songDuration[i]); } else { playSongs(songDuration, songList, songTestedList, true); } } } } return sumDuration(songList); } public static int sumDuration(ArrayList<Integer> songList) { int res = 0; for (Integer songDuration : songList) { res += songDuration; } return res; } }
- import java.util.ArrayList;
import java.util.Arrays;import java.util.List;- public class HowManySongYouCanPlay {
- public static void main(String[] args) {
- ArrayList<Integer> songList = new ArrayList<>();
- ArrayList<Integer> songTestedList = new ArrayList<>();
- System.out.println(playSongs(new Integer[] { 30, 50, 32, 22, 1, 22, 21 }, songList, songTestedList, false));
- }
public static int playSongs(int[] songs) {return songs.length;}- public static int playSongs(Integer[] songDuration, ArrayList<Integer> songList, ArrayList<Integer> songTestedList,
- boolean status) {
- int maxTime = 60;
- if (status) {
- int optimalDuration = sumDuration(songList);
- int currentDuration = sumDuration(songTestedList);
- if (currentDuration > optimalDuration) {
- ArrayList<Integer> aux = songTestedList;
- songList.clear();
- for (Integer element : aux) {
- if (element != null) {
- songList.add(element);
- }
- }
- }
- } else {
- for (int i = 0; i < songDuration.length; i++) {
- if (!songTestedList.contains(songDuration[i])) {
- if (maxTime > sumDuration(songTestedList) + songDuration[i]) {
- songTestedList.add(songDuration[i]);
- playSongs(songDuration, songList, songTestedList, false);
- songTestedList.remove(songDuration[i]);
- } else {
- playSongs(songDuration, songList, songTestedList, true);
- }
- }
- }
- }
- return sumDuration(songList);
- }
- public static int sumDuration(ArrayList<Integer> songList) {
- int res = 0;
- for (Integer songDuration : songList) {
- res += songDuration;
- }
- return res;
- }
- }
//Description here
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");
}
}