Skip to content

Commit 25c5875

Browse files
committed
🚀 29-Jun-2020
1 parent 83c101a commit 25c5875

10 files changed

+580
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus, the itinerary must begin with JFK.
2+
3+
Note:
4+
5+
If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string. For example, the itinerary ["JFK", "LGA"] has a smaller lexical order than ["JFK", "LGB"].
6+
All airports are represented by three capital letters (IATA code).
7+
You may assume all tickets form at least one valid itinerary.
8+
One must use all the tickets once and only once.
9+
Example 1:
10+
11+
Input: [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]]
12+
Output: ["JFK", "MUC", "LHR", "SFO", "SJC"]
13+
Example 2:
14+
15+
Input: [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
16+
Output: ["JFK","ATL","JFK","SFO","ATL","SFO"]
17+
Explanation: Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"].
18+
But it is larger in lexical order.
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
class Solution {
30+
public:
31+
vector<string> findItinerary(vector<vector<string>>& tickets) {
32+
unordered_map<string, multiset<string> > adj;
33+
vector<string> res;
34+
int n=tickets.size();
35+
for(int i=0;i<n;i++){
36+
adj[tickets[i][0]].insert(tickets[i][1]);
37+
}
38+
stack<string> s;
39+
s.push("JFK");
40+
41+
while(!s.empty()){
42+
string src=s.top();
43+
if(adj[src].size()==0){ // no further travel possible
44+
res.push_back(src);
45+
s.pop();
46+
} else {
47+
auto dst=adj[src].begin();
48+
s.push(*dst);
49+
adj[src].erase(dst);
50+
}
51+
}
52+
53+
reverse(res.begin(), res.end());
54+
55+
return res;
56+
}
57+
};
58+
59+
60+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs from JFK. Thus, the itinerary must begin with JFK.
2+
3+
Note:
4+
5+
If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string. For example, the itinerary ["JFK", "LGA"] has a smaller lexical order than ["JFK", "LGB"].
6+
All airports are represented by three capital letters (IATA code).
7+
You may assume all tickets form at least one valid itinerary.
8+
One must use all the tickets once and only once.
9+
Example 1:
10+
11+
Input: [["MUC", "LHR"], ["JFK", "MUC"], ["SFO", "SJC"], ["LHR", "SFO"]]
12+
Output: ["JFK", "MUC", "LHR", "SFO", "SJC"]
13+
Example 2:
14+
15+
Input: [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
16+
Output: ["JFK","ATL","JFK","SFO","ATL","SFO"]
17+
Explanation: Another possible reconstruction is ["JFK","SFO","ATL","JFK","ATL","SFO"].
18+
But it is larger in lexical order.
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
class Solution {
30+
public:
31+
vector<string> findItinerary(vector<vector<string>>& tickets) {
32+
unordered_map<string, multiset<string> > adj;
33+
vector<string> res;
34+
int n=tickets.size();
35+
for(int i=0;i<n;i++){
36+
adj[tickets[i][0]].insert(tickets[i][1]);
37+
}
38+
stack<string> s;
39+
s.push("JFK");
40+
41+
while(!s.empty()){
42+
string src=s.top();
43+
if(adj[src].size()==0){ // no further travel possible
44+
res.push_back(srac);
45+
s.pop();
46+
} else {
47+
auto dst=adj[src].begin();
48+
s.push(*dst);
49+
adj[src].erase(dst);
50+
}
51+
}
52+
53+
reverse(res.begin(), res.end());
54+
55+
return res;
56+
}
57+
};

linked list/merge_two_sorted_list.cpp

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list (in-place) and return head of the merged list.
3+
Note: It is strongly recommended to do merging in-place using O(1) extra space.
4+
5+
Input:
6+
First line of input contains number of testcases T. For each testcase, first line of input contains N and M, and next two line contains N and M sorted elements in two lines for each.
7+
8+
Output:
9+
For each testcase, print the merged list in sorted form.
10+
11+
User Task:
12+
The task is to complete the function sortedMerge() which takes references to the heads of two linked lists as the arguments and returns the head of merged linked list.
13+
14+
Expected Time Complexity : O(n+m)
15+
Expected Auxilliary Space : O(1)
16+
17+
Constraints:
18+
1 <= T <= 100
19+
1 <= N, M <= 104
20+
1 <= Node's data <= 105
21+
22+
Example:
23+
Input:
24+
2
25+
4 3
26+
5 10 15 40
27+
2 3 20
28+
2 2
29+
1 1
30+
2 4
31+
Output:
32+
2 3 5 10 15 20 40
33+
1 1 2 4
34+
35+
Explanation:
36+
Testcase 1: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40.
37+
Testcase 2: After merging the given two linked list , we have 1, 1, 2, 4 as output.
38+
*/
39+
40+
41+
42+
43+
44+
45+
// { Driver Code Starts
46+
#include<iostream>
47+
using namespace std;
48+
49+
/* Link list Node */
50+
struct Node {
51+
int data;
52+
struct Node *next;
53+
54+
Node(int x) {
55+
data = x;
56+
next = NULL;
57+
}
58+
};
59+
60+
Node* sortedMerge(struct Node* a, struct Node* b);
61+
62+
63+
/* Function to print Nodes in a given linked list */
64+
void printList(struct Node *n)
65+
{
66+
while (n!=NULL)
67+
{
68+
cout << n->data << " ";
69+
n = n->next;
70+
}
71+
cout << endl;
72+
}
73+
74+
/* Driver program to test above function*/
75+
int main()
76+
{
77+
int t;
78+
cin>>t;
79+
while(t--)
80+
{
81+
int n,m;
82+
cin>>n>>m;
83+
84+
int data;
85+
cin>>data;
86+
struct Node *head1 = new Node(data);
87+
struct Node *tail1 = head1;
88+
for (int i = 1; i < n; ++i)
89+
{
90+
cin>>data;
91+
tail1->next = new Node(data);
92+
tail1 = tail1->next;
93+
}
94+
95+
cin>>data;
96+
struct Node *head2 = new Node(data);
97+
struct Node *tail2 = head2;
98+
for(int i=1; i<m; i++)
99+
{
100+
cin>>data;
101+
tail2->next = new Node(data);
102+
tail2 = tail2->next;
103+
}
104+
105+
Node *head = sortedMerge(head1, head2);
106+
printList(head);
107+
}
108+
return 0;
109+
}
110+
// } Driver Code Ends
111+
112+
113+
114+
115+
/* Link list Node
116+
struct Node {
117+
int data;
118+
struct Node *next;
119+
120+
Node(int x) {
121+
data = x;
122+
next = NULL;
123+
}
124+
};
125+
*/
126+
127+
Node* sortedMerge(Node* h1, Node* h2)
128+
{
129+
if(!h1) return h1;
130+
if(!h2) return h1;
131+
132+
struct Node *start=(struct Node*)malloc(sizeof(struct Node)); // dummy
133+
struct Node *tail=start;
134+
135+
while(h1 && h2){
136+
if(h1->data <= h2->data){
137+
tail->next=h1;
138+
tail=tail->next;
139+
h1=h1->next;
140+
} else {
141+
tail->next=h2;
142+
tail=tail->next;
143+
h2=h2->next;
144+
}
145+
}
146+
if(h1){
147+
tail->next=h1;
148+
}
149+
if(h2){
150+
tail->next=h2;
151+
}
152+
153+
return start->next;
154+
}

linked list/pairwise_swap.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
1+
/*
2+
Given a singly linked list of size N. The task is to swap elements in the linked list pairwise.
3+
For example, if the input list is 1 2 3 4, the resulting list after swaps will be 2 1 4 3.
4+
5+
Input:
6+
The first line of input contains the number of test cases T. For each test case, the first line of input contains the length of the linked list and the next line contains linked list data.
7+
8+
Output:
9+
Output the linked list after swapping pairwise nodes.
10+
11+
User Task:
12+
The task is to complete the function pairWiseSwap() which takes the head node as the only argument and returns the modified head.
13+
14+
Expected Time Complexity: O(N).
15+
Expected Auxiliary Space: O(1).
16+
17+
Constraints:
18+
1 <= T <= 100
19+
1 <= N <= 103
20+
21+
Example:
22+
Input:
23+
1
24+
8
25+
1 2 2 4 5 6 7 8
26+
27+
Output:
28+
2 1 4 2 6 5 8 7
29+
30+
Explanation:
31+
Testcase 1: After swapping each pair considering (1,2), (2, 4), (5, 6).. so on as pairs, we get 2, 1, 4, 2, 6, 5, 8, 7 as a new linked list.
32+
*/
33+
34+
35+
36+
37+
38+
39+
140
#include<bits/stdc++.h>
241
using namespace std;
342

linked list/palindrome_check.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,46 @@
1+
/*
2+
Given a singly linked list of size N of integers. The task is to check if the given linked list is palindrome or not.
3+
Expected Time Complexity: O(N)
4+
Expected Auxialliary Space Usage: O(1) (ie, you should not use the recursive stack space as well)
5+
6+
Input:
7+
First line of input contains number of testcases T. For each testcase, first line of input contains length of linked list N and next line contains N integers as data of linked list.
8+
9+
Output:
10+
For each test case output will be 1 if the linked list is a palindrome else 0.
11+
12+
User Task:
13+
The task is to complete the function isPalindrome() which takes head as reference as the only parameter and returns true or false if linked list is palindrome or not respectively.
14+
15+
Constraints:
16+
1 <= T <= 103
17+
1 <= N <= 50
18+
19+
Example:
20+
Input:
21+
2
22+
3
23+
1 2 1
24+
4
25+
1 2 3 4
26+
Output:
27+
1
28+
0
29+
30+
Explanation:
31+
Testcase 1: The given linked list is 1 2 1 , which is a pallindrome and Hence, the output is 1.
32+
Testcase 2: The given linked list is 1 2 3 4 , which is not a pallindrome and Hence, the output is 0.
33+
*/
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
144
#include<bits/stdc++.h>
245
using namespace std;
346

0 commit comments

Comments
 (0)