Simon Lai's Blog
  • 首頁
  • 歸檔
  • 分類
  • 標籤
  • 關於

3488. Closest Equal Element Queries - Medium

前往題目 想法 紀錄所有數字的index 根據query計算每個數字距離鄰居的最短距離 思路寫完先瘋一半,要一直切換數字與index,腦袋快炸掉,這是這週周賽的第二題 紀錄所有數字的index 找出當前query的index在query的實際數字的index list中的位置(這樣才能更快判斷鄰居,因為index list中鄰居都是相同數字,只紀錄index) 每個query數字計算其左邊鄰
2025-03-16
Leetcode > Medium
#Leetcode #心得 #Array #Binary Search

2594. Minimum Time to Repair Cars - Medium

前往題目 想法 猜答案,二元搜尋 思路 二元搜尋分鐘數,範圍是最小可能到最大可能 計算以當前分鐘數來看可以修多少台車 比目標少,就增加分鐘;比目標多就減少分鐘數 最後收斂的位置就是答案 Codeclass Solution { public long repairCars(int[] ranks, int cars) { long l = 0, r
2025-03-16
Leetcode > Medium
#Leetcode #心得 #Array #Binary Search

什麼是Model Context Protocol(MCP),串接AI與工具之間的橋樑

MCP 是一種創新的開放標準,旨在簡化 AI 系統(特別是大型語言模型,LLMs)與外部工具、數據源和服務的互動方式。MCP 最初由 Anthropic 開發,目的是解決傳統 API 整合中的碎片化問題以及資訊不互通現象,這些問題阻礙了 AI 獲取所需的數據以產生相關且準確的回應。通過提供一個通用協議,MCP 簡化了 AI 整合,使其比傳統的 API 方法更高效、可擴展且安全。 什麼是 MCP?M
2025-03-14
AI
#API #Model-Context-Protocol #AI

2523. Closest Prime Numbers in Range - Medium

前往題目 想法 先用昨天的Sieve of Eratosthenes找出範圍內所有質數 比較各質數區間就可以回傳結果 思路 Sieve of Eratosthenes找出範圍內所有質數 比較質數區間的大小找出最小值 Codeclass Solution { public int[] closestPrimes(int left, int right) {
2025-03-14
Leetcode > Medium
#Leetcode #心得 #Math #Number Theory

204. Count Primes - Medium

前往題目 回歸leetcode,四個月的當兵猶如一場夢,自由真好 想法 略過2, 3, 5, 7的倍數 思路Sieve of Eratosthenes Algorithm 從第一個質數開始(2),並標記2的所有倍數(<n)為非質數 往下一個未被標記的數字,重複步驟一 直到當前數字的平方大於n,後面的數字直接看標記為質數或非質數更新結果即可 Code class Solution &
2025-03-13
Leetcode > Medium
#Leetcode #心得 #Array #Math #Number Theory #Enumeration

96. Unique Binary Search Trees - Medium

前往題目 想法 找規律? 思路 DP 0和1個節點是base case 嘗試把每個點當作root,左樹和右樹再各自當成root直到base case就可以推導出當前n總共有幾種樹 例如n=2總共是兩種可能的樹[0,1], [1,0],n=3是五種,因為[0,2], [1,1], [2, 0] => 2 + 1 + 2 = 5,n=4是14種,因為[0,3], [1,2], [2,1],
2024-11-16
Leetcode > Medium
#Leetcode #心得 #Binary Search Tree #Binary Tree #Math #Dynamic Programming #Tree

1376. Time Needed to Inform All Employees - Medium

前往題目 想法 DFS找最長路徑 思路 BFS 先建立manager -> employees的map 然後就是一般的BFS,每個employee都要加上其manager的時間,這樣才是通知到那位employee的完整時間 每次到新的僱員時更新結果 Code class Solution { public int numOfMinutes(int n, int he
2024-11-16
Leetcode > Medium
#Leetcode #心得 #Depth-First Search #Breadth-First Search #Tree

662. Maximum Width of Binary Tree - Medium

前往題目 想法 BFS 思路紀錄index就可以完美解決 一般的BFS 每個點除了把自身加入到queue,還要加入節點的index,表示在這層當中的index 利用節點的index就可以在最後算出這層可以多長 循環每層到最後取最大值 Code class Solution { public int widthOfBinaryTree(TreeNode root) 
2024-11-11
Leetcode > Medium
#Leetcode #心得 #Binary Tree #Depth-First Search #Breadth-First Search #Tree

106. Construct Binary Tree from Inorder and Postorder Traversal - Medium

前往題目 想法 postorder最後面一定是root 思路用root去定位,就可以得知左右子樹 dfs inorder和postorder都使用雙指針 當左指針超過右指針時回傳null,代表沒有子節點 取得根節點的值,以及其在inorder中的index 然後就可以建立父節點以及其左右子節點並遞迴呼叫 postorder只是拿來找根節點的 Code網友解答 class Solution
2024-11-07
Leetcode > Medium
#Leetcode #心得 #Binary Tree #Array #Hash Table #Tree #Divide and Conquer

958. Check Completeness of a Binary Tree - Medium

前往題目 想法 BFS檢查左邊node是否為null 思路 BFS 紀錄上一個node,只要當前node不為null上一個node為null,就是有空缺處,回傳false Code網友解答 class Solution { public boolean isCompleteTree(TreeNode root) { Queue<TreeNod
2024-11-07
Leetcode > Medium
#Leetcode #心得 #Binary Tree #Breadth-First Search #Tree
123456…36

搜尋

Hexo Fluid