Skip to content

Commit

Permalink
Added for 15 Jan
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jan 15, 2024
1 parent bd4ad26 commit a0702a5
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
58 changes: 58 additions & 0 deletions GeeksForGeeks/15-1-24/GFG.java
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);
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/15-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n * total)
Space complexity - O(n * total)
2 changes: 2 additions & 0 deletions LeetCode/15-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n logn)
Space complexity - O(n)
28 changes: 28 additions & 0 deletions LeetCode/15-1-24/Solution.java
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;
}
}

0 comments on commit a0702a5

Please sign in to comment.