TechTrajectory

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.

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

1456.MaxVowelsSubstr.py class Solution: def maxVowels(self, s: str, k: int) -> int: vowels = set('aeiou') # 定義母音集合 count = sum(1 for char in s[:k] if char in vowels) # 使用列表生成式來計算母音數量 # 初始化最大母音數量為目前的count max_count = count # 滑動窗口 for i in range(k, len(s)): # 如果左邊的字母是母音,則減少count if s[i - k] in vowels: count -= 1 # 如果右邊的字母是母音,則增加count if s[i] in vowels: count += 1 # 更新最大母音數量 max_count = max(max_count, count) return max_count if __name__ == '__main__': sol = Solution() assert sol.

LeetCode 735. Asteroid Collision

lc735AsteroidCollision_test.go package lc import ( "reflect" "testing" ) // asteroidCollision 處理小行星碰撞的情況並返回碰撞後的小行星狀態 func asteroidCollision(asteroids []int) []int { // stack 用於儲存目前還未發生碰撞的小行星 var stack []int // 遍歷每個小行星 for _, a := range asteroids { // 若堆疊為空或小行星向右移動(代表其不會與堆疊內的小行星碰撞) if len(stack) == 0 || a > 0 { stack = append(stack, a) } else { // 若當前小行星向左移動,則需要檢查其是否會與堆疊頂部的小行星碰撞 for len(stack) > 0 && stack[len(stack)-1] > 0 && absIntG(stack[len(stack)-1]) < absIntG(a) { // 若堆疊頂部的小行星會被破壞,則移除它 stack = stack[:len(stack)-1] } // 若堆疊為空或堆疊頂部的小行星向左移動(代表其不會與當前小行星碰撞) if len(stack) == 0 || stack[len(stack)-1] < 0 { stack = append(stack, a) } else if stack[len(stack)-1] == -a { // 若堆疊頂部的小行星與當前小行星大小相同,兩者都會被破壞 stack = stack[:len(stack)-1] } } } // 返回碰撞後的小行星狀態 return stack } func TestAsteroidCollision(t *testing.

LeetCode 2390. Removing Stars From a String

2390.RemovingStarsFromaString.py class Solution: def removeStars(self, s: str) -> str: stack = [] for char in s: # 遍歷字串s if char == '*': # 如果字符是星號 if stack: # 且堆疊不為空 stack.pop() # 就彈出堆疊的頂部元素 else: # 如果字符不是星號,就加入堆疊 stack.append(char) return ''.join(stack) # 將堆疊中的元素組成答案字串 if __name__ == '__main__': sol = Solution() assert sol.removeStars("leet**cod*e") == "lecoe" assert sol.removeStars("erase*****") == "" lc2390RemovingStarsFromaString_test.go package lc import "testing" func removeStars(s string) string { stack := []rune{} for _, char := range s { if char == '*' { if len(stack) > 0 { stack = stack[:len(stack)-1] // Pop the top element } } else { stack = append(stack, char) } } return string(stack) } func TestRemoveStars(t *testing.

LeetCode 11. Container With Most Water

11.ContainerWithMostWater.py from typing import List class Solution: def maxArea(self, height: List[int]) -> int: maxArea = 0 left, right = 0, len(height) - 1 while left < right: currentArea = (right - left) * min(height[left], height[right]) maxArea = max(currentArea, maxArea) if height[left] < height[right]: left += 1 else: right -= 1 return maxArea if __name__ == '__main__': sol = Solution() assert sol.maxArea(height=[1, 8, 6, 2, 5, 4, 8, 3, 7]) == 49 assert sol.

LeetCode 1679. Max Number of K-Sum Pairs

lc1679MaxNumberofK-SumPairsBySort_test.go package lc import ( "sort" "testing" ) func maxOperationsSort(nums []int, k int) int { sort.Ints(nums) // 先排序 left, right := 0, len(nums)-1 // 使用左右指針 operations := 0 for left < right { currentSum := nums[left] + nums[right] // 如果當前和等於k,則找到一對,左右指針都要移動 if currentSum == k { operations++ left++ right-- } else if currentSum < k { // 如果當前和小於k,則左指針向右移動,使得和增加 left++ } else { // 如果當前和大於k,則右指針向左移動,使得和減少 right-- } } return operations } func TestMaxOperationsSort(t *testing.

Analytical Writing

Analytical Writing English 中文 “Social media isolates people more than it connects them.” “社交媒體使人更加孤立,而不是讓人更加聯結。” While social media was initially designed to connect people, to some extent, it has indeed made many feel isolated. Firstly, the time people spend on social media reduces face-to-face interaction, which is foundational to building deep interpersonal relationships. Secondly, the highlights of people’s lives often showcased on social media can make some users feel that their lives are inferior to others, leading to feelings of isolation.