solving-questions-with-brainpower.dart (1127B)
1 //Given a list of questions where the first value is the number 2 //of points awarded for the question and the second is the brainpower 3 //return the highest number of points one could achieve assuming that if 4 //you answer a question that you will need to skip the next questions where n 5 //is the brainpower of the question. 6 //My solution is 2^n time complexity because each question can either be answered 7 //or not thus creating two paths for each question in the list. 8 //Time : TLE 9 //Memory: TLE 10 class Solution { 11 int max_points = 0; 12 List<List<int>> questions = []; 13 int mostPoints(List<List<int>> question_list) { 14 questions = question_list; 15 recurse(0 , 0); 16 return max_points; 17 } 18 void recurse(int points , int question_number){ 19 if(points > max_points){ 20 max_points = points; 21 } 22 if(questions.length <= question_number){ 23 return; 24 } 25 recurse(points, question_number + 1); 26 int point_val= questions[question_number][0]; 27 int bp = questions[question_number][1]; 28 recurse(points + point_val, question_number + bp + 1); 29 } 30 }