diff --git a/C++/maximum-value-of-an-ordered-triplet-i.cpp b/C++/maximum-value-of-an-ordered-triplet-i.cpp new file mode 100644 index 00000000..34ceb1bc --- /dev/null +++ b/C++/maximum-value-of-an-ordered-triplet-i.cpp @@ -0,0 +1,23 @@ +// https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-i/ + + +//Simple Brute force approach: +//For every (i, j, k) calculate the value and finally consider the maximum + +class Solution { +public: + long long maximumTripletValue(vector& nums) { + long long ans = 0; // Final Answer + int length = nums.size(); + + for (int i = 0; i < length; i++) { + for (int j = i + 1; j < length; j++) { + for (int k = j + 1; k < length; k++) { + //Calculate the value + ans = max(ans, 1ll * (nums[i] - nums[j]) * nums[k]); + } + } + } + return ans; + } +}; \ No newline at end of file diff --git a/C++/maximum-value-of-an-ordered-triplet-ii.cpp b/C++/maximum-value-of-an-ordered-triplet-ii.cpp new file mode 100644 index 00000000..e80c2076 --- /dev/null +++ b/C++/maximum-value-of-an-ordered-triplet-ii.cpp @@ -0,0 +1,51 @@ +// https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-ii/ +// Optimized Version of maximum-value-of-an-ordered-triplet-i problem + +/* + Approach: + Since we want to maximize (nums[i] - nums[j]) * nums[k] and i < j < k, we see that for + nums[i] we can only take values before jth index and for nums[k] we can only take values + after jth index. + + Since we want to maximize the expression, nums[i] and nums[k] should be maximum with + respect to the jth position. Hence, we can use prefix-max concept. We can create two arrays + max_left and max_right. + + max_left[i] ---> max of all the numbers before ith index + max_right[i] ---> max of all the numbers after ith index + + Finally we iterate from j = 0 to j = nums.size() and calculate the expression: + (max_left[i] - nums[j]) * max_right[k] + + Finally we record the max value of the expression, and return it. +*/ + + +class Solution { +public: + long long maximumTripletValue(vector& nums) { + int n = nums.size(); + + // Initializing vectors with value = 0 (Min value nums[i] can take) + vector max_left(n,0); + vector max_right(n,0); + + // Calculating max from left + for (int i = 1; i < n; i++) { + max_left[i] = max(max_left[i - 1], nums[i - 1]); + } + + // Calculating max from right + for (int i = n - 2; i >= 0; i--) { + max_right[i] = max(max_right[i + 1], nums[i + 1]); + } + + // Calculating the expression for every i and recording the maximum + long ans = 0; + for (int i = 1; i < n - 1; i++) { + long temp = (max_left[i] - nums[i]) * ((long) max_right[i]); + ans = max(ans, temp); + } + return ans; + } +}; \ No newline at end of file diff --git a/C++/minimum-size-subarray-in-infinite-array.cpp b/C++/minimum-size-subarray-in-infinite-array.cpp new file mode 100644 index 00000000..9c0039c8 --- /dev/null +++ b/C++/minimum-size-subarray-in-infinite-array.cpp @@ -0,0 +1,58 @@ +// https://leetcode.com/problems/minimum-size-subarray-in-infinite-array/ +/* + Approach: + The problem is similar to 'Find minimum length circular subarray such that it's sum == + target.', but with a slight twist. + + Here we are given to consider array of infinite length, (concatenate the array multiple + times). Let's say sum of all the numbers in a single array = S. + + So if we think about it: + if target <= S, we have to consider only one instance of the array + + but if target > S: + Here, we have to consider multiple instances. We can do it in the following way: + (sum of some last elements) + k*(sum of the array) + (sum of some front elements) + + The (sum of some front elements) + (sum of some last elements) is = to target%S and + k = (target/S) * n + + minimum length circular subarray such that it's sum == target%S can be calculated by + using the sliding window technique. +*/ + +class Solution { +public: + int minSizeSubarray(vector& nums, int target) { + int n = nums.size(); + + // Calculate the sum of all elements + long long sum = accumulate(nums.begin(),nums.end(),0ll); + + // If target > sum, then we have to consider multiple copies of the array, hence we + // initialize the ans to number of elements we have to definitely consider and rest we + // calculate. + int ans = (target/sum) * n; + target%=sum; + + // Finding minimum length circular subarray such that sum of it == target. + // Using sliding window technique for the calculation. + int mn = INT_MAX, st = 0, ed = 0; + long long tempSum = 0; + for(; ed<2*n; ed++){ + tempSum += nums[ed%n]; + while(tempSum>target){ + tempSum -= nums[st%n]; + st++; + } + if(tempSum == target){ + mn = min(mn, ed-st+1); + } + } + + if (mn == INT_MAX) return -1; + + ans+=mn; + return ans; + } +}; \ No newline at end of file diff --git a/README.md b/README.md index b2cac739..3f09db4b 100644 --- a/README.md +++ b/README.md @@ -148,7 +148,8 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 162 | [Find Peak element](https://leetcode.com/problems/find-peak-element/) | [javascript](https://github.com/codedecks-in/LeetCode-Solutions/blob/master/JavaScript/findPeakElement.js) | o(Logn) | O(1) | Medium | Array | | 54 | [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [C++](./C++/Spiral-matrix.cpp) | O(M\*N) | O(M\*N) | Medium | Array | | 238 | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) | [C++](./C++/238.Product_of_array_except_self) | O(N) | O(N) | Medium | Array | - +| 2873 | [Maximum Value of an Ordered Triplet I](https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-i/) | [C++](./C++/maximum-value-of-an-ordered-triplet-i.cpp) | O(N^3) | O(1) | Easy | | +| 2874 | [Maximum Value of an Ordered Triplet II](https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-ii/) | [C++](./C++/maximum-value-of-an-ordered-triplet-ii.cpp) | O(N) | O(N) | Medium | |
@@ -296,6 +297,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu | 845 | [Longest Mountain in Array](https://leetcode.com/problems/longest-mountain-in-array/) | [C++](./C++/Longest-Mountain-in-Array.cpp) | _O(N)_ | _O(1)_ | Medium | Two Pointer | | 015 | [3 Sum](https://leetcode.com/problems/3sum/) | [C++](./C++/3sum.cpp) | _O(N)_ | _O(1)_ | Medium | Two Pointer | | | 021 | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [C++](./C++/Longest-Mountain-in-Array.cpp) | _O(N)_ | _O(1)_ | Easy | Two Pointer | | +| 2875 | [Minimum Size Subarray in Infinite Array](https://leetcode.com/problems/minimum-size-subarray-in-infinite-array/) | [C++](./C++/minimum-size-subarray-in-infinite-array.cpp) | O(N) | O(1) | Medium | |
@@ -515,6 +517,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) +| [Prathamesh Gadekar](https://github.com/Pr0-C0der)
| India | C++ | [GitHub](https://github.com/Pr0-C0der)