1823. Find the Winner of the Circular Game 發表於 2023-02-13 | 分類於 leetcode problemsolutionoption 1 - queue12345678910111213141516class Solution {public: int findTheWinner(int n, int k) { queue<int> q; for(int i=1;i<=n;++i) q.push(i); while(q.size()!=1){ int t = k; while(--t){ q.push(q.front()); q.pop(); } q.pop(); } return q.front(); }}; option 2 - vector1234567891011121314class Solution {public: int findTheWinner(int n, int k) { vector<int> vec(n,0); for(int i=1;i<=n;++i) vec[i-1]=i; int offset = 0; while(vec.size()!=1){ int t = (offset+ k - 1)%vec.size() ; vec.erase(vec.begin() + t); offset = t; } return vec.front(); }}; analysis time complexity O(n) space complexity O(n)