classSolution: defnumSubarrayBoundedMax(self, nums: List[int], left: int, right: int) -> int: res = 0 last2 = last1 = -1 for i, x inenumerate(nums): if left <= x <= right: last1 = i elif x > right: last2 = i last1 = -1 if last1 != -1: res += last1 - last2 return res
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { public: intnumSubarrayBoundedMax(vector<int>& nums, int left, int right){ int res = 0, last2 = -1, last1 = -1; for (int i = 0; i < nums.size(); i++) { if (nums[i] >= left && nums[i] <= right) { last1 = i; } elseif (nums[i] > right) { last2 = i; last1 = -1; } if (last1 != -1) { res += last1 - last2; } } return res; } };
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { publicintnumSubarrayBoundedMax(int[] nums, int left, int right) { intres=0, last2 = -1, last1 = -1; for (inti=0; i < nums.length; i++) { if (nums[i] >= left && nums[i] <= right) { last1 = i; } elseif (nums[i] > right) { last2 = i; last1 = -1; } if (last1 != -1) { res += last1 - last2; } } return res; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
publicclassSolution { publicintNumSubarrayBoundedMax(int[] nums, int left, int right) { int res = 0, last2 = -1, last1 = -1; for (int i = 0; i < nums.Length; i++) { if (nums[i] >= left && nums[i] <= right) { last1 = i; } elseif (nums[i] > right) { last2 = i; last1 = -1; } if (last1 != -1) { res += last1 - last2; } } return res; } }
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
funcnumSubarrayBoundedMax(nums []int, left int, right int) (res int) { last2, last1 := -1, -1 for i, x := range nums { if left <= x && x <= right { last1 = i } elseif x > right { last2 = i last1 = -1 } if last1 != -1 { res += last1 - last2 } } return }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
var numSubarrayBoundedMax = function(nums, left, right) { let res = 0, last2 = -1, last1 = -1; for (let i = 0; i < nums.length; i++) { if (nums[i] >= left && nums[i] <= right) { last1 = i; } elseif (nums[i] > right) { last2 = i; last1 = -1; } if (last1 !== -1) { res += last1 - last2; } } return res; };
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
intnumSubarrayBoundedMax(int* nums, int numsSize, int left, int right) { int res = 0, last2 = -1, last1 = -1; for (int i = 0; i < numsSize; i++) { if (nums[i] >= left && nums[i] <= right) { last1 = i; } elseif (nums[i] > right) { last2 = i; last1 = -1; } if (last1 != -1) { res += last1 - last2; } } return res; }
classSolution: defnumSubarrayBoundedMax(self, nums: List[int], left: int, right: int) -> int: defcount(lower: int) -> int: res = cur = 0 for x in nums: if x <= lower: cur += 1 else: cur = 0 res += cur return res return count(right) - count(left - 1)
[sol2-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution { public: intnumSubarrayBoundedMax(vector<int>& nums, int left, int right){ returncount(nums, right) - count(nums, left - 1); }
intcount(vector<int>& nums, int lower){ int res = 0, cur = 0; for (auto x : nums) { cur = x <= lower ? cur + 1 : 0; res += cur; } return res; } };
[sol2-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution { publicintnumSubarrayBoundedMax(int[] nums, int left, int right) { return count(nums, right) - count(nums, left - 1); }
publicintcount(int[] nums, int lower) { intres=0, cur = 0; for (int x : nums) { cur = x <= lower ? cur + 1 : 0; res += cur; } return res; } }
[sol2-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
publicclassSolution { publicintNumSubarrayBoundedMax(int[] nums, int left, int right) { return Count(nums, right) - Count(nums, left - 1); }
publicintCount(int[] nums, int lower) { int res = 0, cur = 0; foreach (int x in nums) { cur = x <= lower ? cur + 1 : 0; res += cur; } return res; } }
[sol2-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
funcnumSubarrayBoundedMax(nums []int, left int, right int)int { count := func(lower int) (res int) { cur := 0 for _, x := range nums { if x <= lower { cur++ } else { cur = 0 } res += cur } return } return count(right) - count(left-1) }
[sol2-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12
var numSubarrayBoundedMax = function(nums, left, right) { returncount(nums, right) - count(nums, left - 1); }
constcount = (nums, lower) => { let res = 0, cur = 0; for (const x of nums) { cur = x <= lower ? cur + 1 : 0; res += cur; } return res; };
[sol2-C]
1 2 3 4 5 6 7 8 9 10 11 12
intcount(constint *nums, int numsSize, int lower) { int res = 0, cur = 0; for (int i = 0; i < numsSize; i++) { cur = nums[i] <= lower ? cur + 1 : 0; res += cur; } return res; }
intnumSubarrayBoundedMax(int* nums, int numsSize, int left, int right) { return count(nums, numsSize, right) - count(nums, numsSize, left - 1); }