Leetcode Weekly Contest 290
這場開始後 25 分鐘我才注意到,然後剛寫完一題又被拉出去買東西,所以就把這場 Contest 當作平常刷題吧。
2248. Intersection of Multiple Arrays
Difficulty: Easy
Given a 2D integer array nums where nums[i] is a non-empty array of distinct positive integers, return the list of integers that are present in each array of nums sorted in ascending order.
1 | Input: nums = [[3,1,2,4,5],[1,2,3,4],[3,4,5,6]] |
1 | Input: nums = [[1,2,3],[4,5,6]] |
1 <= nums.length <= 10001 <= sum(nums[i].length) <= 10001 <= nums[i][j] <= 1000- All the values of
nums[i]are unique.
題目解釋
求出所有 nums[i] 的交集,沒錯,就是 Easy。
Solution
第一直覺就是用交集,但還真的沒遇過兩個 list 以上的交集,所以爬了一下文發現可以用 map 將 nums[i] 一口氣轉為 set,接著使用 * 俗稱 Unpacking Operators,將所有 set 放出來進行交集比對,最後排序一下就可以回傳了。
1 | class Solution: |
2250. Count Number of Rectangles Containing Each Point
Difficulty: Medium, Open in Leetcode
題目解釋
給你很多很多長方形 rectangles,再給你很多 points,找出每一個 point 在有被包含在多少個 rectangles 中。
換句話說,算出每一個 point 有小於等於多少個 rectangle。
Solution
全部跑過一遍的暴力解這次碰壁了,直接吃 Time Limit Exceeded。
1 | # Time Limit Exceeded |
事後上 Discuss 看大神怎麼解,發現都是用 Binary Search 處理。
✅ [Java/ C++/ Python] Detailed Explanation, fully commented Binary Search l among points of h