-
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
345281e
commit 137f9fb
Showing
8 changed files
with
165 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,52 @@ | ||
//{ Driver Code Starts | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
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 x; | ||
x = Integer.parseInt(br.readLine()); | ||
|
||
int y; | ||
y = Integer.parseInt(br.readLine()); | ||
|
||
Solution obj = new Solution(); | ||
int res = obj.findWinner(n, x, y); | ||
|
||
System.out.println(res); | ||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
|
||
class Solution { | ||
public static int findWinner(int n, int x, int y) { | ||
// code here | ||
int []dp= new int[n+1]; | ||
dp[1]=1; | ||
for(int i=2;i<dp.length;i++) | ||
{ | ||
if(dp[i-1]==0) | ||
dp[i]=1; | ||
else if(i-x>=0 && dp[i-x]==0) | ||
dp[i]=1; | ||
else if(i-y>=0 && dp[i-y]==0) | ||
dp[i]=1; | ||
else | ||
dp[i]=0; | ||
} | ||
|
||
return dp[n]==1?1:0; | ||
} | ||
} |
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) | ||
Space complexity - O(n) |
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,58 @@ | ||
//{ Driver Code Starts | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
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) { | ||
|
||
String s1; | ||
s1 = br.readLine(); | ||
|
||
String s2; | ||
s2 = br.readLine(); | ||
|
||
Solution obj = new Solution(); | ||
int res = obj.countWays(s1, s2); | ||
|
||
System.out.println(res); | ||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
|
||
class Solution { | ||
public static int countWays(String s1, String s2) { | ||
// code here | ||
int MOD = 1000000007; | ||
int n = s1.length(); | ||
int m = s2.length(); | ||
int[][] dp = new int[n + 1][m + 1]; | ||
|
||
for (int i = 0; i <= n; i++) | ||
dp[i][0] = 1; | ||
|
||
for (int i = 1; i <= n; i++) | ||
{ | ||
for (int j = 1; j <= m; j++) | ||
{ | ||
if (s1.charAt(i - 1) == s2.charAt(j - 1)) | ||
{ | ||
dp[i][j] = (dp[i - 1][j - 1] + dp[i - 1][j]) % MOD; | ||
} | ||
else | ||
{ | ||
dp[i][j] = dp[i - 1][j] % MOD; | ||
} | ||
} | ||
} | ||
|
||
return dp[n][m]; | ||
} | ||
} |
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*m) | ||
Space complexity - O(n*m) |
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) | ||
Space complexity - O(n) |
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,27 @@ | ||
class Solution | ||
{ | ||
public int numSteps(String s) | ||
{ | ||
int ans = 0; | ||
StringBuilder sb = new StringBuilder(s); | ||
|
||
while (sb.charAt(sb.length() - 1) == '0') | ||
{ | ||
sb.deleteCharAt(sb.length() - 1); | ||
++ans; | ||
} | ||
|
||
if (sb.toString().equals("1")) | ||
return ans; | ||
|
||
// `s` is now odd, so add 1 to `s` and cost 1 step. | ||
++ans; | ||
|
||
// All the 1s will become 0s and can be popped by 1 step. | ||
// All the 0s will become 1s and can be popped by 2 steps (adding 1 then dividing by 2). | ||
for (char c : sb.toString().toCharArray()) | ||
ans += c == '1' ? 1 : 2; | ||
|
||
return ans; | ||
} | ||
} |
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^2) | ||
Space complexity - O(1) |
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,20 @@ | ||
class Solution | ||
{ | ||
public int countTriplets(int[] arr) | ||
{ | ||
int count = 0; | ||
|
||
for(int i = 0; i < arr.length; i++) | ||
{ | ||
int xor = 0; | ||
for(int j = i; j < arr.length; j++) | ||
{ | ||
xor ^= arr[j]; | ||
if(xor == 0) | ||
count += (j - i); | ||
} | ||
} | ||
|
||
return count; | ||
} | ||
} |