id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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 - 1)\n {\n return max_element(skills.begin(), skills.end()) - skills.begin();\n }\n\n // mp{ skills, ind , wins}\n unordered_map<int, pair<int, int>> mp;\n for (int i = 0; i < skills.size(); i++)\n {\n mp.insert({skills[i], {i, 0}});\n }\n\n deque<int> dq(skills.begin(), skills.end());\n\n while (1)\n {\n int player1 = dq.front();\n dq.pop_front();\n int player2 = dq.front();\n dq.pop_front();\n\n int winner, loser;\n if (player1 > player2)\n {\n winner = player1;\n loser = player2;\n }\n else\n {\n winner = player2;\n loser = player1;\n }\n mp[winner].second++;\n\n if (mp[winner].second == k)\n {\n return mp[winner].first;\n }\n dq.push_front(winner);\n dq.push_back(loser);\n }\n return -1;\n }\n};", "memory": "173198" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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 max_num=0;\n unordered_map<int, int> mp;\n for(int i=0; i<skills.size(); i++){\n mp[skills[i]] = i;\n max_num = max(max_num, skills[i]);\n }\n \n int count=0, curr=skills[0];\n \n \n for(int i=1; i<skills.size(); i++){\n if(curr>skills[i]){\n count += 1;\n }\n else{\n count = 1;\n curr = skills[i];\n }\n \n if(count==k) return mp[curr];\n }\n \n return mp[max_num];\n }\n};", "memory": "174496" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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 \n for(int i=0;i<skills.size();i++){\n mp[skills[i]]=i;\n }\n int maxVal=*max_element(skills.begin(), skills.end());\n \n // if(k>skills.size()-2)\n // return mp[maxVal];\n \n \n int prev=skills[0];\n \n int maxTillNow=skills[0];\n \n int count=0;\n \n for(int i=1;i<skills.size();i++){\n \n if(count==k)\n return mp[maxTillNow];\n maxTillNow=max(skills[i], maxTillNow);\n \n if(prev==maxTillNow){\n count+=1;\n // if(count==k)\n // return mp[prev];\n }else{\n prev=maxTillNow;\n count=1;\n }\n \n }\n \n return mp[maxVal];\n \n \n \n \n \n }\n};", "memory": "175795" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 10<sup>6</sup></code></li> <li>All integers in <code>skills</code> are unique.</li> </ul>
3
{ "code": "class Solution {\npublic:\n typedef pair<int, int> PII; \n\n int findWinningPlayer(vector<int>& skills, int k) {\n int n = skills.size(); \n int mx = skills[0]; \n int mxi = 0; \n for(int i=1; i<n; i++){\n if(mx < skills[i]){\n mx = skills[i]; \n mxi = i; \n }\n }\n\n if(k>=n-1) return mxi;\n\n list<PII> ls;\n for(int i=0; i<n; i++){\n ls.push_back({skills[i], i});\n }\n\n int c = 0; \n \n while(c < k){\n PII ff = *ls.begin();\n PII ss = *(++ls.begin());\n if(ff.first < ss.first){\n ls.erase(ls.begin());\n ls.push_back(ff);\n c=1;\n }\n else{\n ls.erase(++ls.begin());\n ls.push_back(ss);\n c++;\n }\n }\n\n return (*ls.begin()).second; \n }\n};", "memory": "177094" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 10<sup>6</sup></code></li> <li>All integers in <code>skills</code> are unique.</li> </ul>
3
{ "code": "class Solution {\npublic:\n typedef pair<int, int> PII; \n\n int findWinningPlayer(vector<int>& skills, int k) {\n int n = skills.size(); \n int mx = skills[0]; \n int mxi = 0; \n for(int i=1; i<n; i++){\n if(mx < skills[i]){\n mx = skills[i]; \n mxi = i; \n }\n }\n\n if(k>=n-1) return mxi;\n\n list<PII> ls;\n for(int i=0; i<n; i++){\n ls.push_back({skills[i], i});\n }\n\n int c = 0; \n \n while(c < k){\n PII ff = *ls.begin();\n PII ss = *(++ls.begin());\n if(ff.first < ss.first){\n ls.erase(ls.begin());\n ls.push_back(ff);\n c=1;\n }\n else{\n ls.erase(++ls.begin());\n ls.push_back(ss);\n c++;\n }\n }\n\n return (*ls.begin()).second; \n }\n};", "memory": "178393" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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-1) return max_element(skills.begin(),skills.end()) - skills.begin();\n deque<pair<int,int>> dq;\n map<int,int> mp;\n for(int i=0;i<n;i++){\n mp[skills[i]]=0;\n dq.push_back({skills[i],i});\n }\n while(1){\n auto p1=dq.front();dq.pop_front();\n auto p2=dq.front();dq.pop_front();\n\n if(p1.first>p2.first){\n dq.push_front({p1});\n dq.push_back({p2});\n mp[p2.first]=0;\n mp[p1.first]++;\n if(mp[p1.first]==k){\n return p1.second;\n }\n }\n else{\n dq.push_front({p2});\n dq.push_back({p1});\n mp[p1.first]=0;\n mp[p2.first]++;\n if(mp[p2.first]==k){\n return p2.second;\n }\n }\n }\n return 0;\n\n\n\n }\n};", "memory": "179691" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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-1) return max_element(skills.begin(),skills.end()) - skills.begin();\n deque<pair<int,int>> dq;\n map<int,int> mp;\n for(int i=0;i<n;i++){\n mp[skills[i]]=0;\n dq.push_back({skills[i],i});\n }\n while(1){\n auto p1=dq.front();dq.pop_front();\n auto p2=dq.front();dq.pop_front();\n\n if(p1.first>p2.first){\n dq.push_front({p1});\n dq.push_back({p2});\n mp[p2.first]=0;\n mp[p1.first]++;\n if(mp[p1.first]==k){\n return p1.second;\n }\n }\n else{\n dq.push_front({p2});\n dq.push_back({p1});\n mp[p1.first]=0;\n mp[p2.first]++;\n if(mp[p2.first]==k){\n return p2.second;\n }\n }\n }\n return 0;\n\n\n\n }\n};", "memory": "180990" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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-1) return max_element(skills.begin(),skills.end()) - skills.begin();\n deque<pair<int,int>> dq;\n map<int,int> mp;\n for(int i=0;i<n;i++){\n mp[skills[i]]=0;\n dq.push_back({skills[i],i});\n }\n while(1){\n auto p1=dq.front();dq.pop_front();\n auto p2=dq.front();dq.pop_front();\n\n if(p1.first>p2.first){\n dq.push_front({p1});\n dq.push_back({p2});\n mp[p2.first]=0;\n mp[p1.first]++;\n if(mp[p1.first]==k){\n return p1.second;\n }\n }\n else{\n dq.push_front({p2});\n dq.push_back({p1});\n mp[p1.first]=0;\n mp[p2.first]++;\n if(mp[p2.first]==k){\n return p2.second;\n }\n }\n }\n return 0;\n\n\n\n }\n};", "memory": "182289" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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-1) return max_element(skills.begin(),skills.end()) - skills.begin();\n deque<pair<int,int>> dq;\n map<int,int> mp;\n for(int i=0;i<n;i++){\n mp[skills[i]]=0;\n dq.push_back({skills[i],i});\n }\n while(1){\n auto p1=dq.front();dq.pop_front();\n auto p2=dq.front();dq.pop_front();\n\n if(p1.first>p2.first){\n dq.push_front({p1});\n dq.push_back({p2});\n mp[p2.first]=0;\n mp[p1.first]++;\n if(mp[p1.first]==k){\n return p1.second;\n }\n }\n else{\n dq.push_front({p2});\n dq.push_back({p1});\n mp[p1.first]=0;\n mp[p2.first]++;\n if(mp[p2.first]==k){\n return p2.second;\n }\n }\n }\n return 0;\n\n\n\n }\n};", "memory": "182289" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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 curStreak = 0;\n int n = skills.size();\n map<int, int>initIdxs;\n for (int i = 0; i < n; i++) {\n initIdxs[skills[i]] = i;\n }\n int winnerSkill = skills[0];\n int qHead = 1; // queue cicle from 1 no n-1 indexes; last winner not in queue\n while (curStreak < min(k, n)) {\n int skill2 = skills[qHead % n];\n if (winnerSkill < skill2) {\n curStreak = 0;\n swap(skills[0], skills[qHead % n]);\n winnerSkill = skill2;\n }\n qHead++;\n if (qHead % n == 0) {\n qHead++;\n }\n curStreak++;\n }\n return initIdxs[winnerSkill];\n }\n};", "memory": "183588" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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 map<int, int> mp;\n int i = 0;\n for(auto &x: arr){\n mp[x] = i++;\n }\n int c = 0, a = arr[0];\n for(int i = 1; i < arr.size(); i++){\n int b = arr[i];\n if(a > b){\n c++;\n }else{\n c = 1;\n a = b;\n }\n if(c == k){\n return mp[a];\n }\n }\n return mp[a];\n }\n};", "memory": "184886" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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(skills.begin(), skills.end());\n int n = skills.size();\n unordered_map<int,int>mpp;\n int max_skill = *max_element(skills.begin(), skills.end()); \n for(int i=0;i<n;i++){\n mpp[skills[i]] = i;\n }\n int cnt = 0; // To keep track of consecutive wins\n int current_winner = skills[0]; // Start with the first player\n \n // If k >= n, the strongest player will eventually win\n if (k >= n) {\n return mpp[max_skill];\n }\n\n for (int i = 1; i < n; i++) {\n if (current_winner > skills[i]) {\n cnt++;\n } else {\n current_winner = skills[i];\n cnt = 1; // Reset count for new winner\n }\n\n if (cnt == k) {\n return mpp[current_winner];\n }\n }\n\n // If no player has won `k` times consecutively, the maximum skill player is the winner.\n return mpp[max_skill];\n }\n};", "memory": "186185" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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>& s, int k) {\n map <int,int> mp;\n int n=s.size();\n int maxele=0;\n\n for(int i=0;i<n;i++){\n maxele=max(maxele,s[i]);\n mp[s[i]]=i;\n }\n\n if(k>=n)\n return mp[maxele];\n \n\n vector <int> premax(n-1,0);\n premax[0]=max(s[0],s[1]);\n\n for(int i=2;i<n;i++){\n premax[i-1]=max(premax[i-2],s[i]);\n }\n\n int curr=premax[0];\n if(k==1)\n return mp[curr];\n int count=0;\n for(auto it:premax)\n cout<<it<<\" \";\n\n for(int i=1;i<n-1;i++){\n if(premax[i]==curr){\n count++;\n if(count>=k-1)\n return mp[curr];\n }\n else{\n count=0;\n curr=premax[i];\n }\n }\n\n return mp[maxele];\n }\n}; ", "memory": "187484" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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 int ma=INT_MIN;\n int n=skills.size();\n for(int i=0;i<skills.size();i++)\n {\n mp[skills[i]]=i;\n \n }\n queue<int>q;\n for(int i:skills)\n {\n q.push(i);\n ma=max(ma,i);\n }\n if(k>=n)\n return mp[ma];\n int f=q.front();\n q.pop();\n int c=0;\n while(c<k)\n {\n if(q.front()<f)\n {\n c++;\n int t=q.front();\n q.pop();\n q.push(t);\n }\n else\n {\n c=1;\n q.push(f);\n f=q.front();\n q.pop();\n }\n }\n \nreturn mp[f];\n }\n};", "memory": "188783" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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 int n = skills.size();\n int maxElement = *max_element(skills.begin(), skills.end());\n deque<int> dq;\n for(int i = 0; i < skills.size(); i++){\n mp[skills[i]] = i;\n dq.push_back(skills[i]);\n }\n\n if(k >= n){\n return mp[maxElement];\n }\n\n int currWinner = -1, lastWinner = -2, first, second, count = 1;\n\n while(1){\n first = dq.front();\n dq.pop_front();\n if(first == maxElement)return mp[maxElement];\n second = dq.front();\n dq.pop_front();\n if(first > second){\n currWinner = first;\n dq.push_back(second);\n dq.push_front(first);\n }\n else{\n currWinner = second;\n dq.push_back(first);\n dq.push_front(second);\n }\n if(currWinner == lastWinner){\n count++;\n }\n \n else count = 1;\n if(count == k)return mp[currWinner];\n lastWinner = currWinner;\n }\n \n return mp[dq.front()]; \n }\n};", "memory": "190081" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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 int n = skills.size();\n int maxElement = *max_element(skills.begin(), skills.end());\n deque<int> dq;\n for(int i = 0; i < skills.size(); i++){\n mp[skills[i]] = i;\n dq.push_back(skills[i]);\n }\n\n if(k >= n){\n return mp[maxElement];\n }\n\n int K = k;\n int currWinner = -1, lastWinner = -2, prevFirst = -3, first, second, count;\n\n currWinner = (skills[0] > skills[1]) ? skills[0] : skills[1];\n\n while(1){\n first = dq.front();\n dq.pop_front();\n if(first == maxElement)return mp[maxElement];\n second = dq.front();\n dq.pop_front();\n if(first > second){\n currWinner = first;\n dq.push_back(second);\n dq.push_front(first);\n }\n else{\n currWinner = second;\n dq.push_back(first);\n dq.push_front(second);\n }\n if(k == 1){\n return mp[currWinner];\n }\n if(currWinner == lastWinner){\n count++;\n if(count == K){\n return mp[currWinner];\n }\n }\n else{\n count = 1;\n }\n\n lastWinner = currWinner;\n }\n \n\n\n return mp[dq.front()];\n \n }\n};", "memory": "191380" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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 int n = skills.size();\n int maxElement = *max_element(skills.begin(), skills.end());\n deque<int> dq;\n for(int i = 0; i < skills.size(); i++){\n mp[skills[i]] = i;\n dq.push_back(skills[i]);\n }\n\n if(k >= n){\n return mp[maxElement];\n }\n\n int currWinner = -1, lastWinner = -2, first, second, count;\n\n // currWinner = (skills[0] > skills[1]) ? skills[0] : skills[1];\n\n while(1){\n first = dq.front();\n dq.pop_front();\n if(first == maxElement)return mp[maxElement];\n second = dq.front();\n dq.pop_front();\n if(first > second){\n currWinner = first;\n dq.push_back(second);\n dq.push_front(first);\n }\n else{\n currWinner = second;\n dq.push_back(first);\n dq.push_front(second);\n }\n if(k == 1)return mp[currWinner];\n if(currWinner == lastWinner){\n count++;\n if(count == k){\n return mp[currWinner];\n }\n }\n else count = 1;\n lastWinner = currWinner;\n }\n \n return mp[dq.front()]; \n }\n};", "memory": "191380" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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 int mx=-1,mxpos,n=skills.size(),x,y,val,prev=-1,i;\n deque<int> q;\n unordered_map<int,int> mp;\n\n for(i=0;i<n;i++){\n q.push_back(skills[i]);\n mp[skills[i]]=k;\n\n if(skills[i]>mx){\n mx=skills[i];\n mxpos=i;\n }\n }\n\n if(k>=n)\n return mxpos;\n\n while(true){\n x=q.front();\n q.pop_front();\n y=q.front();\n q.pop_front();\n\n if(x==mx or y==mx)\n return mxpos;\n \n if(x>y){\n q.push_front(x);\n q.push_back(y);\n\n if(prev!=-1 and prev!=x){\n mp[prev]=k;\n prev=-1;\n }\n \n prev=x;\n mp[prev]--;\n\n if(mp[prev]==0){\n val=prev;\n break;\n }\n }\n else{\n q.push_front(y);\n q.push_back(x);\n\n if(prev!=-1 and prev!=y){\n mp[prev]=k;\n prev=-1;\n }\n \n prev=y;\n mp[prev]--;\n\n if(mp[prev]==0){\n val=prev;\n break;\n }\n }\n }\n\n for(i=0;i<n;i++){\n if(skills[i]==val)\n return i;\n }\n\n return 0;\n }\n};", "memory": "192679" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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 int n = skills.size();\n int maxElement = *max_element(skills.begin(), skills.end());\n deque<int> dq;\n for(int i = 0; i < skills.size(); i++){\n mp[skills[i]] = i;\n dq.push_back(skills[i]);\n }\n\n if(k >= n){\n return mp[maxElement];\n }\n\n int currWinner = -1, lastWinner = -2, first, second, count;\n\n currWinner = (skills[0] > skills[1]) ? skills[0] : skills[1];\n\n while(1){\n first = dq.front();\n dq.pop_front();\n if(first == maxElement)return mp[maxElement];\n second = dq.front();\n dq.pop_front();\n if(first > second){\n currWinner = first;\n dq.push_back(second);\n dq.push_front(first);\n }\n else{\n currWinner = second;\n dq.push_back(first);\n dq.push_front(second);\n }\n if(k == 1)return mp[currWinner];\n if(currWinner == lastWinner){\n count++;\n if(count == k){\n return mp[currWinner];\n }\n }\n else count = 1;\n lastWinner = currWinner;\n }\n \n return mp[dq.front()]; \n }\n};", "memory": "193978" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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 int n = skills.size();\n int maxElement = *max_element(skills.begin(), skills.end());\n deque<int> dq;\n for(int i = 0; i < skills.size(); i++){\n mp[skills[i]] = i;\n dq.push_back(skills[i]);\n }\n\n if(k >= n){\n return mp[maxElement];\n }\n\n int currWinner = -1, lastWinner = -2, first, second, count = 1;\n\n while(1){\n first = dq.front();\n dq.pop_front();\n if(first == maxElement)return mp[maxElement];\n second = dq.front();\n dq.pop_front();\n if(first > second){\n currWinner = first;\n dq.push_back(second);\n dq.push_front(first);\n }\n else{\n currWinner = second;\n dq.push_back(first);\n dq.push_front(second);\n }\n if(k == 1)return mp[currWinner];\n if(currWinner == lastWinner){\n count++;\n if(count == k){\n return mp[currWinner];\n }\n }\n else count = 1;\n lastWinner = currWinner;\n }\n \n return mp[dq.front()]; \n }\n};", "memory": "193978" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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> toIdx; // skill to idx\n int n = skills.size();\n int first = skills[0];\n deque<int> ring;\n for (int i = 0; i < n; i++) {\n toIdx[skills[i]] = i;\n if (i != 0) {\n ring.push_back(skills[i]);\n }\n }\n k = min(n-1, k);\n int wins = 0;\n while (wins < k) {\n int other = ring.front();\n ring.pop_front();\n if (first > other) {\n wins++;\n ring.push_back(other);\n } else {\n wins = 1;\n ring.push_back(first);\n first = other;\n }\n }\n return toIdx[first];\n }\n};", "memory": "195276" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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 int n=skills.size();\n deque<int>dq;\n for(int i=0;i<n;i++){\n mp[i]=skills[i];\n dq.push_back(i);\n }\n cout<<n<<endl;\n if(k>=n-1){\n int maxi=-1e9;\n int idx=-1;\n for(int i=0;i<n;i++){\n if(maxi<skills[i]){\n maxi=skills[i];\n idx=i;\n }\n }\n return idx;\n }\n int p=-1;\n int c=0;\n while(true){\n \n int a=dq.front();dq.pop_front();\n int b=dq.front();dq.pop_front();\n int val1=mp[a];\n int val2=mp[b];\n if(val1>val2){\n dq.push_back(b);\n dq.push_front(a);\n }\n else{\n dq.push_back(a);\n dq.push_front(b);\n }\n if(p==-1 or p==dq.front()){\n c++;\n p=dq.front();\n if(c==k)return p;\n }else {\n c=1;\n p=dq.front();\n }\n \n cout<<p<<\" \"<<dq.front()<<\" \"<<c<<endl;\n }\n\n\n return -1;\n }\n};", "memory": "196575" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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(skills.begin(), skills.end());\n int n = skills.size();\n unordered_map<int,int>mpp;\n for(int i=0;i<n;i++){\n mpp[skills[i]] = i;\n }\n int cnt = 0; // To keep track of consecutive wins\n int current_winner = dq.front();\n int max_skill = *max_element(skills.begin(), skills.end()); \n if (k >= n) {\n return mpp[max_skill];\n }\n while (cnt < k) {\n int first = dq.front();\n dq.pop_front();\n int second = dq.front();\n dq.pop_front();\n\n if (first > second) {\n dq.push_front(first);\n dq.push_back(second);\n if (current_winner == first) {\n cnt++;\n } else {\n current_winner = first;\n cnt = 1;\n }\n } else {\n dq.push_front(second);\n dq.push_back(first);\n current_winner = second;\n cnt = 1; // Reset count for new winner\n }\n }\n\n return mpp[current_winner];\n }\n};", "memory": "197874" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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(skills.begin(), skills.end());\n int n = skills.size();\n unordered_map<int,int>mpp;\n for(int i=0;i<n;i++){\n mpp[skills[i]] = i;\n }\n int cnt = 0; // To keep track of consecutive wins\n int current_winner = dq.front();\n int max_skill = *max_element(skills.begin(), skills.end()); \n if (k >= n) {\n return mpp[max_skill];\n }\n while (cnt < k) {\n int first = dq.front();\n dq.pop_front();\n int second = dq.front();\n dq.pop_front();\n\n if (first > second) {\n dq.push_front(first);\n dq.push_back(second);\n if (current_winner == first) {\n cnt++;\n } else {\n current_winner = first;\n cnt = 1;\n }\n } else {\n dq.push_front(second);\n dq.push_back(first);\n current_winner = second;\n cnt = 1; // Reset count for new winner\n }\n }\n\n return mpp[current_winner];\n }\n};", "memory": "197874" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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 deque<int>q;\n unordered_map<int,int>count; // for win count\n int highest=INT_MIN;\n int index;\n for (int i=0; i<n; i++)\n {\n q.push_back(i);\n count[i]=0;\n if (skills[i]>highest) {highest=skills[i]; index=i;} \n // 'index' refers to the player with highest skill\n }\n while (1)\n {\n int one = q.front(); // first palyer\n q.pop_front();\n int two = q.front(); // second player\n q.pop_front();\n if (one==index||two==index) return index; // player with the highest skill has arrived!\n \n if (skills[one]>skills[two])\n {\n count[one]++;\n if (count[one]==k) return one;\n q.push_front(one);\n q.push_back(two);\n }\n else {\n count[two]++;\n if (count[two]==k) return two;\n q.push_front(two);\n q.push_back(one);\n }\n } \n return 0;\n }\n};", "memory": "199173" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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> arr(n, 0);\n int x = 0, y = 0; \n unordered_map<int, int> mp;\n for(int i = 0; i < n; i++) {\n mp[skills[i]] = i;\n }\n int max_el = *max_element(skills.begin(), skills.end());\n deque<int>q;\n for(auto it: skills) {\n q.push_back(it);\n }\n while(q.front() != max_el) {\n x = q.front();\n q.pop_front();\n y = q.front();\n q.pop_front();\n if(x > y) {\n arr[mp[x]]++;\n if(arr[mp[x]] == k) return mp[x];\n q.push_front(x);\n }\n else {\n arr[mp[y]]++;\n if(arr[mp[y]] == k) return mp[y];\n q.push_front(y);\n }\n }\n return mp[max_el];\n }\n};", "memory": "200471" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 10<sup>6</sup></code></li> <li>All integers in <code>skills</code> are unique.</li> </ul>
3
{ "code": "class Solution {\npublic:\n // void rotate(vector<int>&arr , int n)\n // {\n // int temp[n];\n\n // int k = 0;\n // for (int i = 1; i < n; i++) {\n // temp[k] = arr[i];\n // k++;\n // }\n\n // for (int i = 0; i < 1; i++) {\n // temp[k] = arr[i];\n // k++;\n // }\n\n // for (int i = 0; i < n; i++) {\n // arr[i] = temp[i];\n // }\n // }\n int findWinningPlayer(vector<int>& skills, int k) {\n int n = skills.size();\n unordered_map<int,int> mp;\n deque<int> dq;\n int maxi = 0;\n int max_ind = -1;\n for(int i = 0 ; i < skills.size() ; i++)\n {\n mp[skills[i]] = i;\n if(i != 0)\n dq.push_back(skills[i]);\n if(skills[i] > maxi)\n {\n maxi = skills[i];\n max_ind = i; \n }\n }\n vector<int> playerWins(n , 0);\n\n if(k >= n)\n {\n return max_ind;\n }\n\n int el = skills[0];\n\n while(true)\n {\n if(el > dq.front())\n {\n playerWins[mp[el]]++;\n if(playerWins[mp[el]] == k) return mp[el];\n int x = dq.front();\n dq.pop_front();\n dq.push_back(x);\n }\n else\n {\n int x = dq.front();\n dq.pop_front();\n dq.push_back(el);\n el = x;\n playerWins[mp[el]]++;\n if(playerWins[mp[el]] == k) return mp[el];\n }\n }\n return -1;\n }\n};", "memory": "201770" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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 deque<int> q;\n map<int , int> mp;\n int n = skills.size();\n for(int i = 0;i<n;i++) q.push_back(i);\n \n for(int i = 0;i<3*n;i++)\n {\n int a = q.front(); q.pop_front();\n int b = q.front(); q.pop_front();\n if(skills[a] < skills[b]) \n {\n q.push_front(b);\n q.push_back(a);\n mp[b]+=1;\n mp[a]=0;\n } \n else \n {\n q.push_front(a);\n q.push_back(b);\n mp[a]+=1;\n mp[b]=0;\n }\n if(mp[a] >= k) return a;\n else if(mp[b] >= k) return b;\n }\n return q.front();\n }\n};", "memory": "203069" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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 int mx=-1,mxpos,n=skills.size(),x,y,val,prev=-1,i;\n deque<int> q;\n map<int,int> mp;\n\n for(i=0;i<n;i++){\n q.push_back(skills[i]);\n mp[skills[i]]=k;\n\n if(skills[i]>mx){\n mx=skills[i];\n mxpos=i;\n }\n }\n\n if(k>=n)\n return mxpos;\n\n while(true){\n x=q.front();\n q.pop_front();\n y=q.front();\n q.pop_front();\n\n if(x==mx or y==mx)\n return mxpos;\n \n if(x>y){\n q.push_front(x);\n q.push_back(y);\n\n if(prev!=-1 and prev!=x){\n mp[prev]=k;\n prev=-1;\n }\n \n prev=x;\n mp[prev]--;\n\n if(mp[prev]==0){\n val=prev;\n break;\n }\n }\n else{\n q.push_front(y);\n q.push_back(x);\n\n if(prev!=-1 and prev!=y){\n mp[prev]=k;\n prev=-1;\n }\n \n prev=y;\n mp[prev]--;\n\n if(mp[prev]==0){\n val=prev;\n break;\n }\n }\n }\n\n for(i=0;i<n;i++){\n if(skills[i]==val)\n return i;\n }\n\n return 0;\n }\n};", "memory": "204368" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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> arr(n, 0);\n int x = 0, y = 0; \n unordered_map<int, int> mp;\n for(int i = 0; i < n; i++) {\n mp[skills[i]] = i;\n }\n int max_el = *max_element(skills.begin(), skills.end());\n deque<int>q;\n for(auto it: skills) {\n q.push_back(it);\n }\n while(q.front() != max_el) {\n x = q.front();\n q.pop_front();\n y = q.front();\n q.pop_front();\n if(x > y) {\n arr[mp[x]]++;\n if(arr[mp[x]] == k) return mp[x];\n q.push_front(x);\n q.push_back(y);\n }\n else {\n arr[mp[y]]++;\n if(arr[mp[y]] == k) return mp[y];\n q.push_front(y);\n q.push_back(x);\n }\n }\n return mp[max_el];\n }\n};", "memory": "205666" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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, pair<int, int>> mp;\n int mx = 0;\n int n = skills.size();\n for(int i = 0; i < n; i++){\n mp[skills[i]] = {i, 0};\n mx = max(mx, skills[i]);\n }\n int val = skills[0];\n for(int i = 1; i < n; i++){\n if(val == mx)break;\n if(val > skills[i]){\n auto& tmp = mp[val];\n tmp.second++;\n if(tmp.second == k)return tmp.first;\n }\n else{\n val = skills[i];\n auto& tmp = mp[val];\n tmp.second++;\n if(tmp.second == k)return tmp.first;\n }\n }\n return mp[mx].first;\n }\n};", "memory": "206965" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 10<sup>6</sup></code></li> <li>All integers in <code>skills</code> are unique.</li> </ul>
3
{ "code": "// same ques as 1535. Find the Winner of an Array Game (got to know after contest)...\n\nclass Solution {\npublic:\n int findWinningPlayer(vector<int>& a, int k) {\n int ans=0;\n queue<int> q;\n map<int,int> m;\n for(int i=0;i<a.size();i++)\n q.push(a[i]);\n for(int i=0;i<a.size();i++)\n m[a[i]]=i;\n int count=0;\n int curr=q.front();\n q.pop();\n while(1)\n {\n if(q.front()<curr)\n count++,q.push(q.front());\n else\n q.push(curr),curr=q.front(),count=1;\n q.pop();\n int yy=a.size()-1;\n if(count==min(yy,k))\n return (*m.find(curr)).second;\n }\n return -1;\n }\n};", "memory": "208264" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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 unordered_map<int, int> map;\n int i = 0;\n for(auto skill : skills) {\n dq.push_back(skill);\n map[skill] = i;\n i++;\n }\n int wins = 0, curr = skills[0];\n while(1) {\n auto first = dq.front();dq.pop_front();\n auto second = dq.front();dq.pop_front();\n if(first > second) {\n dq.push_back(second);\n wins++;\n dq.push_front(first);\n curr = first;\n }\n else {\n curr = second;\n wins = 1;\n dq.push_back(first);\n dq.push_front(second);\n }\n if(wins == k or wins >= skills.size()-1) {\n break;\n }\n }\n return(map[curr]); \n }\n};", "memory": "209563" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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 map < int, int > m;\n for (int i = 0; i < skills.size(); i++) {\n m[skills[i]] = i;\n }\n int n= skills.size();\n deque<int> de;\n for (int i = 0; i < skills.size(); i++) {\n de.push_back(skills[i]);\n }\n int maxy = INT_MIN, cnt = 0,ans,cnt1=0;\n while (!de.empty()) {\n int a = de.front();\n de.pop_front();\n int b = de.front();\n // cout<<a<<\" \"<<b<<endl;\n cnt1++;\n if (a > b) {\n de.pop_front();\n de.push_back(b);\n de.push_front(a);\n if (maxy == a) {\n cnt++;\n } else {\n maxy = a;\n cnt = 1;\n }\n } else {\n de.push_back(a);\n if (maxy == b) {\n cnt++;\n } else {\n maxy = b;\n cnt = 1;\n }\n }\n if (cnt == k) {\n ans= m[maxy];\n break;\n }\n if(cnt1>=n-1){\n ans=m[maxy];\n break;\n }\n }\n return ans;\n }\n};", "memory": "210861" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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({skills[i], i});\n unordered_map<int,int> mp;\n while(true){\n pair<int,int> a = dq.front();\n dq.pop_front();\n pair<int,int> b = dq.front();\n dq.pop_front();\n if(a.first > b.first){\n dq.push_front(a);\n dq.push_back(b);\n mp[a.first]++;\n mp[b.first] = 0;\n if(mp[a.first] == k || mp[a.first] == n-1)\n return a.second;\n }else{\n dq.push_front(b);\n dq.push_back(a);\n mp[b.first]++;\n mp[a.first] = 0;\n if(mp[b.first] == k || mp[b.first] == n-1)\n return b.second;\n }\n }\n return 0;\n }\n};", "memory": "212160" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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 unordered_map<int,int> mpp;\n int maxi = INT_MIN;\n int maxIndex = -1;\n for(int i=0;i<n;i++){\n mpp[i] = skills[i];\n if(skills[i] > maxi){\n maxi = max(skills[i],maxi);\n maxIndex = i;\n }\n }\n if(k>=n)return maxIndex;\n // for(auto it:mpp){\n // cout<<it.first<<\" \"<<it.second<<endl;\n // }\n unordered_map<int,int> winCnt;\n deque<int> dq;\n for(int i=0;i<n;i++){\n dq.push_back(i);\n }\n while(true){\n int first = dq.front();\n dq.pop_front();\n int second = dq.front();\n dq.pop_front();\n if(mpp[first] < mpp[second]){\n winCnt[second]++;\n if(winCnt[second]==k)return second;\n dq.push_front(second);\n dq.push_back(first);\n }\n else{\n winCnt[first]++;\n if(winCnt[first]==k)return first;\n dq.push_front(first);\n dq.push_back(second);\n }\n }\n return -1;\n }\n};", "memory": "217355" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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>& a, int k) {\n int n= a.size();\n unordered_map<int,int> playerwins,mapping;\n \n for(int i=0;i<n;i++)\n mapping[a[i]]= i;\n \n int check=0;\n deque<int> q;\n \n for(int i=0;i<n;i++)\n q.push_back(a[i]);\n \n if(k >= n)\n return mapping[*max_element(begin(a),end(a))];\n \n while(true){\n int first= q.front();\n q.pop_front();\n int second = q.front();\n \n if(first > second){\n q.pop_front();\n q.push_front(first);\n q.push_back(second);\n playerwins[first]++;\n if(playerwins[first] == k)\n return mapping[first];\n }\n else{\n q.push_back(first);\n playerwins[second]++;\n if(playerwins[second] == k)\n return mapping[second];\n }\n }\n return -1;\n }\n};", "memory": "218654" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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>& a, int k) {\n int n= a.size();\n deque<int> q;\n unordered_map<int,int> playerwins,mapping;\n \n for(int i=0;i<n;i++)\n mapping[a[i]]= i;\n \n for(int i=0;i<n;i++)\n q.push_back(a[i]);\n \n if(k >= n)\n return mapping[*max_element(begin(a),end(a))];\n \n while(true){\n int first= q.front();\n q.pop_front();\n int second = q.front();\n \n if(first > second){\n q.pop_front();\n q.push_front(first);\n q.push_back(second);\n playerwins[first]++;\n if(playerwins[first] == k)\n return mapping[first];\n }\n else{\n q.push_back(first);\n playerwins[second]++;\n if(playerwins[second] == k)\n return mapping[second];\n }\n }\n return -1;\n }\n};", "memory": "218654" }
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>&nbsp;</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>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == skills.length</code></li> <li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li> <li><code>1 &lt;= skills[i] &lt;= 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 int mx=0;\n\n map<int,int>idx;\n for(int i=0;i<n;i++){\n if(mx<skills[i]){\n mx=skills[i];\n \n }\n idx[skills[i]]=i;\n }\n if(k>=n){\n return idx[mx];\n }\n else{\n map<int,int>freq;\n deque<int>q;\n for(auto it:skills){\n q.push_back(it);\n }\n int a=q.front();\n q.pop_front();\n while(freq[a]<k){\n int b=q.front();\n q.pop_front();\n if(a>b){\n freq[a]++;\n q.push_back(b);\n }\n else{\n freq[a]=0;\n freq[b]++;\n q.push_back(a);\n a=b;\n \n }\n }\n return idx[a];\n\n }\n }\n};", "memory": "219953" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n \n int n = nums.size();\n int dp[n][k+1];\n int ans = 0;\n for(int i = 0;i<n;i++){\n for(int j = 0;j<=k;j++){\n int maxi = 0;\n for(int l = 0;l<i;l++){\n if(nums[i]==nums[l]){\n maxi = max(maxi,dp[l][j]);\n }\n else{\n if(j-1>=0) maxi = max(maxi,dp[l][j-1]);\n }\n }\n \n dp[i][j] = 1 + maxi;\n if(j-1>=0) dp[i][j] = max(dp[i][j],dp[i][j-1]);\n if(j==k) ans = max(ans,dp[i][j]);\n }\n }\n\n return ans;\n }\n};", "memory": "25148" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maximumLength(vector<int>& a, int k) {\n int i,j,n=a.size(),t;\n int dp[n][k+1],ans=0;\n memset(dp,0,sizeof(dp));\n dp[0][0]=1;\n for(i=1;i<n;i++){\n dp[i][0]=1;\n for(j=0;j<i;j++){\n for(t=0;t<=k;t++){\n if(a[i]!=a[j] && t>=1) dp[i][t]=max(dp[i][t],dp[j][t-1]+1);\n else if(a[i]==a[j]) dp[i][t]=max(dp[i][t],dp[j][t]+1);\n }\n }\n }\n // for(i=0;i<n;i++){\n // for(j=0;j<=k;j++) cout<<dp[i][j]<<\" \";\n // cout<<\"\\n\";\n // }\n for(i=0;i<n;i++)\n for(j=0;j<=k;j++) ans=max(ans,dp[i][j]);\n // return *max_element(dp[n-1],dp[n-1]+k+1);\n return ans;\n }\n};", "memory": "25148" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
1
{ "code": "template<class Info, class Tag>\nstruct LazySegmentTree {\n const int n;\n std::vector<Info> info;\n std::vector<Tag> tag;\n LazySegmentTree(int n) : n(n), info(4 << std::__lg(n)), tag(4 << std::__lg(n)) {}\n LazySegmentTree(std::vector<Info> init) : LazySegmentTree(init.size()) {\n std::function<void(int, int, int)> build = [&](int p, int l, int r) {\n if (r - l == 1) {\n info[p] = init[l];\n return;\n }\n int m = (l + r) / 2;\n build(2 * p, l, m);\n build(2 * p + 1, m, r);\n pull(p);\n };\n build(1, 0, n);\n }\n void pull(int p) {\n info[p] = info[2 * p] + info[2 * p + 1];\n }\n void apply(int p, const Tag &v) {\n info[p].apply(v);\n tag[p].apply(v);\n }\n void push(int p) {\n apply(2 * p, tag[p]);\n apply(2 * p + 1, tag[p]);\n tag[p] = Tag();\n }\n void modify(int p, int l, int r, int x, const Info &v) {\n if (r - l == 1) {\n info[p] = v;\n return;\n }\n int m = (l + r) / 2;\n push(p);\n if (x < m) {\n modify(2 * p, l, m, x, v);\n } else {\n modify(2 * p + 1, m, r, x, v);\n }\n pull(p);\n }\n void modify(int p, const Info &v) {\n modify(1, 0, n, p, v);\n }\n Info rangeQuery(int p, int l, int r, int x, int y) {\n if (l >= y || r <= x) {\n return Info();\n }\n if (l >= x && r <= y) {\n return info[p];\n }\n int m = (l + r) / 2;\n push(p);\n return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m, r, x, y);\n }\n Info rangeQuery(int l, int r) {\n return rangeQuery(1, 0, n, l, r);\n }\n void rangeApply(int p, int l, int r, int x, int y, const Tag &v) {\n if (l >= y || r <= x) {\n return;\n }\n if (l >= x && r <= y) {\n apply(p, v);\n return;\n }\n int m = (l + r) / 2;\n push(p);\n rangeApply(2 * p, l, m, x, y, v);\n rangeApply(2 * p + 1, m, r, x, y, v);\n pull(p);\n }\n void rangeApply(int l, int r, const Tag &v) {\n return rangeApply(1, 0, n, l, r, v);\n }\n};\n \nstruct Tag {\n int x = 0;\n Tag(int x = 0): x(x) {}\n \n void apply(const Tag &t) {\n x = x + t.x;\n }\n};\n \nstruct Max {\n int x = 0;\n\n void apply(const Tag &t) {\n x += t.x;\n }\n};\n \nMax operator+(const Max &a, const Max &b) {\n if (a.x > b.x) {\n return a;\n } else {\n return b;\n }\n}\n\n\n\nclass Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n vector<vector<int>> dp(k + 2, vector<int>(n + 1));\n unordered_map<int, vector<int>> pos;\n for (int i = 0; i < n; i++){\n pos[nums[i]].push_back(i);\n }\n \n for (int j = 1; j <= k + 1; j++){\n vector<Max> vec(n + 1);\n for (int i = 0; i < n; i++){\n vec[i + 1].x = dp[j - 1][i + 1];\n }\n LazySegmentTree<Max, Tag> seg(vec);\n \n for (auto &&[k, v] : pos){\n for (auto &val : v){\n seg.rangeApply(0, val + 1, Tag(1));\n auto res = seg.rangeQuery(0, val + 1);\n dp[j][val + 1] = max(dp[j][val], res.x);\n }\n for (auto &val : v){\n seg.rangeApply(0, val + 1, Tag(-1));\n }\n }\n }\n int res = 0;\n for (int i = 0; i <= n; i++){\n res = max(res, dp[k + 1][i]);\n }\n return res;\n //dp[n][k + 1];\n \n }\n};", "memory": "32644" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
1
{ "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));\nll dp[505][26];\nclass Solution {\npublic:\n vector<int> v;\n ll n,k;\n // ll rec(ll lev,ll klf){\n // if(lev==n)return 0;\n // auto &ans=dp[lev][klf];\n // if(ans!=-1)return ans;\n // ans=rec(lev+1,klf);\n // if(pv==n){\n // ans=max(ans,1+rec(lev+1,klf));\n // return ans;\n // }\n // if(v[pv]==v[lev])ans=max(ans,1+rec(lev+1,klf));\n // else{\n // if(klf){\n // ans=max(ans,1+rec(lev+1,klf-1));\n // }\n // }\n // return ans;\n // }\n int maximumLength(vector<int>& nums, int kk) {\n v=nums;\n n=v.size();\n k=kk;\n REP(i,0,n+2){\n REP(j,0,25){\n dp[i][j]=0;\n }\n }\n map<ll,map<ll,ll>>maxx;\n map<ll,ll>maxx1;\n // n-1-i\n ll mx=1;\n RREP(i,n-1,0){\n REP(j,0,k){\n // REP(l,i+1,n){\n // if(j)dp[i][j]=max(dp[l][j-1]+1,dp[i][j]);\n // if(l<n && v[l]==v[i])dp[i][j]=max(1+dp[l][j],dp[i][j]);\n // } \n dp[i][j]=max((ll)1,dp[i][j]);\n if(j)dp[i][j]=max((ll)maxx1[j-1]+1,dp[i][j]);\n dp[i][j]=max((ll)maxx[j][v[i]]+1,dp[i][j]);\n cout<<dp[i][j]<<\" \";\n } \n REP(j,0,25){\n maxx1[j]=max(maxx1[j],dp[i][j]);\n maxx[j][v[i]]=max(maxx[j][v[i]],dp[i][j]);\n }\n\n cout<<'\\n';\n mx=max(mx,dp[i][k]);\n }\n\n return mx;\n }\n};", "memory": "32644" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int som=1;\n vector<int>res;\n for(int i=0;i<(nums.size()-1);i++){\n if(nums[i]==nums[i+1]){\n som++;\n }\n else{\n res.push_back(som);\n som=1;\n }\n }\n res.push_back(som);\n int val=0;\n int ans2=0;\n vector<int>::iterator ip;\n ip = unique(nums.begin(),nums.end());\n nums.resize(distance(nums.begin(), ip));\n vector<vector<int>>dp1(nums.size());\n vector<vector<int>>dp2(nums.size());\n map<int,int>hashing;\n for(int i=0;i<nums.size();i++){\n hashing.clear();\n hashing[0]=res[i];\n for(int j=0;j<i;j++){\n if(nums[i]!=nums[j]){\n for(int c=0;c<dp1[j].size();c++){\n if((dp1[j][c]+1)<=k){\n hashing[dp1[j][c]+1]=max(hashing[dp1[j][c]+1],dp2[j][c]+res[i]);\n }\n } \n }\n else{\n for(int c=0;c<dp1[j].size();c++){\n if((dp1[j][c])<=k){\n hashing[dp1[j][c]]=max(hashing[dp1[j][c]],dp2[j][c]+res[i]);\n } \n }\n }\n }\n for(auto v:hashing){\n dp1[i].push_back(v.first);\n dp2[i].push_back(v.second);\n }}\n int ans=0;\n for(int i=0;i<dp2.size();i++){\n for(auto v:dp2[i]){\n ans=max(ans,v);\n }\n }\nreturn ans;\n \n\n \n \n \n\n\n }\n};", "memory": "40140" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n dp = vector<vector<int>>(503,vector<int>(k+1,0));\n // [x,x,x,j,x,x],x,x,i\n int n = nums.size();\n int ret = 0;\n\n for(int i = 0; i < n; i++){\n for(int t = 0; t <= k; t++){\n int ans = 1;\n for(int j = 0; j < i; j++){\n if(nums[i] == nums[j]){\n ans = max(ans, dp[j][t] + 1);\n }else{\n if(t > 0){\n ans = max(ans, dp[j][t-1] + 1);\n }\n }\n }\n\n dp[i][t] = ans;\n ret = max(ret,ans);\n }\n }\n\n return ret;\n }\n\n \nprivate:\n vector<vector<int>> dp;\n};", "memory": "40140" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
1
{ "code": "//Q: A sequence of integers seq is called good if there are ATMOST k indices i in the range [0, seq.length - 2] such that seq[i] != seq[i + 1]. Return the maximum possible LENGTH of a good subsequence of nums[].\n#define ll long long\nint dp[501][501][26];\n//1 <= nums.length <= 500, 0 <= k <= min(nums.length, 25)\n\nclass Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) \n {\n memset(dp, -1, sizeof(dp));\n\n int currIndex = 0, prevIndex = 500; //prevIndex is set to 500 to avoid -ve array index in dp[][][]\n return solve(nums, k, currIndex, prevIndex);\n }\n\n int solve(vector<int>& nums, int k, int currIndex, int prevIndex)\n {\n if (currIndex == nums.size())\n return 0; //end of nums[] reached\n\n if (dp[currIndex][prevIndex][k] != -1)\n return dp[currIndex][prevIndex][k];\n\n int taken = 0, not_taken = 0;\n\n if (prevIndex == 500 || nums[prevIndex] == nums[currIndex])\n {\n not_taken = solve(nums, k, currIndex + 1, prevIndex);\n taken = 1 + solve(nums, k, currIndex + 1, currIndex); //k won't be reduced since nums[prevIndex] == nums[currIndex]\n }\n else if (nums[prevIndex] != nums[currIndex])\n {\n not_taken = solve(nums, k, currIndex + 1, prevIndex);\n\n if (k > 0)\n taken = 1 + solve(nums, k - 1, currIndex + 1, currIndex); //k will be reduced since nums[prevIndex] != nums[currIndex]\n }\n\n return dp[currIndex][prevIndex][k] = max(taken, not_taken);\n }\n};", "memory": "47636" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
1
{ "code": "//Q: A sequence of integers seq is called good if there are ATMOST k indices i in the range [0, seq.length - 2] such that seq[i] != seq[i + 1]. Return the maximum possible LENGTH of a good subsequence of nums[].\n#define ll long long\nint dp[501][501][26];\n//1 <= nums.length <= 500, 0 <= k <= min(nums.length, 25)\n\nclass Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) \n {\n memset(dp, -1, sizeof(dp));\n\n int currIndex = 0, prevIndex = 500; //prevIndex is set to 500 to avoid -ve array index in dp[][][]\n return solve(nums, k, currIndex, prevIndex);\n }\n\n int solve(vector<int>& nums, int k, int currIndex, int prevIndex)\n {\n if (currIndex == nums.size())\n return 0; //end of nums[] reached\n\n if (dp[currIndex][prevIndex][k] != -1)\n return dp[currIndex][prevIndex][k];\n\n int taken = 0, not_taken = 0; //based on whether currIndex element is included or not\n\n if (prevIndex == 500 || nums[prevIndex] == nums[currIndex])\n {\n not_taken = solve(nums, k, currIndex + 1, prevIndex);\n taken = 1 + solve(nums, k, currIndex + 1, currIndex); \n //k won't be reduced since nums[prevIndex] == nums[currIndex], length of sequence is increased by 1 for \n }\n else if (nums[prevIndex] != nums[currIndex])\n {\n not_taken = solve(nums, k, currIndex + 1, prevIndex);\n\n if (k > 0)\n taken = 1 + solve(nums, k - 1, currIndex + 1, currIndex); \n //k will be reduced since nums[prevIndex] != nums[currIndex]\n }\n\n return dp[currIndex][prevIndex][k] = max(taken, not_taken);\n }\n};", "memory": "47636" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int dp[502][502][27];\n\n int helper(int index, int lst, int k, vector<int> &nums){\n if(index > nums.size())\n return 0;\n if(dp[index][lst][k] != -1)\n return dp[index][lst][k];\n dp[index][lst][k] = helper(index + 1, lst, k, nums);\n if(lst == 0)\n dp[index][lst][k] = max(dp[index][lst][k], helper(index + 1, index, k, nums) + 1);\n else{\n if(nums[index - 1] == nums[lst - 1])\n dp[index][lst][k] = max(dp[index][lst][k], helper(index + 1, index, k, nums) + 1);\n else if(k)\n dp[index][lst][k] = max(dp[index][lst][k], helper(index + 1, index, k - 1, nums) + 1);\n }\n return dp[index][lst][k];\n }\n\n int maximumLength(vector<int>& nums, int k) {\n memset(dp, -1, sizeof(dp));\n return helper(1, 0, k, nums);\n }\n};", "memory": "55133" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int dp[502][502][27];\n int fun(vector<int> &v, int x ,int k, int last, int n) {\n // cout << x << \" \" << k << endl;\n if(n <= x) {\n return 0;\n }\n if(dp[x][last][k] != -1) {\n return dp[x][last][k];\n }\n int t = 0;\n // cout << last << \" \" << x << endl;\n if(last != 501 && k != 0 && v[last] != v[x]) {\n t = max(t, fun(v, x + 1, k - 1, x, n) + 1);\n }\n if(last == 501 || v[last] == v[x]) {\n t = max(t, fun(v, x + 1, k, x, n) + 1);\n }\n t = max(t, fun(v, x + 1, k, last, n));\n dp[x][last][k] = t;\n return t;\n }\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n memset(dp, -1, sizeof(dp));\n int ans = fun(nums, 0, k, 501, n);\n return ans;\n }\n};", "memory": "55133" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int dp[600][600][30];\nint f(int index,int prevIndex,int k,vector<int> &nums){\n if(index==nums.size()) return 0;\n\n if(dp[index][prevIndex+1][k]!=-1) return dp[index][prevIndex+1][k];\n int pick = 0;\n // pick\n if(prevIndex==-1 || nums[prevIndex]==nums[index]){\n pick = 1+f(index+1,index,k,nums);\n }\n else if(nums[prevIndex]!=nums[index] && k>0){\n pick = 1+f(index+1,index,k-1,nums);\n }\n\n // not pick\n int notPick = f(index+1,prevIndex,k,nums);\n \n return dp[index][prevIndex+1][k] =max(pick,notPick);\n }\n \n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n \n memset(dp,0,sizeof(dp));\n\n for(int index=n-1;index>=0;index--){\n for(int prevIndex=index-1;prevIndex>=-1;prevIndex--){\n for(int kk=0;kk<=k;kk++){\n \n int pick = 0;\n if(prevIndex==-1 || nums[prevIndex]==nums[index]){\n pick = 1 + dp[index+1][index+1][kk];\n }\n else if(nums[prevIndex]!=nums[index] && kk>0){\n pick = 1 + dp[index+1][index+1][kk-1];\n }\n\n int notPick = dp[index+1][prevIndex+1][kk];\n\n dp[index][prevIndex+1][kk] =max(pick,notPick);\n }\n }\n }\n return dp[0][0][k];\n }\n};", "memory": "62629" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int dp[600][600][30];\nint f(int index,int prevIndex,int k,vector<int> &nums){\n if(index==nums.size()) return 0;\n\n if(dp[index][prevIndex+1][k]!=-1) return dp[index][prevIndex+1][k];\n int pick = 0;\n // pick\n if(prevIndex==-1 || nums[prevIndex]==nums[index]){\n pick = 1+f(index+1,index,k,nums);\n }\n else if(nums[prevIndex]!=nums[index] && k>0){\n pick = 1+f(index+1,index,k-1,nums);\n }\n\n // not pick\n int notPick = f(index+1,prevIndex,k,nums);\n \n return dp[index][prevIndex+1][k] =max(pick,notPick);\n }\n \n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n \n memset(dp,-1,sizeof(dp));\n\n return f(0,-1,k,nums);\n }\n};", "memory": "70125" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int dp[600][600][30];\nint f(int index,int prevIndex,int k,vector<int> &nums){\n if(index==nums.size()) return 0;\n\n if(dp[index][prevIndex+1][k]!=-1) return dp[index][prevIndex+1][k];\n int pick = 0;\n // pick\n if(prevIndex==-1 || nums[prevIndex]==nums[index]){\n pick = 1+f(index+1,index,k,nums);\n }\n else if(nums[prevIndex]!=nums[index] && k>0){\n pick = 1+f(index+1,index,k-1,nums);\n }\n\n // not pick\n int notPick = f(index+1,prevIndex,k,nums);\n \n return dp[index][prevIndex+1][k] =max(pick,notPick);\n }\n \n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n \n memset(dp,0,sizeof(dp));\n\n for(int index=n-1;index>=0;index--){\n for(int prevIndex=n-1;prevIndex>=-1;prevIndex--){\n for(int kk=0;kk<=k;kk++){\n \n int pick = 0;\n if(prevIndex==-1 || nums[prevIndex]==nums[index]){\n pick = 1 + dp[index+1][index+1][kk];\n }\n else if(nums[prevIndex]!=nums[index] && kk>0){\n pick = 1 + dp[index+1][index+1][kk-1];\n }\n\n int notPick = dp[index+1][prevIndex+1][kk];\n\n dp[index][prevIndex+1][kk] =max(pick,notPick);\n }\n }\n }\n return dp[0][0][k];\n }\n};", "memory": "70125" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
2
{ "code": "\n\n\n\nclass Solution {\n // int dp[505][26];\npublic:\n int maximumLength(vector<int>& nums, int k) {\n vector<vector<int>> dp(505, vector<int>(26, 0));\n int N = nums.size();\n int rst = 1;\n for (int i=0; i<N; i++){\n for (int t=0; t<=k; t++){\n int temp = 1;\n for (int j=0; j<i; j++){\n if (nums[j] == nums[i])\n temp = max(temp, dp[j][t]+1);\n else if (t>=1)\n temp = max(temp, dp[j][t-1]+1);\n }\n dp[i][t] = temp;\n rst = max(rst, temp);\n }\n }\n // return dp[N][k];\n return rst;\n }\n};", "memory": "77621" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
2
{ "code": "\n// https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/3177.Find-the-Maximum-Length-of-a-Good-Subsequence-II\n// 对于常规的DP解法,我们容易设置状态变量dp[i][t]表示前i个元素里、\n// 我们已经出现了t次相邻元素不等的情况下,能够得到的goode subsequence的最大长度。\n// 显然转移的突破口就在于nums[i]是否与sequence的前一个元素相同。\n// 我们枚举j<i作为序列里i之前的一个元素,只要nums[j]==nums[i],\n// 意味着此次append nums[i]不会增加“相邻元素不等”的情况,\n// 故有 dp[i][t] = dp[j][t]+1. 如果nums[j]!=nums[i],那么nums[i]的加\n// 入会增加一次“相邻元素不等”,故dp[i][t] = dp[j][t-1]+1.\n// 显然我们会在j的遍历中,对于dp[i][t]取最大值。\n\n\n// TC=O(N^2*k);\n// SC=O(N*K);\nclass Solution {\n // int dp[505][26];\npublic:\n int maximumLength(vector<int>& nums, int k) {\n vector<vector<int>> dp(505, vector<int>(26, 0));\n int N = nums.size();\n int rst = 1;\n for (int i=0; i<N; i++){\n for (int t=0; t<=k; t++){\n int temp = 1;\n for (int j=0; j<i; j++){\n if (nums[j] == nums[i])\n temp = max(temp, dp[j][t]+1);\n else if (t>=1)\n temp = max(temp, dp[j][t-1]+1);\n }\n dp[i][t] = temp;\n rst = max(rst, temp);\n }\n }\n return rst;\n }\n};", "memory": "77621" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
2
{ "code": "using int64 = long long;\n\nclass segtree {\npublic:\n struct node {\n // don't forget to set default value (used for leaves)\n // not necessarily neutral element!\n int64 tag = 0, mx = 0;\n\n void apply(int l, int r, int64 v) {\n tag = v;\n mx = (r - l + 1) * v;\n }\n };\n\n node unite(const node &a, const node &b) const {\n node res;\n res.mx = max(a.mx, b.mx);\n return res;\n }\n\n inline void push(int x, int l, int r) {\n int y = (l + r) >> 1;\n int z = x + ((y - l + 1) << 1);\n // push from x into (x + 1) and z\n if (tree[x].tag != 0) {\n tree[x + 1].apply(l, y, tree[x].tag);\n tree[z].apply(y + 1, r, tree[x].tag);\n tree[x].tag = 0;\n }\n }\n\n inline void pull(int x, int z) {\n tree[x] = unite(tree[x + 1], tree[z]);\n }\n\n int n;\n vector<node> tree;\n\n void build(int x, int l, int r) {\n if (l == r) {\n return;\n }\n int y = (l + r) >> 1;\n int z = x + ((y - l + 1) << 1);\n build(x + 1, l, y);\n build(z, y + 1, r);\n pull(x, z);\n }\n\n template <typename M>\n void build(int x, int l, int r, const vector<M> &v) {\n if (l == r) {\n tree[x].apply(l, r, v[l]);\n return;\n }\n int y = (l + r) >> 1;\n int z = x + ((y - l + 1) << 1);\n build(x + 1, l, y, v);\n build(z, y + 1, r, v);\n pull(x, z);\n }\n\n node get(int x, int l, int r, int ll, int rr) {\n if (ll <= l && r <= rr) {\n return tree[x];\n }\n int y = (l + r) >> 1;\n int z = x + ((y - l + 1) << 1);\n push(x, l, r);\n node res{};\n if (rr <= y) {\n res = get(x + 1, l, y, ll, rr);\n } else {\n if (ll > y) {\n res = get(z, y + 1, r, ll, rr);\n } else {\n res = unite(get(x + 1, l, y, ll, rr), get(z, y + 1, r, ll, rr));\n }\n }\n pull(x, z);\n return res;\n }\n\n template <typename... M>\n void modify(int x, int l, int r, int ll, int rr, const M&... v) {\n if (ll <= l && r <= rr) {\n tree[x].apply(l, r, v...);\n return;\n }\n int y = (l + r) >> 1;\n int z = x + ((y - l + 1) << 1);\n push(x, l, r);\n if (ll <= y) {\n modify(x + 1, l, y, ll, rr, v...);\n }\n if (rr > y) {\n modify(z, y + 1, r, ll, rr, v...);\n }\n pull(x, z);\n }\n\n segtree(int _n) : n(_n) {\n assert(n > 0);\n tree.resize(2 * n - 1);\n build(0, 0, n - 1);\n }\n\n template <typename M>\n segtree(const vector<M> &v) {\n n = v.size();\n assert(n > 0);\n tree.resize(2 * n - 1);\n build(0, 0, n - 1, v);\n }\n\n node get(int ll, int rr) {\n assert(0 <= ll && ll <= rr && rr <= n - 1);\n return get(0, 0, n - 1, ll, rr);\n }\n\n node get(int p) {\n assert(0 <= p && p <= n - 1);\n return get(0, 0, n - 1, p, p);\n }\n\n template <typename... M>\n void modify(int ll, int rr, const M&... v) {\n assert(0 <= ll && ll <= rr && rr <= n - 1);\n modify(0, 0, n - 1, ll, rr, v...);\n }\n};\n\nclass Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n \n map<int, vector<int>> pos;\n vector<int> pre(n); \n for (int i = 0; i < n; i++) {\n if (pos[nums[i]].empty()) {\n pre[i] = 0;\n } else {\n pre[i] = pos[nums[i]].back();\n }\n pos[nums[i]].push_back(i + 1);\n }\n \n vector<segtree> trees(k + 1, segtree(505));\n int64 ans = 0;\n for (int i = 1; i <= n; i++) {\n for (int c = 0; c <= k; c++) {\n trees[c].modify(i, i, \n max(trees[c].get(i, i).mx,\n trees[c].get(pre[i - 1], pre[i - 1]).mx + 1)\n );\n \n trees[c].modify(i, i,\n max(trees[c].get(i, i).mx,\n (c == 0 ? 0 : trees[c - 1].get(min(i - 1, pre[i - 1] + 1), i - 1).mx) + 1)\n );\n ans = max(ans, trees[c].get(i, i).mx);\n }\n }\n return ans;\n }\n};\n", "memory": "85118" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
2
{ "code": "using int64 = long long;\n\nclass segtree {\npublic:\n struct node {\n // don't forget to set default value (used for leaves)\n // not necessarily neutral element!\n int64 tag = 0, mx = 0;\n\n void apply(int l, int r, int64 v) {\n tag = v;\n mx = (r - l + 1) * v;\n }\n };\n\n node unite(const node &a, const node &b) const {\n node res;\n res.mx = max(a.mx, b.mx);\n return res;\n }\n\n inline void push(int x, int l, int r) {\n int y = (l + r) >> 1;\n int z = x + ((y - l + 1) << 1);\n // push from x into (x + 1) and z\n if (tree[x].tag != 0) {\n tree[x + 1].apply(l, y, tree[x].tag);\n tree[z].apply(y + 1, r, tree[x].tag);\n tree[x].tag = 0;\n }\n }\n\n inline void pull(int x, int z) {\n tree[x] = unite(tree[x + 1], tree[z]);\n }\n\n int n;\n vector<node> tree;\n\n void build(int x, int l, int r) {\n if (l == r) {\n return;\n }\n int y = (l + r) >> 1;\n int z = x + ((y - l + 1) << 1);\n build(x + 1, l, y);\n build(z, y + 1, r);\n pull(x, z);\n }\n\n template <typename M>\n void build(int x, int l, int r, const vector<M> &v) {\n if (l == r) {\n tree[x].apply(l, r, v[l]);\n return;\n }\n int y = (l + r) >> 1;\n int z = x + ((y - l + 1) << 1);\n build(x + 1, l, y, v);\n build(z, y + 1, r, v);\n pull(x, z);\n }\n\n node get(int x, int l, int r, int ll, int rr) {\n if (ll <= l && r <= rr) {\n return tree[x];\n }\n int y = (l + r) >> 1;\n int z = x + ((y - l + 1) << 1);\n push(x, l, r);\n node res{};\n if (rr <= y) {\n res = get(x + 1, l, y, ll, rr);\n } else {\n if (ll > y) {\n res = get(z, y + 1, r, ll, rr);\n } else {\n res = unite(get(x + 1, l, y, ll, rr), get(z, y + 1, r, ll, rr));\n }\n }\n pull(x, z);\n return res;\n }\n\n template <typename... M>\n void modify(int x, int l, int r, int ll, int rr, const M&... v) {\n if (ll <= l && r <= rr) {\n tree[x].apply(l, r, v...);\n return;\n }\n int y = (l + r) >> 1;\n int z = x + ((y - l + 1) << 1);\n push(x, l, r);\n if (ll <= y) {\n modify(x + 1, l, y, ll, rr, v...);\n }\n if (rr > y) {\n modify(z, y + 1, r, ll, rr, v...);\n }\n pull(x, z);\n }\n\n segtree(int _n) : n(_n) {\n assert(n > 0);\n tree.resize(2 * n - 1);\n build(0, 0, n - 1);\n }\n\n template <typename M>\n segtree(const vector<M> &v) {\n n = v.size();\n assert(n > 0);\n tree.resize(2 * n - 1);\n build(0, 0, n - 1, v);\n }\n\n node get(int ll, int rr) {\n assert(0 <= ll && ll <= rr && rr <= n - 1);\n return get(0, 0, n - 1, ll, rr);\n }\n\n node get(int p) {\n assert(0 <= p && p <= n - 1);\n return get(0, 0, n - 1, p, p);\n }\n\n template <typename... M>\n void modify(int ll, int rr, const M&... v) {\n assert(0 <= ll && ll <= rr && rr <= n - 1);\n modify(0, 0, n - 1, ll, rr, v...);\n }\n};\n\nclass Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n \n map<int, vector<int>> pos;\n vector<int> pre(n); \n for (int i = 0; i < n; i++) {\n if (pos[nums[i]].empty()) {\n pre[i] = 0;\n } else {\n pre[i] = pos[nums[i]].back();\n }\n pos[nums[i]].push_back(i + 1);\n }\n \n vector<segtree> trees(k + 1, segtree(505));\n int64 ans = 0;\n for (int i = 1; i <= n; i++) {\n for (int c = 0; c <= k; c++) {\n trees[c].modify(i, i, \n max(trees[c].get(i, i).mx,\n trees[c].get(pre[i - 1], pre[i - 1]).mx + 1)\n );\n \n trees[c].modify(i, i,\n max(trees[c].get(i, i).mx,\n (c == 0 ? 0 : trees[c - 1].get(min(i - 1, pre[i - 1] + 1), i - 1).mx) + 1)\n );\n ans = max(ans, trees[c].get(i, i).mx);\n }\n }\n return ans;\n }\n};\n", "memory": "85118" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int dp[600][600][50];\n int f(int i,int p,int cnt,int k,vector<int>& nums){\n if(i==nums.size()) return 0;\n if(dp[i][p+1][cnt]!=-1) return dp[i][p+1][cnt];\n int ans=0;\n if(p==-1){\n ans=max(ans,1+f(i+1,i,cnt,k,nums));\n ans=max(ans,f(i+1,p,cnt,k,nums));\n } else {\n if(nums[p]==nums[i]){\n ans=max(ans,1+f(i+1,i,cnt,k,nums)); \n } else {\n ans=max(ans,f(i+1,p,cnt,k,nums));\n if(k>cnt){\n ans=max(ans,1+f(i+1,i,cnt+1,k,nums)); \n } \n }\n }\n return dp[i][p+1][cnt]=ans;\n }\n int maximumLength(vector<int>& nums, int k) {\n memset(dp, -1, sizeof(dp));\n return f(0,-1,0, k, nums); \n }\n};\n\n", "memory": "92614" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int dp[600][600][50];\n int f(int i,int p,int cnt,int k,vector<int>& nums){\n if(i==nums.size()) return 0;\n if(dp[i][p+1][cnt]!=-1) return dp[i][p+1][cnt];\n int ans=0;\n if(p==-1){\n ans=max(ans,1+f(i+1,i,cnt,k,nums));\n ans=max(ans,f(i+1,p,cnt,k,nums));\n } else {\n if(nums[p]==nums[i]){\n ans=max(ans,1+f(i+1,i,cnt,k,nums)); \n } else {\n ans=max(ans,f(i+1,p,cnt,k,nums));\n if(k>cnt){\n ans=max(ans,1+f(i+1,i,cnt+1,k,nums)); \n } \n }\n }\n return dp[i][p+1][cnt]=ans;\n }\n int maximumLength(vector<int>& nums, int k) {\n memset(dp, -1, sizeof(dp));\n return f(0,-1,0, k, nums); \n }\n};", "memory": "100110" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int dp[600][600][50];\n int f(int i, int p, int cnt, int k, vector<int>& nums) {\n if (i == nums.size())\n return 0;\n if (dp[i][p + 1][cnt] != -1)\n return dp[i][p + 1][cnt];\n int ans = 0;\n if (p == -1) {\n ans = max(ans, 1 + f(i + 1, i, cnt, k, nums));\n ans = max(ans, f(i + 1, p, cnt, k, nums));\n } else {\n if (nums[p] == nums[i]) {\n ans = max(ans, 1 + f(i + 1, i, cnt, k, nums));\n } else {\n ans = max(ans, f(i + 1, p, cnt, k, nums));\n if (k > cnt) {\n ans = max(ans, 1 + f(i + 1, i, cnt + 1, k, nums));\n }\n }\n }\n return dp[i][p + 1][cnt] = ans;\n }\n int maximumLength(vector<int>& nums, int k) {\n memset(dp, -1, sizeof(dp));\n return f(0, -1, 0, k, nums);\n }\n};\n", "memory": "100110" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
2
{ "code": "#include <bits/stdc++.h>\n#pragma GCC optimize(2)\n#define rep(i, a, b) for (int i = (a); i < (b); ++i)\n#define rep_(i, a, b) for (int i = (a); i > (b); i--)\n#define mst(x, a) memset(x, a, sizeof(x))\n#define all(a) begin(a), end(a)\n#define lowbit(x) ((x) & (-(x)))\n#define bitcnt(x) (__builtin_popcountll(x))\n#define se second\n#define fi first\n#define pb push_back\n#define maxe max_element\n#define mine min_element\nusing namespace std;\ntypedef long long ll;\ntypedef unsigned long long ull;\ntypedef pair<ll, ll> pll;\ntypedef pair<int, ll> pil;\ntypedef pair<int, int> pii;\ntypedef vector<int> vi;\ntypedef vector<ll> vll;\ntypedef vector<vi> vvi;\nconstexpr static int dirs[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};\nconstexpr static int inf = 0x3f3f3f3f, mod = 1e9 + 7;\nconst ll infl = 0x3f3f3f3f3f3f3f3fll;\ntemplate<class T> bool chmax(T &a, T b) {\n if (a >= b) return false;\n a = b; return true;\n}\ntemplate<class T> bool chmin(T &a, T b) {\n if (a <= b) return false;\n a = b; return true;\n}\ntemplate<class T> bool chsum(T &a, T b) {\n a = (a + b + mod) % mod;\n return 0;\n}\nclass Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n \n // 离散化处理\n set<int> unique_nums_set(nums.begin(), nums.end());\n vector<int> unique_nums(unique_nums_set.begin(), unique_nums_set.end());\n unordered_map<int, int> mapping;\n for (int i = 0; i < unique_nums.size(); ++i) {\n mapping[unique_nums[i]] = i;\n }\n vector<int> mapped_nums(n);\n for (int i = 0; i < n; ++i) {\n mapped_nums[i] = mapping[nums[i]];\n }\n int max_value = unique_nums.size();\n \n // 定义 dp 数组\n vector<vector<vector<int>>> dp(n, vector<vector<int>>(max_value, vector<int>(k + 1, 0)));\n \n // 初始化\n dp[0][mapped_nums[0]][0] = 1;\n \n // 更新 dp 数组\n for (int i = 1; i < n; ++i) {\n for (int j = 0; j < i; ++j) {\n for (int l = 0; l < k + 1; ++l) {\n if (mapped_nums[i] == mapped_nums[j]) {\n dp[i][mapped_nums[i]][l] = max(dp[i][mapped_nums[i]][l], dp[j][mapped_nums[j]][l] + 1);\n } else {\n if (l < k) {\n dp[i][mapped_nums[i]][l + 1] = max(dp[i][mapped_nums[i]][l + 1], dp[j][mapped_nums[j]][l] + 1);\n }\n }\n }\n }\n // 初始化第 i 个元素第一次出现的情况\n dp[i][mapped_nums[i]][0] = max(dp[i][mapped_nums[i]][0], 1);\n }\n \n // 找到结果\n int result = 0;\n for (int i = 0; i < n; ++i) {\n for (int c = 0; c < max_value; ++c) {\n for (int l = 0; l < k + 1; ++l) {\n result = max(result, dp[i][c][l]);\n }\n }\n }\n \n return result;\n }\n};\n// dp[i][j]: 以a[i]", "memory": "107606" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int rec(int i,bool same,int k,vector<int>&nums,unordered_map<int,vector<int>>&mp,vector<vector<vector<int>>>&dp)\n {\n if(i>=nums.size() || k<0) return 0;\n if(dp[i][same][k]!=-1) return dp[i][same][k];\n vector<int>temp=mp[nums[i]];\n int next_ind=upper_bound(temp.begin(),temp.end(),i)-temp.begin();\n int pick=0;\n if(next_ind==temp.size())\n {\n pick=1+0;\n }\n else\n {\n pick=1+rec(temp[next_ind],true,k,nums,mp,dp);\n }\n if(k>0)\n {\n pick=max(pick,1+rec(i+1,false,k-1,nums,mp,dp));\n }\n int not_pick=0;\n if(same==false)\n {\n not_pick=rec(i+1,false,k,nums,mp,dp);\n }\n return dp[i][same][k]=max(pick,not_pick); \n }\n int maximumLength(vector<int>& nums, int k) {\n int n=nums.size();\n vector<vector<vector<int>>>dp(n,vector<vector<int>>(2,vector<int>(k+1,-1)));\n unordered_map<int,vector<int>>mp;\n for(int i=0;i<n;i++)\n {\n mp[nums[i]].push_back(i);\n }\n\n return rec(0,false,k,nums,mp,dp);\n }\n};", "memory": "115103" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int K;\n int solve(vector<int> & nums, int index, int prev, int k ,vector<int>& dp){\n if(index>=nums.size()) return 0;\n\n // if(dp[index][prev+1][k]!=-1) return dp[index][prev+1][k];\n if(dp[(index*(nums.size()+1)*(K+1))+((prev+1)*(K+1))+k]!=-1) return dp[(index*(nums.size()+1)*(K+1))+((prev+1)*(K+1))+k];\n\n int inc=0;\n if(prev==-1 || nums[index]==nums[prev]){\n inc=solve(nums,index+1,index,k,dp)+1;\n }\n else {\n if(k!=0){\n inc=solve(nums,index+1,index,k-1,dp)+1;\n }\n }\n\n int exc=solve(nums, index+1,prev,k,dp);\n return dp[(index*(nums.size()+1)*(K+1))+((prev+1)*(K+1))+k]=max(inc,exc);\n }\n int maximumLength(vector<int>& nums, int k) {\n K=k;\n\n // vector<vector<vector<int>>>dp(nums.size(),vector<vector<int>>(nums.size()+1,vector<int>(k+1,-1)));\n vector<int> dp((nums.size()+1)*(nums.size()+2)*(k+3),-1);\n return solve(nums,0,-1,k,dp);\n }\n};\n\n\n// equal hai toh lelo no doubt \n// if not equal toh either take it or not take it \n// if at any poiunt of time , k becomes 0 then you cant take if value is not equal \n\n\n// so i need to hold the previous value k and index ", "memory": "122599" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int yesRec(int i, int curr, int prev, vector<int> &nums, int k, vector<vector<vector<int>>> &dp)\n {\n if (i == nums.size())\n return 0;\n\n if (dp[i][curr][prev + 1] != -1)\n return dp[i][curr][prev + 1];\n\n int notTake = yesRec(i + 1, curr, prev, nums, k, dp);\n int take = 0;\n if (prev == -1 || nums[i] == nums[prev])\n {\n take = 1 + yesRec(i + 1, curr, i, nums, k, dp);\n }\n else if (curr <= k)\n {\n take = 1 + yesRec(i + 1, curr + 1, i, nums, k, dp);\n }\n\n return dp[i][curr][prev + 1] = max(take, notTake);\n }\n\n int maximumLength(vector<int> &nums, int k)\n {\n vector<vector<vector<int>>> dp(nums.size() + 1, vector<vector<int>>(k + 2, vector<int>(nums.size() + 1, -1)));\n return yesRec(0, 1, -1, nums, k, dp);\n }\n};", "memory": "130095" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
2
{ "code": "class Solution {\n\nprivate:\n\n int f(int ind , int last , int k , vector<int> &nums , vector<vector<vector<int>>> &dp){\n\n if(ind == nums.size()){\n return dp[ind][k][last+1] = 0;\n }\n\n if(dp[ind][k][last+1] != -1) return dp[ind][k][last+1];\n\n int np = f(ind + 1 , last , k , nums ,dp);\n\n int p = 0;\n if (last == -1 || nums[ind] == nums[last]){\n p = 1 + f(ind +1 , ind , k , nums ,dp);\n }\n\n else if(last == -1 || nums[ind] != nums[last] && k > 0){\n p = 1 + f (ind + 1 , ind , k -1 , nums , dp);\n }\n \n return dp[ind][k][last+1] = max(p,np);\n }\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n\n vector<vector<vector<int>>> dp(n+1 , (vector<vector<int>>(k+2 , vector<int>(n+2 , -1))));\n return f(0,-1,k,nums,dp);\n\n }\n};", "memory": "137591" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n\n int getMax(vector<int>nums, int k, int idx, vector<vector<int>>&memo){\n\n if(idx == nums.size()-1){\n memo[idx][k] = 1;\n return 1;\n }\n\n int element = nums[idx];\n int max_ans = 1;\n for(int i = idx+1; i <nums.size(); i++){\n if(nums[i]==element){\n if(memo[i][k] == -1){\n int ans = 1 + getMax(nums, k, i, memo);\n max_ans = max(ans, max_ans);\n } else{\n max_ans = max(max_ans, 1+memo[i][k]);\n }\n } else if(k-1>=0){\n\n if(memo[i][k-1] == -1){\n int ans = 1 + getMax(nums, k-1, i, memo);\n max_ans = max(ans, max_ans);\n } else {\n max_ans = max(max_ans, 1 + memo[i][k-1]);\n }\n\n }\n }\n\n memo[idx][k] = max_ans;\n return max_ans;\n // return max(max_ans, getMax(nums, k, idx+1, memo));\n \n // return max()\n \n }\n\n int maximumLength(vector<int>& nums, int k) {\n \n if(nums.size()==1){\n return 1;\n }\n unordered_map<int, bool>mm2;\n int max_ans = INT_MIN;\n\n bool rep = false;\n for(int i = 0; i < nums.size(); i++){\n if(mm2[nums[i]]==false){\n mm2[nums[i]] = true;\n } else{\n rep = true;\n }\n }\n\n if(rep == false){\n int si = nums.size();\n return min(k+1, si);\n }\n \n unordered_map<int, bool>mm;\n\n vector<vector<int>>memo(nums.size()+1, vector<int>(k+1, -1));\n for(int i = 0; i < nums.size(); i++){\n if(mm[nums[i]]==false){\n\n max_ans = max(max_ans, getMax(nums, k, i, memo));\n mm[nums[i]] = true;\n }\n }\n return max_ans;\n // return max_ans;\n\n }\n};", "memory": "145088" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
2
{ "code": "class Solution\n{\npublic:\n int yesRec(int i, int curr, int prev, vector<int> &nums, int k, vector<vector<vector<int>>> &dp)\n {\n if (i == nums.size())\n return 0;\n\n if (dp[i][curr][prev + 1] != -1)\n return dp[i][curr][prev + 1];\n\n int notTake = yesRec(i + 1, curr, prev, nums, k, dp);\n int take = 0;\n if (prev == -1 || nums[i] == nums[prev])\n {\n take = 1 + yesRec(i + 1, curr, i, nums, k, dp);\n }\n else if (curr < k)\n {\n take = 1 + yesRec(i + 1, curr + 1, i, nums, k, dp);\n }\n\n return dp[i][curr][prev + 1] = max(take, notTake);\n }\n\n int maximumLength(vector<int> &nums, int k)\n {\n vector<vector<vector<int>>> dp(nums.size(), vector<vector<int>>(k+1, vector<int>(nums.size() , -1)));\n return yesRec(0, 0, -1, nums, k, dp);\n }\n};", "memory": "152584" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int solve(int ind,vector<int>& nums, int k,int last,vector<vector<vector<int>>> &dp){\n int n=nums.size();\n if(ind>=n) return 0;\n int take=0;\n int notTake=0;\n if(last!=-1 && dp[ind][k][last]!=-1) return dp[ind][k][last];\n notTake=solve(ind+1,nums,k,last,dp);\n if(last==-1 || (last!=-1 && nums[ind]==nums[last])){\n take=1+solve(ind+1,nums,k,ind,dp);\n }else if(k>0) take=1+solve(ind+1,nums,k-1,ind,dp);\n \n if(last!=-1) return dp[ind][k][last]=max(take,notTake);\n return max(take,notTake);\n \n }\n int maximumLength(vector<int>& nums, int k) {\n int n=nums.size();\n vector<vector<vector<int>>> dp(n,vector<vector<int>>(k+1,vector<int>(n,-1)));\n return solve(0,nums,k,-1,dp);\n }\n};", "memory": "160080" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int dp(int i, int cnt, int k, int prevIndex, vector<vector<vector<int>>> &arr, vector<int> &nums)\n {\n if(cnt > k) return -1;\n \n if(prevIndex != -1 && arr[i][cnt][prevIndex] != -1) return arr[i][cnt][prevIndex];\n \n if(prevIndex != -1 && nums[i] != nums[prevIndex])\n {\n if(i == nums.size() - 1)\n {\n if(cnt >= k) return 0;\n else return 1;\n }\n \n int take = dp(i+1, cnt + 1, k, i, arr, nums) + 1;\n int skip = dp(i+1, cnt, k, prevIndex, arr, nums);\n arr[i][cnt][prevIndex] = max(take, skip);\n }\n else\n {\n if(i == nums.size() - 1) return 1;\n \n int take = dp(i+1, cnt, k, i, arr, nums) + 1;\n if(prevIndex == -1) return take;\n arr[i][cnt][prevIndex] = take;\n }\n return arr[i][cnt][prevIndex];\n }\n int maximumLength(vector<int>& nums, int k) {\n vector<vector<vector<int>>> arr(nums.size(), vector<vector<int>>(k+1, vector<int>(nums.size(), -1)));\n\n int maxVal = INT_MIN;\n for(int i=0;i<nums.size();i++)\n {\n int val = dp(i, 0, k, -1, arr, nums);\n //cout<<val<<endl;\n maxVal = max(val, maxVal);\n }\n return maxVal;\n }\n};", "memory": "167576" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int f(int idx,int k,int prev,vector<int>& nums,vector<vector<vector<int>>>& dp)\n {\n if(k<0)return INT_MIN;\n if(idx==nums.size())return 0;\n\n if(dp[idx][k][prev+1]!=-1)return dp[idx][k][prev+1];\n\n int not_take = f(idx+1,k,prev,nums,dp);\n //take\n int take = 1;\n if(idx>0 && prev!=-1 && nums[idx]!=nums[prev])take += f(idx+1,k-1,idx,nums,dp);\n else take += f(idx+1,k,idx,nums,dp);\n \n return dp[idx][k][prev+1] = max(take,not_take);\n }\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n vector<vector<vector<int>>>dp(n,vector<vector<int>>(k+1,vector<int>(n+1,-1)));\n return f(0,k,-1,nums,dp);\n }\n};", "memory": "175073" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int helper(int index,int k,int last,vector<int>& nums,vector<vector<vector<int>>>& dp){\n if(index==nums.size()){\n return 0;\n }\n if(dp[index][k][last]!=-1){\n return dp[index][k][last];\n }\n int x=helper(index+1,k,last,nums,dp);\n if(last==nums.size()){\n int y=1+helper(index+1,k,index,nums,dp);\n int maximum=max(x,y);\n dp[index][k][last]=maximum;\n return maximum;\n }\n else{\n if(nums[index]==nums[last]){\n int y=1+helper(index+1,k,index,nums,dp);\n int maximum=max(x,y);\n dp[index][k][last]=maximum;\n return maximum;\n }\n else{\n if(k>0){\n int y=1+helper(index+1,k-1,index,nums,dp);\n int maximum=max(x,y);\n dp[index][k][last]=maximum;\n return maximum;\n }\n else{\n int y=helper(index+1,k,last,nums,dp);\n dp[index][k][last]=y;\n return y;\n }\n }\n }\n }\n int maximumLength(vector<int>& nums, int k) {\n vector<vector<vector<int>>> dp(nums.size(),vector<vector<int>>(k+1,vector<int>(nums.size()+1,-1)));\n return helper(0,k,nums.size(),nums,dp);\n }\n};", "memory": "182569" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution\n{\npublic:\n int yesRec(int i, int curr, int prev, vector<int> &nums, int k, vector<vector<vector<int>>> &dp)\n {\n if (i == nums.size())\n return 0;\n\n if (dp[i][curr][prev + 1] != -1)\n return dp[i][curr][prev + 1];\n\n int notTake = yesRec(i + 1, curr, prev, nums, k, dp);\n int take = 0;\n if (prev == -1 || nums[i] == nums[prev])\n {\n take = 1 + yesRec(i + 1, curr, i, nums, k, dp);\n }\n else if (curr < k)\n {\n take = 1 + yesRec(i + 1, curr + 1, i, nums, k, dp);\n }\n\n return dp[i][curr][prev + 1] = max(take, notTake);\n }\n\n int maximumLength(vector<int> &nums, int k)\n {\n vector<vector<vector<int>>> dp(nums.size(), vector<vector<int>>(k + 1, vector<int>(nums.size() + 1, -1)));\n return yesRec(0, 0, -1, nums, k, dp);\n }\n};", "memory": "190065" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n int find(int ind,const vector<int>&nums,int k,int prev_ind,vector<vector<vector<int>>>&dp) {\n if (ind >= nums.size())return 0;\n if (~dp[ind][k][prev_ind + 1])return dp[ind][k][prev_ind + 1];\n int notTake=find(ind+1,nums,k,prev_ind,dp);\n int take=0;\n if(prev_ind!=-1 && nums[ind]==nums[prev_ind])take=1+find(ind+1,nums,k,prev_ind,dp);\n else if(prev_ind==-1)take = 1+find(ind+1,nums,k,ind,dp);\n else if(k>0)take=1+find(ind+1,nums,k-1,ind,dp);\n return dp[ind][k][prev_ind + 1]=max(take,notTake);\n }\npublic:\n int maximumLength(const vector<int>& nums,const int &k) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n int n = (int)nums.size();\n vector<vector<vector<int>>> dp(n,vector<vector<int>>(k+1,vector<int>(n+1,-1)));\n return find(0,nums,k,-1,dp);\n }\n};", "memory": "197561" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution\n{\npublic:\n int yesRec(int i, int curr, int prev, vector<int> &nums, int k, vector<vector<vector<int>>> &dp)\n {\n if (i == nums.size())\n return 0;\n\n if (dp[i][curr][prev + 1] != -1)\n return dp[i][curr][prev + 1];\n\n int take = 0;\n if (prev == -1 || nums[i] == nums[prev])\n {\n take = 1 + yesRec(i + 1, curr, i, nums, k, dp);\n }\n else if (k > 0)\n {\n take = 1 + yesRec(i + 1, curr + 1, i, nums, k - 1, dp);\n }\n int notTake = yesRec(i + 1, curr, prev, nums, k, dp);\n\n return dp[i][curr][prev + 1] = max(take, notTake);\n }\n\n int maximumLength(vector<int> &nums, int k)\n {\n vector<vector<vector<int>>> dp(nums.size() + 1, vector<vector<int>>(k + 1, vector<int>(nums.size() + 1, -1)));\n return yesRec(0, 0, -1, nums, k, dp);\n }\n};", "memory": "227546" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int n;\n vector <int> arr;\n\n int f(int ind,int k,int prev,vector <vector <vector <int>>> &dp){\n\n if(ind==n)\n return 0;\n if(dp[ind][k][prev+1]!=-1)\n return dp[ind][k][prev+1];\n\n if(prev == -1){\n return dp[ind][k][prev+1]=max(1+f(ind+1,k,ind,dp),f(ind+1,k,-1,dp));\n } \n\n else if(arr[ind]==arr[prev]){\n return dp[ind][k][prev+1]=1+f(ind+1,k,prev,dp);\n }\n \n\n else{\n if(k-1>=0)\n return dp[ind][k][prev+1]=max(1+f(ind+1,k-1,ind,dp),f(ind+1,k,prev,dp));\n else\n return dp[ind][k][prev+1]=f(ind+1,k,prev,dp);\n\n }\n\n return 0;\n\n }\n\n\n\n int maximumLength(vector<int>& nums, int k) {\n n=nums.size();\n arr=nums;\n \n \n vector<vector<vector<int>>> dp(n+1, vector<vector<int>>(k + 1, vector<int>(n + 1, -1)));\n\n return f(0,k,-1,dp);\n }\n};", "memory": "235043" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(vector<int>& a, int k, int index, int pre,\n vector<vector<vector<int>>>& dp) {\n if (index >= a.size())\n return 0;\n if (dp[index][k][pre + 1] != -1)\n return dp[index][k][pre + 1];\n int ans1 = 0;\n if (pre == -1 || a[pre] == a[index]) {\n ans1 = 1 + solve(a, k, index + 1, index, dp);\n }\n\n else if (k > 0)\n ans1 = 1 + solve(a, k - 1, index + 1, index, dp);\n int ans2 = solve(a, k, index + 1, pre, dp);\n return dp[index][k][pre + 1] = max(ans1, ans2);\n }\n int maximumLength(vector<int>& a, int k) {\n int pre = -1;\n int n = a.size();\n vector<vector<vector<int>>> dp(\n n + 1, vector<vector<int>>(k + 1, vector<int>(n + 2, -1)));\n return solve(a, k, 0, pre, dp);\n }\n};", "memory": "242539" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> nums;\n unordered_map<int,int> mp;\n vector<vector<vector<int>>> M;\n int dp(int left, int rem, int last){\n int pl = last;\n last = mp[last];\n if(rem < 0)\n return 0;\n if(left == nums.size())\n return 0;\n if(M[left][rem][last] != -1)\n return M[left][rem][last];\n\n if(pl == 0)\n M[left][rem][last] = max(dp(left+1,rem,0), dp(left+1,rem,nums[left])+1);\n else if(nums[left] == pl)\n M[left][rem][last] = dp(left+1,rem,pl)+1;\n else if(rem)\n M[left][rem][last] = max(dp(left+1,rem,pl), dp(left+1,rem-1,nums[left])+1);\n else\n M[left][rem][last] = dp(left+1,rem,pl);\n \n // cout << left << \" \" << last << \" \" << rem << \" = \" << M[left][last][rem] << endl;\n return M[left][rem][last];\n }\n\n int maximumLength(vector<int>& nums, int k) {\n this->nums = nums;\n cout << nums.size();\n int inc = 0;\n for(int i=0;i<nums.size();++i)\n if(!mp.contains(nums[i]))\n mp[nums[i]] = ++inc;\n mp[0] = 0;\n M.assign(nums.size(),vector<vector<int>>(k+1, vector<int>(nums.size()+1,-1)));\n return dp(0,k,0);\n }\n};", "memory": "250035" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> A;\n unordered_map<int,int> mp;\n vector<vector<vector<int>>> M;\n int inc = 0, n;\n int D(int i, int r, int l){\n if(i == n)\n return 0;\n if(M[i][r][l] == -1){\n if(l == 0)\n M[i][r][l] = max(D(i+1,r,0), D(i+1,r,mp[A[i]])+1);\n else if(mp[A[i]] == l)\n M[i][r][l] = D(i+1,r,l)+1;\n else\n M[i][r][l] = max(D(i+1,r,l), r ? D(i+1,r-1,mp[A[i]])+1 : 0);\n }\n return M[i][r][l];\n }\n\n int maximumLength(vector<int>& nums, int k) {\n this->A = nums;\n n = nums.size();\n for(int i=0; i<n; ++i)\n if(!mp.contains(nums[i]))\n mp[nums[i]] = ++inc;\n mp[0] = 0;\n M.assign(n,vector<vector<int>>(k+1,vector<int>(n+1,-1)));\n return D(0,k,0);\n }\n};", "memory": "250035" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int f(int ind,int k,int prev,vector<int>&nums,vector<vector<vector<int>>>&dp){\n\n if(k<0) return-1e7;\n if(ind == nums.size())return 0;\n if(dp[ind][k+1][prev+1]!=-1)return dp[ind][k+1][prev+1];\n\n int total = 0;\n if(prev!=-1 && nums[prev]==nums[ind]){\n total = max(total,1+f(ind+1,k,prev,nums,dp));\n }else{\n total = max(total,1+f(ind+1,k-1,ind,nums,dp));\n total = max(total,f(ind+1,k,prev,nums,dp));\n }\n return dp[ind][k+1][prev+1] = total; \n }\n int maximumLength(vector<int>& nums, int k){\n int n = nums.size();\n vector<vector<vector<int>>>dp(n+1,vector<vector<int>>(k+5,vector<int>(n+1,-1)));\n return f(0,k+1,-1,nums,dp);\n }\n};", "memory": "257531" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int solve(vector<int>& nums, int k, int i, int prev, vector <vector <vector <int>>> &dp) {\n\n if(i == nums.size()) return 0;\n\n if(dp[i][k][prev] != -1) return dp[i][k][prev];\n\n int pick = 0, leave = 0;\n\n if(prev == nums.size() or nums[i] != nums[prev] and k-1 >= 0) {\n pick = 1 + solve(nums, k-1, i+1, i, dp);\n }\n\n else if(nums[i] == nums[prev]) {\n pick = 1 + solve(nums, k, i+1, i, dp);\n }\n\n if(prev == nums.size() or nums[i] != nums[prev]) \n leave = solve(nums, k, i+1, prev, dp);\n\n return dp[i][k][prev] = max(pick, leave);\n \n }\n\n int maximumLength(vector<int>& nums, int k) {\n\n k++;\n\n vector <vector <vector <int>>> dp(nums.size(), vector <vector <int> > (27, vector <int> (nums.size()+1, -1)));\n\n return solve(nums, k, 0, nums.size(), dp);\n \n }\n};", "memory": "265028" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(vector<int>&nums,int k,int prev,int curr,vector<vector<vector<int>>>&dp){\n\n if(curr>=nums.size()){\n\n return 0;\n }\n if(dp[k][prev+1][curr]!=-1){\n\n return dp[k][prev+1][curr];\n }\n int include=0;\n\n if(prev==-1){\n\n include=1+solve(nums,k,curr,curr+1,dp);\n }\n else if(prev!=-1 && nums[prev]==nums[curr]){\n\n include=1+solve(nums,k,curr,curr+1,dp);\n }\n else if(k>0 && nums[prev]!=nums[curr]){\n\n include=1+solve(nums,k-1,curr,curr+1,dp);\n }\n int exclude=solve(nums,k,prev,curr+1,dp);\n dp[k][prev+1][curr]=max(include,exclude);\n return dp[k][prev+1][curr];\n }\n int maximumLength(vector<int>& nums, int k) {\n\n if(nums.size()==1){\n\n return 1;\n }\n\n int prev=-1;\n int curr=0;\n vector<vector<vector<int>>>dp(k+1,vector<vector<int>>(nums.size()+1,vector<int>(nums.size(),-1)));\n\n return solve(nums,k,prev,curr,dp);\n }\n};", "memory": "272524" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n long long f(int i,vector<int>& nums, int k,int prev,vector<vector<vector<long long>>>&dp){\nif(i>=nums.size())return 0;\n\n if(dp[i][k][prev+1]!=-1)return dp[i][k][prev+1];\n long long take = INT_MIN;\n long long notake = f(i+1,nums,k,prev,dp);\n if(k==0 || prev==-1){\n if(prev == -1){\n take = 1+f(i+1,nums,k,i,dp);\n }\n else if(nums[prev] == nums[i]){\n take = 1+f(i+1,nums,k,i,dp);\n }\n }\n else if(k>0){\n if(nums[prev] == nums[i]){\n take = 1+f(i+1,nums,k,i,dp);\n }\n else if(nums[i]!=nums[prev]){\n take = 1+f(i+1,nums,k-1,i,dp);\n }\n }\n return dp[i][k][prev+1] = max(take,notake);\n }\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n // unordered_map<int,int>mp;\n //long long N = 1e9;\n vector<vector<vector<long long>>>dp(n+1,vector<vector<long long>>(k+1,vector<long long>(n+2,-1)));\n return f(0,nums,k,-1,dp);\n }\n};", "memory": "280020" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<vector<int>>>dp;\n int dfs(int i, int prev, int k, vector<int>&nums, int n){\n if(i == nums.size()) return 0;\n if(dp[i][prev + 1][k] != -1) return dp[i][prev + 1][k];\n\n int ans = 0;\n if(prev == -1){\n ans = max(ans, dfs(i + 1, prev, k, nums, n));\n ans = max(ans, 1 + dfs(i + 1, i, k, nums, n));\n }else{\n if(nums[i] == nums[prev]){\n ans = max(ans, 1 + dfs(i + 1, i, k, nums, n));\n // ans = max(ans, dfs(i + 1, prev, k, nums, n));\n }else{\n ans = max(ans, dfs(i + 1, prev, k, nums, n));\n if(k > 0){\n ans = max(ans, 1 + dfs(i + 1, i, k-1, nums, n));\n }\n }\n }\n\n dp[i][prev + 1][k] = ans;\n return ans;\n }\n\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n dp.assign(n + 1, vector<vector<int>>(n + 1, vector<int>(28, -1)));\n return dfs(0, -1, k, nums, n);\n }\n};", "memory": "287516" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int cnt(int ctr,int prev,vector<vector<vector<int>>>&dp,vector<int>& nums,int k)\n {\n if(ctr>=nums.size())\n return 0;\n\n \n if(dp[ctr][prev+1][k]!=-1)\n return dp[ctr][prev+1][k];\n\n /* if(ctr==nums.size()-1)\n {\n \n if(prev==-1)\n return dp[ctr][prev+1][k]=1;\n\n if(prev!=-1 && nums[ctr]==nums[prev])\n return dp[ctr][prev+1][k]=1;\n\n if(prev!=-1 && nums[ctr]!=nums[prev] && k>0)\n return dp[ctr][prev+1][k]=1;\n\n\n\n return INT_MIN;\n }*/\n\n \n \n\n int tk=INT_MIN;\n int nt=INT_MIN;\n\n //int e=INT_MIN;\n\n // if( prev==-1 || nums[ctr]==nums[prev] || (nums[ctr]!=nums[prev] && k>0))\n //e=1;\n\n nt=cnt(ctr+1,prev,dp,nums,k);\n\n if(prev==-1 || nums[prev]==nums[ctr])\n tk=1+cnt(ctr+1,ctr,dp,nums,k);\n\n else if(k>0 && nums[prev]!=nums[ctr])\n tk=1+cnt(ctr+1,ctr,dp,nums,k-1);\n\n return dp[ctr][prev+1][k]=max(tk,nt);\n }\n int maximumLength(vector<int>& nums, int k) \n {\n vector<vector<vector<int>>>dp(nums.size(),vector<vector<int>>(nums.size(),(vector<int>(26,-1))));\n return cnt(0,-1,dp,nums,k);\n \n }\n};", "memory": "295013" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int cnt(int ctr,int prev,vector<vector<vector<int>>>&dp,vector<int>& nums,int k)\n {\n if(ctr>=nums.size())\n return 0;\n\n \n if(dp[ctr][prev+1][k]!=-1)\n return dp[ctr][prev+1][k];\n\n /* if(ctr==nums.size()-1)\n {\n \n if(prev==-1)\n return dp[ctr][prev+1][k]=1;\n\n if(prev!=-1 && nums[ctr]==nums[prev])\n return dp[ctr][prev+1][k]=1;\n\n if(prev!=-1 && nums[ctr]!=nums[prev] && k>0)\n return dp[ctr][prev+1][k]=1;\n\n\n\n return INT_MIN;\n }*/\n\n \n \n\n int tk=INT_MIN;\n int nt=INT_MIN;\n\n //int e=INT_MIN;\n\n // if( prev==-1 || nums[ctr]==nums[prev] || (nums[ctr]!=nums[prev] && k>0))\n //e=1;\n\n nt=cnt(ctr+1,prev,dp,nums,k);\n\n if(prev==-1 || nums[prev]==nums[ctr])\n tk=1+cnt(ctr+1,ctr,dp,nums,k);\n\n else if(k>0 && nums[prev]!=nums[ctr])\n tk=1+cnt(ctr+1,ctr,dp,nums,k-1);\n\n return dp[ctr][prev+1][k]=max(tk,nt);\n }\n int maximumLength(vector<int>& nums, int k) \n {\n vector<vector<vector<int>>>dp(nums.size(),vector<vector<int>>(nums.size(),(vector<int>(26,-1))));\n return cnt(0,-1,dp,nums,k);\n \n }\n};", "memory": "295013" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solver(vector<int>& nums, int m, int j,int i, int n, int k,vector<vector<vector<int>>>&dp )\n {\n if(m>k)\n return -1;\n\n if(i>=n)\n return 0;\n\n if(dp[i][j][m]!=-1)\n return dp[i][j][m];\n\n int ans = 0;\n ans = max(ans,solver(nums,m,j,i+1,n,k,dp));\n \n if(j==0 || nums[j-1]==nums[i])\n ans = max(ans,solver(nums,m,i+1,i+1,n,k,dp)+1);\n else\n ans = max(ans,solver(nums,m+1,i+1,i+1,n,k,dp)+1);\n \n return dp[i][j][m] = ans;\n \n }\n int maximumLength(vector<int>& nums, int k) {\n \n int n = nums.size();\n vector<vector<vector<int>>>dp(n+1,vector<vector<int>>(n+1,vector<int>(26,-1)));\n return solver(nums,0,0,0,n,k,dp);\n }\n};", "memory": "302509" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int finder(int curr, int prev, int count, vector<int>& nums, vector<vector<vector<int>>>& dp) {\n if (curr == nums.size())\n return 0;\n int ans = -1;\n\n if (dp[curr][prev + 1][count] != -1)\n return dp[curr][prev + 1][count];\n \n if (prev == -1) {\n ans = max(ans, finder(curr + 1, -1, count, nums, dp));\n ans = max(ans, 1 + finder(curr + 1, curr, count, nums, dp));\n } else {\n if (nums[curr] == nums[prev])\n ans = max(ans, 1 + finder(curr + 1, curr, count, nums, dp));\n else {\n ans = max(ans, finder(curr + 1, prev, count, nums, dp));\n if (count > 0)\n ans = max(ans, 1 + finder(curr + 1, curr, count - 1, nums, dp));\n }\n }\n\n return dp[curr][prev + 1][count] = ans;\n }\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n vector<vector<vector<int>>> dp(\n n + 1, vector<vector<int>>(n + 1, vector<int>(26, -1)));\n int ans = finder(0, -1, k, nums, dp);\n return ans;\n }\n};\n", "memory": "310005" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n vector<vector<vector<int>>> memo(\n n + 1, vector<vector<int>>(n + 1, vector<int>(26, -1)));\n return find(0, -1, k, nums, memo);\n }\n //memoization 3D DP include - exclude\n int find(int pos, int prev, int cnt, vector<int>& nums,\n vector<vector<vector<int>>>& memo) {\n if (pos == nums.size())\n return 0;\n int ans = -1;\n if (memo[pos][prev + 1][cnt] != -1)\n return memo[pos][prev + 1][cnt];\n if (prev == -1) { // not started\n ans = max(ans, find(pos + 1, -1, cnt, nums, memo)); // skipping\n ans = max(ans, 1 + find(pos + 1, pos, cnt, nums, memo)); // started\n } else {\n if (nums[pos] == nums[prev]) { // equal\n ans = max(ans, 1 + find(pos + 1, pos, cnt, nums, memo));\n } else {\n ans = max(ans, find(pos + 1, prev, cnt, nums, memo)); // skipped\n if (cnt > 0) { // if cnt >0 then adding\n ans = max(ans, 1 + find(pos + 1, pos, cnt - 1, nums, memo));\n }\n }\n }\n memo[pos][prev + 1][cnt] = ans;\n return ans;\n }\n};", "memory": "317501" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\n vector<vector<vector<int>>> dp;\n\n int func(vector<int> &nums,int k,int ind,int prev_ind,int c)\n {\n int n=nums.size();\n\n if(c>k)\n return INT_MIN;\n\n if(ind<0)\n {\n return 0;\n }\n\n if(dp[ind][prev_ind][c]!=-1)\n return dp[ind][prev_ind][c];\n\n int ans=func(nums,k,ind-1,prev_ind,c);\n\n int nc=c;\n \n if(prev_ind!=n && nums[prev_ind]!=nums[ind])\n nc=c+1;\n\n ans=max(ans,1+func(nums,k,ind-1,ind,nc));\n\n return dp[ind][prev_ind][c]=ans;\n }\npublic:\n int maximumLength(vector<int>& nums, int k) {\n int n=nums.size();\n dp.resize(n+2,vector<vector<int>>(n+2,vector<int>(52,-1)));\n int ans=func(nums,k,n-1,n,0);\n\n return ans;\n }\n};", "memory": "324998" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(int idx, int lastIdx, int k, vector<int>& nums, vector<vector<vector<int>>>& dp) {\n if(idx == nums.size()) return 0;\n\n if(dp[idx][lastIdx + 1][k] != -1)\n return dp[idx][lastIdx + 1][k];\n \n int res = solve(idx + 1, lastIdx, k, nums, dp);\n\n if(lastIdx == -1 || nums[idx] == nums[lastIdx] || k)\n res = max(res, 1 + solve(idx + 1, idx,\n k - (lastIdx != -1 && nums[idx] != nums[lastIdx]), nums, dp));\n \n return dp[idx][lastIdx + 1][k] = res;\n }\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n\n vector<vector<vector<int>>> dp(n, vector<vector<int>>(n, vector<int>(k + 1, -1)));\n\n return solve(0, -1, k, nums, dp);\n }\n};", "memory": "332494" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int f(int prev,int i,int curr,int k,vector<int>&nums,int n,vector<vector<vector<int>>>&dp){\n if(i == n){\n return 0;\n }\n\n if(dp[i][prev+1][curr] != -1)return dp[i][prev+1][curr];\n \n int nottake=f(prev,i+1,curr,k,nums,n,dp);\n int take=0;\n if(prev == -1 || nums[i] == nums[prev]){\n take=1+f(i,i+1,curr,k,nums,n,dp);\n }\n else if(curr < k){\n take=1+f(i,i+1,curr+1,k,nums,n,dp);\n }\n return dp[i][prev+1][curr]=max(take,nottake);\n }\n int maximumLength(vector<int>& nums, int k) {\n int n=nums.size();\n vector<vector<vector<int>>>dp(n,vector<vector<int>>(n,vector<int>(k+1,-1)));\n return f(-1,0,0,k,nums,nums.size(),dp);\n }\n};", "memory": "339990" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "#define dc false\n#define print(arr) if(dc){for(int i=0;i<arr.size();i++){cout<<arr[i]<<\" \";}cout<<endl;}\n#define deb(x) if(dc) cout<<\" --> \"<<(#x)<<\" : \"<<x<<endl;\n#define deb2(x, y) if(dc) cout<<\" --> \"<<(#x)<<\" : \"<<x<<\" , \"<<(#y)<<\" : \"<<y<<endl;\n#define debs(x) if(dc) cout<<\"--> \"<<x<<endl;\n\nclass Solution {\npublic:\ntypedef int ll;\n ll fun(ll i, ll p, ll k, vector<int> &arr, vector<vector<vector<ll>>> &dp){\n if(i >= arr.size()) return 0;\n if(p != -1 and dp[i][p][k] != -1){\n return dp[i][p][k];\n }\n ll ans = fun(i+1, p, k, arr, dp);\n if(p == -1){\n ans = max(ans, fun(i+1, i, k, arr, dp));\n }else{\n if(arr[i] == arr[p]){\n ans++;\n }else if(k > 0){\n ans = max(ans, fun(i+1, i, k-1, arr, dp) + 1);\n }\n }\n if(p != -1){\n dp[i][p][k] = ans;\n }\n return ans;\n }\n int maximumLength(vector<int>& nums, int k) {\n ll n = nums.size();\n vector<vector<vector<ll>>> dp(n, vector<vector<ll>>(n, vector<ll>(k+1, -1)));\n return fun(0, -1, k, nums, dp) + 1;\n }\n};", "memory": "347486" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int func(vector<int>& nums, int curr, int prev, int k, vector<vector<vector<int>>>& dp){\n if(curr<0) return 0;\n if(dp[curr][prev][k]!=-1) return dp[curr][prev][k];\n if(nums[curr]!=nums[prev]){\n if(k==0) return dp[curr][prev][k] = func(nums, curr-1, prev, k, dp);\n return dp[curr][prev][k] = max(func(nums, curr-1, prev, k, dp), 1 + func(nums, curr-1, curr, k-1, dp));\n }\n return dp[curr][prev][k] = 1 + func(nums, curr-1, curr, k, dp); \n }\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size(), ans = 0;\n vector<vector<vector<int>>> vec(n, vector<vector<int>>(n, vector<int>(k+1, -1))), temp;\n for(int i = 1; i < n; i++){\n ans = max(ans, func(nums, i-1, i, k, vec));\n }\n return ans + 1;\n }\n};", "memory": "354983" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int solve(vector<int> &nums, int prev, int i, int k, vector<vector<vector<int>>> &dp) {\n int n = nums.size();\n if(i == n)\n return 0;\n \n if(dp[prev+1][i][k] != -1)\n return dp[prev+1][i][k];\n \n int choose = 0;\n int notChoose = solve(nums, prev, i+1, k, dp);\n \n if(prev == -1)\n choose = solve(nums, i, i+1, k, dp) + 1;\n else {\n if(nums[prev] != nums[i] and k > 0)\n choose = solve(nums, i, i+1, k-1, dp) + 1;\n else if(nums[prev] == nums[i])\n choose = solve(nums, i, i+1, k, dp) + 1;\n }\n \n return dp[prev+1][i][k] = max(choose, notChoose);\n }\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n vector<vector<vector<int>>> dp(n+1, vector<vector<int>> (n, vector<int> (k+1, -1)));\n \n return solve(nums, -1, 0, k, dp);\n }\n};", "memory": "362479" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\n vector<vector<vector<int>>>dp;\n int n;\npublic:\n int maximumLength(vector<int>& nums, int k) {\n n = nums.size();\n if(!n)\n return 0;\n int ret = 0;\n dp = vector<vector<vector<int>>>(n+1, vector<vector<int>>(n, vector<int>(k+1, INT_MIN)));\n return dfs(nums, n, 0, k);\n }\n \n \n int dfs(const vector<int>&nums, int pre, int i, int k){\n if(i == nums.size())\n return 0;\n if(dp[pre][i][k] != INT_MIN)\n return dp[pre][i][k];\n int ret = dfs(nums, pre, i+1, k);\n if(pre == n || nums[pre] == nums[i])\n ret = max(ret, 1+dfs(nums, i, i+1, k));\n else if(k)\n ret = max(ret, 1+dfs(nums, i, i+1, k-1));\n return dp[pre][i][k] = ret;\n }\n};", "memory": "369975" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\n\nprivate:\n \n int findMinLen(vector<int>& nums, int k, int i, int prevIdx, \n int usedK, vector<vector<vector<int>>>& dp) {\n \n if (i >= nums.size()) {\n return 0;\n }\n \n if (dp[i][prevIdx+1][usedK] != -1) {\n return dp[i][prevIdx+1][usedK];\n }\n \n int op1 = findMinLen(nums, k, i+1, prevIdx, usedK, dp);\n \n int op2 = 0;\n \n if (prevIdx == -1 || nums[i] == nums[prevIdx]) {\n op2 = 1 + findMinLen(nums, k, i+1, i, usedK, dp);\n }\n else if (usedK < k) {\n op2 = 1 + findMinLen(nums, k, i+1, i, usedK+1, dp);\n }\n \n return dp[i][prevIdx+1][usedK] = max(op1, op2);\n \n }\n\npublic:\n \n int maximumLength(vector<int>& nums, int k) {\n \n vector<vector<vector<int>>> dp(nums.size(), vector<vector<int>>(nums.size()+1, vector<int>(k+1, -1)));\n \n // memset(dp, sizeof(int) * 501 * 502 * 26, -1);\n \n return findMinLen(nums, k, 0, -1, 0, dp);\n \n }\n};", "memory": "377471" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\n int f(int ind,int prev,int count,vector<vector<vector<int>>>&dp,vector<int>&arr,int k){\n if(count>k)return -1e9;\n if(ind == arr.size()){\n return 0;\n }\n\n if(dp[ind][prev+1][count]!=-1)return dp[ind][prev+1][count];\n\n int nottake = f(ind+1,prev,count,dp,arr,k);\n int take = -1e9;\n if(prev == -1 || (prev!=-1 && arr[ind] == arr[prev])){\n take = 1 + f(ind+1,ind,count,dp,arr,k);\n }else if(arr[ind]!=arr[prev]){\n take = 1 + f(ind+1,ind,count+1,dp,arr,k);\n }\n return dp[ind][prev+1][count] = max(nottake,take);\n }\npublic:\n int maximumLength(vector<int>& nums, int k) {\n //subsequece .. not substring\n int n = nums.size();\n vector<vector<vector<int>>>dp(n,vector<vector<int>>(n+1,vector<int>(k+1,-1)));\n int ans = f(0,-1,0,dp,nums,k);\n return ans;\n }\n};", "memory": "384968" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n int func(int ind, int prev, int k, vector<int> &nums,vector<vector<vector<int>>> &dp){\n if(ind == nums.size()) return 0;\n\n int take = 0, nt = 0;\n if(dp[ind][prev+1][k]!=-1) return dp[ind][prev+1][k];\n\n nt = func(ind+1, prev, k,nums,dp);\n \n if(prev == -1 || nums[prev]==nums[ind])\n take = 1 + func(ind+1, ind, k, nums,dp);\n else if(k>0){\n take = 1+ func(ind+1, ind, k-1, nums,dp);\n }\n \n\n \n \n return dp[ind][prev+1][k] = max(take,nt);\n\n }\n\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n vector<vector<vector<int>>> dp(n,vector<vector<int>> (n+1,vector<int>(min(k+1,n+1),-1)));\n return func(0, -1, k, nums,dp);\n\n }\n};", "memory": "392464" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<vector<vector<int>>> dp;\n int solve(int i, int j, int k,vector<int>& nums){\n // cout<<i<<' '<<j<<' '<<k<<endl;\n if(i==nums.size())\n return 0;\n if(dp[i][j][k]!=-1) return dp[i][j][k];\n int ans=solve(i+1,j,k,nums);\n if(j==nums.size()){\n ans=max(ans, solve(i+1,i,k,nums)+1);\n }\n else if(nums[i]!=nums[j]&&k>0){\n ans= max(ans,solve(i+1,i,k-1,nums)+1);\n }else if(nums[i]==nums[j]){\n ans= max(ans,solve(i+1,i,k,nums)+1);\n }\n // cout<<ans<<endl;\n dp[i][j][k]=ans;\n return ans;\n }\n int maximumLength(vector<int>& nums, int k) {\n dp.assign(nums.size(),vector<vector<int>>(nums.size()+1,vector<int>(k+1,-1)));\n return solve(0,nums.size(),k,nums);\n }\n};", "memory": "399960" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> arr;\n int K;\n vector<vector<vector<int>>> memo;\n\n int solve(int i, int prevIndex, int count) {\n \n if (i >= arr.size()) {\n return (count <= K) ? 0 : INT_MIN;\n }\n if (memo[i][prevIndex][count] != -1) {\n return memo[i][prevIndex][count];\n }\n\n int notpick = solve(i + 1, prevIndex, count);\n\n // Option 2: Pick the current element\n int pick = 0;\n int newCount = count;\n if(prevIndex!=0 && arr[prevIndex-1] !=arr[i]) newCount++;\n if(newCount<=K)\n pick = 1 + solve(i + 1, i+1, newCount); // Include current element in the subsequence\n \n\n // Store the result in the memoization table and return\n memo[i][prevIndex][count] = max(notpick, pick);\n return memo[i][prevIndex][count];\n // return max(notpick, pick);\n }\n\n int maximumLength(vector<int>& nums, int k) {\n arr = nums;\n K = k;\n int n = arr.size();\n memo.resize(n, vector<vector<int>>(n+1, vector<int>(k+1, -1)));\n return solve(0, 0, 0);}\n};\n", "memory": "407456" }
3,456
<p>You are given an integer array <code>nums</code> and a <strong>non-negative</strong> integer <code>k</code>. A sequence of integers <code>seq</code> is called <strong>good</strong> if there are <strong>at most</strong> <code>k</code> indices <code>i</code> in the range <code>[0, seq.length - 2]</code> such that <code>seq[i] != seq[i + 1]</code>.</p> <p>Return the <strong>maximum</strong> possible length of a <strong>good</strong> <span data-keyword="subsequence-array">subsequence</span> of <code>nums</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">nums = [1,2,1,1,3], k = 2</span></p> <p><strong>Output:</strong> <span class="example-io">4</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,<u>2</u>,<u>1</u>,<u>1</u>,3]</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,2,3,4,5,1], k = 0</span></p> <p><strong>Output:</strong> <span class="example-io">2</span></p> <p><strong>Explanation:</strong></p> <p>The maximum length subsequence is <code>[<u>1</u>,2,3,4,5,<u>1</u>]</code>.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 500</code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li> <li><code>0 &lt;= k &lt;= min(nums.length, 25)</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n // Below is a Memoized code of TC - close to 1e7 -- TLE\n /*\n int solve(int ind , int prev , int k , int n , vector<vector<vector<int>>> &dp , vector<int> &nums){\n if(k < 0) return -1e6;\n if(ind >= n) return 0;\n\n if(dp[ind][prev + 1][k] != -1) return dp[ind][prev + 1][k];\n\n int pick = 0 , unPick = 0;\n \n if(prev == -1){\n pick = 1 + solve(ind + 1 , ind , k , n , dp , nums);\n }\n else{\n if(nums[ind] != nums[prev]){\n pick = 1 + solve(ind + 1 , ind , k - 1 , n , dp , nums);\n }\n else{\n pick = 1 + solve(ind + 1 , ind , k , n , dp , nums);\n }\n }\n\n unPick = 0 + solve(ind + 1 , prev , k , n , dp , nums);\n\n return dp[ind][prev + 1][k] = max(pick , unPick);\n\n }\n\n int maximumLength(vector<int>& nums, int k) {\n int n = nums.size();\n vector<vector<vector<int>>> dp(n + 2 , vector<vector<int>>(n + 2 , vector<int>(k + 2 , -1)));\n\n return solve(0 , -1 , k , n , dp , nums);\n }\n */\n\n int maximumLength(vector<int>& nums, int kk) {\n int n = nums.size();\n vector<vector<vector<int>>> dp(n + 1 , vector<vector<int>>(n + 1 , vector<int>(kk + 1 , 0)));\n \n for(int ind = n - 1; ind >= 0; ind--){\n for(int prev = n; prev >= 0; prev--){\n for(int k = 0; k <= kk; k++){\n \n int pick = 0 , unPick = 0;\n \n if(prev == 0 || nums[ind] == nums[prev - 1]){\n pick = 1 + dp[ind + 1][ind + 1][k];\n }\n else{\n if(nums[ind] != nums[prev - 1] && k > 0){\n pick = 1 + dp[ind + 1][ind + 1][k - 1];\n }\n }\n\n unPick = 0 + dp[ind + 1][prev][k];\n\n dp[ind][prev][k] = max(pick , unPick);\n\n }\n }\n }\n\n return dp[0][0][kk];\n\n }\n};", "memory": "414953" }