逆向模拟构造题
使用双端队列数据结构。
/*
* @lc app=leetcode id=950 lang=cpp
*
* [950] Reveal Cards In Increasing Order
*/
// @lc code=start
class Solution {
public:
vector<int> deckRevealedIncreasing(vector<int>& deck) {
int N = deck.size();
sort(deck.rbegin(), deck.rend());
deque<int> q;
q.push_front(deck[0]);
int t ;
for(int i=1;i<N;i++){
t = q.back();
q.pop_back();
q.push_front(t);
q.push_front(deck[i]);
}
vector<int> ans(N, 0);
t = 0;
while(!q.empty()){
ans[t++] = q.front();
q.pop_front();
}
return ans;
}
};
// @lc code=end
这篇博客介绍了一种C++实现的算法,通过双端队列数据结构解决LeetCode第950题RevealCardsInIncreasingOrder,展示了如何按递增顺序揭示一副扑克牌。通过队列操作巧妙地保持牌堆的动态更新,确保了时间复杂度的高效性。
320

被折叠的 条评论
为什么被折叠?



