Skip to content

Latest commit

 

History

History
68 lines (54 loc) · 1.88 KB

1456. Maximum Number of Vowels in a Substring of Given Length.md

File metadata and controls

68 lines (54 loc) · 1.88 KB

1456. Maximum Number of Vowels in a Substring of Given Length

https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/description/

solution

class Solution:
    def maxVowels(self, s: str, k: int) -> int:
        vowels = ['a', 'o', 'e', 'i', 'u']
        temp = 0
        res = 0

        for i in range(len(s)):
            if i >= k:
                if s[i-k] in vowels:
                    temp -= 1
            if s[i] in vowels:
                temp += 1
            res = max(res, temp)
        return res

follow up

1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold

  • 关于分别处理小于,等于和大于的情况
class Solution:
    def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int:
        res = 0
        l = 0
        total = 0
        for r, num in enumerate(arr):
            total += num
            if r - l + 1 > k:
                total -= arr[l]
                l += 1

            if r - l + 1 == k:
                if total >= threshold * k:
                    res += 1
        return res

1838. Frequency of the Most Frequent Element

  • 判断条件的灵活调整
class Solution:
    def maxFrequency(self, nums: List[int], k: int) -> int:
        l = 0
        total = 0
        res = 0
        nums.sort()
        for r, num in enumerate(nums):
            total += num

            while total + k < num * (r - l + 1):
                total -= nums[l]
                l += 1

            res = max(res, r - l + 1)
        return res