File tree Expand file tree Collapse file tree 5 files changed +175
-0
lines changed Expand file tree Collapse file tree 5 files changed +175
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < climits>
2
+ #include < vector>
3
+ #include < algorithm>
4
+ #include < iostream>
5
+ #include < unordered_map>
6
+
7
+ using namespace std ;
8
+
9
+ class Solution
10
+ {
11
+ public:
12
+ int minStartValue (vector<int > &nums) {
13
+ int n = nums.size ();
14
+ int mn = INT_MAX;
15
+ int sum = 0 ;
16
+ for (int i = 0 ; i < n; ++i) {
17
+ sum += nums[i];
18
+ mn = min (sum, mn);
19
+ }
20
+ return 1 - mn < 1 ? 1 : 1 - mn;
21
+ }
22
+ };
Original file line number Diff line number Diff line change
1
+ #include < vector>
2
+ #include < algorithm>
3
+ #include < climits>
4
+ #include < iostream>
5
+ #include < unordered_map>
6
+
7
+ using namespace std ;
8
+
9
+ class Solution
10
+ {
11
+ public:
12
+ struct range
13
+ {
14
+ int value;
15
+ int start;
16
+ mutable int end;
17
+ mutable int sum;
18
+ bool operator <(const range &b) {
19
+ return this ->sum > b.sum ;
20
+ }
21
+
22
+ bool operator ==(int b) {
23
+ return this ->value == b;
24
+ }
25
+ };
26
+
27
+ int maximumBeauty (vector<int > &flowers) {
28
+ int n = flowers.size ();
29
+ vector<range> ranges;
30
+ vector<int > sum (n + 1 , 0 );
31
+
32
+ for (int i = 0 ; i < n; ++i) {
33
+ int beauty = flowers[i];
34
+ sum[i + 1 ] = sum[i] + beauty;
35
+ auto r = find (ranges.begin (), ranges.end (), beauty);
36
+ if (r == ranges.end ()) {
37
+ ranges.push_back ({beauty, i, -1 });
38
+ } else {
39
+ r->end = i;
40
+ r->sum = sum[i + 1 ] - sum[r->start ];
41
+ }
42
+ }
43
+
44
+ sort (ranges.begin (), ranges.end ());
45
+
46
+ int mx = INT_MIN;
47
+ for (auto r : ranges) {
48
+ if (r.end == -1 )
49
+ continue ;
50
+ int local = r.sum ;
51
+ for (auto i = r.start + 1 ; i <= r.end - 1 ; ++i) {
52
+ if (flowers[i] < 0 ) {
53
+ local = local - flowers[i];
54
+ }
55
+ }
56
+ mx = max (local, mx);
57
+ }
58
+
59
+ return mx;
60
+ }
61
+ };
62
+
63
+ int main (int argc, char const *argv[]) {
64
+ Solution s;
65
+ vector<int > flowers = {-1 , -2 , 0 , -1 };
66
+ cout << s.maximumBeauty (flowers) << endl;
67
+ return 0 ;
68
+ }
Original file line number Diff line number Diff line change
1
+ #include < vector>
2
+ #include < algorithm>
3
+ #include < iostream>
4
+ #include < unordered_map>
5
+
6
+ using namespace std ;
7
+
8
+ class Solution
9
+ {
10
+ public:
11
+ int findMiddleIndex (vector<int > &nums) {
12
+ int n = nums.size ();
13
+ vector<int > sum (n + 1 , 0 );
14
+ for (int i = 0 ; i < n; ++i) {
15
+ sum[i + 1 ] = sum[i] + nums[i];
16
+ }
17
+
18
+ for (int i = 0 ; i < n; ++i) {
19
+ if (sum[i] == sum[n] - sum[i + 1 ])
20
+ return i;
21
+ }
22
+
23
+ return -1 ;
24
+ }
25
+ };
Original file line number Diff line number Diff line change
1
+ #include < vector>
2
+ #include < algorithm>
3
+ #include < iostream>
4
+ #include < unordered_map>
5
+
6
+ using namespace std ;
7
+
8
+ class Solution
9
+ {
10
+ public:
11
+ long long numberOfSubstrings (string s) {
12
+ int n = s.size ();
13
+ long long ret = 0 ;
14
+ long long hash[26 ] = {0 };
15
+
16
+ for (int i = 0 ; i < n; ++i) {
17
+ ret += hash[s[i] - ' a' ];
18
+ hash[s[i] - ' a' ]++;
19
+ }
20
+
21
+ return ret + n;
22
+ }
23
+ };
Original file line number Diff line number Diff line change @@ -60,3 +60,40 @@ Simple prefix XOR;
60
60
- 2D prefix sum & subarray sum to target problem
61
61
- Time complexity $O(M^2N)$
62
62
63
+ ## 1788. Maximize the Beauty of the Garden
64
+
65
+ - Negative flowers affect the total beauty
66
+ - Iterate over valid pairs
67
+
68
+ ## 2083. Substrings That Begin and End With the Same Letter
69
+
70
+
71
+ ### Intuition
72
+
73
+ - Similar to [ 560. Subarray Sum Equals K] ( https://leetcode.com/problems/subarray-sum-equals-k/ )
74
+
75
+
76
+ ### Complexity
77
+
78
+ - Time complexity: $O(n)$
79
+ - Space complexity: $O(1)$
80
+
81
+ ### Code
82
+
83
+ ``` c++
84
+ class Solution {
85
+ public:
86
+ long long numberOfSubstrings(string s) {
87
+ int n = s.size();
88
+ long long ret = 0;
89
+ long long hash[ 26] = {0};
90
+
91
+ for(int i = 0;i < n;++i){
92
+ ret += hash[ s[ i] - 'a'] ;
93
+ hash[ s[ i] - 'a'] ++;
94
+ }
95
+
96
+ return ret + n;
97
+ }
98
+ };
99
+ ```
You can’t perform that action at this time.
0 commit comments