Skip to content

Commit a192df0

Browse files
committed
Added one Linked list and one greedy problem
1 parent 04b2ecf commit a192df0

File tree

2 files changed

+198
-0
lines changed

2 files changed

+198
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
Given a link list of size N, modify the list such that all the even numbers appear before all the
3+
odd numbers in the modified list.
4+
The order of appearance of numbers within each segregation should be same as that in the original list.
5+
6+
Example 1:
7+
8+
Input:
9+
N = 7
10+
Linked List =
11+
17 -> 15 -> 8 -> 9 -> 2 -> 4 -> 6 -> NULL
12+
13+
Output: 8 2 4 6 17 15 9
14+
15+
Example 2:
16+
17+
Input:
18+
N = 4
19+
Linked List = 1 -> 3 -> 5 -> 7
20+
21+
Output: 1 3 5 7
22+
23+
*/
24+
25+
// { Driver Code Starts
26+
//Initial template for C++
27+
28+
#include <bits/stdc++.h>
29+
using namespace std;
30+
31+
struct Node
32+
{
33+
int data;
34+
struct Node* next;
35+
36+
Node(int x){
37+
data = x;
38+
next = NULL;
39+
}
40+
};
41+
void printList(Node* node)
42+
{
43+
while (node != NULL) {
44+
cout << node->data <<" ";
45+
node = node->next;
46+
}
47+
cout<<"\n";
48+
}
49+
50+
51+
/*
52+
Method followed:
53+
- Use to queues to store even and odd data containing nodes of the List.
54+
- Traverse the LL and populates the queues
55+
- Re-Traverse the Ll emptying the even queue first followed by odd one.
56+
- return head
57+
*/
58+
class Solution{
59+
public:
60+
Node* divide(int N, Node *head){
61+
queue<int> odd;
62+
queue<int> even;
63+
Node* curr = head;
64+
Node* curr2 = head;
65+
while(curr){
66+
if(curr->data%2==0){
67+
even.push(curr->data);
68+
}else{
69+
odd.push(curr->data);
70+
}
71+
curr=curr->next;
72+
}
73+
74+
while(!even.empty()){
75+
curr2->data=even.front();
76+
curr2=curr2->next;
77+
even.pop();
78+
}
79+
while(!odd.empty()){
80+
curr2->data=odd.front();
81+
curr2=curr2->next;
82+
odd.pop();
83+
}
84+
return head;
85+
}
86+
};
87+
88+
89+
90+
int main() {
91+
92+
int t;
93+
cin>>t;
94+
while(t--){
95+
int N;
96+
cin>>N;
97+
int data;
98+
cin>>data;
99+
struct Node *head = new Node(data);
100+
struct Node *tail = head;
101+
for (int i = 0; i < N-1; ++i)
102+
{
103+
cin>>data;
104+
tail->next = new Node(data);
105+
tail = tail->next;
106+
}
107+
108+
Solution ob;
109+
Node *ans = ob.divide(N, head);
110+
printList(ans);
111+
}
112+
return 0;
113+
}

Greedy/n_meetings_in_a_room.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
There is one meeting room in a firm.
3+
There are N meetings in the form of (S[i], F[i]) where S[i] is
4+
start time of meeting i and F[i] is finish time of meeting i.
5+
6+
What is the maximum number of meetings that can be accommodated in the meeting room
7+
when only one meeting can be held in the meeting room at a particular time?
8+
9+
Also note start time of one chosen meeting can't be equal to the end time of the other chosen meeting.
10+
*/
11+
12+
13+
14+
#include <bits/stdc++.h>
15+
using namespace std;
16+
17+
18+
19+
/*
20+
Method followed:
21+
- make a vector of pair of meeting's start and end time
22+
- sort this vector with respect to the end time
23+
- count the first meeting and set time limit as the end of first meeting
24+
- traverse the vector
25+
- if start time of any meeting is greater than time limit.
26+
- increment count
27+
- set time limit as end time of this meeting
28+
29+
*/
30+
class Solution
31+
{
32+
public:
33+
static bool comp(pair<int,int> a,pair<int,int> b){
34+
if(a.second<b.second){
35+
return true;
36+
}
37+
return false;
38+
39+
}
40+
int maxMeetings(int start[], int end[], int n)
41+
{
42+
vector<pair<int,int>> m;
43+
for(int i=0;i<n;i++){
44+
m.push_back(make_pair(start[i],end[i]));
45+
}
46+
47+
sort(m.begin(),m.end(),comp);
48+
49+
50+
51+
int currMeet=m[0].first;
52+
53+
int timeLimit = m[0].second;
54+
55+
int count=1;
56+
57+
for(int i=1;i<n;i++){
58+
auto a=m[i];
59+
if(a.first>timeLimit){
60+
count++;
61+
timeLimit=a.second;
62+
}
63+
}
64+
65+
return count;
66+
}
67+
};
68+
69+
int main() {
70+
int t;
71+
cin >> t;
72+
while (t--) {
73+
int n;
74+
cin >> n;
75+
int start[n], end[n];
76+
for (int i = 0; i < n; i++) cin >> start[i];
77+
78+
for (int i = 0; i < n; i++) cin >> end[i];
79+
80+
Solution ob;
81+
int ans = ob.maxMeetings(start, end, n);
82+
cout << ans << endl;
83+
}
84+
return 0;
85+
}

0 commit comments

Comments
 (0)