Ad
Code
Diff
  • import java.util.Arrays;
    
    public class FeedTheDogs {
    	public static int[] solution(int[] food, int[] dogs) {
    		int[] position = dogs;
    		boolean first = true;
    
    		while (!checkEmpty(food)) {
    			for (int i = 0; i < position.length; i++) {
    				int j = position[i];
    				if (food[j] > 0) food[j]--;
    				else {
    					int move = moveDog(food, i, first, position);
    					if (move != position[i]) {
    						position[i] = move;
    						food[move]--;
    					}
    
    				}
    				first = false;
    
    			}
    		}
    
    		return position;
    	}
    
    	public static int closeDog(int[] position, int dog) {
    		int closestDog = Integer.MAX_VALUE;
    		int closest = Integer.MAX_VALUE;
    		for (int i : position) {
    			int dif = Math.abs(i - dog);
    			if (dif != 0 && dif < closest) {
    				closest = dif;
    				closestDog = i;
    			} else if (dif != 0 && dif == closest)
    				return dog;
    		}
    		return closestDog;
    
    	}
    
    	public static int moveDog(int[] food, int i, boolean first, int[] position) {
    		int r = position[i];
    		int l= position[i];
    		while(r < food.length -1 || l > 0) {
    			if(r < food.length-1 ) r++;
    			if(l > 0) l--;
    			if (food[r] > 0 && food[l] == 0) return r;
    			if (food[r] == 0 && food[l] > 0) return l;
    			if (food[r] > 0 && food[l] > 0) {
    				int closestDog = closeDog(position, position[i]);
    				if (closestDog < position[i])return r;
    				if (closestDog > position[i] || closestDog == Integer.MAX_VALUE) return l;
    				if (first) {
    					for (int j = 0; j < i; j++) {
    						if(position[j]>position[i])return position[i];
    					}
    					return l;
    				}
    				return position[i];
    			}
    			
    		}
    
    	return position[i];
    
    	}
    
    	public static boolean checkEmpty(int[] food) {
    		for (int f : food) {
    			if (f != 0) return false;
    		}
    		return true;
    	}
    
    }
    
    • def feedTheDogs(food,dogs):
    • return dogsPosition
    • import java.util.Arrays;
    • public class FeedTheDogs {
    • public static int[] solution(int[] food, int[] dogs) {
    • int[] position = dogs;
    • boolean first = true;
    • while (!checkEmpty(food)) {
    • for (int i = 0; i < position.length; i++) {
    • int j = position[i];
    • if (food[j] > 0) food[j]--;
    • else {
    • int move = moveDog(food, i, first, position);
    • if (move != position[i]) {
    • position[i] = move;
    • food[move]--;
    • }
    • }
    • first = false;
    • }
    • }
    • return position;
    • }
    • public static int closeDog(int[] position, int dog) {
    • int closestDog = Integer.MAX_VALUE;
    • int closest = Integer.MAX_VALUE;
    • for (int i : position) {
    • int dif = Math.abs(i - dog);
    • if (dif != 0 && dif < closest) {
    • closest = dif;
    • closestDog = i;
    • } else if (dif != 0 && dif == closest)
    • return dog;
    • }
    • return closestDog;
    • }
    • public static int moveDog(int[] food, int i, boolean first, int[] position) {
    • int r = position[i];
    • int l= position[i];
    • while(r < food.length -1 || l > 0) {
    • if(r < food.length-1 ) r++;
    • if(l > 0) l--;
    • if (food[r] > 0 && food[l] == 0) return r;
    • if (food[r] == 0 && food[l] > 0) return l;
    • if (food[r] > 0 && food[l] > 0) {
    • int closestDog = closeDog(position, position[i]);
    • if (closestDog < position[i])return r;
    • if (closestDog > position[i] || closestDog == Integer.MAX_VALUE) return l;
    • if (first) {
    • for (int j = 0; j < i; j++) {
    • if(position[j]>position[i])return position[i];
    • }
    • return l;
    • }
    • return position[i];
    • }
    • }
    • return position[i];
    • }
    • public static boolean checkEmpty(int[] food) {
    • for (int f : food) {
    • if (f != 0) return false;
    • }
    • return true;
    • }
    • }
Code
Diff
  • import java.util.ArrayList;
    import java.util.Comparator;
    import java.util.List;
    import java.util.Map;
    import java.util.TreeMap;
    
    public class TreeJava {
    
    	public static String[] solution(String str) {
    		Map<String, Integer> t = new TreeMap<>();
    
    		String[] words = str.split("[,. ]");
    		List<String> sol = new ArrayList<>();
    		int max = words[0].length();
    		for (String string : words) {
    			if (max < string.length())
    				max = string.length();
    
    		}
    		for (int i = 1; i < max; i++) {
    
    			for (String string : words) {
    				if (i <= string.length())
    					t.put(string.substring(0, i), i);
    			}
    		}
    
    		for (Map.Entry<String, Integer> entry : t.entrySet()) {
    			String key = entry.getKey();
    			Integer val = entry.getValue();
    			int cont = 0;
    			List<String> l = new ArrayList<>();
    			for (String string : words) {
    				if (string.startsWith(key)) {
    					cont++;
    					l.add(string);
    				}
    			}
    			if (cont == val)
    				sol.addAll(l);
    
    		}
        Comparator<String> compare=new Comparator<String>() {
    
    			@Override
    			public int compare(String o1, String o2) {
    				if(str.indexOf(o1)<str.indexOf(o2)) return -1;
    				if(str.indexOf(o1)>str.indexOf(o2)) return 1;
    				return 0;
    			}
    		};
    		sol.sort(compare);
        System.out.println(sol.toString());
    		return sol.toArray(new String[sol.size()]);
    	}
    
    }
    • def letras(str):
    • return ["mejorate", "hola", "ho","esto","estas","estreñido"]
    • import java.util.ArrayList;
    • import java.util.Comparator;
    • import java.util.List;
    • import java.util.Map;
    • import java.util.TreeMap;
    • public class TreeJava {
    • public static String[] solution(String str) {
    • Map<String, Integer> t = new TreeMap<>();
    • String[] words = str.split("[,. ]");
    • List<String> sol = new ArrayList<>();
    • int max = words[0].length();
    • for (String string : words) {
    • if (max < string.length())
    • max = string.length();
    • }
    • for (int i = 1; i < max; i++) {
    • for (String string : words) {
    • if (i <= string.length())
    • t.put(string.substring(0, i), i);
    • }
    • }
    • for (Map.Entry<String, Integer> entry : t.entrySet()) {
    • String key = entry.getKey();
    • Integer val = entry.getValue();
    • int cont = 0;
    • List<String> l = new ArrayList<>();
    • for (String string : words) {
    • if (string.startsWith(key)) {
    • cont++;
    • l.add(string);
    • }
    • }
    • if (cont == val)
    • sol.addAll(l);
    • }
    • Comparator<String> compare=new Comparator<String>() {
    • @Override
    • public int compare(String o1, String o2) {
    • if(str.indexOf(o1)<str.indexOf(o2)) return -1;
    • if(str.indexOf(o1)>str.indexOf(o2)) return 1;
    • return 0;
    • }
    • };
    • sol.sort(compare);
    • System.out.println(sol.toString());
    • return sol.toArray(new String[sol.size()]);
    • }
    • }