diff --git a/GeeksForGeeks/May/24-5-24/GFG.java b/GeeksForGeeks/May/24-5-24/GFG.java new file mode 100644 index 0000000..02fa5b5 --- /dev/null +++ b/GeeksForGeeks/May/24-5-24/GFG.java @@ -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 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]; + } +} diff --git a/GeeksForGeeks/May/24-5-24/README.md b/GeeksForGeeks/May/24-5-24/README.md new file mode 100644 index 0000000..eec59e8 --- /dev/null +++ b/GeeksForGeeks/May/24-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n*sum(arr)) +Space complexity - O(sum(arr)) diff --git a/LeetCode/May/24-5-24/README.md b/LeetCode/May/24-5-24/README.md new file mode 100644 index 0000000..f22789a --- /dev/null +++ b/LeetCode/May/24-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(|letters| + 2^|words|) +Space complexity - O(|letters| + 2^|words|) diff --git a/LeetCode/May/24-5-24/Solution.java b/LeetCode/May/24-5-24/Solution.java new file mode 100644 index 0000000..5110e82 --- /dev/null +++ b/LeetCode/May/24-5-24/Solution.java @@ -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']; + } +}