-
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
b4e33c8
commit bd6f5f2
Showing
4 changed files
with
88 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,62 @@ | ||
//{ Driver Code Starts | ||
//Initial Template for Java | ||
|
||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
|
||
// } Driver Code Ends | ||
//User function Template for Java | ||
|
||
|
||
class Solution | ||
{ | ||
public static int ways(int n, int m) | ||
{ | ||
// complete the function | ||
int[][] dp = new int[n + 1][m + 1]; | ||
for (int i = 0; i <= n; i++) | ||
{ | ||
dp[i][0] = 1; | ||
} | ||
|
||
for (int i = 0; i <= m; i++) | ||
{ | ||
dp[0][i] = 1; | ||
} | ||
|
||
for (int i = 1; i <= n; i++) | ||
{ | ||
for (int j = 1; j <= m; j++) | ||
{ | ||
dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % 1000000007; | ||
} | ||
} | ||
return dp[n][m]; | ||
} | ||
} | ||
|
||
//{ Driver Code Starts. | ||
|
||
// Driver class | ||
class Array { | ||
|
||
// Driver code | ||
public static void main (String[] args) throws IOException{ | ||
// Taking input using buffered reader | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
int testcases = Integer.parseInt(br.readLine()); | ||
// looping through all testcases | ||
while(testcases-- > 0){ | ||
String line = br.readLine(); | ||
String[] elements = line.trim().split("\\s+"); | ||
int x=Integer.parseInt(elements[0]); | ||
int y=Integer.parseInt(elements[1]); | ||
Solution ob = new Solution(); | ||
System.out.println(ob.ways(x,y)); | ||
} | ||
} | ||
} | ||
// } Driver Code Ends |
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(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,22 @@ | ||
class Solution | ||
{ | ||
public int tribonacci(int n) | ||
{ | ||
if (n<2) | ||
return n; | ||
|
||
int prev = 0; | ||
int curr = 1; | ||
int next = 1; | ||
|
||
for (int i=3; i<=n; i++) | ||
{ | ||
int temp = next; | ||
next = next + curr + prev; | ||
prev = curr; | ||
curr = temp; | ||
} | ||
|
||
return next; | ||
} | ||
} |