Skip to content

Commit

Permalink
Added codes for 27 April
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Apr 27, 2024
1 parent 35b2479 commit 49aaa38
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
98 changes: 98 additions & 0 deletions GeeksForGeeks/April/27-4-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//{ Driver Code Starts
import java.io.*;
import java.util.*;

class Node {
int data;
Node next, prev;

Node(int key) {
data = key;
next = prev = null;
}
}

class Driverclass {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();

while (t-- > 0) {
int n = sc.nextInt();
int a1 = sc.nextInt();
Node head = new Node(a1);
Node temp = head;

for (int i = 1; i < n; i++) {
int a = sc.nextInt();
Node n1 = new Node(a);
n1.prev = temp;
temp.next = n1;
temp = n1;
}

head = new Solution().sortDoubly(head);
printList(head);
}
}

public static void printList(Node node) {
Node temp = node;
while (node != null) {
System.out.print(node.data + " ");
temp = node;
node = node.next;
}
System.out.println();
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.prev;
}
System.out.println();
}
}

// } Driver Code Ends


// User function Template for Java

/*
class Node
{
int data;
Node next, prev;
Node(int data)
{
this.data = data;
next = prev = null;
}
}
*/
class Solution
{
// Function to sort the given doubly linked list using Merge Sort.
static Node sortDoubly(Node head)
{
// add your code here
ArrayList<Integer> arr=new ArrayList<>();
Node curr1=head;

while(curr1.next!=null)
{
arr.add(curr1.data);
curr1=curr1.next;
}

arr.add(curr1.data);
Collections.sort(arr);
Node curr=head;

for(int i=0;i<arr.size();i++)
{
curr.data=arr.get(i);
curr=curr.next;
}
return head;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/April/27-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n * logn)
Space complexity - O(logn)
2 changes: 2 additions & 0 deletions LeetCode/April/27-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(k*r*r)
Space complexity - O(k)
33 changes: 33 additions & 0 deletions LeetCode/April/27-4-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution {
public int findRotateSteps(String ring, String key)
{
Map<String, Integer> mem = new HashMap<>();
return dfs(ring, key, 0, mem) + key.length();
}

private int dfs(final String ring, final String key, int index, Map<String, Integer> mem)
{
if (index == key.length())
return 0;

String hashKey = ring + index;
if (mem.containsKey(hashKey))
return mem.get(hashKey);

int ans = Integer.MAX_VALUE;

for (int i = 0; i < ring.length(); ++i)
{
if (ring.charAt(i) == key.charAt(index))
{
int minRotates = Math.min(i, ring.length() - i);
String newRing = ring.substring(i) + ring.substring(0, i);
int remainingRotates = dfs(newRing, key, index + 1, mem);
ans = Math.min(ans, minRotates + remainingRotates);
}
}

mem.put(hashKey, ans);
return ans;
}
}

0 comments on commit 49aaa38

Please sign in to comment.