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

看板Marginalman作者 (虛構史學家)時間1年前 (2024/06/10 21:58), 編輯推噓4(401)
留言5則, 5人參與, 1年前最新討論串338/1548 (看更多)
※ 引述《sustainer123 (caster )》之銘言: : 1051. Height Checker : 給定一數列heights 我們期待heights是一非遞減數列 : 看解答好像能把時間複雜度降到n 等等研究一下 思路: 用bucket 然後這題用HashMap沒特別省時間 還會耗空間資源 這種小範圍的還是直接用Vec比較好 但我都寫了 :( Code: use std::collections::HashMap; impl Solution { pub fn height_checker(heights: Vec<i32>) -> i32 { let mut heights_freq= HashMap::new(); for &num in &heights { *heights_freq.entry(num).or_insert(0) += 1; } let mut index = 0; let mut result = 0; for height in 0..=100 { if let Some(&count) = heights_freq.get(&(height as i32)) { for _ in 0..count { if heights[index] != height as i32 { result +=1; } index += 1; } } } result } } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.193.249.242 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1718027883.A.B0D.html

06/10 21:59, 1年前 , 1F
別捲了
06/10 21:59, 1F

06/10 21:59, 1年前 , 2F
大佬
06/10 21:59, 2F

06/10 21:59, 1年前 , 3F
大師
06/10 21:59, 3F

06/10 22:00, 1年前 , 4F
大師
06/10 22:00, 4F

06/10 22:02, 1年前 , 5F
大師 尼板剩我不會hashmap
06/10 22:02, 5F
文章代碼(AID): #1cPmPhiD (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cPmPhiD (Marginalman)