Skip to content

Commit 389b60e

Browse files
committed
Adds some solutions and fixes a naming issue
1 parent 5ffc6ba commit 389b60e

File tree

10 files changed

+218
-25
lines changed

10 files changed

+218
-25
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Find the Score of All Prefixes of an Array
2+
3+
**Question ID**: 2640
4+
**Language**: C++
5+
**Status**: Accepted
6+
**Runtime**: 0 ms
7+
**Memory**: 58.91 MB
8+
9+
## Solution Code
10+
```cpp
11+
//https://leetcode.com/problems/find-the-score-of-all-prefixes-of-an-array/
12+
class Solution {
13+
public:
14+
vector<long long> findPrefixScore(vector<int>& nums) {
15+
int currMax = nums[0];
16+
vector<long long> ans(nums.size());
17+
18+
for (int i = 0; i < nums.size(); i++)
19+
{
20+
currMax = max(currMax, nums[i]);
21+
22+
ans[i] = nums[i] + currMax;
23+
24+
if (i)
25+
ans[i] += ans[i - 1];
26+
}
27+
28+
return ans;
29+
}
30+
};
31+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": 1478720447,
3+
"question_id": 2640,
4+
"lang": "cpp",
5+
"lang_name": "C++",
6+
"timestamp": 1734195610,
7+
"status": 10,
8+
"status_display": "Accepted",
9+
"runtime": "0 ms",
10+
"url": "https://leetcode.com/submissions/detail/1478720447/",
11+
"is_pending": "Not Pending",
12+
"title": "Find the Score of All Prefixes of an Array",
13+
"memory": "58.91 MB",
14+
"title_slug": "find-the-score-of-all-prefixes-of-an-array",
15+
"has_notes": false,
16+
"flag_type": 1
17+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
//https://leetcode.com/problems/find-the-score-of-all-prefixes-of-an-array/
5+
class Solution
6+
{
7+
public:
8+
vector<long long> findPrefixScore(vector<int> &nums)
9+
{
10+
int currMax = nums[0];
11+
vector<long long> ans(nums.size());
12+
13+
for (int i = 0; i < nums.size(); i++)
14+
{
15+
currMax = max(currMax, nums[i]);
16+
17+
ans[i] = nums[i] + currMax;
18+
19+
if (i)
20+
ans[i] += ans[i - 1];
21+
}
22+
23+
return ans;
24+
}
25+
};

solutions_leetCode/2640_maximum-number-of-integers-to-choose-from-a-range-i/solution.java

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Continuous Subarrays
2+
3+
**Question ID**: 2762
4+
**Language**: C++
5+
**Status**: Accepted
6+
**Runtime**: 92 ms
7+
**Memory**: 111.86 MB
8+
9+
## Solution Code
10+
```cpp
11+
// https://leetcode.com/problems/continuous-subarrays
12+
class Solution {
13+
public:
14+
long long continuousSubarrays(vector<int> &nums)
15+
{
16+
17+
int lo = 0, hi = 0;
18+
19+
int n = nums.size();
20+
int currMax = nums[0], currMin = nums[0];
21+
22+
long long count = 0,len;
23+
24+
for (hi = 0; hi < n; hi++)
25+
{
26+
currMax = max(currMax, nums[hi]);
27+
currMin = min(currMin, nums[hi]);
28+
29+
if (currMax - currMin > 2)
30+
{
31+
32+
len = hi - lo;
33+
34+
count += (len * (len + 1) / 2);
35+
36+
currMax = currMin = nums[hi];
37+
38+
lo = hi;
39+
40+
while (lo > 0 && abs(nums[hi] - nums[lo-1]) <= 2)
41+
{
42+
lo--;
43+
currMax = max(currMax, nums[lo]);
44+
currMin = min(currMin, nums[lo]);
45+
}
46+
47+
if (lo < hi)
48+
{
49+
len = hi - lo;
50+
51+
count -= (len * (len + 1) / 2);
52+
}
53+
}
54+
}
55+
len = hi - lo;
56+
57+
count += (len * (len + 1) / 2);
58+
59+
return count;
60+
61+
}
62+
};
63+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": 1478711307,
3+
"question_id": 2762,
4+
"lang": "cpp",
5+
"lang_name": "C++",
6+
"timestamp": 1734193894,
7+
"status": 10,
8+
"status_display": "Accepted",
9+
"runtime": "92 ms",
10+
"url": "/submissions/detail/1478711307/",
11+
"is_pending": "Not Pending",
12+
"title": "Continuous Subarrays",
13+
"memory": "111.86 MB",
14+
"title_slug": "continuous-subarrays",
15+
"has_notes": false,
16+
"flag_type": 1
17+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
// https://leetcode.com/problems/continuous-subarrays
4+
class Solution
5+
{
6+
public:
7+
long long continuousSubarrays(vector<int> &nums)
8+
{
9+
10+
int lo = 0, hi = 0;
11+
12+
int n = nums.size();
13+
int currMax = nums[0], currMin = nums[0], len;
14+
15+
long long count = 0;
16+
17+
for (hi = 0; hi < n; hi++)
18+
{
19+
currMax = max(currMax, nums[hi]);
20+
currMin = min(currMin, nums[hi]);
21+
22+
if (currMax - currMin > 2)
23+
{
24+
25+
len = hi - lo;
26+
27+
count += (len * (len + 1) / 2);
28+
29+
currMax = currMin = nums[hi];
30+
31+
lo = hi;
32+
33+
while (lo > 0 && abs(nums[hi] - nums[lo]) <= 2)
34+
{
35+
lo--;
36+
currMax = max(currMax, nums[lo]);
37+
currMin = min(currMin, nums[lo]);
38+
}
39+
40+
if (left < right)
41+
{
42+
len = hi - lo;
43+
44+
count -= (len * (len + 1) / 2);
45+
}
46+
}
47+
}
48+
len = hi - lo;
49+
50+
count += (len * (len + 1) / 2);
51+
52+
return count;
53+
54+
}
55+
};
56+
57+
int main()
58+
{
59+
Solution obj;
60+
61+
vector<int> a = {5, 4, 2, 4};
62+
63+
obj.continuousSubarrays(a);
64+
return 0;
65+
}

0 commit comments

Comments
 (0)