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 {
public static int calc() {opt = 0;playSongs(new int[]{420, 180, 360, 240, 120, 240, 300, 180, 400, 350}, 3600, 0, 60, 3600);- 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;
- }
static int opt = 0;public static void playSongs(int[] songs, int remaining, int count, int interval, int maxTime) {if (count == songs.length || remaining == 0) {- 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[count] + interval) {int actualValue = remaining - songs[count];if (remaining != maxTime) actualValue += interval;playSongs(songs, actualValue, count + 1, interval, maxTime);- 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, count + 1, interval, 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 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 @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)); } @Test @Tag("SampleTest") @DisplayName("Sample Test") void seenTest() { //los dos primeros funcionan, el ultimo no 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)); } }
- 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) {- public static int calculateOpt(int[] songs, int remaining, int contador, int intervalo, int maxTime) {
- opt = 0;
playSongs(songs,remaining,count,interval,maxTime);- playSongs(songs,remaining,contador,intervalo,maxTime);
- return opt;
- }
public static void playSongs(int[] songs, int remaining, int count, int interval, int maxTime) {if (count == songs.length || remaining == 0) {- 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[count] + interval) {int actualValue = remaining - songs[count];if (remaining != maxTime) actualValue += interval;playSongs(songs, actualValue, count + 1, interval, maxTime);- 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, count + 1, interval, maxTime);- playSongs(songs, remaining, contador + 1, intervalo, 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(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(0, calc(new int[]{53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4}, 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(10, calc(new int[]{240, 300, 180, 360, 120, 240, 300, 240, 180, 240}, 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(0, calc(new int[]{420, 180, 360, 240, 120, 240, 300, 180}, 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(0, calc(new int[]{300, 240, 480, 180, 240, 240, 300, 360}, 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(10, calc(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, 120}, remaining,counter,interval,maxTime), HowManySongYouCanPlay.getOpt(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));- 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() {
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));- //los dos primeros funcionan, el ultimo no
- 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));
- }
- }
package ej2.func; public class HowManySongYouCanPlay { public static void main(String[] args) { calcular(); System.out.println(optimo); } public static int calcular() { optimo = 0; playSongs(new int[]{420, 180, 360, 240, 120, 240, 300, 180, 400, 350}, 3600, 0, 60, 3600); return optimo; } static int optimo = 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; } 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 java.util.ArrayList;- package ej2.func;
- public class HowManySongYouCanPlay {
static int optimo = 0;public static int playSongs(int[] songs, int remanente, int contador, int maxTime) {if (contador == songs.length || optimo == maxTime) {optimo =maxTime-remanente;} else {if (remanente >= songs[contador]) {playSongs(songs, remanente - songs[contador],contador + 1, maxTime);} else {playSongs(songs, remanente, contador + 1, maxTime);- public static void main(String[] args) {
- calcular();
- System.out.println(optimo);
- }
- public static int calcular() {
- optimo = 0;
- playSongs(new int[]{420, 180, 360, 240, 120, 240, 300, 180, 400, 350}, 3600, 0, 60, 3600);
- return optimo;
- }
- static int optimo = 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;
- } 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);
- }
return optimo;- }
}- }
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; @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 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; } 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(3, calcular(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(10, calcular(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, calcular(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, calcular(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)); } }
- 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;
- @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 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;
- } 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(3, HowManySongYouCanPlay.playSongs(new int[]{20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60}));- assertEquals(3, calcular(new int[]{20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60}, remaining,counter,interval,maxTime));
assertEquals(0, HowManySongYouCanPlay.playSongs(new int[]{53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4}));- assertEquals(0, calcular(new int[]{53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4}, remaining,counter,interval,maxTime));
assertEquals(10, HowManySongYouCanPlay.playSongs(new int[]{240, 300, 180, 360, 120, 240, 300, 240, 180, 240}));- assertEquals(10, calcular(new int[]{240, 300, 180, 360, 120, 240, 300, 240, 180, 240}, remaining,counter,interval,maxTime));
assertEquals(0, HowManySongYouCanPlay.playSongs(new int[]{420, 180, 360, 240, 120, 240, 300, 180}));- assertEquals(0, calcular(new int[]{420, 180, 360, 240, 120, 240, 300, 180}, remaining,counter,interval,maxTime));
assertEquals(0, HowManySongYouCanPlay.playSongs(new int[]{300, 240, 480, 180, 240, 240, 300, 360}));- assertEquals(0, calcular(new int[]{300, 240, 480, 180, 240, 240, 300, 360}, remaining,counter,interval,maxTime));
assertEquals(10, HowManySongYouCanPlay.playSongs(new int[]{240, 180, 360, 240, 300, 240, 240, 240, 240, 120}));- assertEquals(10, calcular(new int[]{240, 180, 360, 240, 300, 240, 240, 240, 240, 120}, remaining,counter,interval,maxTime));
assertEquals(10, HowManySongYouCanPlay.playSongs(new int[]{240, 180, 360, 240, 300, 240, 240, 240, 240, 240}));- assertEquals(10, calcular(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(0, HowManySongYouCanPlay.playSongs(new int[]{240, 300, 180, 360, 120, 240, 300}));assertEquals(10, HowManySongYouCanPlay.playSongs(new int[]{420, 180, 360, 240, 120, 240, 300, 180, 400, 350}));assertEquals(0, HowManySongYouCanPlay.playSongs(new int[]{300, 240, 480, 180, 240, 240, 300, 360}));- 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));
- }
- }
import java.util.ArrayList; public class HowManySongYouCanPlay { static int optimo = 0; public static int playSongs(int[] songs, int remanente, int contador, int maxTime) { if (contador == songs.length || optimo == maxTime) { optimo =maxTime-remanente; } else { if (remanente >= songs[contador]) { playSongs(songs, remanente - songs[contador], contador + 1, maxTime); } else { playSongs(songs, remanente, contador + 1, maxTime); } } return optimo; } }
- import java.util.ArrayList;
- public class HowManySongYouCanPlay {
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> list){int sum = 0;for (int i = 0; i < list.size(); i++) {if (i == list.size()-1){sum += list.get(i);- static int optimo = 0;
- public static int playSongs(int[] songs, int remanente, int contador, int maxTime) {
- if (contador == songs.length || optimo == maxTime) {
- optimo =maxTime-remanente;
- } else {
- if (remanente >= songs[contador]) {
- playSongs(songs, remanente - songs[contador],
- contador + 1, maxTime);
- } else {
sum += list.get(i)+60;- playSongs(songs, remanente, contador + 1, maxTime);
- }
- }
return sum;- return optimo;
- }
- }
import java.util.ArrayList; public class HowManySongYouCanPlay { 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> list){ int sum = 0; for (int i = 0; i < list.size(); i++) { if (i == list.size()-1){ sum += list.get(i); } else { sum += list.get(i)+60; } } return sum; } }
- 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;}- public static int sumDuration(ArrayList<Integer> list){
- int sum = 0;
- for (int i = 0; i < list.size(); i++) {
- if (i == list.size()-1){
- sum += list.get(i);
- } else {
- sum += list.get(i)+60;
- }
- }
- return sum;
- }
- }
import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class HowManySongYouCanPlay { public static int playSongs(int[] songs) { return songs.length; } }
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- public class HowManySongYouCanPlay {
public static String playSongs(Integer[] songDuration) {return "";}- public static int playSongs(int[] songs) {
- return songs.length;
- }
- }
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 { @Test @Tag("Hidden Test") @DisplayName("Hidden Test") void testSample() { assertEquals(3, HowManySongYouCanPlay.playSongs(new int[]{20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60})); assertEquals(0, HowManySongYouCanPlay.playSongs(new int[]{53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4})); assertEquals(10, HowManySongYouCanPlay.playSongs(new int[]{240, 300, 180, 360, 120, 240, 300, 240, 180, 240})); assertEquals(0, HowManySongYouCanPlay.playSongs(new int[]{420, 180, 360, 240, 120, 240, 300, 180})); assertEquals(0, HowManySongYouCanPlay.playSongs(new int[]{300, 240, 480, 180, 240, 240, 300, 360})); assertEquals(10, HowManySongYouCanPlay.playSongs(new int[]{240, 180, 360, 240, 300, 240, 240, 240, 240, 120})); assertEquals(10, HowManySongYouCanPlay.playSongs(new int[]{240, 180, 360, 240, 300, 240, 240, 240, 240, 240})); } @Test @Tag("SampleTest") @DisplayName("Sample Test") void seenTest() { assertEquals(0, HowManySongYouCanPlay.playSongs(new int[]{240, 300, 180, 360, 120, 240, 300})); assertEquals(10, HowManySongYouCanPlay.playSongs(new int[]{420, 180, 360, 240, 120, 240, 300, 180, 400, 350})); assertEquals(0, HowManySongYouCanPlay.playSongs(new int[]{300, 240, 480, 180, 240, 240, 300, 360})); } }
- 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;// TODO: Replace examples and use TDD by writing your own tests- import static org.junit.jupiter.api.Assertions.assertEquals;
- class SolutionTest {
- @Test
- @Tag("Hidden Test")
- @DisplayName("Hidden Test")
- void testSample() {
- assertEquals(3, HowManySongYouCanPlay.playSongs(new int[]{20 * 60, 20 * 60, 18 * 60, 27 * 60, 61 * 60, 25 * 60, 36 * 60}));
- assertEquals(0, HowManySongYouCanPlay.playSongs(new int[]{53, 22, 54, 62, 90, 56, 72, 2, 8, 17, 36, 5, 36, 4}));
- assertEquals(10, HowManySongYouCanPlay.playSongs(new int[]{240, 300, 180, 360, 120, 240, 300, 240, 180, 240}));
- assertEquals(0, HowManySongYouCanPlay.playSongs(new int[]{420, 180, 360, 240, 120, 240, 300, 180}));
- assertEquals(0, HowManySongYouCanPlay.playSongs(new int[]{300, 240, 480, 180, 240, 240, 300, 360}));
- assertEquals(10, HowManySongYouCanPlay.playSongs(new int[]{240, 180, 360, 240, 300, 240, 240, 240, 240, 120}));
- assertEquals(10, HowManySongYouCanPlay.playSongs(new int[]{240, 180, 360, 240, 300, 240, 240, 240, 240, 240}));
- }
- @Test
void testSomething() {// assertEquals("expected", "actual");- @Tag("SampleTest")
- @DisplayName("Sample Test")
- void seenTest() {
- assertEquals(0, HowManySongYouCanPlay.playSongs(new int[]{240, 300, 180, 360, 120, 240, 300}));
- assertEquals(10, HowManySongYouCanPlay.playSongs(new int[]{420, 180, 360, 240, 120, 240, 300, 180, 400, 350}));
- assertEquals(0, HowManySongYouCanPlay.playSongs(new int[]{300, 240, 480, 180, 240, 240, 300, 360}));
- }
- }