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 1207. Unique Number of Occurrences

1207.UniqueNumberofOccurrences.py from typing import List class Solution: def uniqueOccurrences(self, arr: List[int]) -> bool: count_dic = {} for num in arr: count_dic[num] = count_dic.get(num, 0) + 1 count_set = set(count_dic.values()) return len(count_set) == len(count_dic.values()) if __name__ == '__main__': sol = Solution() assert sol.uniqueOccurrences([1, 2, 2, 1, 1, 3]) == True assert sol.uniqueOccurrences([1, 2]) == False assert sol.uniqueOccurrences([-3, 0, 1, -3, 1, 1, 1, -3, 10, 0]) == True lc1207UniqueNumberofOccurrences_test.go package lc import ( "testing" ) func uniqueOccurrences(arr []int) bool { // 创建一个 map 来存储每个元素出现的次数 countMap := make(map[int]int) for _, num := range arr { countMap[num]++ } // 使用另一个 map 来检查各个出现次数的唯一性 uniqueCountMap := make(map[int]bool) for _, count := range countMap { if uniqueCountMap[count] { return false // 如果已经存在,说明出现次数不唯一,返回 false } uniqueCountMap[count] = true } // 所有出现次数都是唯一的 return true } func TestUniqueOccurrences(t *testing.

LeetCode 1732. Find the Highest Altitude

643.MaximumAverageSubarrayI.py from typing import List class Solution: def largestAltitude(self, gain: List[int]) -> int: current_altitude, max_altitude = 0, 0 # 初始化最高海拔和当前海拔 for g in gain: current_altitude += g # 使用 Prefix Sum 更新当前海拔 max_altitude = max(max_altitude, current_altitude) # 更新最高海拔 return max_altitude if __name__ == '__main__': sol = Solution() assert sol.largestAltitude([-5, 1, 5, 0, -7]) == 1 assert sol.largestAltitude([-4, -3, -2, -1, 4, 3, 2]) == 0 lc643MaximumAverageSubarrayI_test.go package lc import ( "testing" ) func largestAltitude(gain []int) int { var currentAltitude, maxAltitude int for _, g := range gain { currentAltitude += g maxAltitude = max(currentAltitude, maxAltitude) } return maxAltitude } func TestLargestAltitude(t *testing.

LeetCode 643. Maximum Average Subarray I

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.

LeetCode 345. Reverse Vowels of a String

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 "".

Algorithms Roadmap

Roadmap 此流程圖描繪了從基礎技術如陣列和雜湊,逐步深入到更專業、高階的技術領域,如數學和幾何等技術的學習路徑。 陣列和雜湊 (Arrays and Hashing): 雙指針 (Two Pointers) 堆疊 (Stack) 雙指針 (Two Pointers) 技術可以再進一步細分為: 二分搜尋 (Binary Search) 滑動窗口 (Sliding Window) 鏈結列表 (Linked List) 樹 (Trees): 字典樹 (Tries) 堆積或優先隊列 (Heap or Priority Queue) 回溯 (Backtracking) 回溯 (Backtracking): 圖 (Graphs) 一維動態規劃 (1-space Dynamic Programming) 堆積或優先隊列 (Heap or Priority Queue): 區間 (Intervals) 貪婪 (Greedy) 進階圖形 (Advanced Graphs) 一維動態規劃 (1-space Dynamic Programming): 二維間動態規劃 (2-space Dynamic Programming) 位元操作 (Bit Manipulation) 數學和幾何 (Math and Geometry) reference: neetcode.

HUGO Quick start

HUGO Quick start hugo new site quickstart cd quickstart git init git submodule add https://github.com/dillonzq/LoveIt.git themes/LoveIt