From 646b1ca06a320b75addb74b14eae1978f07a5d20 Mon Sep 17 00:00:00 2001 From: Shivangi Date: Sat, 30 Dec 2023 21:25:08 +0530 Subject: [PATCH] feat(cpp): Maximum product subarray --- C++/maximum-product-subarray.cpp | 36 ++++++++++++++++++++++++++++++++ README.md | 3 ++- 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 C++/maximum-product-subarray.cpp diff --git a/C++/maximum-product-subarray.cpp b/C++/maximum-product-subarray.cpp new file mode 100644 index 00000000..2ff2bca6 --- /dev/null +++ b/C++/maximum-product-subarray.cpp @@ -0,0 +1,36 @@ + /* + +Problem - + 152. Maximum Product Subarray + Given an integer array nums, find a subarray that has the largest product, and return the product. + +The Optimized Solution - + Time Complexity: O(N) + Space Complexity: O(1) + +Example - + Input: nums = [2,3,-2,4] + Output: 6 + Explanation: [2,3] has the largest product 6. + +*/ + + +class Solution { +public: + int maxProduct(vector& nums) { + + int max_end = nums[0]; + int min_end = nums[0]; + int max_so_far = nums[0]; + + for (int i = 1; i < nums.size(); i++) { + int temp = max({ nums[i], nums[i] * max_end, nums[i] * min_end }); + min_end = min({ nums[i], nums[i] * max_end, nums[i] * min_end }); + max_end = temp; + max_so_far = max(max_so_far, max_end); + } + + return max_so_far; + } +}; \ No newline at end of file diff --git a/README.md b/README.md index b2cac739..3b089a8b 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 15 | [3 Sum](https://leetcode.com/problems/3sum/) | [Python](./Python/ThreeNumbersSum.py) | O( nLog(n) ) | O(1) | Medium | Array | | 1200 | [Minimum Absolute Difference](https://leetcode.com/problems/minimum-absolute-difference/) | [Python](./python/SmallestDifference.py) | O(n) | O(1) | Easy | Array | | 532 | [K-diff Pairs in an Array](https://leetcode.com/problems/k-diff-pairs-in-an-array/) | [C++](./C++/k-diff-pairs-in-an-array.cpp) | O(n) | O(n) | Medium | Array | -| 152 | [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) | [Javascript](./JavaScript/152.Maximum-Product-Subarray.js) | O(n) | O(n) | Medium | Array | +| 152 | [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) | [Javascript](./JavaScript/152.Maximum-Product-Subarray.js)
[C++](./C++/maximum-product-subarray.cpp) | O(n) | O(n) | Medium | Array | | 073 | [Set-Matrix-Zeroes](https://leetcode.com/problems/set-matrix-zeroes/) | [Java](./Java/set-matrix-zeroes.java) | O(MN) | O(1) | Medium | Array | | 1288 | [Remove-Covered-Intervals](https://leetcode.com/problems/remove-covered-intervals) | [C++](./C++/Remove-Covered-Intervals.cpp) | O(N\*N) | O(1) | Medium | Array | | 189 | [Rotate-Array](https://leetcode.com/problems/rotate-array/) | [Python](./Python/rotate-array.py) | O(N) | O(1) | Medium | Array | @@ -515,6 +515,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if | [Shrimadh V Rao](https://github.com/Shrimadh)
| India | C++ | [GitHub](https://github.com/Shrimadh) | [Shreyas Shrawage](https://github.com/shreyventure)
| India | Python | [CodeChef](https://www.codechef.com/users/shreyventure)
[LeetCode](https://leetcode.com/shreyventure/)
[HackerRank](https://www.hackerrank.com/shreyas_shrawage) | [Surbhi Mayank](https://github.com/surbhi2408)
| India | C++ | [GitHub](https://github.com/surbhi2408) +| [Shivangi Rai](https://github.com/ShivangiRai1310)
| India | C++ | [GitHub](https://github.com/ShivangiRai1310)
⬆️ Back to Top