solving-questions-with-brainpowerV2.dart (1175B)
1 //This is a better versioni of the previous algorithm 2 //that uses memoization so if a question has been answered 3 //before and the points that the sequence that one used is higher 4 //then the current one will stop. (I.e. memoization). 5 6 7 class Solution { 8 int max_points = 0; 9 List<List<int>> questions = []; 10 List<int> question_max_score = []; 11 int mostPoints(List<List<int>> question_list) { 12 for(int i = 0 ; i < question_list.length ; ++i){ 13 question_max_score.add(0); 14 } 15 questions = question_list; 16 recurse(0 , 0); 17 return max_points; 18 } 19 void recurse(int points , int question_number){ 20 21 if(points > max_points){ 22 max_points = points; 23 } 24 if(questions.length <= question_number){ 25 return; 26 } 27 if(question_max_score[question_number] > points){ 28 return; 29 } 30 else{ 31 question_max_score[question_number] = points; 32 } 33 int point_val= questions[question_number][0]; 34 int bp = questions[question_number][1]; 35 recurse(points, question_number + 1); 36 recurse(points + point_val, question_number + bp + 1); 37 } 38 }