Re: [姆咪] 每日LeetCode
1345.
jump game iv
じゃんけんぽん
上古時代的hard真的好爽喔
水題==
算dp嗎 bfs
應該queue就行了
沒有很漂亮
我懶:)
class Solution {
public:
int minJumps(vector<int>& arr) {
int n = arr.size();
vector<int> step(n, n);
step[0] = 0;
unordered_map<int, vector<int>> mp;
// mp<num, idx>
for(int i = 0; i < n; i++){
mp[arr[i]].push_back(i);
}
unordered_set<int> round;
round.insert(arr[0]);
for(int cnt = 1; step[n-1] == n; cnt++){
// cout << "cnt = " << cnt << '\n';
unordered_set<int> next_round;
for(auto num: round){
// cout << num << '\n';
for(auto idx : mp[num]){
if(step[idx] < cnt){
if((idx+1) < n and step[idx+1] > cnt){
step[idx+1] = cnt;
next_round.insert(arr[idx+1]);
}
if((idx-1) >= 0 and step[idx-1] > cnt){
step[idx-1] = cnt;
next_round.insert(arr[idx-1]);
}
}
else {
step[idx] = cnt;
next_round.insert(arr[idx]);
}
// std::string s = std::format("{}: {}", idx, step[idx]);
// cout << s << '\n';
}
}
round = move(next_round);
}
return step[n-1];
}
};
--
很姆的咪
姆之咪
http://i.imgur.com/5sw7QOj.jpg

--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 39.12.91.45 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/SocialHealth/M.1779306748.A.6F2.html
→
05/21 03:53,
4周前
, 1F
05/21 03:53, 1F
推
05/21 03:54,
4周前
, 2F
05/21 03:54, 2F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):
姆咪
5
8