Skip to content

Commit 8d26ede

Browse files
committed
🚀 16-Aug-2020
1 parent 8df51b1 commit 8d26ede

File tree

4 files changed

+232
-0
lines changed

4 files changed

+232
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Given an integer array arr, return true if there are three consecutive odd numbers in the array. Otherwise, return false.
2+
3+
4+
Example 1:
5+
6+
Input: arr = [2,6,4,1]
7+
Output: false
8+
Explanation: There are no three consecutive odds.
9+
Example 2:
10+
11+
Input: arr = [1,2,34,3,4,5,7,23,12]
12+
Output: true
13+
Explanation: [5,7,23] are three consecutive odds.
14+
15+
16+
Constraints:
17+
18+
1 <= arr.length <= 1000
19+
1 <= arr[i] <= 1000
20+
21+
22+
23+
24+
25+
26+
27+
28+
class Solution {
29+
public:
30+
bool threeConsecutiveOdds(vector<int>& arr) {
31+
int n=arr.size();
32+
if(n<=2) return false;
33+
for(int i=0;i<n-2;i++){
34+
if(arr[i]%2!=0 && arr[i+1]%2!=0 && arr[i+2]%2!=0)
35+
return true;
36+
}
37+
return false;
38+
}
39+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
You have an array arr of length n where arr[i] = (2 * i) + 1 for all valid values of i (i.e. 0 <= i < n).
2+
3+
In one operation, you can select two indices x and y where 0 <= x, y < n and subtract 1 from arr[x] and add 1 to arr[y] (i.e. perform arr[x] -=1 and arr[y] += 1). The goal is to make all the elements of the array equal. It is guaranteed that all the elements of the array can be made equal using some operations.
4+
5+
Given an integer n, the length of the array. Return the minimum number of operations needed to make all the elements of arr equal.
6+
7+
8+
9+
Example 1:
10+
11+
Input: n = 3
12+
Output: 2
13+
Explanation: arr = [1, 3, 5]
14+
First operation choose x = 2 and y = 0, this leads arr to be [2, 3, 4]
15+
In the second operation choose x = 2 and y = 0 again, thus arr = [3, 3, 3].
16+
Example 2:
17+
18+
Input: n = 6
19+
Output: 9
20+
21+
22+
Constraints:
23+
24+
1 <= n <= 10^4
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
class Solution {
37+
public:
38+
int minOperations(int n) {
39+
int res=0;
40+
int first = 1;
41+
int last = 2 * (n-1) + 1;
42+
int i=0;
43+
while(i<n/2){
44+
res+=(last-first)/2;
45+
last-=2;
46+
first+=2;
47+
i++;
48+
}
49+
return res;
50+
}
51+
};
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
2+
3+
4+
5+
Example 1:
6+
7+
Input: [[1,2],[2,3],[3,4],[1,3]]
8+
Output: 1
9+
Explanation: [1,3] can be removed and the rest of intervals are non-overlapping.
10+
Example 2:
11+
12+
Input: [[1,2],[1,2],[1,2]]
13+
Output: 2
14+
Explanation: You need to remove two [1,2] to make the rest of intervals non-overlapping.
15+
Example 3:
16+
17+
Input: [[1,2],[2,3]]
18+
Output: 0
19+
Explanation: You don't need to remove any of the intervals since they're already non-overlapping.
20+
21+
22+
Note:
23+
24+
You may assume the interval's end point is always bigger than its start point.
25+
Intervals like [1,2] and [2,3] have borders "touching" but they don't overlap each other.
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
class Solution {
43+
public:
44+
45+
static bool comp(vector<int> a, vector<int> b){
46+
return a[1] < b[1];
47+
}
48+
49+
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
50+
int res=0;
51+
int n=intervals.size();
52+
if(n==0) return 0;
53+
54+
sort(intervals.begin(), intervals.end(), comp);
55+
56+
57+
pair<int, int> cur;
58+
cur.first=intervals[0][0];
59+
cur.second=intervals[0][1];
60+
61+
for(int i=1;i<n;i++){
62+
if(cur.second>intervals[i][0]){
63+
res++;
64+
} else {
65+
cur.first=intervals[i][0];
66+
cur.second=intervals[i][1];
67+
}
68+
}
69+
return res;
70+
}
71+
};
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.
2+
3+
4+
5+
Example 1:
6+
7+
Input: [[1,2],[2,3],[3,4],[1,3]]
8+
Output: 1
9+
Explanation: [1,3] can be removed and the rest of intervals are non-overlapping.
10+
Example 2:
11+
12+
Input: [[1,2],[1,2],[1,2]]
13+
Output: 2
14+
Explanation: You need to remove two [1,2] to make the rest of intervals non-overlapping.
15+
Example 3:
16+
17+
Input: [[1,2],[2,3]]
18+
Output: 0
19+
Explanation: You don't need to remove any of the intervals since they're already non-overlapping.
20+
21+
22+
Note:
23+
24+
You may assume the interval's end point is always bigger than its start point.
25+
Intervals like [1,2] and [2,3] have borders "touching" but they don't overlap each other.
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
class Solution {
43+
public:
44+
45+
static bool comp(vector<int> a, vector<int> b){
46+
return a[1] < b[1];
47+
}
48+
49+
int eraseOverlapIntervals(vector<vector<int>>& intervals) {
50+
int res=0;
51+
int n=intervals.size();
52+
if(n==0) return 0;
53+
54+
sort(intervals.begin(), intervals.end(), comp);
55+
56+
57+
pair<int, int> cur;
58+
cur.first=intervals[0][0];
59+
cur.second=intervals[0][1];
60+
61+
for(int i=1;i<n;i++){
62+
if(cur.second>intervals[i][0]){
63+
res++;
64+
} else {
65+
cur.first=intervals[i][0];
66+
cur.second=intervals[i][1];
67+
}
68+
}
69+
return res;
70+
}
71+
};

0 commit comments

Comments
 (0)