Untitled
class Pair{ int x; int y; Pair(int a, int b){ x = a; y = b; } public String toString(){ return " <<<< x -> " + x + " ||| y -> " + y + ">>>> \n"; } } class Solution { public int bestTeamScore(int[] scores, int[] ages) { int n = scores.length; List<Pair> ls = new ArrayList<>(); for(int i = 0; i < n; i++){ ls.add(new Pair(scores[i], ages[i])); } Collections.sort(ls, new Comp()); //System.out.println(ls); int ans = getAns(-1, Integer.MIN_VALUE, ls, 0); return ans; } int getAns(int prevAge, int prevScore, List<Pair> ls, int i){ //System.out.println("input for i "+i +" minAge-> "+ minAge +" maxSocore-> "+maxSocore); if(i == ls.size()) return 0; int take = Integer.MIN_VALUE; if(prevScore < ls.get(i).x || (prevAge == ls.get(i).y)) take = ls.get(i).x + getAns(ls.get(i).y, Math.max(prevScore, ls.get(i).x), ls, i + 1); int leave = getAns(prevAge, prevScore, ls, i + 1); return Math.max(take, leave); } } class Comp implements Comparator<Pair>{ public int compare(Pair a, Pair b){ if(a.y > b.y){ return 1; } else if(a.y < b.y){ return -1; } else{ if(a.x < b.y){ return 1; } else if(a.x > b.y){ return 1; } else{ return 0; } } } }
Leave a Comment