Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add LeetCode Weekly Contest 365 solutions (first 3 problems) #445

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
23 changes: 23 additions & 0 deletions C++/maximum-value-of-an-ordered-triplet-i.cpp
Original file line number Diff line number Diff line change
@@ -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<int>& 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;
}
};
51 changes: 51 additions & 0 deletions C++/maximum-value-of-an-ordered-triplet-ii.cpp
Original file line number Diff line number Diff line change
@@ -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<int>& nums) {
int n = nums.size();

// Initializing vectors with value = 0 (Min value nums[i] can take)
vector<int> max_left(n,0);
vector<int> 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;
}
};
58 changes: 58 additions & 0 deletions C++/minimum-size-subarray-in-infinite-array.cpp
Original file line number Diff line number Diff line change
@@ -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<int>& 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;
}
};
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | |

<br/>

Expand Down Expand Up @@ -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 | |

<br/>
<div align="right">
Expand Down Expand Up @@ -515,6 +517,7 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
| [Shrimadh V Rao](https://github.com/Shrimadh) <br> <img src="https://avatars.githubusercontent.com/u/64469917?v=4" width="100" height="100"> | India | C++ | [GitHub](https://github.com/Shrimadh)
| [Shreyas Shrawage](https://github.com/shreyventure) <br> <img src = "https://avatars.githubusercontent.com/u/55741087?v=4" width="100" height="100"> | India | Python | [CodeChef](https://www.codechef.com/users/shreyventure)<br/>[LeetCode](https://leetcode.com/shreyventure/)<br/>[HackerRank](https://www.hackerrank.com/shreyas_shrawage)
| [Surbhi Mayank](https://github.com/surbhi2408) <br> <img src="https://avatars.githubusercontent.com/u/58289829?s=400&u=68fd396819b927ec4d8820d87d6d1e311c3abd01&v=4" width="100" height="100"> | India | C++ | [GitHub](https://github.com/surbhi2408)
| [Prathamesh Gadekar](https://github.com/Pr0-C0der) <br> <img src="https://avatars.githubusercontent.com/u/93116210?s=400&u=54cb21eb37cf68fee82a4e8b8da42db15889615d&v=4" width="100" height="100"> | India | C++ | [GitHub](https://github.com/Pr0-C0der)
<div align="right">
<b><a href="#algorithms">⬆️ Back to Top</a></b>
</div>
Expand Down