-
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
c8cfbb2
commit a57d32c
Showing
4 changed files
with
131 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,111 @@ | ||
//{ Driver Code Starts | ||
//Initial Template for Java | ||
|
||
import java.util.*; | ||
class Node | ||
{ | ||
int data; | ||
Node next; | ||
Node(int d) {data = d; next = null; } | ||
} | ||
class insertion | ||
{ | ||
Node head; | ||
Node tail; | ||
public void addToTheLast(Node node) | ||
{ | ||
if (head == null) | ||
{ | ||
head = node; | ||
tail = node; | ||
} | ||
else | ||
{ | ||
tail.next = node; | ||
tail = node; | ||
} | ||
} | ||
void printList(Node head) | ||
{ | ||
Node temp = head; | ||
while (temp != null) | ||
{ | ||
System.out.print(temp.data+" "); | ||
temp = temp.next; | ||
} | ||
System.out.println(); | ||
} | ||
/* Drier program to test above functions */ | ||
public static void main(String args[]) | ||
{ | ||
Scanner sc = new Scanner(System.in); | ||
int t=sc.nextInt(); | ||
while(t>0) | ||
{ | ||
int n = sc.nextInt(); | ||
insertion llist = new insertion(); | ||
int a1=sc.nextInt(); | ||
Node head= new Node(a1); | ||
llist.addToTheLast(head); | ||
for (int i = 1; i < n; i++) | ||
{ | ||
int a = sc.nextInt(); | ||
llist.addToTheLast(new Node(a)); | ||
} | ||
|
||
Solution ob = new Solution(); | ||
head = ob.insertionSort(llist.head); | ||
llist.printList(head); | ||
|
||
t--; | ||
} | ||
}} | ||
// } Driver Code Ends | ||
|
||
|
||
//User function Template for Java | ||
|
||
/*class Node | ||
{ | ||
int data; | ||
Node next; | ||
Node(int d) {data = d; next = null; } | ||
} | ||
*/ | ||
class Solution | ||
{ | ||
static Node sorted; | ||
|
||
public static Node insertionSort(Node headref) | ||
{ | ||
sorted = null; | ||
Node current = headref; | ||
while (current != null) | ||
{ | ||
Node next = current.next; | ||
sortedInsert(current); | ||
current = next; | ||
} | ||
headref = sorted; | ||
return headref; | ||
} | ||
|
||
static void sortedInsert(Node newnode) | ||
{ | ||
if (sorted == null || sorted.data >= newnode.data) | ||
{ | ||
newnode.next = sorted; | ||
sorted = newnode; | ||
} | ||
else | ||
{ | ||
Node current = sorted; | ||
while (current.next != null && current.next.data < newnode.data) | ||
{ | ||
current = current.next; | ||
} | ||
newnode.next = current.next; | ||
current.next = newnode; | ||
} | ||
} | ||
} |
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^2) | ||
Space complexity - O(1) |
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,3 @@ | ||
Time complexity - O(len(s+t)) | ||
Space complexity - O(26) = O(1) | ||
|
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,15 @@ | ||
class Solution | ||
{ | ||
public int minSteps(String s, String t) | ||
{ | ||
int[] count = new int[26]; | ||
|
||
for (final char c : s.toCharArray()) | ||
++count[c - 'a']; | ||
|
||
for (final char c : t.toCharArray()) | ||
--count[c - 'a']; | ||
|
||
return Arrays.stream(count).map(Math::abs).sum() / 2; | ||
} | ||
} |