Skip to content

Commit 2e3ac03

Browse files
committed
0795 solved.
1 parent c10b1f4 commit 2e3ac03

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(cpp_0795)
3+
4+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
5+
6+
set(SOURCE_FILES main2.cpp)
7+
add_executable(cpp_0795 ${SOURCE_FILES})
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/// Source : https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2018-03-05
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// Counting
12+
///
13+
/// Transform vector A into nums, which only includes elements 0, 1, 2
14+
/// which means elements in A: less than L; between L and R (includes); larger than R
15+
/// Then counting how many subarrays in only 01 subarray that include element 1
16+
/// Using all the subarray number for any 01 subarray MINUS
17+
/// all the subarray number for only 0 subarrays
18+
///
19+
/// Time Complexity: O(n)
20+
/// Space Complexity: O(n)
21+
class Solution {
22+
public:
23+
int numSubarrayBoundedMax(vector<int>& A, int L, int R) {
24+
25+
vector<int> nums;
26+
for(int a: A)
27+
if(a < L)
28+
nums.push_back(0);
29+
else if(a > R)
30+
nums.push_back(2);
31+
else
32+
nums.push_back(1);
33+
34+
// print_vec(nums, 0, nums.size());
35+
36+
int start = start_of_non(2, nums, 0);
37+
int res = 0;
38+
for(int i = start + 1 ; i <= nums.size() ; )
39+
if(i == nums.size() || nums[i] == 2){
40+
// print_vec(nums, start, i);
41+
res += count(nums, start, i);
42+
// cout << "res = " << res << endl;
43+
start = start_of_non(2, nums, i);
44+
i = start + 1;
45+
}
46+
else
47+
i ++;
48+
return res;
49+
}
50+
51+
private:
52+
int count(const vector<int>& nums, int l, int r){
53+
54+
int res = (1 + r - l) * (r - l) / 2;
55+
int start = start_of_non(1, nums, l);
56+
for(int i = start + 1 ; i <= r ; )
57+
if(i == r || nums[i] == 1){
58+
res -= (1 + i - start) * (i - start) / 2;
59+
start = start_of_non(1, nums, i);
60+
i = start + 1;
61+
}
62+
else
63+
i ++;
64+
return res;
65+
}
66+
67+
int start_of_non(int target, const vector<int>& nums, int start){
68+
for(int i = start ; i < nums.size() ; i ++)
69+
if(nums[i] != target)
70+
return i;
71+
return nums.size();
72+
}
73+
74+
void print_vec(const vector<int>& vec, int l, int r){
75+
for(int i = l ; i < r ; i ++)
76+
cout << vec[i] << " ";
77+
cout << endl;
78+
}
79+
};
80+
81+
int main() {
82+
83+
// vector<int> A1 = {2, 1, 4, 3};
84+
// cout << Solution().numSubarrayBoundedMax(A1, 2, 3) << endl;
85+
86+
vector<int> A2 = {2, 9, 2, 5, 6};
87+
cout << Solution().numSubarrayBoundedMax(A2, 2, 8) << endl;
88+
89+
return 0;
90+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/// Source : https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2018-03-05
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// Counting
12+
/// Using function count to calculate the subarray numbers of maximum <= bound
13+
///
14+
/// Time Complexity: O(n)
15+
/// Space Complexity: O(1)
16+
class Solution {
17+
public:
18+
int numSubarrayBoundedMax(vector<int>& A, int L, int R) {
19+
20+
return count(A, R) - count(A, L - 1);
21+
}
22+
23+
private:
24+
int count(const vector<int>& nums, int bound){
25+
26+
int res = 0;
27+
int cur = 0;
28+
for(int num: nums)
29+
if(num <= bound){
30+
cur ++;
31+
res += cur;
32+
}
33+
else
34+
cur = 0;
35+
return res;
36+
}
37+
};
38+
39+
int main() {
40+
41+
// vector<int> A1 = {2, 1, 4, 3};
42+
// cout << Solution().numSubarrayBoundedMax(A1, 2, 3) << endl;
43+
44+
vector<int> A2 = {2, 9, 2, 5, 6};
45+
cout << Solution().numSubarrayBoundedMax(A2, 2, 8) << endl;
46+
47+
return 0;
48+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
256256
| 792 | [Number of Matching Subsequences](https://leetcode.com/problems/number-of-matching-subsequences/description/) | [solution](https://leetcode.com/problems/number-of-matching-subsequences/solution/) | [C++](0792-Number-of-Matching-Subsequences/cpp-0792/) | | |
257257
| 793 | [Preimage Size of Factorial Zeroes Function](https://leetcode.com/problems/preimage-size-of-factorial-zeroes-function/description/) | [solution](https://leetcode.com/problems/preimage-size-of-factorial-zeroes-function/solution/) | [C++](0793-Preimage-Size-of-Factorial-Zeroes-Function/cpp-0793/) | | |
258258
| 794 | [Valid Tic-Tac-Toe State](https://leetcode.com/problems/valid-tic-tac-toe-state/description/) | [solution](https://leetcode.com/problems/valid-tic-tac-toe-state/solution/) | [C++](0794-Valid-Tic-Tac-Toe-State/cpp-0794/) | | |
259+
| 795 | [Number of Subarrays with Bounded Maximum](https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/description/) | [solution](https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/solution/)| [C++](0795-Number-of-Subarrays-with-Bounded-Maximum/cpp-0795/) | | |
259260

260261
---
261262

0 commit comments

Comments
 (0)