aboutsummaryrefslogtreecommitdiff
path: root/other/max_score_good_subarr.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'other/max_score_good_subarr.cpp')
-rw-r--r--other/max_score_good_subarr.cpp36
1 files changed, 36 insertions, 0 deletions
diff --git a/other/max_score_good_subarr.cpp b/other/max_score_good_subarr.cpp
new file mode 100644
index 0000000..3c7bc89
--- /dev/null
+++ b/other/max_score_good_subarr.cpp
@@ -0,0 +1,36 @@
+// 1793. Maximum Score of a Good Subarray
+#include "leetcode.h"
+
+/*
+ * given an array of integers 'nums' (0-indexed) and an integer 'k'. the score
+ * of a subarray '(i, j)' is defined as 'min(nums[i], nums[i + 1], ..., nums[j])
+ * * (j - i + 1)'. a good subarray is a subarray where 'i <= k <= j'. return the
+ * maximum possible score of a good subarray.
+ */
+
+class Solution {
+public:
+ int maximumScore(vector<int> &nums, int k) {
+ nums.push_back(0);
+ stack<int> st;
+ int n = nums.size(), ans = 0;
+ for (int i = 0; i < n; i++) {
+ while (!st.empty() && nums[st.top()] >= nums[i]) {
+ int height = nums[st.top()];
+ st.pop();
+ int left = st.empty() ? -1 : st.top();
+ if (k < i && k > left)
+ ans = max(ans, height * (i - left - 1));
+ }
+ st.push(i);
+ }
+ return ans;
+ }
+};
+
+int main() {
+ Solution obj;
+ vector<int> n1 = {1, 4, 3, 7, 4, 5}, n2 = {5, 5, 4, 5, 4, 1, 1, 1};
+ printf("%d\n", obj.maximumScore(n1, 3)); // expect: 15
+ printf("%d\n", obj.maximumScore(n2, 0)); // expect: 20
+}