Ad
Code
Diff
  • 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);
    • }
    • }
    • }
Code
Diff
  • 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;
    • }
    • }
    • }
Code
Diff
  • 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;
    • }
    • }
Code
Diff
  • 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;
    • }
    • }
Code
Diff
  • 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;
    • }
    • }