Skip to content

Commit 03a30b3

Browse files
committed
🚀 28-Aug-2020
1 parent 2308ceb commit 03a30b3

File tree

4 files changed

+255
-0
lines changed

4 files changed

+255
-0
lines changed

array/kadanes_algorithm.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,34 @@ and write sum+=a[j];
8787
8888
*/
8989

90+
91+
92+
// For index of the subarray
93+
/*
94+
int maxSubArraySum(int a[], int size)
95+
{
96+
int max_so_far = INT_MIN, max_ending_here = 0,
97+
start =0, end = 0, s=0;
98+
99+
for (int i=0; i< size; i++ )
100+
{
101+
max_ending_here += a[i];
102+
103+
if (max_so_far < max_ending_here)
104+
{
105+
max_so_far = max_ending_here;
106+
start = s;
107+
end = i;
108+
}
109+
110+
if (max_ending_here < 0)
111+
{
112+
max_ending_here = 0;
113+
s = i + 1;
114+
}
115+
}
116+
cout << "Maximum contiguous sum is "<< max_so_far << endl;
117+
cout << "Starting index "<< start << endl << "Ending index "<< end << endl;
118+
}
119+
*/
120+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product.
2+
3+
Example 1:
4+
5+
Input: [2,3,-2,4]
6+
Output: 6
7+
Explanation: [2,3] has the largest product 6.
8+
Example 2:
9+
10+
Input: [-2,0,-1]
11+
Output: 0
12+
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
13+
14+
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
class Solution {
25+
public:
26+
int maxProduct(vector<int>& nums) {
27+
int n=nums.size();
28+
if(n==0) return 0;
29+
int ans=INT_MIN, maxVal=1, minVal=1, prevMax;
30+
for(int i=0;i<n;i++){
31+
if(nums[i]>0){
32+
maxVal=maxVal*nums[i];
33+
minVal=min(1, minVal*nums[i]);
34+
} else if(nums[i]==0){
35+
maxVal=0;
36+
minVal=1;
37+
} else if(nums[i]<0){
38+
prevMax=maxVal;
39+
maxVal=minVal*nums[i];
40+
minVal=prevMax*nums[i];
41+
}
42+
43+
ans=max(ans, maxVal);
44+
45+
if(maxVal<=0)
46+
maxVal=1;
47+
}
48+
return ans;
49+
}
50+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal to the end point of the interval i, which can be called that j is on the "right" of i.
2+
3+
For any interval i, you need to store the minimum interval j's index, which means that the interval j has the minimum start point to build the "right" relationship for interval i. If the interval j doesn't exist, store -1 for the interval i. Finally, you need output the stored value of each interval as an array.
4+
5+
Note:
6+
7+
You may assume the interval's end point is always bigger than its start point.
8+
You may assume none of these intervals have the same start point.
9+
10+
11+
Example 1:
12+
13+
Input: [ [1,2] ]
14+
15+
Output: [-1]
16+
17+
Explanation: There is only one interval in the collection, so it outputs -1.
18+
19+
20+
Example 2:
21+
22+
Input: [ [3,4], [2,3], [1,2] ]
23+
24+
Output: [-1, 0, 1]
25+
26+
Explanation: There is no satisfied "right" interval for [3,4].
27+
For [2,3], the interval [3,4] has minimum-"right" start point;
28+
For [1,2], the interval [2,3] has minimum-"right" start point.
29+
30+
31+
Example 3:
32+
33+
Input: [ [1,4], [2,3], [3,4] ]
34+
35+
Output: [-1, 2, -1]
36+
37+
Explanation: There is no satisfied "right" interval for [1,4] and [3,4].
38+
For [2,3], the interval [3,4] has minimum-"right" start point.
39+
NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.
40+
41+
42+
43+
44+
45+
46+
47+
class Solution {
48+
public:
49+
50+
int bSearch(vector<int> &first, int item){
51+
int l=0, h=first.size()-1;
52+
while(l<=h){
53+
int mid=l+(h-l)/2;
54+
if(item>first[mid]) l=mid+1;
55+
else h=mid-1;
56+
}
57+
return l;
58+
}
59+
60+
vector<int> findRightInterval(vector<vector<int>>& intervals) {
61+
int n=intervals.size();
62+
vector<int> ans;
63+
64+
unordered_map<int, int> mp; // <start, index>
65+
for(int i=0;i<n;i++){
66+
mp[intervals[i][0]]=i;
67+
}
68+
69+
vector<int> first;
70+
for(int i=0;i<n;i++)
71+
first.push_back(intervals[i][0]);
72+
sort(first.begin(), first.end());
73+
74+
for(int i=0;i<n;i++){
75+
int item=intervals[i][1];
76+
int key=bSearch(first, item);
77+
if(key==n)
78+
ans.push_back(-1);
79+
else if(key==0){
80+
if(intervals[i][1]<=first[0])
81+
ans.push_back(mp[first[0]]);
82+
else ans.push_back(-1);
83+
} else ans.push_back(mp[first[key]]);
84+
}
85+
return ans;
86+
}
87+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal to the end point of the interval i, which can be called that j is on the "right" of i.
2+
3+
For any interval i, you need to store the minimum interval j's index, which means that the interval j has the minimum start point to build the "right" relationship for interval i. If the interval j doesn't exist, store -1 for the interval i. Finally, you need output the stored value of each interval as an array.
4+
5+
Note:
6+
7+
You may assume the interval's end point is always bigger than its start point.
8+
You may assume none of these intervals have the same start point.
9+
10+
11+
Example 1:
12+
13+
Input: [ [1,2] ]
14+
15+
Output: [-1]
16+
17+
Explanation: There is only one interval in the collection, so it outputs -1.
18+
19+
20+
Example 2:
21+
22+
Input: [ [3,4], [2,3], [1,2] ]
23+
24+
Output: [-1, 0, 1]
25+
26+
Explanation: There is no satisfied "right" interval for [3,4].
27+
For [2,3], the interval [3,4] has minimum-"right" start point;
28+
For [1,2], the interval [2,3] has minimum-"right" start point.
29+
30+
31+
Example 3:
32+
33+
Input: [ [1,4], [2,3], [3,4] ]
34+
35+
Output: [-1, 2, -1]
36+
37+
Explanation: There is no satisfied "right" interval for [1,4] and [3,4].
38+
For [2,3], the interval [3,4] has minimum-"right" start point.
39+
NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.
40+
41+
42+
43+
44+
45+
46+
47+
class Solution {
48+
public:
49+
50+
int bSearch(vector<int> &first, int item){
51+
int l=0, h=first.size()-1;
52+
while(l<=h){
53+
int mid=l+(h-l)/2;
54+
if(item>first[mid]) l=mid+1;
55+
else h=mid-1;
56+
}
57+
return l;
58+
}
59+
60+
vector<int> findRightInterval(vector<vector<int>>& intervals) {
61+
int n=intervals.size();
62+
vector<int> ans;
63+
64+
unordered_map<int, int> mp; // <start, index>
65+
for(int i=0;i<n;i++){
66+
mp[intervals[i][0]]=i;
67+
}
68+
69+
vector<int> first;
70+
for(int i=0;i<n;i++)
71+
first.push_back(intervals[i][0]);
72+
sort(first.begin(), first.end());
73+
74+
for(int i=0;i<n;i++){
75+
int item=intervals[i][1];
76+
int key=bSearch(first, item);
77+
if(key==n)
78+
ans.push_back(-1);
79+
else if(key==0){
80+
if(intervals[i][1]<=first[0])
81+
ans.push_back(mp[first[0]]);
82+
else ans.push_back(-1);
83+
} else ans.push_back(mp[first[key]]);
84+
}
85+
return ans;
86+
}
87+
};

0 commit comments

Comments
 (0)