Skip to content

Commit 2a36c3c

Browse files
committed
Time: 53 ms (36.58%), Space: 67.9 MB (5.17%) - LeetHub
1 parent f8b21fe commit 2a36c3c

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

1235-maximum-profit-in-job-scheduling/1235-maximum-profit-in-job-scheduling.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,7 @@ public int jobScheduling(int[] startTime, int[] endTime, int[] profit) {
1313
map =new HashMap<>();
1414
return solveMemoOptimised(0,triple,n);
1515
}
16-
public static int solveMemoOptimised(int indx,List<Triple> tp,int n){
17-
//Base Case::
18-
if(indx == n) return 0;
19-
if(map.containsKey(indx)){
20-
return map.get(indx);
21-
}
22-
// Choices we have ::: pick or not pick
23-
24-
int next =getNextIndx(tp,indx,n);
25-
int pick =tp.get(indx).profit+solveMemoOptimised(next,tp,n);
26-
27-
int notPick =solveMemoOptimised(indx+1,tp,n);
28-
map.put(indx,Math.max(pick,notPick));
29-
return Math.max(pick,notPick);
30-
31-
}
32-
// Previous Approch : getting tle ::
33-
// public static int solveMemo(int indx,int lasttime,List<Triple> tp){
16+
// public static int solveMemo(int indx,int lasttime,List<Triple> tp){
3417
// //Base Case::
3518
// if(indx == tp.size()) return 0;
3619
// // Choices we have ::: pick or not pick
@@ -47,6 +30,22 @@ public static int solveMemoOptimised(int indx,List<Triple> tp,int n){
4730
// map.put(key,Math.max(pick,notPick));
4831
// return Math.max(pick,notPick);
4932
// }
33+
public static int solveMemoOptimised(int indx,List<Triple> tp,int n){
34+
//Base Case::
35+
if(indx == n) return 0;
36+
if(map.containsKey(indx)){
37+
return map.get(indx);
38+
}
39+
// Choices we have ::: pick or not pick
40+
41+
int next =getNextIndx(tp,indx,n);
42+
int pick =tp.get(indx).profit+solveMemoOptimised(next,tp,n);
43+
44+
int notPick =solveMemoOptimised(indx+1,tp,n);
45+
map.put(indx,Math.max(pick,notPick));
46+
return Math.max(pick,notPick);
47+
48+
}
5049
public static int getNextIndx(List<Triple> tp,int indx,int n){
5150
int l=indx+1;
5251
int h=n-1;

0 commit comments

Comments
 (0)