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