Re: [閒聊] 每日leetcode已回收

看板Marginalman作者 (caster )時間1年前 (2024/06/10 09:23), 編輯推噓1(100)
留言1則, 1人參與, 1年前最新討論串336/1548 (看更多)
https://leetcode.com/problems/height-checker 1051. Height Checker 給定一數列heights 我們期待heights是一非遞減數列 此理想數列設為expected 請回傳heights[i] != expected[i]的數量 Example 1: Input: heights = [1,1,4,2,1,3] Output: 3 Explanation: heights: [1,1,4,2,1,3] expected: [1,1,1,2,3,4] Indices 2, 4, and 5 do not match. Example 2: Input: heights = [5,1,2,3,4] Output: 5 Explanation: heights: [5,1,2,3,4] expected: [1,2,3,4,5] All indices do not match. Example 3: Input: heights = [1,2,3,4,5] Output: 0 Explanation: heights: [1,2,3,4,5] expected: [1,2,3,4,5] All indices match. Constraints: 1 <= heights.length <= 100 1 <= heights[i] <= 100 思路: 排序 Python Code: class Solution: def heightChecker(self, heights: List[int]) -> int: n = sorted(heights) result = 0 for i in range(len(n)): if n[i] != heights[i]: result += 1 return result 看解答好像能把時間複雜度降到n 等等研究一下 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.194.160.111 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1717982637.A.1F3.html

06/10 09:57, 1年前 , 1F
bucket吧 但我好懶
06/10 09:57, 1F
文章代碼(AID): #1cPbMj7p (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cPbMj7p (Marginalman)