From 137e5725be77657c02cacabd73714667e21014fa Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Wed, 8 May 2024 07:00:18 +0530 Subject: [PATCH] Added codes for 8 May --- GeeksForGeeks/May/08-5-24/GFG.java | 198 ++++++++++++++++++++++++++++ GeeksForGeeks/May/08-5-24/README.md | 2 + LeetCode/May/08-5-24/README.md | 2 + LeetCode/May/08-5-24/Solution.java | 29 ++++ 4 files changed, 231 insertions(+) create mode 100644 GeeksForGeeks/May/08-5-24/GFG.java create mode 100644 GeeksForGeeks/May/08-5-24/README.md create mode 100644 LeetCode/May/08-5-24/README.md create mode 100644 LeetCode/May/08-5-24/Solution.java diff --git a/GeeksForGeeks/May/08-5-24/GFG.java b/GeeksForGeeks/May/08-5-24/GFG.java new file mode 100644 index 0000000..0323ae5 --- /dev/null +++ b/GeeksForGeeks/May/08-5-24/GFG.java @@ -0,0 +1,198 @@ +//{ Driver Code Starts +import java.io.*; +import java.util.*; + + +class Node +{ + int data; + Node left; + Node right; + + Node(int data) + { + this.data = data; + left = null; + right = null; + } + + public static Node buildTree(String str) + { + // Corner Case + if(str.length()==0 || str.charAt(0)=='N') + return null; + + // Creating array of Strings from input + // String after spliting by space + String ip[] = str.split(" "); + + // Create the root of the tree + Node root = new Node(Integer.parseInt(ip[0])); + + // Push the root to the queue + Queue queue = new LinkedList<>(); + queue.add(root); + + // Starting from the second element + int i = 1; + while(queue.size()>0 && i < ip.length) + { + + // Get and remove the front of the queue + Node currNode = queue.peek(); + queue.remove(); + + // Get the current node's value from the string + String currVal = ip[i]; + + // If the left child is not null + if(!currVal.equals("N")) + { + + // Create the left child for the current node + currNode.left = new Node(Integer.parseInt(currVal)); + // Push it to the queue + queue.add(currNode.left); + } + + // For the right child + i++; + if(i >= ip.length) + break; + + currVal = ip[i]; + + // If the right child is not null + if(!currVal.equals("N")) + { + + // Create the right child for the current node + currNode.right = new Node(Integer.parseInt(currVal)); + + // Push it to the queue + queue.add(currNode.right); + } + i++; + } + + return root; + } + + public static Node inputTree(BufferedReader br) throws IOException + { + return buildTree(br.readLine().trim()); + } + + public static void inorder(Node root) + { + if (root == null) + return; + inorder(root.left); + System.out.print(root.data + " "); + inorder(root.right); + } +} + + +class IntMatrix +{ + public static int[][] input(BufferedReader br, int n, int m) throws IOException + { + int[][] mat = new int[n][]; + + for(int i = 0; i < n; i++) + { + String[] s = br.readLine().trim().split(" "); + mat[i] = new int[s.length]; + for(int j = 0; j < s.length; j++) + mat[i][j] = Integer.parseInt(s[j]); + } + + return mat; + } + + public static void print(int[][] m) + { + for(var a : m) + { + for(int e : a) + System.out.print(e + " "); + System.out.println(); + } + } + + public static void print(ArrayList> m) + { + for(var a : m) + { + for(int e : a) + System.out.print(e + " "); + System.out.println(); + } + } +} + +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){ + + Node root = Node.inputTree(br); + + Solution obj = new Solution(); + ArrayList> res = obj.Paths(root); + + IntMatrix.print(res); + + } + } +} + +// } Driver Code Ends + + +/* + +Definition for Binary Tree Node +class Node +{ + int data; + Node left; + Node right; + + Node(int data) + { + this.data = data; + left = null; + right = null; + } +} +*/ + +class Solution { + private static void helper(Node root, ArrayList temp, ArrayList> ans) + { + if(root==null) + return; + + temp.add(root.data); + + if(root.left==null && root.right==null) + ans.add(new ArrayList<>(temp)); + + helper(root.left, temp, ans); + helper(root.right, temp, ans); + + temp.remove(temp.size()-1); + } + + public static ArrayList> Paths(Node root) + { + // code here + ArrayList> ans = new ArrayList<>(); + helper(root, new ArrayList<>(), ans); + return ans; + } +} diff --git a/GeeksForGeeks/May/08-5-24/README.md b/GeeksForGeeks/May/08-5-24/README.md new file mode 100644 index 0000000..722dfdc --- /dev/null +++ b/GeeksForGeeks/May/08-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(h) diff --git a/LeetCode/May/08-5-24/README.md b/LeetCode/May/08-5-24/README.md new file mode 100644 index 0000000..905c94b --- /dev/null +++ b/LeetCode/May/08-5-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n*logn) +Space complexity - O(n) diff --git a/LeetCode/May/08-5-24/Solution.java b/LeetCode/May/08-5-24/Solution.java new file mode 100644 index 0000000..3e245a2 --- /dev/null +++ b/LeetCode/May/08-5-24/Solution.java @@ -0,0 +1,29 @@ +class Solution +{ + public String[] findRelativeRanks(int[] score) + { + int n = score.length; + int[] sortedScore = score.clone(); + Arrays.sort(sortedScore); + String[] ranks = new String[n]; + + for (int i = 0; i < n; i++) + { + int rank = Arrays.binarySearch(sortedScore, score[i]); + if (rank == n - 1) { + ranks[i] = "Gold Medal"; + } + else if (rank == n - 2) { + ranks[i] = "Silver Medal"; + } + else if (rank == n - 3) { + ranks[i] = "Bronze Medal"; + } + else { + ranks[i] = String.valueOf(n - rank); + } + } + + return ranks; + } +}