Skip to content

Commit

Permalink
Added for 16 Jan
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jan 16, 2024
1 parent a0702a5 commit 9f090f1
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
53 changes: 53 additions & 0 deletions GeeksForGeeks/16-1-24/GFG.java
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;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/16-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(m*n)
Space complexity - O(m*n)
2 changes: 2 additions & 0 deletions LeetCode/16-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(1)
Space complexity - O(n)
50 changes: 50 additions & 0 deletions LeetCode/16-1-24/Solution.java
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();
*/

0 comments on commit 9f090f1

Please sign in to comment.