-
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
c74906f
commit aa5e9c2
Showing
4 changed files
with
136 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,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]; | ||
} | ||
} |
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*sum(arr)) | ||
Space complexity - O(sum(arr)) |
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(|letters| + 2^|words|) | ||
Space complexity - O(|letters| + 2^|words|) |
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,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']; | ||
} | ||
} |