Re: [閒聊] 每日leetcode已回收
※ 引述 《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
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 337 之 1548 篇):