From 9f090f1c21c9a906afaba2ffd7aacd08174e88d0 Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Tue, 16 Jan 2024 09:53:33 +0530 Subject: [PATCH] Added for 16 Jan --- GeeksForGeeks/16-1-24/GFG.java | 53 +++++++++++++++++++++++++++++++++ GeeksForGeeks/16-1-24/README.md | 2 ++ LeetCode/16-1-24/README.md | 2 ++ LeetCode/16-1-24/Solution.java | 50 +++++++++++++++++++++++++++++++ 4 files changed, 107 insertions(+) create mode 100644 GeeksForGeeks/16-1-24/GFG.java create mode 100644 GeeksForGeeks/16-1-24/README.md create mode 100644 LeetCode/16-1-24/README.md create mode 100644 LeetCode/16-1-24/Solution.java diff --git a/GeeksForGeeks/16-1-24/GFG.java b/GeeksForGeeks/16-1-24/GFG.java new file mode 100644 index 0000000..268f17e --- /dev/null +++ b/GeeksForGeeks/16-1-24/GFG.java @@ -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; + } +} diff --git a/GeeksForGeeks/16-1-24/README.md b/GeeksForGeeks/16-1-24/README.md new file mode 100644 index 0000000..d345d8d --- /dev/null +++ b/GeeksForGeeks/16-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(m*n) +Space complexity - O(m*n) diff --git a/LeetCode/16-1-24/README.md b/LeetCode/16-1-24/README.md new file mode 100644 index 0000000..9a0ef5d --- /dev/null +++ b/LeetCode/16-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(1) +Space complexity - O(n) diff --git a/LeetCode/16-1-24/Solution.java b/LeetCode/16-1-24/Solution.java new file mode 100644 index 0000000..97fa93d --- /dev/null +++ b/LeetCode/16-1-24/Solution.java @@ -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 valToIndex = new HashMap<>(); + private List vals = new ArrayList<>(); + private Random rand = new Random(); + + private int last(List 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(); + */