id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
3,436 | <p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p>
<p>Return the <strong>minimum</strong> possible value of the absolute difference.</p>
<p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong></p>
<p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n // O(30*N) - Time complexity\n int minimumDifference(vector<int>&nums, int k){\n int n=nums.size();\n unordered_set<int>prev;\n int ans=INT_MAX;\n\n for(int i=0; i<n; i++){\n unordered_set<int>curr;\n for(auto it: prev){\n curr.insert(it|nums[i]);\n }\n curr.insert(nums[i]);\n for(auto it: curr){\n ans = min(ans, abs(it-k));\n }\n prev=curr;\n }\n return ans;\n }\n};",
"memory": "373968"
} |
3,436 | <p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p>
<p>Return the <strong>minimum</strong> possible value of the absolute difference.</p>
<p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong></p>
<p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n unordered_map<int,bool> prev;\n int ans=INT_MAX; \n for(int i=nums.size()-1;i>=0;i--){\n unordered_map<int,bool> curr;\n curr[nums[i]]=true;\n ans=min(ans,abs(k-nums[i]));\n for(auto p:prev){\n curr[p.first | nums[i]]=true;\n ans=min(ans,abs(k-(p.first | nums[i])));\n } \n // for(auto i:curr) cout<<i.first<<\" \";\n prev=curr;\n }\n return ans;\n }\n};",
"memory": "377719"
} |
3,436 | <p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p>
<p>Return the <strong>minimum</strong> possible value of the absolute difference.</p>
<p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong></p>
<p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n int n = nums.size();\n unordered_set<int> feasible;\n int mini = INT_MAX;\n for(int i=n-1; i>=0; i--){\n unordered_set<int> new_feasible;\n for(auto j : feasible){\n new_feasible.insert(j|nums[i]);\n }\n new_feasible.insert(nums[i]);\n for(auto i : new_feasible){\n mini = min(mini, abs(i-k));\n }\n feasible = new_feasible;\n }\n return mini;\n }\n};",
"memory": "377719"
} |
3,436 | <p>You are given an array <code>nums</code> and an integer <code>k</code>. You need to find a <span data-keyword="subarray-nonempty">subarray</span> of <code>nums</code> such that the <strong>absolute difference</strong> between <code>k</code> and the bitwise <code>OR</code> of the subarray elements is as<strong> small</strong> as possible. In other words, select a subarray <code>nums[l..r]</code> such that <code>|k - (nums[l] OR nums[l + 1] ... OR nums[r])|</code> is minimum.</p>
<p>Return the <strong>minimum</strong> possible value of the absolute difference.</p>
<p>A <strong>subarray</strong> is a contiguous <b>non-empty</b> sequence of elements within an array.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,2,4,5], k = 3</span></p>
<p><strong>Output:</strong> 0</p>
<p><strong>Explanation:</strong></p>
<p>The subarray <code>nums[0..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 3| = 0</code>.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1,3,1,3], k = 2</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>The subarray <code>nums[1..1]</code> has <code>OR</code> value 3, which gives the minimum absolute difference <code>|3 - 2| = 1</code>.</p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [1], k = 10</span></p>
<p><strong>Output:</strong> <span class="example-io">9</span></p>
<p><strong>Explanation:</strong></p>
<p>There is a single subarray with <code>OR</code> value 1, which gives the minimum absolute difference <code>|10 - 1| = 9</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= nums.length <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int minimumDifference(vector<int>& nums, int k) {\n set<int> s;\n s.insert(0);\n int n=nums.size();\n int ans=INT_MAX;\n for(int i=0;i<n;i++)\n {\n set<int> sn;\n for(auto el:s)\n {\n sn.insert(el|nums[i]);\n }\n \n for(auto el:sn)\n {\n ans=min(ans,abs(k-el));\n }\n sn.insert(0);\n s=sn;\n }\n return ans;\n }\n};",
"memory": "381470"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string clearDigits(string s) {\n while(true){\n bool flag = false;\n for(int i=0; i<s.size(); i++){\n if(isdigit(s[i])){\n flag = true;\n for(int j=i-1; j>=0; j--){\n if(!isdigit(s[j])){\n s.erase(s.begin()+j);\n i--;\n break;\n }\n }\n s.erase(s.begin()+i);\n break;\n }\n }\n if(!flag){\n break;\n }\n }\n return s;\n }\n};",
"memory": "7600"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string clearDigits(string s) {\n int n=s.size(), j=0;\n string res=\"\";\n for(int i=0;i<n;i++){\n if( s[i]>='0' && s[i]<='9'){\n if(res.size()!=0){\n res.pop_back();\n }\n }else{\n res.push_back(s[i]);\n }\n }\n return res;\n }\n};",
"memory": "7700"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string clearDigits(string s) {\n // Continue until there are digits in the string\n while (true) {\n int digitIndex = -1;\n // Find the first digit\n for (int i = 0; i < s.size(); ++i) {\n if (isdigit(s[i])) {\n digitIndex = i;\n break;\n }\n }\n // If no digit is found, break\n if (digitIndex == -1) {\n break;\n }\n // Find the closest non-digit character to its left\n for (int i = digitIndex - 1; i >= 0; --i) {\n if (!isdigit(s[i])) {\n s.erase(i, 1); // Remove the non-digit character\n break;\n }\n }\n s.erase(digitIndex - 1, 1); // Remove the digit\n }\n return s;\n }\n};",
"memory": "7800"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string clearDigits(string s) {\n string ans;\n for(char c:s){\n if(isdigit(c)){\n if(!ans.empty()) ans.pop_back();\n }\n else {\n ans.push_back(c);\n }\n }\n return ans;\n }\n};",
"memory": "7800"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string clearDigits(string s) {\n string ans=\"\";\n for(char ch:s){\n if(!ans.empty() && isalpha(ans.back()) && isdigit(ch) ){\n ans.pop_back();\n }else{\n ans.push_back(ch);\n }\n }\n return ans;\n }\n};",
"memory": "7900"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string clearDigits(string s) {\n \n int l = s.length();\n\n for (int i = 0; i < l; i++) {\n if (isdigit(s[i])) {\n if (i != 0) {\n s.erase(i-1, 2); l -= 2; i -= 2; }\n else { s.erase(i, 2); l--; i--; }\n }\n }\n return s;\n }\n};",
"memory": "7900"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string clearDigits(string s) {\n int j=0 ;\n for(int i=0 ; i<s.length();i++){\n if(isdigit(s[i])){\n j--;\n }else{\n s[j]=s[i];\n j++;\n }\n }\n return s.substr(0,j);\n }\n};",
"memory": "8000"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string clearDigits(string s) {\n \n int i = 0;\n string result;\n while (i < s.size())\n {\n if (!isdigit(s[i]))\n {\n result+= s[i];\n }\n else\n {\n result.pop_back();\n }\n i++;\n }\n\n return result;\n }\n};",
"memory": "8000"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n string clearDigits(string s) {\n int n=s.size();\n string ans=\"\";\n for(int i=0;i<n;i++){\n if(isdigit(s[i])){\n ans.pop_back();\n }\n else{\n ans.push_back(s[i]);\n }\n }\n return ans; \n }\n};",
"memory": "8100"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n string clearDigits(string s) {\n int n=s.size();\n string ans;\n for(int i=0;i<n;i++){\n if(isdigit(s[i])){\n ans.pop_back();\n }\n else{\n ans.push_back(s[i]);\n }\n }\n return ans; \n }\n};",
"memory": "8100"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n string clearDigits(string s) {\n int j = 0;\n\n for (int i = 0; i < s.length(); i++) {\n if (isdigit(s[i])) {\n j--;\n } else {\n s[j++] = s[i];\n }\n }\n\n return s.substr(0, j);\n }\n};\n",
"memory": "8200"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n string clearDigits(string s) {\n vector<char> v;\n for(auto i : s){\n (i >= '0' and i <= '9') ? v.pop_back() : v.push_back(i);\n }\n string x;\n for(auto i : v) x.push_back(i);\n return x;\n }\n};",
"memory": "8200"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n string clearDigits(string s) {\n vector<char> vec;\n for(int x:s)\n {\n if(x>47 && x<59)\n {\n vec.pop_back();\n continue;\n }\n vec.push_back(x);\n }\n string ans;\n for(auto i:vec)\n {\n ans+=i;\n }\n return ans;\n }\n};",
"memory": "8300"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n string clearDigits(string s) {\n int n=s.length();\n vector<bool> p(n,true);\n\n for(int i=0;i<n;i++)\n {\n if(s[i]>='0' && s[i]<='9')\n {\n p[i]=false;\n int j=i-1;\n while(j>=0)\n {\n if(s[j]>='a' && s[j]<='z' && p[j])\n {\n p[j]=false;\n break;\n }\n j--;\n }\n }\n }\n\n string ans=\"\";\n for(int i=0;i<n;i++)\n {\n if(p[i])\n ans+=s[i];\n }\n\n return ans;\n }\n};",
"memory": "8400"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n string clearDigits(string s) {\n int n=s.length();\n vector<char> ch;\n for(int i=0;i<n;i++)\n {\n if(isdigit(s[i]))\n {\n if(!ch.empty())\n ch.pop_back();\n }\n else\n ch.push_back(s[i]);\n }\n string ans=\"\";\n while(!ch.empty())\n {\n ans=ch.back()+ans;\n ch.pop_back();\n }\n return ans;\n }\n};",
"memory": "8500"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n string clearDigits(string s) {\n stack<char>d;\n for(int i=0;i<s.length();i++){\n if(isdigit(s[i])){\n d.pop();\n }\n else{\n d.push(s[i]);\n }\n }\n string ans = \"\";\n while(!d.empty()){\n ans += d.top();\n d.pop();\n }\n reverse(ans.begin(),ans.end());\n return ans;\n\n\n \n }\n};",
"memory": "8600"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n string clearDigits(string s) {\n stack<char> st;\n for(int i=0;i<s.size();i++){\n char ch = s[i];\n if(ch>='a' && ch<='z'){\n st.push(ch);\n }\n else{\n st.pop();\n }\n }\n string ans;\n while(!st.empty()){\n ans.insert(ans.begin(),st.top());\n st.pop();\n }\n \n // reverse(ans.begin(),ans.end());\n return ans;\n }\n};",
"memory": "8700"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n \n string clearDigits(string s) {\n stack<char>st;\n\n for(int i=0; i<s.length(); i++)\n {\n if(isdigit(s[i])) st.pop();\n else\n st.push(s[i]);\n }\n\n int n = st.size();\n\n for (int i = n - 1; i >= 0; i--) {\n // Place the characters from the stack into the string from the back\n s[i] = st.top();\n st.pop();\n }\n\n // Resize the string to fit the new size\n s.resize(n);\n return s;\n \n }\n};",
"memory": "8800"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n \n string clearDigits(string s) {\n stack<char>st;\n for(int i=0; i<s.length(); i++)\n {\n if(isdigit(s[i])) st.pop();\n else\n st.push(s[i]);\n }\n int n = st.size();\n for (int i = n - 1; i >= 0; i--) {\n // Place the characters from the stack into the string from the back\n s[i] = st.top();\n st.pop();\n }\n\n // Resize the string to fit the new size\n s.resize(n);\n return s;\n \n }\n};",
"memory": "8800"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n string clearDigits(string s) {\n stack<char>st;\n for(int i=0;i<s.length();i++)\n {\n if(!isdigit(s[i]))\n st.push(s[i]);\n else\n st.pop();\n }\n string t=\"\";\n while(!st.empty())\n {\n t+=st.top();\n st.pop();\n }\n reverse(t.begin(),t.end());\n return t;\n }\n};",
"memory": "8900"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n string clearDigits(string s) {\n set<int> st;\n for(int i = s.size()-1;i>=0;i--){\n if(isdigit(s[i])){\n st.insert(i);\n for(int j=i-1;j>=0;j--){\n if(isalpha(s[j]) && st.find(j) == st.end()){\n st.insert(j);\n break;\n }\n }\n }\n }\n\n string str = \"\";\n\n for(int i=0;i<s.size();i++){\n if(st.find(i) == st.end()) str+=s[i];\n }\n return str;\n }\n};",
"memory": "9000"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string clearDigits(string s) {\n stack<char>st;\n for(int i=0;i<s.size();i++)\n {\n st.push(s[i]);\n if(isdigit(s[i]))\n {\n st.pop();\n if(isalpha(st.top()))\n {\n st.pop();\n \n }\n }\n }\n string str;\n while(!st.empty()) \n {\n str=st.top()+str;\n st.pop();\n }\n return str;\n } \n};",
"memory": "9100"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool isNum(char c){\n return( c <='9' && c>='1' || c=='0');\n }\n bool isChar(char c){\n return( c <='z' && c>='a');\n }\n string clearDigits(string s) {\n stack<char>st; \n for (int i=0;i<s.length();i++){\n if (isNum(s[i])){\n if (!st.empty() && isChar(st.top())){\n st.pop();\n }\n }\n else{\n st.push(s[i]);\n }\n }\n string ans =\"\";\n while(!st.empty()){\n ans.push_back(st.top()); \n st.pop(); \n }\n reverse(ans.begin(),ans.end());\n return ans;\n }\n};",
"memory": "9100"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string clearDigits(string s) {\n stack<char>st;\n for(int i=0; i<s.size(); i++){\n if(!st.empty() && s[i]>='0' && s[i]<='9'){\n st.pop();\n }else{\n st.push(s[i]);\n }\n }\n if(st.empty()) return \"\";\n string ans = \"\";\n while(!st.empty()){\n ans = ans + st.top();\n st.pop();\n }\n reverse(ans.begin(), ans.end());\n return ans;\n }\n};",
"memory": "9200"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string clearDigits(string s) {\n stack<char>st;\n for(int i=0; i<s.size(); i++){\n if(!st.empty() && s[i]>='0' && s[i]<='9'){\n st.pop();\n }else{\n st.push(s[i]);\n }\n }\n if(st.empty()) return \"\";\n string ans = \"\";\n while(!st.empty()){\n ans = ans + st.top();\n st.pop();\n }\n reverse(ans.begin(), ans.end());\n return ans;\n }\n};",
"memory": "9200"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string clearDigits(string s) {\n string ans = \"\";\n stack<char> st;\n for(char ch: s){\n if((int)ch >= 48 && (int)ch <= 57){\n st.pop();\n }\n else st.push(ch);\n }\n while(!st.empty()){\n ans = st.top() + ans;\n st.pop();\n }\n return ans;\n }\n};",
"memory": "9300"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string clearDigits(string s) {\n stack<char> St;\n stack<char> reverse_s;\n\n for(int i=0; s[i]!='\\0' ; i++)\n {\n if(isalpha(s[i]))\n {\n St.push(s[i]);\n }\n if(isdigit(s[i]))\n {\n St.pop();\n }\n }\n while(!St.empty())\n {\n reverse_s.push(St.top());\n St.pop();\n }\n string ans;\n while(!reverse_s.empty())\n {\n ans+=reverse_s.top();\n reverse_s.pop();\n }\n return ans;\n }\n};",
"memory": "9400"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n string clearDigits(string s) {\n stack<char>st;\n for(int i=0; i<s.length(); i++)\n {\n if(isdigit(s[i])) st.pop();\n else\n st.push(s[i]);\n }\n int n = st.size();\n for (int i = n - 1; i >= 0; i--) {\n // Place the characters from the stack into the string from the back\n s[i] = st.top();\n st.pop();\n }\n\n // Resize the string to fit the new size\n s.resize(n);\n stack<char>().swap(st);\n return s;\n \n }\n};",
"memory": "9500"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n \n string clearDigits(string s) {\n stack<char>st;\n for(int i=0; i<s.length(); i++)\n {\n if(isdigit(s[i])) st.pop();\n else\n st.push(s[i]);\n }\n int n = st.size();\n for (int i = n - 1; i >= 0; i--) {\n // Place the characters from the stack into the string from the back\n s[i] = st.top();\n st.pop();\n }\n\n // Resize the string to fit the new size\n s.resize(n);\n stack<char>().swap(st);\n return s;\n \n }\n};",
"memory": "9600"
} |
3,447 | <p>You are given a string <code>s</code>.</p>
<p>Your task is to remove <strong>all</strong> digits by doing this operation repeatedly:</p>
<ul>
<li>Delete the <em>first</em> digit and the <strong>closest</strong> <b>non-digit</b> character to its <em>left</em>.</li>
</ul>
<p>Return the resulting string after removing all digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "abc"</span></p>
<p><strong>Output:</strong> <span class="example-io">"abc"</span></p>
<p><strong>Explanation:</strong></p>
<p>There is no digit in the string.<!-- notionvc: ff07e34f-b1d6-41fb-9f83-5d0ba3c1ecde --></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "cb34"</span></p>
<p><strong>Output:</strong> <span class="example-io">""</span></p>
<p><strong>Explanation:</strong></p>
<p>First, we apply the operation on <code>s[2]</code>, and <code>s</code> becomes <code>"c4"</code>.</p>
<p>Then we apply the operation on <code>s[1]</code>, and <code>s</code> becomes <code>""</code>.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 100</code></li>
<li><code>s</code> consists only of lowercase English letters and digits.</li>
<li>The input is generated such that it is possible to delete all digits.</li>
</ul>
| 3 | {
"code": "bool IsDigit(char a)\n{\n string digits = {\"1234567890\"};\n for(int i = 0; i < digits.length(); i++)\n if(a == digits[i]) return true;\n return false;\n}\n\nclass Solution {\npublic:\n string clearDigits(string s) \n {\n int l = s.length();\n for(int i = 0; i < l; i++)\n {\n if(IsDigit(s[i]))\n {\n if(i != 0) {s.erase(i-1,2); l -=2; i-=2;}\n else {s.erase(i,2); l--; i--;}\n }\n }\n return s;\n }\n};",
"memory": "9700"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n int highest = skills[1] > skills[0];\n int count = 1;\n for (int i = 2; i < skills.size(); i++) {\n if (count >= k)\n return highest;\n if (skills[highest] > skills[i]) {\n count++;\n }\n else {\n highest = i;\n count = 1;\n }\n }\n return highest;\n }\n};",
"memory": "119949"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n int n=skills.size(), cnt=0, mx=skills[0], idx=0;\n for(int i=1;i<n;i++){\n if(skills[i]<mx){\n cnt++;\n if(cnt>=k){\n return idx;\n }\n }else{\n mx=skills[i];\n idx=i;\n cnt=1;\n }\n if(cnt>=k){\n return idx;\n }\n }\n return idx;\n }\n};",
"memory": "119949"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n int n = skills.size();\n\n stack<pair<int,pair<int,int>>> st;\n\n for(int i=0;i<n;i++){\n if(st.empty()){\n st.push({skills[i],{0,i}});\n }\n else{\n if(st.top().first > skills[i]){\n st.top().second.first++;\n if(st.top().second.first >= k){\n return st.top().second.second;\n }\n continue;\n }\n int count = 0;\n while(!st.empty() && st.top().first < skills[i]){\n st.pop();\n count++;\n if(count >= k){\n return i;\n }\n }\n st.push({skills[i],{count,i}});\n }\n }\n\n return st.top().second.second;\n }\n};",
"memory": "121248"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n int n = skills.size();\n\n stack<pair<int,pair<int,int>>> st;\n\n for(int i=0;i<n;i++){\n if(st.empty()){\n st.push({skills[i],{0,i}});\n }\n else{\n if(st.top().first > skills[i]){\n st.top().second.first++;\n if(st.top().second.first >= k){\n return st.top().second.second;\n }\n continue;\n }\n int count = 0;\n while(!st.empty() && st.top().first < skills[i]){\n st.pop();\n count++;\n if(count >= k){\n return i;\n }\n }\n st.push({skills[i],{count,i}});\n }\n }\n\n return st.top().second.second;\n }\n};",
"memory": "121248"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n if (k >= skills.size()) {\n return ranges::max_element(skills) - skills.begin();\n } else if (k == 1) {\n return skills[0] > skills[1] ? 0 : 1;\n }\n vector<int> pos(skills.size() - 1);\n ranges::iota(pos, 1);\n int candidate = 0;\n int skill = skills[candidate];\n auto it = pos.begin();\n int streak = 0;\n for (;;) {\n if (skills[*it] > skill) {\n swap(*it, candidate);\n skill = skills[candidate];\n streak = 1;\n } else if (++streak == k) {\n return candidate;\n }\n if (++it == pos.end()) {\n it = pos.begin();\n }\n }\n }\n};",
"memory": "122546"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n int i = 1, n = skills.size()-1, c = 0;\n if(k >= n) {\n return max_element(skills.begin(), skills.end()) - skills.begin();\n }\n vector<int> I(n+1); iota(I.begin(), I.end(), 0);\n swap(skills.front(), skills.back());\n swap(I.front(), I.back());\n while(true) {\n if(skills[i] > skills.back()) {\n swap(skills[i], skills.back());\n swap(I[i], I.back());\n c = 0;\n } \n i = (i+1)%n;\n if(k == ++c)\n break;\n }\n return I.back();\n }\n};",
"memory": "122546"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n int n = skills.size();\n if (is_sorted(skills.begin(), skills.end())) {\n cout << \"a\";\n if (k == 1) return 1;\n else return n-1;\n }\n vector<int> temp(skills.begin(), skills.end());\n map<int, int> mp;\n auto maxElement = max_element(skills.begin(), skills.end());\n int maxValue = *maxElement;\n if (k >= n) {\n return distance(skills.begin(), maxElement);\n }\n int result = 0;\n while (true) {\n int i = skills[0] > skills[1] ? 0 : 1;\n int winner = skills[i];\n skills.push_back(skills[1-i]);\n skills.erase(skills.begin() + 1 - i);\n mp[winner]++;\n if (winner == maxValue || mp[winner] == k) {\n auto winnerIterator = find(temp.begin(), temp.end(), winner);\n result = distance(temp.begin(), winnerIterator);\n break;\n }\n }\n return result;\n }\n};",
"memory": "123845"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n int n = skills.size();\n if (is_sorted(skills.begin(), skills.end())) {\n if (k == 1) return 1;\n else return n-1;\n }\n vector<int> temp(skills.begin(), skills.end());\n map<int, int> mp;\n auto maxElement = max_element(skills.begin(), skills.end());\n int maxValue = *maxElement;\n if (k >= n) {\n return distance(skills.begin(), maxElement);\n }\n int result = 0;\n while (true) {\n int i = skills[0] > skills[1] ? 0 : 1;\n int winner = skills[i];\n skills.push_back(skills[1-i]);\n skills.erase(skills.begin() + 1 - i);\n mp[winner]++;\n if (winner == maxValue || mp[winner] == k) {\n auto winnerIterator = find(temp.begin(), temp.end(), winner);\n result = distance(temp.begin(), winnerIterator);\n break;\n }\n }\n return result;\n }\n};",
"memory": "123845"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n int n = skills.size();\n int maxSkill = 0;\n int maxIndex = 0;\n for(int i=0; i<n;i++) {\n int s = skills[i];\n if(s >= maxSkill) {\n maxIndex = i;\n maxSkill = s;\n }\n }\n int ret = maxIndex;\n vector<int> wins(n,0);\n int lastWinner = 0;\n for(int i=1; i<=maxIndex-1; i++) {\n int lastSkill = skills[lastWinner];\n int currSkill = skills[i];\n // cout << \"play \" << i << \" \" << lastWinner << endl;\n if(lastSkill > currSkill) {\n // cout << \"win last\" << lastWinner << \"@\" << lastSkill << endl;\n wins[lastWinner]++;\n if(wins[lastWinner] == k) return lastWinner;\n }\n else {\n // cout << \"win curr\" << i << \"@\" << currSkill << endl;\n wins[i]++;\n if(wins[i] == k) return i;\n lastWinner = i;\n }\n }\n // for(int i=0; i<n;i++) cout << \"wins \" << i << \" \" << wins[i] << endl;\n return ret;\n \n }\n};",
"memory": "125144"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n int n=skills.size();\n vector<int> v(n+2);\n int maxi=0;\n queue<int> qq;\n int e;\n int cur=0;\n for(int i=0;i+1<n;i++){\n int w,l;\n if(skills[cur]>skills[i+1])\n {\n w=cur;\n l=i+1;\n }\n else{\n w=i+1;\n cur=w;\n l=i;\n }\n if(skills[w]>maxi){\n maxi=skills[w];\n e=w;\n }\n v[w]++;\n if(v[w]==k)\n return cur;\n }\n if(maxi<skills[n-1])\n e=n-1;\n cout<<e<<endl;\n return e;\n }\n};",
"memory": "125144"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n if (k>=skills.size()) {\n // Only the highest skill can win\n int result = 0;\n for (int i=0; i<skills.size(); i++) {\n if (skills[i] > skills[result]) result=i;\n }\n return result;\n }\n\n int me=0;\n int myK = 0;\n while(true) {\n\n int opponent = me + 1;\n while (true) {\n\n if( skills[me] > skills[opponent] ) {\n // we won - send to the back and skip over J\n myK++;\n if (myK==k) return me;\n skills.push_back(skills[opponent]);\n opponent++;\n } else {\n // We lose\n skills.push_back(skills[me]);\n me = opponent;\n myK = 1; // the opponent has one once\n if (myK==k) return opponent;\n break;\n }\n }\n }\n\n return 0;\n \n }\n};",
"memory": "126443"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& a, int k) {\n int n = a.size(), mx1 = 0;\n k = min(k, n - 1);\n for(int i = 1; i < n; i++){\n if(a[i] > a[mx1]) mx1 = i;\n }\n int mx = mx1;\n vector<int> r(n, 0);\n stack<int> s;\n s.push(mx1);\n for(int i = (mx1 - 1 >= 0) ? mx1 - 1 : 0; i >= 0; i--){\n while(!s.empty() && a[i] > a[s.top()]) s.pop();\n r[i] = s.top() - i - 1;\n s.push(i);\n }\n r[mx] = n - 1;\n int m = a[0];\n for(int i = 0; i <= mx; i++) cout<<r[i]<<' ';\n for(int i = 0; i <= mx; i++){\n if(i > 0 && a[i] > m) r[i]++;\n if(r[i] >= k) return i;\n }\n return -1;\n }\n};",
"memory": "126443"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& ss, int k) {\n if (k >= ss.size()-1) {\n int besti = 0;\n for (int i = 1; i < ss.size(); i++) {\n if (ss[i] > ss[besti]) besti = i;\n }\n return besti;\n }\n \n deque<int> q;\n for (int i = 1; i < ss.size(); i++) q.push_back(i);\n \n int i = 0, cnt = 0;\n while (true) {\n if (ss[i] > ss[q.front()]) {\n if (++cnt == k) return i;\n q.push_back(q.front());\n } else {\n if (k == 1) return q.front();\n q.push_back(i);\n i = q.front();\n cnt = 1;\n }\n q.pop_front();\n }\n }\n};",
"memory": "127741"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n if (k >= skills.size())\n {\n int maxe = *max_element(skills.begin(), skills.end());\n for (int i = 0; i < skills.size(); i++)\n {\n if (skills[i] == maxe)\n return i;\n }\n }\n \n deque<int> q;\n for (int i = 0; i < skills.size(); i++)\n {\n q.push_back(i);\n }\n \n int streak = 0;\n int curr = q.front();\n q.pop_front();\n \n while (streak < k)\n {\n int sec = q.front();\n q.pop_front();\n \n if (skills[curr] > skills[sec])\n {\n streak++;\n q.push_back(sec);\n }\n else\n {\n streak = 1;\n q.push_back(curr);\n curr = sec;\n }\n }\n \n return curr;\n }\n};",
"memory": "127741"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int maxSkill(vector<int>& skills) {\n int s = INT_MIN, id = -1;\n for(int i = 0; i < skills.size(); i++) {\n if (skills[i] > s)\n s = skills[i], id = i;\n }\n \n return id;\n }\n \n int find(vector<int> &skills, int val) {\n for(int i = 0; i < skills.size(); i++)\n if (skills[i] == val)\n return i;\n \n return -1;\n }\n \n int findWinningPlayer(vector<int>& skills, int k) {\n int sz = skills.size();\n int ms = maxSkill(skills);\n \n if (k >= sz)\n return ms;\n \n vector<int> nsk(3*sz);\n for(int i = 0; i < sz; i++)\n nsk[i] = skills[i];\n \n int curr = 0, wins = 0, tail = sz;\n while(tail+1 < 3*sz) {\n if ( nsk[curr] > nsk[curr+1]) {\n wins++;\n nsk[tail++] = nsk[curr+1];\n nsk[curr+1] = nsk[curr];\n } else {\n wins = 1;\n nsk[tail++] = nsk[curr];\n }\n curr++;\n if (wins >= k)\n return find(skills, nsk[curr]);\n }\n \n return ms;\n }\n};",
"memory": "129040"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n int n=skills.size();\n deque<int>dq;\n for(int i=0;i<n;i++)dq.push_back(i);\n int winner=dq.front();// top index\n dq.pop_front();\n int cnt=0;//it is telling the no of cons, wins\n while(cnt<k){\n int oppn=dq.front();// top opponent index\n dq.pop_front();\n if(skills[winner]>skills[oppn]){\n cnt++;\n dq.push_back(oppn);// loser at last\n }\n else{\n dq.push_back(winner);// winner lose goes at last\n cnt=1;\n winner=oppn;\n }\n if(cnt==k || k>n+1){\n break;\n }\n }\n if(k>n){// there is the guy who wins from all so - infinite loop so just take the max one \n return max_element(skills.begin(), skills.end()) - skills.begin();\n }\n return winner;\n }\n};",
"memory": "129040"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n if(k >= skills.size()-1){\n return max_element(skills.begin(),skills.end())-skills.begin();\n }\n deque<int>q;\n for(int i = 0;i<skills.size();i++){\n q.push_back(i);\n }\n int curwinner = 0;\n int nowins = 0;\n while(q.size()>1){\n int p1 = q.front();\n q.pop_front();\n int p2 = q.front();\n q.pop_front();\n if(skills[p1]>skills[p2]){\n q.push_front(p1);\n q.push_back(p2);\n if(p1 == curwinner){\n nowins++;\n if(nowins == k)return p1;\n }\n else{\n curwinner = p1;\n nowins = 1;\n if(nowins == k)return p1;\n }\n }\n swap(p1,p2);\n if(skills[p1]>skills[p2]){\n q.push_front(p1);\n q.push_back(p2);\n if(p1 == curwinner){\n nowins++;\n if(nowins == k)return p1;\n }\n else{\n curwinner = p1;\n nowins = 1;\n if(nowins == k)return p1;\n }\n }\n }\n \n return 1;\n }\n};",
"memory": "130339"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& s, int k){\n int n = s.size();\n if(k>=n){\n int maxi = *max_element(s.begin(), s.end());\n return find(s.begin(), s.end(), maxi) - s.begin();\n }\n\n deque<int> q;\n for(auto &it : s) q.push_back(it);\n\n int counter = 0;\n while(counter<k){\n int a = q.front();\n q.pop_front();\n int b = q.front();\n q.pop_front();\n \n if(a>b){\n counter++;\n q.push_front(a);\n q.push_back(b);\n }else{\n counter = 1;\n q.push_front(b);\n q.push_back(a);\n }\n }\n return find(s.begin(), s.end(), q.front()) - s.begin();\n }\n};",
"memory": "130339"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 2 | {
"code": "//BEGIN\n\n\ntypedef long long LL;\nstatic const int INF = 1000000000;\nstatic const int P = 1000000007;\n\n#define REP(i,n) for(int i=0;i<(n);i++)\n#define FOR(i,a,b) for(int i=(a);i<=(b);i++)\n#define FORD(i,a,b) for(int i=(a);i>=(b);i--)\n#define pb push_back\n#define ALL(a) a.begin(), a.end()\n#define SORT(v) sort((v).begin(),(v).end())\n#define VI vector<int>\n#define VS vector<string>\n\ntemplate<typename T>auto mkv(int a, T v){return vector<T>(a, v);}\ntemplate<typename T>auto mkv(int a, int b, T v){return vector<vector<T>>(a,mkv(b,v));}\ntemplate<typename T>auto mkv(int a, int b, int c, T v){return vector<vector<vector<T>>>(a,mkv(b,c,v));}\n\nclass Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n queue<int> q;\n int p = 0;\n int w = 0;\n for (int i = 1; i < skills.size(); ++i) {\n q.push(i);\n }\n int it = 0;\n while (true) {\n int wz1 = q.front(); q.pop();\n if (skills[wz1] < skills[p]) {\n w++;\n q.push(wz1);\n if (w >= k) return p;\n } else {\n w = 1;\n q.push(p);\n p = wz1;\n if (w >= k) return p;\n }\n it++;\n if (it > skills.size()) return p;\n }\n return -1;\n }\n};\n\n\n//FINISH\n",
"memory": "131638"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n deque<int>q;\n int n=skills.size();\n int mx=skills[0];\n int ans=0;\n for(int i=0;i<n;i++){\n q.push_back(i);\n if(skills[i]>mx){\n ans=i;\n mx=skills[i];\n }\n }\n if(k>=n){\n return ans;\n }\n int p=0;\n while(true){\n int cur=q.front();\n q.pop_front();\n if(skills[cur]<skills[q.front()]){\n q.push_back(cur);\n p=1;\n if(p==k){\n return q.front();\n }\n }\n else{\n p++;\n if(p==k){\n return cur;\n }\n q.push_back(q.front());\n q.pop_front();\n q.push_front(cur);\n }\n }\n }\n};",
"memory": "131638"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skill, int k) {\n \n deque<int>q;\n\n int maxIdx=0;\n for(int i=0;i<skill.size();i++)\n {\n q.push_back(i);\n if(skill[i]>skill[maxIdx])\n maxIdx=i;\n }\n\n if(k>skill.size())\n return maxIdx;\n \n int winner=0,wins=0,roundWinner=-1;\n\n while(wins<k)\n {\n auto a=q.front();\n q.pop_front();\n auto b=q.front();\n q.pop_front();\n\n if(skill[a]>skill[b])\n {\n roundWinner=a;\n q.push_front(a);\n q.push_back(b);\n }\n else\n {\n roundWinner=b;\n q.push_front(b);\n q.push_back(a);\n }\n \n if(winner==roundWinner)\n wins++;\n else\n {\n winner=roundWinner;\n wins=1;\n }\n }\n return winner;\n \n }\n};",
"memory": "132936"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skill, int k) {\n \n deque<int>q;\n\n int maxIdx=0;\n for(int i=0;i<skill.size();i++)\n {\n q.push_back(i);\n if(skill[i]>skill[maxIdx])\n maxIdx=i;\n }\n\n if(k>skill.size())\n return maxIdx;\n \n int winner=0,wins=0,roundWinner=-1;\n\n while(wins<k)\n {\n auto a=q.front();\n q.pop_front();\n auto b=q.front();\n q.pop_front();\n\n if(skill[a]>skill[b])\n {\n roundWinner=a;\n q.push_front(a);\n q.push_back(b);\n }\n else\n {\n roundWinner=b;\n q.push_front(b);\n q.push_back(a);\n }\n \n if(winner==roundWinner)\n wins++;\n else\n {\n winner=roundWinner;\n wins=1;\n }\n }\n return winner;\n \n }\n};",
"memory": "132936"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n int n = skills.size();\n int i = 0;\n int j = 1;\n int win = 0;\n while (j < 2 * n) {\n if (skills[i] < skills[j]) {\n skills.push_back(skills[i]);\n i = j;\n win = 1;\n } else {\n skills.push_back(skills[j]);\n win++;\n }\n\n if (win == k) {\n return i;\n }\n j++;\n }\n int maxId = 0;\n int val = skills[0];\n for (int i = 1; i < n; i++) {\n if (val < skills[i]) {\n val = skills[i];\n maxId = i;\n }\n }\n return maxId;\n }\n};",
"memory": "134235"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n deque<int>dq;\n int n=skills.size();\n if(k>=n){\n int m=*max_element(skills.begin(),skills.end());\n for(int i=0;i<n;i++){\n if(m==skills[i])\n return i;\n }\n }\n vector<int>score(n,0);\n for(int i=0;i<n;i++)\n dq.push_back(i);\n\n while(true){\n int i=dq.front();\n dq.pop_front();\n int j=dq.front();\n dq.pop_front();\n \n if(skills[i] > skills[j]){\n dq.push_front(i);\n dq.push_back(j);\n score[i]++;\n score[j]=0;\n if(score[i]==k)\n return i;\n\n }\n\n else{\n dq.push_front(j);\n dq.push_back(i);\n score[j]++;\n score[i]=0;\n if(score[j]==k)\n return j;\n }\n\n }\n\n return -1;\n\n }\n};",
"memory": "134235"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n int ans = 0, n = skills.size(); \n unordered_map<int, int > m; \n for(int i = 1; i<n; i++){\n if(skills[i] > skills[ans]){\n ans = i; \n m[i]++;\n }\n else{\n m[ans]++; \n }\n if(m[ans] == k) return ans; \n }\n cout<<ans<<endl; \n return ans;\n }\n};",
"memory": "135534"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n \n int n = skills.size();\n unordered_map<int, int>mp;\n if(k<n) {\n int maxi=0;\n for(int i=1; i<n;) {\n if(skills[maxi] < skills[i]) {\n maxi=i;\n }\n mp[maxi]++;\n i++;\n if(mp[maxi]==k || i==n) return maxi;\n }\n } else {\n int maxi = 0;\n for(int i=1; i<n; i++) {\n if(skills[maxi] < skills[i]) {\n maxi=i;\n }\n }\n return maxi;\n }\n return 0;\n }\n};",
"memory": "135534"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n int n = skills.size(),z = -1;\n int mx = *max_element(skills.begin(),skills.end());\n for(int i = 0; i < n; i++)\n if(skills[i] == mx)\n z = i;\n if(k>=n)return z;\n map <int,int> win;\n int j = 0;\n for(int i = 1; i < n; i++)\n {\n if(skills[i]>skills[j])\n {\n win[i]++;\n j = i;\n }\n else\n win[j]++;\n if(win[j]>=k)return j;\n }\n return z;\n }\n};",
"memory": "136833"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skill, int k) {\n int n = skill.size();\n deque<int> dp;\n int ans,maxi=0;\n vector<int> wins(n,0);\n for(int i=0;i<n;i++){\n if(skill[i]>maxi){\n ans=i;\n maxi=skill[i];\n }\n dp.push_back(i);\n }\n if(k>n){\n return ans;\n }\n while(true){\n int player1 = dp.front();\n dp.pop_front();\n int player2 = dp.front();\n dp.pop_front();\n if(wins[player1]>=k){\n ans=player1;\n break;\n }\n if(wins[player2]>=k){\n ans=player2;\n break;\n }\n if(skill[player1]>=skill[player2]){\n dp.push_front(player1);\n dp.push_back(player2);\n wins[player1]++;\n }\n else{\n dp.push_front(player2);\n dp.push_back(player1);\n wins[player2]++;\n }\n }\n return ans;\n }\n};",
"memory": "136833"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n int counter = 0;\n std::deque<int> queue(skills.size(), 0);\n for(int i = 1; i != std::ssize(skills); ++i) {\n queue[i] = i;\n }\n int games = std::min(k,static_cast<int>(skills.size()));\n while(counter != games) {\n int first = queue.front();\n queue.pop_front();\n int second = queue.front();\n queue.pop_front();\n if(skills[first] > skills[second]) {\n ++counter;\n queue.push_front(first);\n queue.push_back(second);\n } else {\n counter = 1;\n queue.push_front(second);\n queue.push_back(first);\n }\n }\n return queue.front();\n }\n};",
"memory": "138131"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& nums, int k) {\n int n= nums.size();\n vector<int>dp(2*n+2);\n int prev = 0;\n for(int i=0;i<nums.size()-1;i++){\n int a1= prev;\n int a= nums[prev], b= nums[i+1];\n if(a>b){\n dp[prev]++;\n if(i<n){\n nums.push_back(b);\n }\n }\n else{\n prev = i+1;\n if(i<n){\n nums.push_back(a);\n }\n dp[i+1]++;\n }\n if(dp[a1] == k) return a1;\n if(dp[i+1] == k) return (i+1)%n;\n }\n return prev;\n }\n};",
"memory": "138131"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n // Edge case k larger than n\n if(k >= skills.size()) {\n return distance(skills.begin(), max_element(skills.begin(), skills.end()));\n }\n int winstreak = 0;\n deque<int> queue;\n deque<int> skill;\n for(int i = 0; i < skills.size(); ++i) {\n queue.push_back(i);\n skill.push_back(skills[i]);\n }\n while(winstreak<k) {\n if(skill[0]>skill[1]) {\n ++winstreak;\n win(queue, skill);\n } else {\n winstreak=1;\n lose(queue, skill);\n }\n }\n return queue[0];\n }\n\n void lose(deque<int>& q, deque<int>& s) {\n // When first player loses, first player moves to back of queue and everyone moves up one\n int qq = q[0];\n int ss = s[0];\n q.pop_front();\n s.pop_front();\n q.push_back(qq);\n s.push_back(ss);\n }\n\n void win(deque<int>& q, deque<int>& s) {\n // When the first player wins, 2nd player moves to back of queue and everyone moves up one\n int q1 = q[0];\n int q2 = q[1];\n int s1 = s[0];\n int s2 = s[1];\n q.pop_front();\n q.pop_front();\n s.pop_front();\n s.pop_front();\n q.push_front(q1);\n q.push_back(q2);\n s.push_front(s1);\n s.push_back(s2);\n }\n};",
"memory": "139430"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n // Edge case k larger than n\n if(k >= skills.size()) {\n return distance(skills.begin(), max_element(skills.begin(), skills.end()));\n }\n int winstreak = 0;\n deque<int> queue;\n deque<int> skill;\n for(int i = 0; i < skills.size(); ++i) {\n queue.push_back(i);\n skill.push_back(skills[i]);\n }\n while(winstreak<k) {\n if(skill[0]>skill[1]) {\n ++winstreak;\n win(queue, skill);\n } else {\n winstreak=1;\n lose(queue, skill);\n }\n }\n return queue[0];\n }\n\n void lose(deque<int>& q, deque<int>& s) {\n // When first player loses, first player moves to back of queue and everyone moves up one\n int qq = q[0];\n int ss = s[0];\n q.pop_front();\n s.pop_front();\n q.push_back(qq);\n s.push_back(ss);\n }\n\n void win(deque<int>& q, deque<int>& s) {\n // When the first player wins, 2nd player moves to back of queue and everyone moves up one\n int q1 = q[0];\n int q2 = q[1];\n int s1 = s[0];\n int s2 = s[1];\n q.pop_front();\n q.pop_front();\n s.pop_front();\n s.pop_front();\n q.push_front(q1);\n q.push_back(q2);\n s.push_front(s1);\n s.push_back(s2);\n }\n};",
"memory": "139430"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 2 | {
"code": "#include <vector>\n#include <deque>\n#include <utility>\n\nclass Solution {\npublic:\n int findWinningPlayer(std::vector<int>& skills, int k) {\n int n = skills.size();\n if (k >= n - 1) {\n return std::max_element(skills.begin(), skills.end()) - skills.begin();\n }\n std::deque<std::pair<int, int>> q; \n for (int i = 0; i < n; ++i) {\n q.push_back({i, 0});\n }\n\n while (!q.empty()) {\n auto a = q.front();\n q.pop_front();\n\n if (a.second == k) return a.first; \n\n auto b = q.front();\n q.pop_front();\n\n if (skills[a.first] > skills[b.first]) {\n q.push_front({a.first, a.second + 1});\n q.push_back({b.first, 0});\n } else {\n q.push_front({b.first, b.second + 1});\n q.push_back({a.first, 0});\n }\n }\n return -1;\n }\n};",
"memory": "140729"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 2 | {
"code": "#include <vector>\n#include <deque>\n#include <utility>\n\nclass Solution {\npublic:\n int findWinningPlayer(std::vector<int>& skills, int k) {\n int n = skills.size();\n if (k >= n - 1) {\n return std::max_element(skills.begin(), skills.end()) - skills.begin();\n }\n std::deque<std::pair<int, int>> q; \n for (int i = 0; i < n; ++i) {\n q.push_back({i, 0});\n }\n\n while (!q.empty()) {\n auto a = q.front();\n q.pop_front();\n\n if (a.second == k) return a.first; \n\n auto b = q.front();\n q.pop_front();\n\n if (skills[a.first] > skills[b.first]) {\n q.push_front({a.first, a.second + 1});\n q.push_back({b.first, 0});\n } else {\n q.push_front({b.first, b.second + 1});\n q.push_back({a.first, 0});\n }\n }\n return -1;\n }\n};",
"memory": "140729"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n int n = skills.size();\n vector<int> q(n*4+10), win(n+5, 0);\n int l = 0, r = n-1;\n for(int i = 0; i < n; ++i) q[i] = i;\n int res = -1;\n int mx = 0;\n for(auto v : skills) mx = max(mx, v);\n while(skills[q[l]] != mx) {\n // assert(l <= r && r <= n*2);\n int a = q[l], b = q[l+1];\n // cout << \"a = \" << a << '\\n';\n if(skills[a] < skills[b]) swap(a, b);\n q[++l] = a, q[++r] = b;\n ++win[a];\n if(win[a] == k) { res = a; break; }\n }\n if(res != -1) return res;\n return q[l];\n }\n};",
"memory": "142028"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n deque<int> dq;\n int n=skills.size();\n vector<int> wins(n,0);\n for(int i=0;i<n;i++){\n dq.push_back(i);\n }\n k=min(k,n);\n while(true){\n int a=dq.front();\n dq.pop_front();\n int b=dq.front();\n dq.pop_front();\n if(skills[a]>skills[b]){\n wins[a]++;\n wins[b]=0;\n if(wins[a]==k) return a;\n // if(wins[a]>=)\n dq.push_front(a);\n dq.push_back(b);\n }\n else{\n wins[b]++;\n wins[a]=0;\n if(wins[b]==k) return b;\n dq.push_front(b);\n dq.push_back(a);\n }\n }\n return 0;\n }\n};",
"memory": "142028"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n deque<pair<int,int>> dq;\n int maxi = skills[0];\n int maxi_idx = 0;\n for (int i = 0; i < skills.size(); i++) {\n if (skills[i] > maxi) {\n maxi = skills[i];\n maxi_idx = i;\n }\n dq.push_back({skills[i], i});\n }\n int count = 0;\n while (dq[0].first != maxi) {\n auto front = dq[0];\n auto second = dq[1];\n // cout << front.first << \",\" << second.first << endl;\n if (count >= k) {\n return front.second;\n }\n while (front.first > second.first) {\n count++;\n dq.pop_front();\n dq.pop_front();\n dq.push_back(second);\n dq.push_front(front);\n if (count >= k) {\n return front.second;\n }\n front = dq[0];\n second = dq[1];\n }\n dq.pop_front();\n dq.push_back(front);\n count = 1;\n }\n \n return maxi_idx;\n \n // 4 2 6 3 9\n // 4 6 3 9 2\n // 6 3 9 2 4\n // 6 9 2 4 3\n // 9 2 4 3 6\n // cycle\n }\n};",
"memory": "143326"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n if (k >= skills.size() - 1) {\n return std::max_element(skills.begin(), skills.end()) - skills.begin();\n }\n deque<pair<int,int>> q;\n vector<int> cnt(skills.size(),0);\n for(int i=0;i<skills.size();i++) q.push_back({skills[i],i});\n while(true){\n auto n1=q.front();\n q.pop_front();\n auto n2=q.front();\n q.pop_front();\n if(n1.first>n2.first){\n cnt[n1.second]++;\n cnt[n2.second]=0;\n q.push_front(n1);\n q.push_back(n2);\n }\n else {\n cnt[n2.second]++;\n cnt[n1.second]=0;\n q.push_front(n2);\n q.push_back(n1);\n }\n if(cnt[n1.second]==k) return n1.second;\n if(cnt[n2.second]==k) return n2.second;\n } \n return -1;\n }\n};",
"memory": "143326"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n deque<pair<int,int>>q;\n int n = skills.size();\n for(int i=0;i<n;i++){\n q.push_back({i,0});\n }\n if(k>=n){\n int maxi=-1,index=0;\n for(int i=0;i<skills.size();i++){\n if(skills[i]>maxi){\n maxi=skills[i];\n index=i;\n }\n }\n return index;\n }\n while(q.front().second<k){\n \n int one = q[0].first;\n int oneGames=q[0].second;\n int skillOne = skills[one];\n \n int two = q[1].first;\n int twoGames=q[1].second;\n int skillTwo = skills[two];\n \n q.pop_front();\n q.pop_front();\n \n if(skillOne>skillTwo){\n q.push_back({two,twoGames});\n q.push_front({one,oneGames+1});\n }else{\n q.push_back({one,oneGames});\n q.push_front({two,twoGames+1});\n }\n \n }\n return q.front().first;\n \n }\n};",
"memory": "144625"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n unordered_map<int,int> mp;\n queue<int> que;\n int maxi = 0;\n for(int i = 1; i < skills.size() ; i++){\n que.push(i);\n if(skills[i] > skills[maxi]){\n maxi = i;\n }\n }\n if(k > skills.size()){\n return maxi;\n }\n maxi = 0;\n while(1){\n if(skills[maxi] > skills[que.front()]){\n mp[maxi]++;\n if(mp[maxi] == k){\n return maxi;\n }\n que.push(que.front());\n que.pop();\n }\n else{\n mp[que.front()]++;\n if(mp[que.front()] == k){\n return que.front();\n }\n que.push(maxi);\n maxi = que.front();\n que.pop();\n }\n }\n return -1;\n }\n};",
"memory": "144625"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n unordered_map<int,int> mp;\n queue<int> que;\n int maxi = 0;\n for(int i = 1; i < skills.size() ; i++){\n que.push(i);\n if(skills[i] > skills[maxi]){\n maxi = i;\n }\n }\n if(k > skills.size()){\n return maxi;\n }\n maxi = 0;\n while(1){\n if(skills[maxi] > skills[que.front()]){\n mp[maxi]++;\n if(mp[maxi] == k){\n return maxi;\n }\n que.push(que.front());\n que.pop();\n }\n else{\n mp[que.front()]++;\n if(mp[que.front()] == k){\n return que.front();\n }\n que.push(maxi);\n maxi = que.front();\n que.pop();\n }\n }\n return -1;\n }\n};",
"memory": "145924"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n deque<int> q;\n int maxi = 0, maxInd=0;\n for(int i=0;i<skills.size(); i++){\n q.push_back(skills[i]);\n if(maxi<skills[i]){\n maxi = skills[i];\n maxInd = i;\n }\n }\n\n if(k>=skills.size()) return maxInd;\n\n unordered_map<int,int> mp;\n int ans=0;\n while(!q.empty()){\n int a = q.front();\n q.pop_front();\n int b=q.front();\n q.pop_front();\n if(a>b){\n q.push_front(a);\n q.push_back(b);\n mp[a]++;\n if(mp[a]==k){\n ans=a;\n break;\n }\n }else{\n q.push_front(b);\n q.push_back(a);\n mp[b]++;\n if(mp[b]==k){\n ans=b;\n break;\n }\n }\n }\n for(int i=0; i<skills.size(); i++){\n if(ans==skills[i]) return i;\n }\n return 0;\n }\n};",
"memory": "148521"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) \n {\n unordered_map<int,int>mp;\n deque<int>dq;\n\n int maxi = INT_MIN, idx;\n\n for(int i=0;i<skills.size();i++)\n {\n dq.push_back(i);\n if(skills[i]>maxi)\n {\n maxi = skills[i];\n idx = i;\n }\n }\n\n // if(k>=idx)\n // {\n // return idx;\n // }\n \n while(true)\n {\n int one = dq.front();\n dq.pop_front();\n int two = dq.front();\n dq.pop_front();\n\n if(skills[one]==maxi) return one;\n if(skills[two]==maxi) return two;\n\n if(skills[one]>skills[two])\n {\n dq.push_front(one);\n dq.push_back(two);\n mp[one]++;\n if(mp[one]==k) return one;\n }\n else\n {\n dq.push_front(two);\n dq.push_back(one);\n mp[two]++;\n if(mp[two]==k) return two;\n }\n }\n\n return 0;\n }\n};",
"memory": "148521"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n deque<pair<int,int>>dq;\n int n=skills.size();\n for(int i=0;i<n;i++){\n dq.push_back({i,0});\n }\n int i=0;\n while(i<n&&dq.front().second<k){\n int a=dq.front().first;\n int x=dq.front().second;\n dq.pop_front();\n int b=dq.front().first;\n int y=dq.front().second;\n dq.pop_front();\n if(skills[a]>skills[b]){\n dq.push_front({a,x+=1});\n dq.push_back({b,0});\n }\n else{\n dq.push_front({b,y+=1});\n dq.push_back({a,0});\n }\n i++;\n }\n return dq.front().first;\n }\n};",
"memory": "149820"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n int n=skills.size();\n if(k>=n){\n int maxi=INT_MIN;\n int ans=0;\n for(int i=0;i<n;i++){\n if(maxi<skills[i]){\n maxi=skills[i];\n ans=i;\n }\n }\n return ans;\n }\n deque<pair<pair<int,int>,int>> dq;\n int ans=0;\n for(int i=0;i<skills.size();i++){\n dq.push_back({{skills[i],0},i});\n }\n while(true){\n auto temp1=dq.front();\n dq.pop_front();\n auto temp2=dq.front();\n dq.pop_front();\n if(temp1.first.first>temp2.first.first){\n dq.push_front({{temp1.first.first,temp1.first.second+1},temp1.second});\n dq.push_back({{temp2.first.first,temp2.first.second},temp2.second});\n }\n else{\n dq.push_front({{temp2.first.first,temp2.first.second+1},temp2.second});\n dq.push_back({{temp1.first.first,temp1.first.second},temp1.second});\n }\n if(dq.front().first.second==k){\n ans=dq.front().second;\n break;\n }\n }\n return ans;\n }\n};",
"memory": "151119"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n struct Player {\n int initialIndex = 0;\n int skill = 0;\n int winTimes = 0;\n };\n int findWinningPlayer(vector<int>& skills, int k) {\n Player winner;\n winner.initialIndex = 0;\n winner.skill = skills[0];\n queue<Player> players;\n for (int i = 1; i < skills.size(); i++) {\n Player p;\n p.initialIndex = i;\n p.skill = skills[i];\n players.push(p);\n }\n // cout << \"Players size: \" << players.size();\n while (winner.winTimes < k) {\n Player p = players.front();\n players.pop();\n if (p.skill > winner.skill) {\n players.push(winner);\n winner = p;\n } else {\n players.push(p);\n }\n winner.winTimes++;\n // If went through the whole list, current winner must have the largest skill and will be the final winner.\n if (p.initialIndex == skills.size()-1) {\n break;\n }\n }\n return winner.initialIndex;\n }\n};",
"memory": "151119"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n \n std::deque<std::pair<int, int>> dq;\n\n for(int i = 0; i < skills.size(); i++) {\n dq.push_back({i, skills[i]});\n }\n\n int count = 0;\n int idx_won = -1;\n\n while(count < skills.size() - 1 and count < k) {\n auto p1 = dq.front();\n dq.pop_front();\n auto p2 = dq.front();\n dq.pop_front();\n\n if(p1.second > p2.second) {\n if(idx_won == p1.first) {\n count++;\n }\n else {\n count = 1;\n idx_won = p1.first;\n }\n dq.push_front(p1);\n dq.push_back(p2);\n }\n else {\n if(idx_won == p2.first) {\n count++;\n }\n else {\n count = 1;\n idx_won = p2.first;\n }\n dq.push_front(p2);\n dq.push_back(p1);\n }\n }\n\n return idx_won;\n }\n};\n\n",
"memory": "152418"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n deque<pair<int,int>> dq;\n for (int skill : skills) {\n dq.push_back({skill, 0});\n }\n\n int winningPlayer = -1;\n\n while (true) {\n auto p1 = dq.front();\n dq.pop_front();\n auto p2 = dq.front();\n dq.pop_front();\n\n if (p1.second == k || p1.second == skills.size() - 1) {\n winningPlayer = p1.first;\n break;\n }\n if (p2.second == k || p2.second == skills.size() - 1) {\n winningPlayer = p2.first;\n break;\n }\n\n if (p1.first > p2.first) {\n dq.push_front({p1.first, p1.second + 1});\n dq.push_back({p2.first, p2.second});\n } else {\n dq.push_front({p2.first, p2.second + 1});\n dq.push_back({p1.first, p1.second});\n }\n }\n\n for (int i = 0; i < skills.size(); i++) {\n if (winningPlayer == skills[i]) {\n return i;\n }\n }\n\n return -1;\n }\n};",
"memory": "153716"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n const int n = static_cast<int>(skills.size());\n if (k >= n) {\n auto iter = max_element(skills.begin(), skills.end());\n return static_cast<int>(distance(skills.begin(), iter));\n }\n else {\n deque<pair<int, int>> gameQueue;\n for (int i = 0; i < n; ++i) {\n gameQueue.push_back({ skills[i], i });\n }\n unordered_map<int, int> indexGameWinCount;\n while (1) {\n auto gamer1 = gameQueue.front();\n gameQueue.pop_front();\n auto gamer2 = gameQueue.front();\n gameQueue.pop_front();\n auto winner = gamer1 > gamer2\n ? helper(gamer1, gamer2, gameQueue, indexGameWinCount)\n : helper(gamer2, gamer1, gameQueue, indexGameWinCount);\n if (winner.second == k) {\n return winner.first;\n }\n }\n }\n }\n\nprivate:\n pair<int, int> helper(\n const pair<int, int>& gamer1,\n const pair<int, int>& gamer2,\n deque<pair<int, int>>& gameQueue,\n unordered_map<int, int>& indexGameWinCount)\n {\n gameQueue.push_front(gamer1);\n gameQueue.push_back(gamer2);\n ++indexGameWinCount[gamer1.second];\n return { gamer1.second, indexGameWinCount[gamer1.second] };\n }\n};",
"memory": "155015"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n const int n = static_cast<int>(skills.size());\n if (k >= n) {\n auto iter = max_element(skills.begin(), skills.end());\n return static_cast<int>(distance(skills.begin(), iter));\n }\n else {\n deque<pair<int, int>> gameQueue;\n for (int i = 0; i < n; ++i) {\n gameQueue.push_back({ skills[i], i });\n }\n unordered_map<int, int> indexGameWinCount;\n while (1) {\n auto gamer1 = gameQueue.front();\n gameQueue.pop_front();\n auto gamer2 = gameQueue.front();\n gameQueue.pop_front();\n auto winner = gamer1 > gamer2\n ? helper(gamer1, gamer2, gameQueue, indexGameWinCount)\n : helper(gamer2, gamer1, gameQueue, indexGameWinCount);\n if (winner.second == k) {\n return winner.first;\n }\n }\n }\n }\n\nprivate:\n pair<int, int> helper(\n const pair<int, int>& gamer1,\n const pair<int, int>& gamer2,\n deque<pair<int, int>>& gameQueue,\n unordered_map<int, int>& indexGameWinCount)\n {\n gameQueue.push_front(gamer1);\n gameQueue.push_back(gamer2);\n ++indexGameWinCount[gamer1.second];\n return { gamer1.second, indexGameWinCount[gamer1.second] };\n }\n};",
"memory": "156314"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n deque<pair<int,pair<int,int>>> dq;\n \n int n=skills.size();\n for(int i=0;i<n;i++){\n dq.push_back({skills[i],{i,0}});\n }\n \n int mx=*max_element(skills.begin(),skills.end());\n \n \n while(!dq.empty()){\n auto it1=dq.front();\n dq.pop_front();\n auto it2=dq.front();\n dq.pop_front();\n \n if(it1.first==mx){\n return it1.second.first;\n }\n \n \n if(it2.first==mx){\n return it2.second.first;\n }\n \n if(it1.first>it2.first){\n // cout<<it1.second.first<<\" \"<<it2.second.first<<endl;\n // cout<<it1.first<<\" \"<<it2.first<<endl;\n // cout<<it1.second.second<<\" \"<<it2.second.second<<endl;\n if(it1.second.second==k-1) return it1.second.first;\n \n dq.push_front({it1.first,{it1.second.first,it1.second.second+1}});\n dq.push_back({it2.first,{it2.second.first,it2.second.second+1}});\n }else{\n // cout<<it1.second.first<<\" \"<<it2.second.first<<endl;\n // cout<<it1.first<<\" \"<<it2.first<<endl;\n // cout<<it1.second.second<<\" \"<<it2.second.second<<endl;\n if(it2.second.second==k-1) return it2.second.first;\n dq.push_front({it2.first,{it2.second.first,it2.second.second+1}});\n dq.push_back({it1.first,{it1.second.first,it1.second.second+1}});\n }\n }\n \n return -1;\n }\n};",
"memory": "156314"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& p, int k) {\n if (k >= p.size() + 1) return max_element(p.begin(), p.end()) - p.begin();\n map<int, int> mp;\n deque<pair<int, int>> a(p.size());\n for (int i = 0; i < p.size(); i++) {\n a[i].first = p[i];\n a[i].second = i;\n }\n\n for (;;) {\n auto x = a.front(), y = a[1];\n a.pop_front(); a.pop_front();\n if (x.first < y.first) swap(x, y);\n mp[x.second]++;\n if (mp[x.second] >= k) return x.second; \n a.push_front(x);\n a.push_back(y);\n }\n }\n};",
"memory": "157613"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& ski, int k) \n {\n\n deque<pair<int,int>>dq;\n\n int maxi=max_element(ski.begin(),ski.end())-ski.begin();\n\n if(k>ski.size())\n return maxi;\n\n map<int,int>frq;\n\n\n for(int i=0;i<ski.size();i++)\n {\n dq.push_back({ski[i],i});\n }\n\n while(true)\n {\n auto a=dq.front();\n dq.pop_front();\n \n auto b=dq.front();\n dq.pop_front();\n\n if(a.first>b.first)\n {\n frq[a.second]++;\n\n if(frq[a.second]==k)\n return a.second;\n dq.push_back({b.first,b.second});\n dq.push_front({a.first,a.second});\n }\n else\n {\n frq[b.second]++;\n\n if(frq[b.second]==k)\n return b.second;\n dq.push_back({a.first,a.second});\n dq.push_front({b.first,b.second});\n }\n\n \n }\n\n return -1;\n }\n};",
"memory": "157613"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& arr, int k) {\n int n = arr.size();\n deque<pair<int,int>> q;\n for(int i = 0; i < n; i++) q.push_back({i,0});\n int x = 2*n;\n while(x--){\n int fpos = q.front().first;\n int fp = arr[fpos];\n int fw = q.front().second;\n q.pop_front();\n int spos = q.front().first;\n int sp = arr[spos];\n int sw = q.front().second;\n q.pop_front();\n if(fw==k) return fpos;\n if(sw==k) return spos;\n if(fp>sp){\n q.push_front({fpos,fw+1});\n q.push_back({spos,sw});\n }\n else{\n q.push_front({spos,sw+1});\n q.push_back({fpos,fw}); \n }\n }\n return q.front().first;\n }\n};",
"memory": "158911"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n int maxi=*max_element(skills.begin(),skills.end());\n vector<pair<int,int>> v;\n int j=0;\n int n=skills.size();\n int i=0;\n int count=0;\n int val=1;\n // for(int i=0;i<skills.size();i++){\n while(count<n){\n if(skills[i]==maxi){\n break;\n }\n if(skills[i]>skills[i+val]){\n skills.push_back(skills[i+val]);\n v.push_back({i,1});\n // i+=1;\n val++;\n }\n else{\n skills.push_back(skills[i+val]);\n v.push_back({i+val,1});\n // j++;\n i=i+val;\n val=1;\n }\n count++;\n }\n map<int,int> mp;\n for(int i=0;i<v.size();i++){\n mp[v[i].first]++;\n if(mp[v[i].first]==k){\n // cout<<1<<\" \";\n return v[i].first;\n }\n }\n return i;\n }\n};",
"memory": "158911"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\nusing namespace __gnu_pbds;\nusing namespace std;\nusing ll=long long;\ntypedef tree <pair<ll,ll>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;\n/*\n order_of_key (k)\n find_by_order(k)\n*/\ntemplate<typename T> istream & operator>>(istream & in, vector<T> & lst)\n{ for (auto & e : lst) in >> e; return in; }\n#define endl '\\n'\n#define F first\n#define pb push_back\n#define pf push_front\n#define pob pop_back\n#define pof pop_front\n#define S second\n#define MP make_pair\n// typedef long long ll;\ntypedef long double ld;\ntypedef pair<ll, ll> ii;\ntypedef vector<ll> vi;\ntypedef vector<vi> vvi;\ntypedef vector<ii> vii;\ntypedef set<ll> si;\ntypedef map<string, ll> msi;\n#define all(v) v.begin(),v.end()\n#define REP(i, a, b) \\\nfor (ll i = ll(a); i <= ll(b); i++)\n#define RREP(i, a, b) \\\nfor (ll i = ll(a); i >= ll(b); i--)\n#define ohyes cout<<\"YES\\n\"\n#define ohno cout<<\"NO\\n\"\n#define getvec(v,a,b) \\\nfor (ll i = a; i <=b; i++) \\\n cin >> v[i];\n#define printvec(v,a,b) {for (ll i = a; i <=b; i++){cout << v[i] << \" \";}cout<<'\\n';}\n#define get2dvec(v,a,b,c,d) \\\nfor (ll i = a; i <=b; i++) \\\n for (ll j = c; j <=d; j++) \\\n cin >> v[i][j];\n#define print2dvec(v,a,b,c,d) for (ll i = a; i <=b; i++){for (ll j = c; j <=d; j++){cout << v[i][j]<<\" \";}cout<<'\\n';}\n#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);\n// ll mod = 998244353;\n// ll mod = 1e9 + 7;\nll _t,cs;\n// cout<<\"Case \"<<cs-_t<<\": \"<<ans<<'\\n';\n// memset(dp,-1,sizeof(dp));\nclass Solution {\npublic:\n int findWinningPlayer(vector<int>& v, int k) {\n deque<ii> dq;\n ll n=v.size();\n ll mx=0;\n ll mxind=0;\n REP(i,0,n-1){\n if(mx<v[i]){\n mx=v[i];\n mxind=i;\n }\n }\n if(k>=n-1)return mxind;\n REP(i,2,n-1){\n dq.pb({v[i],i});\n }\n ll cnt=1;\n ll wn=0;\n if(v[1]>v[0]){\n wn=1;\n dq.pf({v[1],1});\n dq.pb({v[0],0});\n }else{\n dq.pb({v[1],1});\n dq.pf({v[0],0});\n }\n\n while(cnt<k){\n ii x=dq.front();\n dq.pop_front();\n ii y=dq.front();\n dq.pop_front();\n if(x.F>y.F){\n dq.pf(x);\n dq.pb(y);\n if(wn==x.S){\n cnt++;\n }else{\n cnt=1;\n wn=x.S;\n }\n }else{\n dq.pb(x);\n dq.pf(y);\n if(wn==y.S){\n cnt++;\n }else{\n cnt=1;\n wn=y.S;\n }\n }\n }\n return wn;\n }\n};",
"memory": "160210"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n int maxi = INT_MIN;\n for(int i=0;i<skills.size();i++)\n {\n maxi = max(maxi,skills[i]);\n }\n deque<int>dq;\n for(int i=0;i<skills.size();i++)\n {\n dq.push_back(skills[i]);\n }\n map<int,int>mpp;\n int winner,ans;\n while(1)\n {\n int first = dq.front();\n dq.pop_front();\n int second = dq.front();\n dq.pop_front();\n if(first==maxi)\n {\n ans = maxi;\n break;\n }\n if(first>second)\n {\n winner = first;\n dq.push_front(first);\n dq.push_back(second);\n }\n else\n {\n winner = second;\n dq.push_front(second);\n dq.push_back(first);\n }\n if(mpp[winner]==0)\n {\n mpp.clear();\n }\n mpp[winner]++;\n if(mpp[winner]==k)\n {\n ans = winner;\n break;\n }\n }\n for(int i=0;i<skills.size();i++)\n {\n if(skills[i]==ans)\n return i;\n }\n return 0;\n }\n};",
"memory": "161509"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n deque<pair<int, int>> dq;\n map<int, int> m;\n int curr=0, ans=0;\n\n \n for (auto it:skills) {\n dq.push_back(pair(it, curr));\n curr++;\n }\n\n curr=0;\n while (true && curr<skills.size()) {\n auto first = dq.front();\n dq.pop_front();\n auto second = dq.front();\n dq.pop_front();\n if (first.first > second.first) {\n dq.push_front(first);\n dq.push_back(second);\n m[first.second]++;\n if (m[first.second]>=k) {\n return first.second;\n }\n } else {\n dq.push_front(second);\n dq.push_back(first);\n m[second.second]++;\n if (m[second.second]>=k) {\n return second.second;\n }\n }\n curr++;\n }\n\n return dq.front().second;\n }\n};",
"memory": "162808"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n if(k >= skills.size()){\n int index = -1;\n int maxi = INT_MIN;\n\n for(int i = 0 ; i < skills.size() ; i++){\n if(skills[i] > maxi){\n index = i;\n maxi = skills[i];\n }\n }\n\n return index;\n }\n\n unordered_map<int , int> mapping;\n deque<int> q;\n\n for(int i = 0 ; i < skills.size() ; i++){\n q.push_back(i);\n }\n\n while(true){\n auto front1 = q.front();\n q.pop_front();\n\n auto front2 = q.front();\n q.pop_front();\n\n if(skills[front1] > skills[front2]){\n mapping[front1]++;\n if(mapping[front1] == k) return front1;\n\n mapping[front2] = 0;\n\n q.push_back(front2);\n q.push_front(front1);\n }else{\n mapping[front2]++;\n if(mapping[front2] == k) return front2;\n\n mapping[front1] = 0;\n\n q.push_back(front1);\n q.push_front(front2);\n }\n }\n\n return -1;\n }\n};",
"memory": "164106"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& nums, int k) {\n \n int n = nums.size();\n if(k>nums.size()){\n return max_element(nums.begin(), nums.end())-nums.begin();\n }\n deque<int> q;\n map<int,int> mp;\n\n for(int i =0 ; i <n;i++){\n q.push_back(i);\n }\n while(!q.empty()){\n int a = q.front();\n q.pop_front();\n int b = q.front();\n q.pop_front();\n if(nums[a] > nums[b]){\n q.push_front(a);\n mp[a]++;\n mp[b]=0;\n q.push_back(b);\n }\n else{\n mp[b]++;\n mp[a]=0;\n q.push_front(b);\n q.push_back(a);\n }\n if(mp[a]==k)return a;\n if(mp[b]==k)return b;\n }\n return -1;\n }\n};",
"memory": "165405"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n vector<pair<int,int>> to_play;\n int ff = 0;\n for (int i = 0; i < skills.size(); i++) {\n to_play.push_back({skills[i], i});\n if (skills[i] > skills[ff]) ff = i;\n }\n int game_index = 0;\n map<int,int> winct;\n while (1) {\n int winner;\n if (to_play[game_index].first < to_play[game_index+1].first) {\n to_play.push_back(to_play[game_index]);\n winner = to_play[game_index+1].second;\n } else {\n to_play.push_back(to_play[game_index+1]);\n winner = to_play[game_index].second;\n swap(to_play[game_index], to_play[game_index+1]);\n }\n ++winct[winner];\n if (winct[winner] == k || winner == ff) return winner;\n ++game_index;\n }\n }\n};",
"memory": "166704"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n if (k >= skills.size()) {\n return distance(skills.begin(), max_element(skills.begin(), skills.end()));\n }\n int i = 0;\n map<int, int> original_pos;\n int wins = 0;\n for (int i = 0; i < skills.size(); ++i) {\n original_pos[skills[i]] = i;\n }\n while (true) {\n int j = i + 1;\n if (j >= skills.size()) {\n j = 0;\n }\n if (skills[i] > skills[j]) {\n wins++;\n if (wins >= k) {\n return original_pos[skills[i]];\n }\n swap(skills[i], skills[j]);\n } else {\n if (k == 1) {\n return j;\n }\n wins = 1;\n }\n i = j;\n }\n }\n};",
"memory": "168003"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution \n{\npublic:\n int findWinningPlayer(vector<int>& skills, int k) \n {\n int n=skills.size();\n if(k>=n)\n {\n int i=0, mx=skills[0];\n for(int x=1; x<n; x++)\n {\n if(mx<skills[x])\n {\n mx=skills[x];\n i=x;\n }\n }\n return i;\n }\n deque<int> dq;\n map<int,int> ind;\n for(int x=0; x<n; x++) \n {\n dq.push_back(skills[x]);\n ind[skills[x]]=x;\n }\n int cnt=0, prev=-1;\n while(true)\n {\n int p1=dq.front();\n dq.pop_front();\n int p2=dq.front();\n dq.pop_front();\n int mx=max(p1,p2), mn=min(p1,p2);\n dq.push_front(mx);\n dq.push_back(mn);\n if(prev==mx)\n {\n cnt++;\n if(cnt==k) \n {\n return ind[mx];\n break;\n }\n }\n else\n {\n prev=mx;\n cnt=1;\n if(cnt==k) \n {\n return ind[mx];\n break;\n }\n }\n }\n return -1;\n }\n};",
"memory": "169301"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& skills, int k) {\n deque<int> q;\n int size = skills.size();\n int max_s = 0;\n\n for(auto skill : skills) {\n q.push_back(skill);\n max_s = max(max_s,skill);\n }\n \n map<int,int> hash;\n while(!q.empty()){\n int first = q.front();\n q.pop_front(); \n int second = q.front();\n q.pop_front(); \n if(first > second){\n q.push_front(first);\n hash[first]++;\n }else{\n q.push_front(second);\n hash[second]++;\n }\n };\n // for(auto pair : hash){\n // cout<<pair.first<<\" \"<<pair.second<<endl;\n // }\n for(int index = 0 ; index < skills.size() ; index++){\n int skill = skills[index];\n if(hash[skill] >= k) return index;\n }\n for(int index = 0 ; index < skills.size() ; index++){\n if(skills[index] == max_s) return index;\n }\n return 0;\n }\n};",
"memory": "170600"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& arr, int k) {\n int n = arr.size();\n \n int maxpos = 0;\n\n int maxx = *max_element(arr.begin() , arr.end());\n\n for(int i=0; i<n ; i++){\n if(arr[i]==maxx){\n maxpos = i;\n break;\n }\n }\n if(k>=n){\n return maxpos;\n }\n\n \n deque<pair<int,int>> q;\n\n\n map<int,int> mp;\n for(int i=0 ; i<n ; i++) {\n mp[i] = 0;\n q.push_back({arr[i] , i});\n }\n\n int matches = maxpos;\n cout << \"matches:\" << matches << \"\\n\";\n\n while(matches>0){\n auto a = q.front();\n q.pop_front();\n auto b = q.front();\n q.pop_front();\n if(a.first>b.first){\n mp[a.second]++;\n if(mp[a.second]==k) return a.second;\n q.push_front(a);\n q.push_back(b);\n }else{\n mp[b.second]++;\n if(mp[b.second]==k) return b.second;\n q.push_front(b);\n q.push_back(a);\n }\n matches--;\n }\n\n return maxpos;\n }\n\n // 11,9,12,2,20,1,8\n // 9,12,2,20,1,8,11\n // 12,2,20,1,8,11,9\n // 12,20,1,8,11,9,2\n // 20,1,8,11,9,2,12\n};",
"memory": "171899"
} |
3,413 | <p>A competition consists of <code>n</code> players numbered from <code>0</code> to <code>n - 1</code>.</p>
<p>You are given an integer array <code>skills</code> of size <code>n</code> and a <strong>positive</strong> integer <code>k</code>, where <code>skills[i]</code> is the skill level of player <code>i</code>. All integers in <code>skills</code> are <strong>unique</strong>.</p>
<p>All players are standing in a queue in order from player <code>0</code> to player <code>n - 1</code>.</p>
<p>The competition process is as follows:</p>
<ul>
<li>The first two players in the queue play a game, and the player with the <strong>higher</strong> skill level wins.</li>
<li>After the game, the winner stays at the beginning of the queue, and the loser goes to the end of it.</li>
</ul>
<p>The winner of the competition is the <strong>first</strong> player who wins <code>k</code> games <strong>in a row</strong>.</p>
<p>Return the initial index of the <em>winning</em> player.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [4,2,6,3,9], k = 2</span></p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2,3,4]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 0 is higher than that of player 1, player 0 wins. The resulting queue is <code>[0,2,3,4,1]</code>.</li>
<li>Players 0 and 2 play a game, since the skill of player 2 is higher than that of player 0, player 2 wins. The resulting queue is <code>[2,3,4,1,0]</code>.</li>
<li>Players 2 and 3 play a game, since the skill of player 2 is higher than that of player 3, player 2 wins. The resulting queue is <code>[2,4,1,0,3]</code>.</li>
</ul>
<p>Player 2 won <code>k = 2</code> games in a row, so the winner is player 2.</p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">skills = [2,5,4], k = 3</span></p>
<p><strong>Output:</strong> 1</p>
<p><strong>Explanation:</strong></p>
<p>Initially, the queue of players is <code>[0,1,2]</code>. The following process happens:</p>
<ul>
<li>Players 0 and 1 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
<li>Players 1 and 2 play a game, since the skill of player 1 is higher than that of player 2, player 1 wins. The resulting queue is <code>[1,0,2]</code>.</li>
<li>Players 1 and 0 play a game, since the skill of player 1 is higher than that of player 0, player 1 wins. The resulting queue is <code>[1,2,0]</code>.</li>
</ul>
<p>Player 1 won <code>k = 3</code> games in a row, so the winner is player 1.</p>
</div>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>n == skills.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= k <= 10<sup>9</sup></code></li>
<li><code>1 <= skills[i] <= 10<sup>6</sup></code></li>
<li>All integers in <code>skills</code> are unique.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int findWinningPlayer(vector<int>& arr, int k) {\n int n = arr.size();\n \n int maxpos = 0;\n\n int maxx = *max_element(arr.begin() , arr.end());\n\n for(int i=0; i<n ; i++){\n if(arr[i]==maxx){\n maxpos = i;\n break;\n }\n }\n if(k>=n){\n return maxpos;\n }\n\n deque<pair<int,int>> q;\n\n\n map<int,int> mp;\n for(int i=0 ; i<n ; i++) {\n mp[i] = 0;\n q.push_back({arr[i] , i});\n }\n\n int matches = maxpos;\n\n while(matches>0){\n auto a = q.front();\n q.pop_front();\n auto b = q.front();\n q.pop_front();\n if(a.first>b.first){\n mp[a.second]++;\n if(mp[a.second]==k) return a.second;\n q.push_front(a);\n q.push_back(b);\n }else{\n mp[b.second]++;\n if(mp[b.second]==k) return b.second;\n q.push_front(b);\n q.push_back(a);\n }\n matches--;\n }\n\n return maxpos;\n }\n\n};",
"memory": "171899"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.