-
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
a0702a5
commit 9f090f1
Showing
4 changed files
with
107 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,53 @@ | ||
//{ Driver Code Starts | ||
//Initial Template for Java | ||
import java.io.*; | ||
import java.util.*; | ||
|
||
class GFG | ||
{ | ||
public static void main(String args[])throws IOException | ||
{ | ||
BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); | ||
int t = Integer.parseInt(read.readLine()); | ||
while(t-- > 0) | ||
{ | ||
String input_line[] = read.readLine().trim().split("\\s+"); | ||
int m = Integer.parseInt(input_line[0]); | ||
int n = Integer.parseInt(input_line[1]); | ||
|
||
Solution ob = new Solution(); | ||
System.out.println(ob.numberSequence(m, n)); | ||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
//User function Template for Java | ||
|
||
class Solution | ||
{ | ||
static int numberSequence(int m, int n) | ||
{ | ||
int[][] dp = new int[n + 1][m + 1]; | ||
return fun(n, 1, m, 0, dp); | ||
} | ||
|
||
static int fun(int i, int j, int m, int last, int[][] dp) | ||
{ | ||
if (i == 0) | ||
return 1; | ||
|
||
if (dp[i][j] != 0) | ||
return dp[i][j]; | ||
|
||
int ans = 0; | ||
for (int k = j; k <= m; k++) | ||
if (last == 0 || k >= 2 * last) | ||
ans += fun(i - 1, k , m, k, dp); | ||
|
||
dp[i][j] = ans; | ||
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(m*n) | ||
Space complexity - O(m*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,2 @@ | ||
Time complexity - O(1) | ||
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,50 @@ | ||
class RandomizedSet | ||
{ | ||
public boolean insert(int val) | ||
{ | ||
if (valToIndex.containsKey(val)) | ||
return false; | ||
|
||
valToIndex.put(val, vals.size()); | ||
vals.add(val); | ||
return true; | ||
} | ||
|
||
public boolean remove(int val) | ||
{ | ||
if (!valToIndex.containsKey(val)) | ||
return false; | ||
|
||
final int index = valToIndex.get(val); | ||
// The order of the following two lines is important when vals.size() == 1. | ||
valToIndex.put(last(vals), index); | ||
valToIndex.remove(val); | ||
vals.set(index, last(vals)); | ||
vals.remove(vals.size() - 1); | ||
return true; | ||
} | ||
|
||
public int getRandom() | ||
{ | ||
final int index = rand.nextInt(vals.size()); | ||
return vals.get(index); | ||
} | ||
|
||
// {val: index in vals} | ||
private Map<Integer, Integer> valToIndex = new HashMap<>(); | ||
private List<Integer> vals = new ArrayList<>(); | ||
private Random rand = new Random(); | ||
|
||
private int last(List<Integer> vals) | ||
{ | ||
return vals.get(vals.size() - 1); | ||
} | ||
} | ||
|
||
/** | ||
* Your RandomizedSet object will be instantiated and called as such: | ||
* RandomizedSet obj = new RandomizedSet(); | ||
* boolean param_1 = obj.insert(val); | ||
* boolean param_2 = obj.remove(val); | ||
* int param_3 = obj.getRandom(); | ||
*/ |