Skip to content

Commit

Permalink
Added codes for 4 May
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed May 4, 2024
1 parent 0b2ef58 commit 1057f80
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
102 changes: 102 additions & 0 deletions GeeksForGeeks/May/04-5-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//{ Driver Code Starts
import java.util.*;

class Node {
int data;
Node left;
Node right;

Node(int value) {
data = value;
left = null;
right = null;
}
}

class InorderPostorderToTree {
public void preOrder(Node root) {
if (root == null) return;

System.out.print(root.data + " ");
preOrder(root.left);

preOrder(root.right);
}

public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
InorderPostorderToTree ip = new InorderPostorderToTree();
int T = sc.nextInt();
while (T > 0) {
int n = sc.nextInt();
int inorder[] = new int[n];
int postorder[] = new int[n];
for (int i = 0; i < n; i++) inorder[i] = sc.nextInt();
for (int i = 0; i < n; i++) postorder[i] = sc.nextInt();
GfG g = new GfG();
Node root = g.buildTree(inorder, postorder, n);
ip.preOrder(root);
System.out.println();

T--;
}
}
}

// } Driver Code Ends


/* Tree node structure
class Node
{
int data;
Node left;
Node right;
Node(int value)
{
data = value;
left = null;
right = null;
}
}*/


class GfG
{
private static int findPos(int[] in, int inStart, int inEnd, int elem)
{
for (int i = inStart; i <= inEnd; i++)
{
if (in[i] == elem)
{
return i;
}
}
return -1;
}

private static Node solve(int[] in, int[] post, int[] postIdx, int inStart, int inEnd, int n)
{
if (postIdx[0] < 0 || inStart > inEnd)
{
return null;
}

int elem = post[postIdx[0]--];
Node root = new Node(elem);
int pos = findPos(in, inStart, inEnd, elem);

root.right = solve(in, post, postIdx, pos + 1, inEnd, n);
root.left = solve(in, post, postIdx, inStart, pos - 1, n);

return root;
}

// Function to return a tree created from postorder and inorder traversals.
Node buildTree(int in[], int post[], int n)
{
int[] postIdx = new int[]{n - 1};
return solve(in, post, postIdx, 0, n - 1, n);
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/May/04-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n^2)
Space complexity - O(n)
4 changes: 4 additions & 0 deletions LeetCode/May/04-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Time complexity - O(|sort|)
Space complexity - O(|sort|)

|sort| = n * logn
16 changes: 16 additions & 0 deletions LeetCode/May/04-5-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public int numRescueBoats(int[] people, int limit) {
int ans = 0;

Arrays.sort(people);

for (int i = 0, j = people.length - 1; i <= j; ++ans)
{
int remain = limit - people[j--];
if (people[i] <= remain)
++i;
}

return ans;
}
}

0 comments on commit 1057f80

Please sign in to comment.