Skip to content

Commit 0ac6107

Browse files
committed
Weekly_Contest_365
1 parent 80c004e commit 0ac6107

3 files changed

+132
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-i/
2+
3+
4+
//Simple Brute force approach:
5+
//For every (i, j, k) calculate the value and finally consider the maximum
6+
7+
class Solution {
8+
public:
9+
long long maximumTripletValue(vector<int>& nums) {
10+
long long ans = 0; // Final Answer
11+
int length = nums.size();
12+
13+
for (int i = 0; i < length; i++) {
14+
for (int j = i + 1; j < length; j++) {
15+
for (int k = j + 1; k < length; k++) {
16+
//Calculate the value
17+
ans = max(ans, 1ll * (nums[i] - nums[j]) * nums[k]);
18+
}
19+
}
20+
}
21+
return ans;
22+
}
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-ii/
2+
// Optimized Version of maximum-value-of-an-ordered-triplet-i problem
3+
4+
/*
5+
Approach:
6+
Since we want to maximize (nums[i] - nums[j]) * nums[k] and i < j < k, we see that for
7+
nums[i] we can only take values before jth index and for nums[k] we can only take values
8+
after jth index.
9+
10+
Since we want to maximize the expression, nums[i] and nums[k] should be maximum with
11+
respect to the jth position. Hence, we can use prefix-max concept. We can create two arrays
12+
max_left and max_right.
13+
14+
max_left[i] ---> max of all the numbers before ith index
15+
max_right[i] ---> max of all the numbers after ith index
16+
17+
Finally we iterate from j = 0 to j = nums.size() and calculate the expression:
18+
(max_left[i] - nums[j]) * max_right[k]
19+
20+
Finally we record the max value of the expression, and return it.
21+
*/
22+
23+
24+
class Solution {
25+
public:
26+
long long maximumTripletValue(vector<int>& nums) {
27+
int n = nums.size();
28+
29+
// Initializing vectors with value = 0 (Min value nums[i] can take)
30+
vector<int> max_left(n,0);
31+
vector<int> max_right(n,0);
32+
33+
// Calculating max from left
34+
for (int i = 1; i < n; i++) {
35+
max_left[i] = max(max_left[i - 1], nums[i - 1]);
36+
}
37+
38+
// Calculating max from right
39+
for (int i = n - 2; i >= 0; i--) {
40+
max_right[i] = max(max_right[i + 1], nums[i + 1]);
41+
}
42+
43+
// Calculating the expression for every i and recording the maximum
44+
long ans = 0;
45+
for (int i = 1; i < n - 1; i++) {
46+
long temp = (max_left[i] - nums[i]) * ((long) max_right[i]);
47+
ans = max(ans, temp);
48+
}
49+
return ans;
50+
}
51+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// https://leetcode.com/problems/minimum-size-subarray-in-infinite-array/
2+
/*
3+
Approach:
4+
The problem is similar to 'Find minimum length circular subarray such that it's sum ==
5+
target.', but with a slight twist.
6+
7+
Here we are given to consider array of infinite length, (concatenate the array multiple
8+
times). Let's say sum of all the numbers in a single array = S.
9+
10+
So if we think about it:
11+
if target <= S, we have to consider only one instance of the array
12+
13+
but if target > S:
14+
Here, we have to consider multiple instances. We can do it in the following way:
15+
(sum of some last elements) + k*(sum of the array) + (sum of some front elements)
16+
17+
The (sum of some front elements) + (sum of some last elements) is = to target%S and
18+
k = (target/S) * n
19+
20+
minimum length circular subarray such that it's sum == target%S can be calculated by
21+
using the sliding window technique.
22+
*/
23+
24+
class Solution {
25+
public:
26+
int minSizeSubarray(vector<int>& nums, int target) {
27+
int n = nums.size();
28+
29+
// Calculate the sum of all elements
30+
long long sum = accumulate(nums.begin(),nums.end(),0ll);
31+
32+
// If target > sum, then we have to consider multiple copies of the array, hence we
33+
// initialize the ans to number of elements we have to definitely consider and rest we
34+
// calculate.
35+
int ans = (target/sum) * n;
36+
target%=sum;
37+
38+
// Finding minimum length circular subarray such that sum of it == target.
39+
// Using sliding window technique for the calculation.
40+
int mn = INT_MAX, st = 0, ed = 0;
41+
long long tempSum = 0;
42+
for(; ed<2*n; ed++){
43+
tempSum += nums[ed%n];
44+
while(tempSum>target){
45+
tempSum -= nums[st%n];
46+
st++;
47+
}
48+
if(tempSum == target){
49+
mn = min(mn, ed-st+1);
50+
}
51+
}
52+
53+
if (mn == INT_MAX) return -1;
54+
55+
ans+=mn;
56+
return ans;
57+
}
58+
};

0 commit comments

Comments
 (0)