From d98b139a145664cd273db7dd7189cc38bc3c2eb9 Mon Sep 17 00:00:00 2001 From: Shrishti Raizada <70793477+ShrishtiRaizada@users.noreply.github.com> Date: Tue, 30 Nov 2021 17:52:29 +0530 Subject: [PATCH] Add files via upload --- Milestone4_Q1.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++++++ Milestone4_Q2.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++++ Milestone4_Q3.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++ Milestone4_Q4.cpp | 32 +++++++++++++++++++ 4 files changed, 257 insertions(+) create mode 100644 Milestone4_Q1.cpp create mode 100644 Milestone4_Q2.cpp create mode 100644 Milestone4_Q3.cpp create mode 100644 Milestone4_Q4.cpp diff --git a/Milestone4_Q1.cpp b/Milestone4_Q1.cpp new file mode 100644 index 0000000..53d840b --- /dev/null +++ b/Milestone4_Q1.cpp @@ -0,0 +1,78 @@ +/* Shrishti Raizada + Milestone 4 + Q1 +*/ + +#include + +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; +} + \ No newline at end of file diff --git a/Milestone4_Q2.cpp b/Milestone4_Q2.cpp new file mode 100644 index 0000000..8fcdad3 --- /dev/null +++ b/Milestone4_Q2.cpp @@ -0,0 +1,76 @@ +/* Shrishti Raizada + Milestone 4 + Q2 +*/ + +#include + +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; +} \ No newline at end of file diff --git a/Milestone4_Q3.cpp b/Milestone4_Q3.cpp new file mode 100644 index 0000000..d4b9eb0 --- /dev/null +++ b/Milestone4_Q3.cpp @@ -0,0 +1,71 @@ +/* Shrishti Raizada + Milestone 4 + Q3 +*/ + +#include +#include +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=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 +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); +} \ No newline at end of file