From 94cd3b523b4d02e68a9cdead8125c9064e1f8978 Mon Sep 17 00:00:00 2001 From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com> Date: Tue, 29 Oct 2024 21:46:04 +0530 Subject: [PATCH 1/3] Made the changes and added c file --- .../Best Time To Buy and Sell/Program.c | 24 -------------- .../Best Time To Buy and Sell/Programbest.c | 33 +++++++++++++++++++ 2 files changed, 33 insertions(+), 24 deletions(-) delete mode 100644 Optimization Algorithms/Best Time To Buy and Sell/Program.c create mode 100644 Optimization Algorithms/Best Time To Buy and Sell/Programbest.c diff --git a/Optimization Algorithms/Best Time To Buy and Sell/Program.c b/Optimization Algorithms/Best Time To Buy and Sell/Program.c deleted file mode 100644 index 195c5483..00000000 --- a/Optimization Algorithms/Best Time To Buy and Sell/Program.c +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include -using namespace std; - -int maxProfit(vector &prices) { - int n = prices.size(); - int profit= 0; - int bestBuy=prices[0]; - - // Explore all possible ways to buy and sell stock - for (int i = 1; i < n; i++) { - if(prices[i]>bestBuy) - { - profit = max(profit, prices[i] - bestBuy); - } - } - return profit; -} - -int main() { - vector prices = {7, 4, 1, 3, 6, 9, 20}; - cout << maxProfit(prices) << endl; - return 0; -} \ No newline at end of file diff --git a/Optimization Algorithms/Best Time To Buy and Sell/Programbest.c b/Optimization Algorithms/Best Time To Buy and Sell/Programbest.c new file mode 100644 index 00000000..14584b41 --- /dev/null +++ b/Optimization Algorithms/Best Time To Buy and Sell/Programbest.c @@ -0,0 +1,33 @@ +#include + +int maxProfit(int* prices, int pricesSize) { + if (pricesSize <= 1) return 0; // No profit can be made + + int minPrice = prices[0]; // Initialize minPrice to the first price + int maxProfit = 0; // Initialize maxProfit to 0 + + for (int i = 1; i < pricesSize; i++) { + // Update minPrice if the current price is lower + if (prices[i] < minPrice) { + minPrice = prices[i]; + } else { + // Calculate profit if we sell at the current price + int profit = prices[i] - minPrice; + if (profit > maxProfit) { + maxProfit = profit; // Update maxProfit if the current profit is higher + } + } + } + + return maxProfit; // Return the maximum profit found +} + +int main() { + int prices[] = {7, 1, 5, 3, 6, 4}; // Example prices + int size = sizeof(prices) / sizeof(prices[0]); + + int result = maxProfit(prices, size); + printf("The maximum profit is: %d\n", result); + + return 0; +} \ No newline at end of file From 07db6b69a3bd874afcb3de9e78ae36fa69a74dc6 Mon Sep 17 00:00:00 2001 From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com> Date: Tue, 29 Oct 2024 21:48:28 +0530 Subject: [PATCH 2/3] Update Readme.md --- Optimization Algorithms/Best Time To Buy and Sell/Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Optimization Algorithms/Best Time To Buy and Sell/Readme.md b/Optimization Algorithms/Best Time To Buy and Sell/Readme.md index 34b183e7..05ff30ac 100644 --- a/Optimization Algorithms/Best Time To Buy and Sell/Readme.md +++ b/Optimization Algorithms/Best Time To Buy and Sell/Readme.md @@ -56,5 +56,5 @@ Return Maximum Profit: Having processed all days, we have found that the maximum ## Time and Space Complexity The time complexity of the given function is O(n), where n is the length of the input list prices. This is because the function includes a single loop that iterates through each element in the list exactly once, performing a constant amount of work at each step; thus, the total work done is linear in the size of the input. -## Space Complexity +## Space Complexity The space complexity of the function is O(1), indicating that the amount of additional memory used by the function does not depend on the size of the input. \ No newline at end of file From b48ca72e209952f6457a3b589047df8ebd273415 Mon Sep 17 00:00:00 2001 From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com> Date: Tue, 29 Oct 2024 22:08:03 +0530 Subject: [PATCH 3/3] Made the modification --- Optimization Algorithms/Best Time To Buy and Sell/Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Optimization Algorithms/Best Time To Buy and Sell/Readme.md b/Optimization Algorithms/Best Time To Buy and Sell/Readme.md index 05ff30ac..34b183e7 100644 --- a/Optimization Algorithms/Best Time To Buy and Sell/Readme.md +++ b/Optimization Algorithms/Best Time To Buy and Sell/Readme.md @@ -56,5 +56,5 @@ Return Maximum Profit: Having processed all days, we have found that the maximum ## Time and Space Complexity The time complexity of the given function is O(n), where n is the length of the input list prices. This is because the function includes a single loop that iterates through each element in the list exactly once, performing a constant amount of work at each step; thus, the total work done is linear in the size of the input. -## Space Complexity +## Space Complexity The space complexity of the function is O(1), indicating that the amount of additional memory used by the function does not depend on the size of the input. \ No newline at end of file