diff --git a/GeeksForGeeks/15-1-24/GFG.java b/GeeksForGeeks/15-1-24/GFG.java new file mode 100644 index 0000000..a48f234 --- /dev/null +++ b/GeeksForGeeks/15-1-24/GFG.java @@ -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); + } +} diff --git a/GeeksForGeeks/15-1-24/README.md b/GeeksForGeeks/15-1-24/README.md new file mode 100644 index 0000000..8771e97 --- /dev/null +++ b/GeeksForGeeks/15-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n * total) +Space complexity - O(n * total) diff --git a/LeetCode/15-1-24/README.md b/LeetCode/15-1-24/README.md new file mode 100644 index 0000000..c702b5d --- /dev/null +++ b/LeetCode/15-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n logn) +Space complexity - O(n) diff --git a/LeetCode/15-1-24/Solution.java b/LeetCode/15-1-24/Solution.java new file mode 100644 index 0000000..15441ff --- /dev/null +++ b/LeetCode/15-1-24/Solution.java @@ -0,0 +1,28 @@ +class Solution +{ + public List> findWinners(int[][] matches) + { + List> ans = Arrays.asList(new ArrayList<>(), new ArrayList<>()); + Map 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; + } +}