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

3375. Minimum Operations to Make Array Values Equal to K - Easy

前往題目 想法 看有幾個distinct value就好,但不要包括k 思路 循環所有數字,紀錄有幾個distinct數字,不包含k 遇到小於k的就直接回傳-1因為無法替換 Codeclass Solution { public int minOperations(int[] nums, int k) { Set<Integer> res =
2025-04-09
Leetcode > Easy
#Leetcode #心得 #Array #Hash Table

3396. Minimum Number of Operations to Make Elements in Array Distinct - Easy

前往題目 想法 用Set暴力解 思路 從後面比較快,每次往前一位就加入set,同時判斷是否已經有 如果已經重複就可以直接算出需要幾次operation Code網友解答 class Solution { public int minimumOperations(int[] nums) { Set<Integer> set = new Hash
2025-04-08
Leetcode > Easy
#Leetcode #心得 #Array #Hash Table

1863. Sum of All Subset XOR Totals - Easy

前往題目 思路 Recursion把所有subset都找到並計算 Code class Solution { public int subsetXORSum(int[] nums) { return dfs(nums, 0, 0); } private int dfs(int[] nums, int index, int
2025-04-05
Leetcode > Easy
#Leetcode #心得 #Array #Math #Bit Manipulation #Backtracking #Combinatorics #Enumeration

1123. Lowest Common Ancestor of Deepest Leaves - Medium

前往題目 想法 DFS 思路 DFS 往最深處找,找到大於當前已知最深的深度就更新,node也要一併更新因為有可能沒有共同ancestor這時答案就是leaf node本身 左右都找到最深,並且每次都要判斷是否當前是最深leaf的ancestor以更新答案 Code class Solution { TreeNode res; int maxDepth;
2025-04-04
Leetcode > Medium
#Leetcode #心得 #Binary Tree #Hash Table #Depth-First Search #Breadth-First Search #Tree

2874. Maximum Value of an Ordered Triplet II - Medium

前往題目 想法 與昨天的daily題目一模一樣,只是這次不允許$n^3$複雜度暴力解法 思路請見 Codeclass Solution { public long maximumTripletValue(int[] nums) { long maxVal = 0, maxTriplet = 0, maxDiff = 0; for (
2025-04-03
Leetcode > Medium
#Leetcode #心得 #Array

2873. Maximum Value of an Ordered Triplet I - Easy

前往題目 想法 三個循環,難怪是Easy,但八成不是最優解 思路 題目要求最大化(nums[i] - nums[j]) * nums[k] 可拆解為 -> nums[i] - nums[j] 以及 nums[k]最大化 於是在疊代的過程中,維護三個值分別是最大的nums[i] - nums[j],最大的nums[k],以及最重要的答案 因為都是往右走,所以不會有順序問題產生,每次的值都是
2025-04-02
Leetcode > Easy
#Leetcode #心得 #Array

Shell進階筆記 - The Missing Semester of Your CS Education

Shell Scripting Variables: Assign: foo=bar Access: $foo Note: No spaces around = in assignment. Strings: Single quotes (') → Literal string (no variable substitution). Double quotes
2025-04-01
電腦科學 > The Missing Semester of Your CS Education
#shell #the-missing-semester-of-your-cs-education

2140. Solving Questions With Brainpower - Medium

前往題目 想法 選或不選都要考慮 思路題目解釋得很糟糕,這題的目的就是當前問題解決了就要跳過接下來的brainpower個問題,不解決就下一個 用遞迴的方式 每次都紀錄選與不選的結果 紀錄每個題目的最大可能值,這樣後面遇到就不需要再重算 Codeclass Solution { // Record the questions already been considered
2025-04-01
Leetcode > Medium
#Leetcode #心得 #Array #Dynamic Programming

763. Partition Labels - Medium

前往題目 想法 每個字母的第一個和最後一個為區間 思路 記錄每個字母的最後一位的位置 開始循環s,每次更新當前的最後一位在哪,取最遠的(因為最大區間不能被切開,否則會有同個字母分隔兩邊的情況) 遇到盡頭時(當前字母最後一位與最遠的同位),切開並記錄 這題是把每個字母都當區間來看,只是開頭並不重要,重要的是每個字母最後在哪裡出現,因為不能在中途就切開,不然就會出現同個字母被切開的情況,這也是題
2025-03-30
Leetcode > Medium
#Leetcode #心得 #String #Two Pointers #Greedy #Hash Table

2780. Minimum Index of a Valid Split - Medium

前往題目 想法 先找出最多的那個元素,然後迭代的時候再算其元素在左邊以及右邊各有多少 思路就和我的想法一樣,不過最後我忘了除二或乘二去正確判斷超過半數 Codeclass Solution { public int minimumIndex(List<Integer> nums) { int[] dominant = new int[]
2025-03-27
Leetcode > Medium
#Leetcode #心得 #Array #Hash Table #Sorting
1234…36

搜尋

Hexo Fluid