Explore the dynamic landscape of technology with TechTrajectory. This blog offers a unique perspective on the ever-evolving tech world, shedding light on the intricacies of system design and the latest innovations.
643.MaximumAverageSubarrayI.py from typing import List class Solution: def findMaxAverage(self, nums: List[int], k: int) -> float: curr_sum = 0 max_sum = float('-inf') for i, num in enumerate(nums): curr_sum += num if i > k - 1: curr_sum -= nums[i - k] if i >= k - 1: max_sum = max(max_sum, curr_sum) return max_sum / k if __name__ == '__main__': sol = Solution() assert sol.findMaxAverage(nums=[1, 12, -5, -6, 50, 3], k=4) == 12.
345. Reverse Vowels of a String.py class Solution: def reverseVowels(self, s: str) -> str: lst = list(s) n = len(s) vowels = "aeiouAEIOU" l, r = 0, n - 1 while l < r: if l < r and lst[l] not in vowels: l += 1 elif r > l and lst[r] not in vowels: r -= 1 else: lst[l], lst[r] = lst[r], lst[l] # swapping the vowels l += 1 r -= 1 return "".