-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35b2479
commit 49aaa38
Showing
4 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n * logn) | ||
Space complexity - O(logn) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(k*r*r) | ||
Space complexity - O(k) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |