Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions Milestone4_Q1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* Shrishti Raizada
Milestone 4
Q1
*/

#include <bits/stdc++.h>

using namespace std;

// A utility function that returns
// maximum of two integers

int max(int a, int b) { return (a > b) ? a : b; }

// Returns the maximum value that
// can be put in a knapsack of capacity W

int knapSack(int W, int wt[], int val[], int n)
{


// Base Case

if (n == 0 || W == 0)

return 0;


// If weight of the nth item is more

// than Knapsack capacity W, then

// this item cannot be included

// in the optimal solution

if (wt[n - 1] > W)

return knapSack(W, wt, val, n - 1);


// Return the maximum of two cases:

// (1) nth item included

// (2) not included

else

return max(

val[n - 1]

+ knapSack(W - wt[n - 1],

wt, val, n - 1),

knapSack(W, wt, val, n - 1));
}

// Driver code

int main()
{

int val[] = { 60, 100, 120 };

int wt[] = { 10, 20, 30 };

int W = 50;

int n = sizeof(val) / sizeof(val[0]);

cout << knapSack(W, wt, val, n);

return 0;
}

76 changes: 76 additions & 0 deletions Milestone4_Q2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* Shrishti Raizada
Milestone 4
Q2
*/

#include <bits/stdc++.h>

using namespace std;


// Prints a maximum set of activities that can be done by a single
// person, one at a time.
// n --> Total number of activities
// s[] --> An array that contains start time of all activities
// f[] --> An array that contains finish time of all activities

void printMaxActivities(int s[], int f[], int n)
{

int i, j;



cout <<"Following activities are selected "<< endl;



// The first activity always gets selected

i = 0;

cout <<" "<< i;



// Consider rest of the activities

for (j = 1; j < n; j++)

{

// If this activity has start time greater than or

// equal to the finish time of previously selected

// activity, then select it

if (s[j] >= f[i])

{

cout <<" " << j;

i = j;

}

}
}


// driver program to test above function

int main()
{

int s[] = {1, 3, 0, 5, 8, 5};

int f[] = {2, 4, 6, 7, 9, 9};

int n = sizeof(s)/sizeof(s[0]);

printMaxActivities(s, f, n);

return 0;
}
71 changes: 71 additions & 0 deletions Milestone4_Q3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* Shrishti Raizada
Milestone 4
Q3
*/

#include<iostream>
#include<algorithm>
using namespace std;

// A structure to represent a job
struct Job
{
char id; // Job Id
int dead; // Deadline of job
int profit; // Profit if job is over before or on deadline
};

// This function is used for sorting all jobs according to profit
bool comparison(Job a, Job b)
{
return (a.profit > b.profit);
}

// Returns minimum number of platforms required
void printJobScheduling(Job arr[], int n)
{
// Sort all jobs according to decreasing order of profit
sort(arr, arr+n, comparison);

int result[n]; // To store result (Sequence of jobs)
bool slot[n]; // To keep track of free time slots

// Initialize all slots to be free
for (int i=0; i<n; i++)
slot[i] = false;

// Iterate through all given jobs
for (int i=0; i<n; i++)
{
// Find a free slot for this job (Note that we start
// from the last possible slot)
for (int j=min(n, arr[i].dead)-1; j>=0; j--)
{
// Free slot found
if (slot[j]==false)
{
result[j] = i; // Add this job to result
slot[j] = true; // Make this slot occupied
break;
}
}
}

// Print the result
for (int i=0; i<n; i++)
if (slot[i])
cout << arr[result[i]].id << " ";
}

// Driver code
int main()
{
Job arr[] = { {'a', 2, 100}, {'b', 1, 19}, {'c', 2, 27},
{'d', 1, 25}, {'e', 3, 15}};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Following is maximum profit sequence of jobs \n";

// Function call
printJobScheduling(arr, n);
return 0;
}
32 changes: 32 additions & 0 deletions Milestone4_Q4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* Shrishti Raizada
Milestone 4
Q4
*/

#include <iostream>
using namespace std;

int maxArea(int A[], int len)
{
int area = 0;
for (int i = 0; i < len; i++) {
for (int j = i + 1; j < len; j++) {
// Calculating the max area
area = max(area, min(A[j], A[i]) * (j - i));
}
}
return area;
}

// Driver code
int main()
{
int a[] = { 1, 5, 4, 3 };
int b[] = { 3, 1, 2, 4, 5 };

int len1 = sizeof(a) / sizeof(a[0]);
cout << maxArea(a, len1);

int len2 = sizeof(b) / sizeof(b[0]);
cout << endl << maxArea(b, len2);
}