bisect(), insort() bisect(), bisect_left() 回傳插入x的index ( O(long(n)) ),保持a的排序 (a是個已排序 list ) 若x已存在a中則index會在a中的所有x的右/左邊 bisect.bisect(a, x[, lo=0, hi=len(a), *, key=None]) ≡ bisect.bisect_right(a, x[, ...
Problem https://leetcode.com/problems/minimum-array-length-after-pair-removals Testcases # Input Expected 1 [1,1] 2 2 [1,2] 0 3 [1,2,2] 1 4 [1,1,2,3,4,4] 0 Failed Solution: Greedy [Wrong Answer 338 ...
Shallow copy: To construct a new compound object and then (to the extent possible) insert references into it to the objects found in the original. Deep copy: To construct a new compound object and the ...
Problem https://leetcode.com/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree Solution: DFS n := number of nodes in the original tree Time Complexity: O(n) Space Complexity: ...
Problem https://leetcode.com/problems/kth-ancestor-of-a-tree-node First Solution: Binary Lifting [Time Limit Exceeded 8 / 17 testcases passed] Time Complexity: O(n ** 2) __init__(): worst case – ...
Problem https://leetcode.com/contest/weekly-contest-361/problems/minimum-edge-weight-equilibrium-queries-in-a-tree/ There is an undirected tree with n nodes labeled from 0 to n – 1. You are give ...
sum(1 for ... in ... if ...) or sum(1 if ... else 0 for ... in ...)
Problem https://leetcode.com/contest/weekly-contest-361/problems/count-of-interesting-subarrays/ 相關例題 Leetcode # 974. ⭐️ Subarray Sums Divisible by K Solution: Dynamic Programming [Time Limit Exceeded ...
Problem https://leetcode.com/problems/subarray-sums-divisible-by-k Solution 令 nums[i] = [4, 5, 0, -2, -3, 1], k = 5 能夠被k整除的子字串有: [5], [0], [5, 0], [-2, -3], [0, -2, -3], [5, 0, -2, -3], [4, 5, 0, -2, ...
Problem https://leetcode.com/contest/weekly-contest-362/problems/minimum-moves-to-spread-stones-over-grid/ Solution z := len([True for row in grid for num in row if num == 0]) Time Complexity: O(z!) S ...