Skip to content

Commit

Permalink
Added codes for 24 May
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed May 24, 2024
1 parent c74906f commit aa5e9c2
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 0 deletions.
85 changes: 85 additions & 0 deletions GeeksForGeeks/May/24-5-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//{ Driver Code Starts
import java.io.*;
import java.util.*;


class IntArray
{
public static int[] input(BufferedReader br, int n) throws IOException
{
String[] s = br.readLine().trim().split(" ");
int[] a = new int[n];
for(int i = 0; i < n; i++)
a[i] = Integer.parseInt(s[i]);

return a;
}

public static void print(int[] a)
{
for(int e : a)
System.out.print(e + " ");
System.out.println();
}

public static void print(ArrayList<Integer> a)
{
for(int e : a)
System.out.print(e + " ");
System.out.println();
}
}

class GFG {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t;
t = Integer.parseInt(br.readLine());
while(t-- > 0){

int n;
n = Integer.parseInt(br.readLine());


int d;
d = Integer.parseInt(br.readLine());


int[] arr = IntArray.input(br, n);

Solution obj = new Solution();
int res = obj.countPartitions(n, d, arr);

System.out.println(res);

}
}
}

// } Driver Code Ends



class Solution
{
public static int countPartitions(int n, int d, int[] arr)
{
// code here
int totalSum = 0;
for (int num : arr)
totalSum += num;

if ((totalSum + d) % 2 != 0 || totalSum < d)
return 0;

int target = (totalSum + d) / 2;
int[] dp = new int[target + 1];
dp[0] = 1;

for (int num : arr)
for (int i = target; i >= num; i--)
dp[i] = (dp[i] + dp[i - num]) % 1000000007;

return dp[target];
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/May/24-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n*sum(arr))
Space complexity - O(sum(arr))
2 changes: 2 additions & 0 deletions LeetCode/May/24-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(|letters| + 2^|words|)
Space complexity - O(|letters| + 2^|words|)
47 changes: 47 additions & 0 deletions LeetCode/May/24-5-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class Solution
{
public int maxScoreWords(String[] words, char[] letters, int[] score)
{
int[] count = new int[26];

for (char c : letters)
++count[c - 'a'];

return dfs(words, 0, count, score);
}

private int dfs(String[] words, int s, int[] count, int[] score)
{
int ans = 0;
for (int i = s; i < words.length; ++i)
{
int earned = useWord(words, i, count, score);

if (earned > 0)
ans = Math.max(ans, earned + dfs(words, i + 1, count, score));

unuseWord(words, i, count);
}
return ans;
}

private int useWord(String[] words, int i, int[] count, int[] score)
{
boolean isValid = true;
int earned = 0;
for (char c : words[i].toCharArray())
{
if (--count[c - 'a'] < 0)
isValid = false;

earned += score[c - 'a'];
}
return isValid ? earned : -1;
}

private void unuseWord(String[] words, int i, int[] count)
{
for (char c : words[i].toCharArray())
++count[c - 'a'];
}
}

0 comments on commit aa5e9c2

Please sign in to comment.