-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd4ad26
commit a0702a5
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
//{ Driver Code Starts | ||
import java.util.*; | ||
import java.io.*; | ||
|
||
public class GFG { | ||
public static void main(String[] args) { | ||
Scanner scanner = new Scanner(System.in); | ||
int t = scanner.nextInt(); | ||
while(t-- > 0) | ||
{ | ||
int n = scanner.nextInt(); | ||
int total = scanner.nextInt(); | ||
int[] cost = new int[n]; | ||
for (int i = 0; i < n; i++) { | ||
cost[i] = scanner.nextInt(); | ||
} | ||
Solution solution = new Solution(); | ||
int result = solution.max_courses(n, total, cost); | ||
System.out.println(result); | ||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
//User function Template for Java | ||
class Solution { | ||
|
||
public int solve(int ind , int n , int total, int cost[] , int dp[][]) | ||
{ | ||
if(total <= 0) | ||
return 0; | ||
|
||
if(ind == n) | ||
return 0; | ||
|
||
if(dp[ind][total] != -1) | ||
return dp[ind][total]; | ||
|
||
int not_pick = solve(ind + 1 , n , total , cost , dp); | ||
int pick = 0; | ||
|
||
if(cost[ind] <= total) | ||
pick = 1 + solve(ind +1 , n , total-cost[ind] + (int)(0.9 * cost[ind]) , cost , dp); | ||
|
||
return dp[ind][total] = Math.max(pick , not_pick); | ||
} | ||
|
||
public int max_courses(int n, int total, int[] cost) | ||
{ | ||
int dp[][] = new int [n][total+1]; | ||
for(int arr[] : dp) | ||
Arrays.fill(arr , -1); | ||
|
||
return solve( 0 , n , total , cost , dp); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n * total) | ||
Space complexity - O(n * total) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n logn) | ||
Space complexity - O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Solution | ||
{ | ||
public List<List<Integer>> findWinners(int[][] matches) | ||
{ | ||
List<List<Integer>> ans = Arrays.asList(new ArrayList<>(), new ArrayList<>()); | ||
Map<Integer, Integer> lossesCount = new TreeMap<>(); | ||
|
||
for (int[] m : matches) | ||
{ | ||
int winner = m[0]; | ||
int loser = m[1]; | ||
|
||
if (!lossesCount.containsKey(winner)) | ||
lossesCount.put(winner, 0); | ||
|
||
lossesCount.merge(loser, 1, Integer::sum); | ||
} | ||
|
||
for (final int player : lossesCount.keySet()) | ||
{ | ||
int nLosses = lossesCount.get(player); | ||
if (nLosses < 2) | ||
ans.get(nLosses).add(player); | ||
} | ||
|
||
return ans; | ||
} | ||
} |