From 9fb559b7b90867bdc9e2bbf4ac3b728239a377ee Mon Sep 17 00:00:00 2001 From: 12345rams <132057037+12345rams@users.noreply.github.com> Date: Sun, 22 Oct 2023 10:02:06 +0530 Subject: [PATCH 1/5] Adding Solution ofmaximum-score-of-a-good-subarray.java --- Hard/maximum-score-of-a-good-subarray.java | 56 ++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Hard/maximum-score-of-a-good-subarray.java diff --git a/Hard/maximum-score-of-a-good-subarray.java b/Hard/maximum-score-of-a-good-subarray.java new file mode 100644 index 0000000..efb8a6b --- /dev/null +++ b/Hard/maximum-score-of-a-good-subarray.java @@ -0,0 +1,56 @@ +class Solution { + public int maximumScore(int[] nums, int k) { + int [] nsr = findNSR(nums); + int [] nsl = findNSL(nums); + + int score =0; + for(int i=0;i= k){ + score = Math.max(score,nums[i] *(r-l-1)); + } + } + return score; + } + +//find next smaller element on right + public int[] findNSR(int [] arr){ + int [] nsr = new int[arr.length]; + Stack s = new Stack<>(); + for(int i=arr.length-1;i>=0;i--){ + while(!s.isEmpty() && arr[i] <= arr[s.peek()] ){ + s.pop(); + } + if(s.isEmpty()){ + nsr[i] =arr.length; + } + else{ + nsr[i] = s.peek(); + } + s.push(i); + } + return nsr; + + } + //find next smaller element on left + + public int[] findNSL(int [] arr){ + int [] nsl = new int[arr.length]; + Stack s = new Stack<>(); + for(int i=0;i Date: Mon, 23 Oct 2023 08:14:00 +0530 Subject: [PATCH 2/5] Delete Hard/maximum-score-of-a-good-subarray.java --- Hard/maximum-score-of-a-good-subarray.java | 56 ---------------------- 1 file changed, 56 deletions(-) delete mode 100644 Hard/maximum-score-of-a-good-subarray.java diff --git a/Hard/maximum-score-of-a-good-subarray.java b/Hard/maximum-score-of-a-good-subarray.java deleted file mode 100644 index efb8a6b..0000000 --- a/Hard/maximum-score-of-a-good-subarray.java +++ /dev/null @@ -1,56 +0,0 @@ -class Solution { - public int maximumScore(int[] nums, int k) { - int [] nsr = findNSR(nums); - int [] nsl = findNSL(nums); - - int score =0; - for(int i=0;i= k){ - score = Math.max(score,nums[i] *(r-l-1)); - } - } - return score; - } - -//find next smaller element on right - public int[] findNSR(int [] arr){ - int [] nsr = new int[arr.length]; - Stack s = new Stack<>(); - for(int i=arr.length-1;i>=0;i--){ - while(!s.isEmpty() && arr[i] <= arr[s.peek()] ){ - s.pop(); - } - if(s.isEmpty()){ - nsr[i] =arr.length; - } - else{ - nsr[i] = s.peek(); - } - s.push(i); - } - return nsr; - - } - //find next smaller element on left - - public int[] findNSL(int [] arr){ - int [] nsl = new int[arr.length]; - Stack s = new Stack<>(); - for(int i=0;i Date: Mon, 23 Oct 2023 08:17:08 +0530 Subject: [PATCH 3/5] Solution Of Maximum_score_of_a_good_subarray MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In Format LeetCode/ ├── [DIFFICULTY]/ │ ├── [PROBLEM]/ │ │ ├── README │ │ ├── solution.java │ │ ├── solution.cpp --- .../README.md | 24 +++++++ .../solution.java | 64 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 Hard/maximum-score-of-a-good-subarray/README.md create mode 100644 Hard/maximum-score-of-a-good-subarray/solution.java diff --git a/Hard/maximum-score-of-a-good-subarray/README.md b/Hard/maximum-score-of-a-good-subarray/README.md new file mode 100644 index 0000000..9d096f7 --- /dev/null +++ b/Hard/maximum-score-of-a-good-subarray/README.md @@ -0,0 +1,24 @@ +This Is Hard Problem of LeetCode +Problem Number 1793 +LinK:https://leetcode.com/problems/maximum-score-of-a-good-subarray/ +Maximum Score of a Good Subarray +This Java code helps you find the maximum possible score of a good subarray in an array of integers. A good subarray is defined as one where i <= k <= j, and the score of a subarray is calculated as min(nums[i], nums[i+1], ..., nums[j]) * (j - i + 1). The code uses a specific algorithm to find the optimal subarray that maximizes this score. + +Usage +To use this code, follow these steps: + +Copy the code and paste it into your Java project. + +Implement the maxGoodSubarrayScore method, passing in the array of integers (nums) and the integer k as parameters. + +The method will return the maximum possible score for a good subarray according to the defined algorithm. + +Method Description +maxGoodSubarrayScore(int[] nums, int k) +This method calculates the maximum possible score for a good subarray in the given array nums using a specific algorithm. + +Parameters: + +nums: An array of integers. +k: The integer representing the pivot point. +Returns: The maximum possible score of a good subarray. \ No newline at end of file diff --git a/Hard/maximum-score-of-a-good-subarray/solution.java b/Hard/maximum-score-of-a-good-subarray/solution.java new file mode 100644 index 0000000..62c7d91 --- /dev/null +++ b/Hard/maximum-score-of-a-good-subarray/solution.java @@ -0,0 +1,64 @@ +import java.util.*; + +public class sol { + public static void main(String[] args) { + // enter your sol here + } +class Solution { + public int maximumScore(int[] nums, int k) { + int [] nsr = findNSR(nums); + int [] nsl = findNSL(nums); + + int score =0; + for(int i=0;i= k){ + score = Math.max(score,nums[i] *(r-l-1)); + } + } + return score; + } + +//find next smaller element on right + public int[] findNSR(int [] arr){ + int [] nsr = new int[arr.length]; + Stack s = new Stack<>(); + for(int i=arr.length-1;i>=0;i--){ + while(!s.isEmpty() && arr[i] <= arr[s.peek()] ){ + s.pop(); + } + if(s.isEmpty()){ + nsr[i] =arr.length; + } + else{ + nsr[i] = s.peek(); + } + s.push(i); + } + return nsr; + + } + //find next smaller element on left + + public int[] findNSL(int [] arr){ + int [] nsl = new int[arr.length]; + Stack s = new Stack<>(); + for(int i=0;i Date: Tue, 24 Oct 2023 08:17:10 +0800 Subject: [PATCH 4/5] Update and rename README.md to README.md --- .../README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename Hard/{maximum-score-of-a-good-subarray => 1793. Maximum Score of a Good Subarray}/README.md (93%) diff --git a/Hard/maximum-score-of-a-good-subarray/README.md b/Hard/1793. Maximum Score of a Good Subarray/README.md similarity index 93% rename from Hard/maximum-score-of-a-good-subarray/README.md rename to Hard/1793. Maximum Score of a Good Subarray/README.md index 9d096f7..dacc74c 100644 --- a/Hard/maximum-score-of-a-good-subarray/README.md +++ b/Hard/1793. Maximum Score of a Good Subarray/README.md @@ -21,4 +21,4 @@ Parameters: nums: An array of integers. k: The integer representing the pivot point. -Returns: The maximum possible score of a good subarray. \ No newline at end of file +Returns: The maximum possible score of a good subarray. From 198716dd89b7c88e5cf1ffe8df44b07424a1e2a6 Mon Sep 17 00:00:00 2001 From: Jarrian Gojar Date: Tue, 24 Oct 2023 08:18:11 +0800 Subject: [PATCH 5/5] Update and rename solution.java to Solution.java --- .../Solution.java} | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) rename Hard/{maximum-score-of-a-good-subarray/solution.java => 1793. Maximum Score of a Good Subarray/Solution.java} (83%) diff --git a/Hard/maximum-score-of-a-good-subarray/solution.java b/Hard/1793. Maximum Score of a Good Subarray/Solution.java similarity index 83% rename from Hard/maximum-score-of-a-good-subarray/solution.java rename to Hard/1793. Maximum Score of a Good Subarray/Solution.java index 62c7d91..3de1b49 100644 --- a/Hard/maximum-score-of-a-good-subarray/solution.java +++ b/Hard/1793. Maximum Score of a Good Subarray/Solution.java @@ -1,9 +1,4 @@ -import java.util.*; -public class sol { - public static void main(String[] args) { - // enter your sol here - } class Solution { public int maximumScore(int[] nums, int k) { int [] nsr = findNSR(nums); @@ -20,7 +15,7 @@ public int maximumScore(int[] nums, int k) { return score; } -//find next smaller element on right + //find next smaller element on right public int[] findNSR(int [] arr){ int [] nsr = new int[arr.length]; Stack s = new Stack<>(); @@ -37,10 +32,9 @@ public int[] findNSR(int [] arr){ s.push(i); } return nsr; - } - //find next smaller element on left - + + //find next smaller element on left public int[] findNSL(int [] arr){ int [] nsl = new int[arr.length]; Stack s = new Stack<>(); @@ -57,8 +51,5 @@ public int[] findNSL(int [] arr){ s.push(i); } return nsl; - } } - -} \ No newline at end of file