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

看板Marginalman作者 (是oin的說)時間1年前 (2024/06/10 12:39), 編輯推噓3(302)
留言5則, 5人參與, 1年前最新討論串337/1548 (看更多)
※ 引述 《sustainer123 (caster)》 之銘言: :   : 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] : 推 DJYOSHITAKA: bucket吧 但我好懶 06/10 09:57 bucket的思路: 塞進去之後 從最小的數到最大的 同時看原本的陣列裡面有哪裡不一樣 class Solution { public: int heightChecker(vector<int>& heights) { int len = heights.size(); vector<int> paper(101,0); for(int k : heights) { paper[k]++; } int ans = 0; int p = 0; for(int i = 0 ; i < 101 ; i ++) { for(int j = 0 ; j < paper[i] ; j ++) { if(heights[p] != i)ans ++; p++; } } return ans; } }; -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.162.8.240 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1717994358.A.F04.html

06/10 12:40, 1年前 , 1F
大師
06/10 12:40, 1F

06/10 12:48, 1年前 , 2F
我好崇拜你
06/10 12:48, 2F

06/10 13:25, 1年前 , 3F
別捲了
06/10 13:25, 3F

06/10 13:26, 1年前 , 4F
芋圓滾滾吃飯沒?
06/10 13:26, 4F

06/10 17:00, 1年前 , 5F
你好厲害
06/10 17:00, 5F
文章代碼(AID): #1cPeDsy4 (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cPeDsy4 (Marginalman)