Skip to content

Commit 3840536

Browse files
committed
new question added
1 parent 86d424f commit 3840536

File tree

7 files changed

+241
-1
lines changed

7 files changed

+241
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ values.
123123
value to be at the top of the tree with min edge length (or path to be traversed) and min at bottom with
124124
max edge or path to be traversed to minimize work.
125125
- Min cost spanning tree and shortest path problems are two different things. In shortest path a source will be given and we will have to reach the target in the shortest path possible given weight of each edge. In case of min cost spanning tree we have to construct a graph with min weight possible connecting ALL the nodes. Therefore priority of the algo varies
126+
- In greedy method, to min or max something (different methods), we can be greedy about something to solve the algo, and bring in cases which can defy out method. One of them will work.
126127

127128
# Topic0: Programming Questions
128129

@@ -298,6 +299,8 @@ max edge or path to be traversed to minimize work.
298299
- [Program for DIJKSTRA algorithm](/greedy/question7.c)
299300
- [Program to implement a simple graph](/greedy/question8.c)
300301
- [Consider n-ropes with different length. Find algo to tie up all the rope into a single rope with min cost](/greedy/question9.c)
302+
- [Find max intervals from given intervals such that none of them are overlapping](/greedy/question10.c)
303+
- [Number of railway platforms](/greedy/question11.c)
301304

302305
## Some important concepts to solve algos better
303306

code/test1.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
int main(){
5+
return 0;
6+
}

code/test2.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
5+
int main(){
6+
int cases;
7+
scanf("%d",&cases);
8+
int i;
9+
for(i=0;i<cases;i++){
10+
11+
}
12+
return 0;
13+
}

code/test3.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
int checkOperations(int *arr, int size){
5+
int sum = 0;
6+
if(size%2==0){
7+
int sum1 = 0;
8+
9+
sum1 = sum1 + arr[0];
10+
arr[0] = 0;
11+
int i,j;
12+
for(i=1; i<=(size/2);i++){
13+
sum1 += (arr[i]-i);
14+
}
15+
int sub = 1;
16+
for(j=size-1;j>=(size/2)+1;j--){
17+
sum1 += (arr[j]-sub);
18+
sub++;
19+
}
20+
21+
int sum2 = 0;
22+
sum2 = sum2 + arr[size-1];
23+
arr[size-1] = 0;
24+
int k,l;
25+
for(k=0; k<=(size-1)/2;k++){
26+
sum2 += (arr[k]-(k+1));
27+
}
28+
int val = 1;
29+
for(l=(size-2);l>=(((size-1)/2)+1);l--){
30+
sum2 += (arr[l]-val);
31+
val++;
32+
}
33+
sum = (sum1 < sum2)?sum1:sum2;
34+
35+
}else{
36+
int i,j;
37+
for(i=0; i<=(size/2);i++){
38+
sum += (arr[i]-(i+1));
39+
}
40+
int sub = 1;
41+
for(j=size-1;j>=(size/2)+1;j--){
42+
sum += (arr[j]-sub);
43+
sub++;
44+
}
45+
}
46+
return sum;
47+
}
48+
49+
50+
int checkOperations(int *arr, int start, int end){
51+
52+
int middle = (start-end)/2;
53+
int size = end+1;
54+
int value = (size/2)+1;
55+
56+
if(arr[middle] >= value || arr[middle-1] >=value){
57+
return calculate(arr,start, end);
58+
}
59+
int subSize;
60+
if(size%2==0){
61+
subSize = size-3;
62+
}else{
63+
subSize= size-2;
64+
}
65+
66+
int i;
67+
for(i=0;i<size-subSize;i++){
68+
j=subSize;
69+
checkOperations(arr,i,j);
70+
}
71+
72+
}
73+
74+
int main(){
75+
int cases,blocks;
76+
scanf("%d",&cases);
77+
int i, j;
78+
for(i=0;i<cases;i++){
79+
scanf("%d",&blocks);
80+
int arr[blocks];
81+
for(j=0;j<blocks;j++){
82+
scanf("%d",&arr[j]);
83+
}
84+
int number = checkOperations(arr,0,blocks-1);
85+
printf("%d\n", number);
86+
}
87+
return 0;
88+
}

greedy/question10.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Find max intervals from given intervals such that none of them are overlapping
3+
4+
METHOD:
5+
we can go by three methods here but only one works fine
6+
Greedy about duration: Thought process is to be greedy about duration and choose the one with min
7+
duration so that probability of overlapping is less. But in this case if a short duration is
8+
overlapping in between two big durations, we wont be able to pick the other two. So wont give us the
9+
max output
10+
11+
Greedy about start time. Choose start time as early as possible. That can minimize overlapping. But
12+
if something starts very early and ends really late, we wont be able to pick the other durations cos
13+
of that
14+
15+
Greedy about end time: This is the correct method because in this case we can have max outputs.
16+
17+
*/
18+
#include <stdio.h>
19+
#include <stdlib.h>
20+
21+
int cmpfunc(const void *a, const void *b){
22+
return (*(int *)a - *(int *)b);
23+
}
24+
25+
struct node{
26+
int left;
27+
int right;
28+
};
29+
30+
int maxIntervals(struct node *arr, int size){
31+
int counter = 1;
32+
for(int i=0; i<size-1; i++){
33+
if(arr[i].right < arr[i+1].left){
34+
counter++;
35+
}
36+
}
37+
if(counter == 1){
38+
return counter;
39+
}
40+
return counter+1;
41+
}
42+
43+
int main(){
44+
int size = 6;
45+
struct node *arr = (struct node *)malloc(sizeof(struct node)*size);
46+
47+
arr[0].right = 11;
48+
arr[0].left = 7;
49+
50+
arr[1].right = 13;
51+
arr[1].left = 10;
52+
53+
arr[2].right = 14;
54+
arr[2].left = 9;
55+
56+
arr[3].right = 16;
57+
arr[3].left = 12;
58+
59+
arr[4].right = 25;
60+
arr[4].left = 20;
61+
62+
arr[5].right = 50;
63+
arr[5].left = 1;
64+
65+
//already sorted
66+
int max = maxIntervals(arr,size);
67+
printf("max intervals are %d\n", max);
68+
return 0;
69+
}

greedy/question11.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Number of railway platforms
3+
What is the min number of railway platforms required such that all trains will stand at station w/o colliding
4+
given train names and arrival time and departure time.
5+
6+
METHOD:
7+
We will be given arrival and departures times for different trains. Basis that we need to find the min
8+
platforms requires at any given time so that trains do not collide
9+
10+
One method is to find all intervals that overlap with each other and see how many trains are the maximum
11+
at any instance of time.
12+
13+
Other method is to sort all given values and place them in a single array. Then see if they are arrival
14+
or departure. If they are arrival increment counter, for departure decrement counter. Maintain a variable
15+
to record the max value from the scan. That will be the answer
16+
17+
Time complexity: O(nlogn) //sorting the inputs
18+
Space complexity: O(n) //because we will store it in structure
19+
*/
20+
#include <stdio.h>
21+
#include <stdlib.h>
22+
23+
int calculatePlatforms(int arr[], int dep[],int size){
24+
int counter = 0;
25+
int max = 0;
26+
int i = 0,j=0;
27+
while(i < size && j < size){
28+
// printf("arrival is %d\n", arr[i]);
29+
// printf("dep is %d\n", dep[j]);
30+
if(arr[i]<dep[j]){
31+
counter++;
32+
i++;
33+
}else{
34+
counter--;
35+
j++;
36+
}
37+
if(max < counter){
38+
max = counter;
39+
}
40+
}
41+
return max;
42+
}
43+
44+
int cmpfnc(const void *a, const void *b){
45+
return (*(int *)a - *(int *)b);
46+
}
47+
48+
int main(){
49+
int arr[] = {1000,1015,1030,1040};
50+
int dep[] = {1020,1400,1500,1055};
51+
52+
int size = sizeof(arr)/sizeof(arr[0]);
53+
54+
qsort(arr,size,sizeof(int),cmpfnc);
55+
qsort(dep,size,sizeof(int),cmpfnc);
56+
57+
int minPlatforms = calculatePlatforms(arr,dep,size);
58+
printf("min platforms req are %d\n", minPlatforms);
59+
return 0;
60+
}

nextquestions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,5 @@ TODO:
6363
- strings question12 to be done
6464
- min heap implementation of PRIMS in greedy
6565
- solve KRUSKAL from greedy
66-
- solve DIJKSTRA alog from greedy
66+
- solve DIJKSTRA alog from greedy
67+
- greedy question2 to be seen from geeks for geeks

0 commit comments

Comments
 (0)