id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
1
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n map<pair<int,int>,int>mp;\n queue<pair<int,int>>q;\n vector<int>ans;\n mp[{0,0}]++;\n q.push({0,0});\n while(!q.empty()){\n int row=q.front().first;\n ...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
1
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n queue<pair<int, int>> q;\n vector<int> out;\n set<pair<int, int>> visited; // Track visited pairs\n\n q.push({0, 0});\n visited.insert({0, 0});\n\n while (!q.empty()) {\n ...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
1
{ "code": "class Solution {\npublic:\n bool solve(int i,int j, int n, vector<vector<int>>& a) {\n return i>=0 && i<=n-1 && j>= 0 && j<=a[i].size()-1;\n }\n vector<int> findDiagonalOrder(vector<vector<int>>& a) {\n int n = a.size();\n vector<int>ans;\n if(n==0) return ans;\n ...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
1
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n map<int, vector<int>> diagonals;\n\n int totalSize = 0;\n for (int i = nums.size() - 1; i >= 0; --i)\n {\n int columns = nums[i].size();\n totalSize += columns;\n ...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
1
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n unordered_map<int, vector<int>> um;\n int m = nums.size();\n int maxIndex = 0;\n for (int i = m - 1; i >= 0; i--) {\n for (int j = 0; j < nums[i].size(); j++) {\n ...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
1
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n int rowSize = nums.size();\n unordered_map<int,vector<int>> mp;\n vector<int> res;\n\n // For each row\n for (int row = rowSize-1; row >= 0; row--){\n int colSize = nums...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
1
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n unordered_map<int, vector<int>> groups;\n for (int i = nums.size() - 1; i >= 0; i--) {\n for (int j = 0; j < nums[i].size(); j++) {\n int diagonal = i + j;\n gr...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n int i,j,k;\n vector<int> ans;\n priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>> > list;\n\n for(i=0;i<nums.size();i++){\n for(j=0;j<nums[i].size();j++){\n...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": " bool sort_func(vector<int> &vec1, vector<int> &vec2){\n if(vec1[0] < vec2[0]) return true;\n else if(vec1[0] == vec2[0] && vec1[1] < vec2[1]) return true;\n else return false;\n }\nclass Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n ...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "struct comp{\n bool operator()(vector<int> &a,vector<int> &b){\n if(a[0] == b[0]){\n return a[1] < b[1];\n }\n return a[0] > b[0];\n }\n}; \n\nclass Solution {\n typedef vector<int> vi;\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n ...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n size_t count = 0;\n unordered_map<size_t, vector<pair<size_t, int>>> res_map;\n for (size_t i = 0; i < nums.size(); i++) {\n for (size_t j = 0; j < nums[i].size(); j++) {\n ...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n unordered_map<int, list<int>> map;\n int size = 0;\n for (int i = 0; i < nums.size(); i++) {\n for (int j = 0; j < nums[i].size(); j++) {\n size++;\n int...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n unordered_map<int,vector<int>> map;\n vector<int> res;\n int sum = 0;\n for(int i = 0; i<nums.size();i++){\n for(int j = 0;j<nums[i].size();j++){\n map[i+j].push...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n unordered_map<int,vector<int>>m;\n for(int i=nums.size()-1;i>=0;i--){\n for(int j=0;j<nums[i].size();j++){\n int sum =i+j;\n m[sum].push_back(nums[i][j]);\n ...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n unordered_map<int, vector<int>> mp;\n for (int i = nums.size() - 1; i >= 0; i--) {\n for (int j = 0; j < nums[i].size(); j++) {\n mp[i + j].push_back(nums[i][j]);\n ...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n unordered_map < int , vector <int> > mp;\n int max_indx = 0;\n for(int i =0; i< nums.size(); i++)\n {\n for(int j = 0; j<nums[i].size(); j++)\n {\n mp[i+j].push_back(nums[i][j]);\n ...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n vector<int> ans;\n unordered_map<int, vector<int>> mp;\n int maxKey = 0;\n\n for(int i=0; i<nums.size(); i++){\n for(int j=0; j<nums[i].size(); j++){\n mp[i+j].p...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n queue<pair<int, int>> q;\n unordered_set<string> visited;\n q.emplace(0, 0);\n visited.insert(\"0,0\");\n \n vector<int> result;\n while (!q.empty()) {\n a...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n map<int, list<int>> diags;\n vector<int> answer;\n int count_of_diags = 0;\n int num_of_diag, size;\n for (int i = 0; i < nums.size(); i++) {\n num_of_diag = i;\n ...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n map<int, list<int>> diags;\n vector<int> answer;\n int count_of_diags = 0;\n int num_of_diag;\n for (int i = 0; i < nums.size(); i++) {\n num_of_diag = i;\n f...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "#include <vector>\n#include <unordered_map>\n#include <list>\n#include <iostream>\n#include <algorithm> // For std::sort\n\nclass Solution {\npublic:\n std::vector<int> findDiagonalOrder(std::vector<std::vector<int>>& nums) {\n std::unordered_map<int, std::list<int>> hashmap;\n \n /...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n unordered_map<int, vector<int>> diagonalMap;\n vector<int> result;\n \n for (int i = 0; i < nums.size(); ++i) {\n for (int j = 0; j < nums[i].size(); ++j) {\n diagonalMap[i + j].push_ba...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n map<int, vector<int>> mp;\n\n for (int i = 0; i < nums.size(); i++) {\n for (int j = 0; j < nums[i].size(); j++) {\n mp[i+j].push_back(nums[i][j]);\n }\n }\n...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& mat) {\n \n unordered_map<int,vector<int>> mp;\n int m = mat.size();\n int n = 0;\n \n for(int i=0;i<m;i++){\n for(int j=0;j<mat[i].size();j++){\n mp[i+j].pu...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n\n vector<int> ans;\n map<int, vector<int>> mp;\n int key = 0;\n for(int i = 0; i<nums.size(); ++i) {\n for(int j = 0; j<nums[i].size(); ++j) {\n mp[i + j].push_...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n\n map<int,vector<int>> mp;\n int size = 0;\n int offset = 0;\n\n for(auto num : nums){\n for(int i=0 ; i<num.size(); i++)\n mp[i + offset].emplace_back(num[i]);\...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n unordered_map<int,vector<int>>diags;\n int t=0;\n for(int i=0;i<nums.size();++i)\n {\n for(int j=0;j<nums[i].size();++j)\n {\n diags[i+j].push_back(nu...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n unordered_map<int,vector<int>>diags;\n int t=0;\n for(int i=0;i<nums.size();++i)\n {\n for(int j=0;j<nums[i].size();++j)\n {\n diags[i+j].push_back(nu...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n unordered_map<int,vector<int>>diags;\n int t=0;\n for(int i=0;i<nums.size();++i)\n {\n for(int j=0;j<nums[i].size();++j)\n {\n diags[i+j].push_back(nu...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n map<int, priority_queue< pair<int,int> , vector<pair<int,int>> > > m1;\n for(int i=0;i<nums.size();i++){\n for(int j=0;j<nums[i].size();j++){\n int sum = i+j;\n ...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n int m=nums.size(),n=nums[0].size();\n int maxi=0;\n unordered_map<int,vector<pair<int,int>>>temp;\n for(int i=0;i<m;i++){\n for(int j=0;j<nums[i].size();j++){\n ...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n map<int,vector<int>>diags;\n int t=0;\n for(int i=0;i<nums.size();++i)\n {\n for(int j=0;j<nums[i].size();++j)\n {\n diags[i+j].push_back(nums[i][j]);...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n size_t count = 0;\n unordered_map<size_t, map<size_t, int, greater<int>>> res_map;\n for (size_t i = 0; i < nums.size(); i++) {\n for (size_t j = 0; j < nums[i].size(); j++) {\n ...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n size_t count = 0;\n unordered_map<size_t, map<size_t, int, greater<int>>> res_map;\n for (size_t i = 0; i < nums.size(); i++) {\n for (size_t j = 0; j < nums[i].size(); j++) {\n ...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n\n vector<int>ans;\n\n map<int,vector<int>>Store;\n\n for(int i=0;i<nums.size();i++) {\n\n for(int j=0;j<nums[i].size();j++) {\n\n Store[i+j].push_back(nums[i][j]);\n ...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n map<int, vector<int>>map;\n vector<int>res;\n for(int i = 0; i<nums.size(); i++) {\n for (int j = 0; j<nums[i].size(); j++) {\n if(map.find(i+j) == map.end()) {\n ...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n map<int,vector<int>> mpp;\n vector<int> ans;\n int n = nums.size();\n for(int i=0;i<n;i++){\n for(int j=0;j<nums[i].size();j++){\n int sum = i+j;\n ...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n\n unordered_map<string, bool> mp;\n queue<pair<int,int>> q;\n q.push({0,0});\n mp[\"0_0\"] = true;\n vector<int> ans;\n while(!q.empty()) {\n int i = q.front().fi...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n vector<int> ans;\n set<pair<int,int>> s;\n map<pair<int,int>,int> mp;\n int n = nums.size();\n for(int i = 0 ; i < n ; i ++){\n int sz = nums[i].size();\n for...
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\n\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n \n int n = nums.size(), m = 0; \n map<int, vector<pair<int, int>>> store; \n\n for(int i = 0; i < n; i++) {\n vector<int> row = nums[i]; \n for(int j = 0; j < row....
1,539
<p>Given a 2D integer array <code>nums</code>, return <em>all elements of </em><code>nums</code><em> in diagonal order as shown in the below images</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width:...
3
{ "code": "class Solution {\n\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n \n int n = nums.size(), m = 0; \n map<int, vector<pair<int, int>>> store; \n\n for(int i = 0; i < n; i++) {\n vector<int> row = nums[i]; \n for(int j = 0; j < row....
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
0
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& arr, int k) {\n int n = arr.size();\n // vector<int> dp(n, INT_MIN); \n\n // int curr = 0;\n int result =INT_MIN;\n\n deque<pair<int,int>> deq; \n // deq.push_back({0,arr[0]});\n\n for(in...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
0
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n deque<int> d = {};\n int ans = -1e9;\n for (int i = 0; i < nums.size(); ++i) {\n while (!d.empty() && i - d.front() > k) {\n d.pop_front();\n }\n \n ...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
0
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n int maxSum = nums[0];\n deque<int> maxSumQueue;\n\n for (int i = 0; i < nums.size(); i++) {\n nums[i] += !maxSumQueue.empty() ? maxSumQueue.front() : 0;\n maxSum = max(maxSum, nu...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
0
{ "code": "// 兩個關鍵\n // 維持deque.front最大\n // 當deque.front為A[i-k]時, pop掉, 代表i-k~i之間都沒拿, dist為k (pefixSum更新A[i])\nclass Solution {\npublic:\n int constrainedSubsetSum(vector<int>& A, int k) {\n // given j-i<=k\n // q.front(): 最左的index, s.t. A[front]~A[i-1] 加總為正\n // q.back(): maxsum start ...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
0
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n deque<int> q;\n int output = nums[0];\n\n for (int i = 0; i < nums.size(); i++) {\n nums[i] += q.size() ? nums[q.front()] : 0;\n output = max(output, nums[i]);\n\n whi...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
0
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n deque<int> q;\n int output = nums[0];\n\n for (int i = 0; i < nums.size(); i++) {\n nums[i] += q.size() ? nums[q.front()] : 0;\n output = max(output, nums[i]);\n\n whi...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
0
{ "code": "class Solution {\npublic:\n int arr[100000];\n int solve(vector<int>& nums, int k, int index){\n if(index<0) return 0;\n if(arr[index]!=-1) return arr[index];\n if(nums[index]>0) return arr[index]=nums[index]+max(0,solve(nums,k,index-1));\n for(int i=index;i>=max(0,index-k...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
0
{ "code": "class Solution {\npublic:\n int arr[100000];\n int solve(vector<int>& nums, int k, int index){\n if(index<0) return 0;\n if(arr[index]!=-1) return arr[index];\n if(nums[index]>0) return arr[index]=nums[index]+max(0,solve(nums,k,index-1));\n for(int i=index;i>=max(0,index-k...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
0
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n deque<pair<int,int>> dq;\n dq.push_back({nums[0], 0});\n int ans=nums[0];\n for (int i=1; i<nums.size(); i++){\n if (i-dq.front().second>k)dq.pop_front();\n int cur=max(nu...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
0
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& arr, int k) {\n int n=arr.size();\n int maxi=INT_MIN;\n deque<pair<int,int>> dq;\n maxi=max(maxi,arr[0]);\n dq.push_back({max(0,arr[0]),0});\n for(int i=1;i<n;i++){\n if(!dq.empty()&&i-...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
1
{ "code": "class Solution {\npublic: \n int tree[4*100001] ; \n void update(int node, int low, int high,int val, int ind) {\n if(low==high) {\n tree[node]=val; \n return ; \n }\n int mid=(low+high)>>1; \n if(ind<=mid) {\n update(2*node,low,mid, val, i...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
1
{ "code": "class Solution {\n priority_queue<pair<int, int>> pq;\n int maxt = 0, i = 0;\n\n public:\n Solution() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n pq.push({0, -1});\n }\n int constrainedSubsetSum(const vector<int> &nums, const int k)...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
1
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> dp(n, INT_MIN);\n deque<int> dq;\n int res = nums[0];\n for(int i = 0; i < n; i++){\n dp[i] = max(dp[i], nums[i] + max(0, (dq.empty() ? 0 : ...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
1
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n k++;\n deque<int> dq, dq2;\n dq.push_back(nums[0]);\n dq2.push_back(nums[0]);\n int ans=nums[0];\n for (int i=1; i<nums.size(); i++){\n if (i-k>=0 && dq.front()==dq2.fr...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
1
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> dp (n+1, INT_MIN);\n for (int i=0; i<n; i++) dp[i] = nums[i];\n deque<pair<int, int>> dq;\n dq.push_back({-1, 0});\n for (int i=0; i<n; i++)\n ...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
1
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> dp(n, 0);\n deque<pair<int,int>> dq;\n dp[0]=nums[0];\n dq.push_back({0,nums[0]});\n for (int i = 1; i < n; i++) {\n dp[i] = nums[i];\n int add = 0;\n ...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
1
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> dp(n, 0);\n deque<pair<int,int>> dq;\n dp[0]=nums[0];\n dq.push_back({0,nums[0]});\n for (int i = 1; i < n; i++) {\n dp[i] = nums[i];\n int add = 0;\n ...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
1
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n vector<int> _max;\n deque<int> d;\n _max.push_back(nums[0]);\n d.push_back(0);\n int ans = _max[0];\n for (int i = 1; i < nums.size(); i++)\n {\n _max.push_back(...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
1
{ "code": "class SegmentTree\n{\n public:\n vector<int> tree;\n int n;\n SegmentTree(int n)\n {\n int x=ceil(log2(n));\n int max_size=2*(int)pow(2,x)-1;\n this->n=max_size;\n this->tree.resize(max_size);\n }\n int query(int ql,int qr,int cl,int cr,int node){\n i...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
1
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n vector<int> dp {nums[0]};\n deque<int> dq {nums[0]};\n int res = nums[0];\n for (int i = 1; i < nums.size(); i++) {\n if (i > k && dp[i-k-1] == dq.front()) dq.pop_front();\n ...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
1
{ "code": "class Solution {\npublic:\n void update(int start, int end, int node, vector<int>& st, int value, int indx){\n if(start==end){\n st[node]=value;\n return;\n }\n int mid=(start+end)/2;\n if(indx<=mid){\n update(start, mid, 2*node+1, st, value, ...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
1
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n\n int n= nums.size();\n int maxno= INT_MIN;\n for(int i=0;i<n;i++)\n {\n maxno= max(maxno, nums[i]);\n }\n if(maxno<=0)\n {\n return maxno;\n }...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
1
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n int n=nums.size(),maxr[n];\n auto comp=[](pair<int,int> &a,pair<int,int> &b){\n return a.first<b.f...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
1
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n int n=nums.size(),maxr[n];\n auto comp=[](pair<int,int> &a,pair<int,int> &b){\n return a.first<b.f...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
1
{ "code": "class Solution {\npublic:\n int getResultFromIdx(vector<int> &nums, vector<int> &idxs, int k,int lastIdx) {\n if(!idxs.size()) return 0;\n if(idxs.size() == 1) return nums[idxs[0]];\n \n int result = 0;\n if(idxs.size() > 1) result += nums[idxs[idxs.size()-1]];\n\n ...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
1
{ "code": "class Solution {\npublic:\n int getResultFromIdx(vector<int> &nums, vector<int> &idxs, int k,int lastIdx) {\n if(!idxs.size()) return 0;\n if(idxs.size() == 1) return nums[idxs[0]];\n \n int result = 0;\n if(idxs.size() > 1) result += nums[idxs[idxs.size()-1]];\n\n ...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
1
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n\n int n = nums.size(), ans = 0;\n priority_queue<pair<int, int>> Q;\n ans = nums[0], Q.push({nums[0], 0});\...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
1
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n priority_queue<pair<int, int>> pq;\n pq.push({nums[0], 0});\n int maxSum = nums[0];\n \n for (int i = 1; i < nums.size(); i++) {\n while (!pq.empty() && (i - pq.top().second) ...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
2
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n priority_queue<pair<int,int>> pq;\n vector<int> dp(nums.size(),0);\n int n=nums.size();\n dp[n-1]=nums[n-1];\n pq.push({nums[n-1],n-1});\n int ans=dp[n-1];\n for(int i=n-2;...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
2
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int>dp(n+1); //maxsum subsequence starting from i \n priority_queue<pair<int,int>>pq;\n for(int i=n-1;i>=0;i--){\n if(i == n-1){\n dp[i] = nums...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
2
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n int n=nums.size();\n vector<int>dp(n,-1);\n dp[n-1]=nums[n-1];\n priority_queue<pair<int,int>>pq;\n pq.push({nums[n-1],n-1});\n for(int i=n-2;i>=0;i--)\n {\n while(pq.top().second-i>k)pq...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
2
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int> dp(n,0);\n dp[n-1] = nums[n-1];\n priority_queue<pair<int,int>> pq;\n pq.push({nums[n-1],n-1});\n for(int i=n-2;i>=0;i--) {\n while(p...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
2
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n priority_queue<pair<int, int>> pq;\n vector<int> dp(nums.size());\n int ans = INT_MIN;\n for (int i ...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
2
{ "code": "class Solution {\npublic:\n \n \n int constrainedSubsetSum(vector<int>& nums, int k) {\n int n=nums.size();\n vector<int>t(n);\n for(int i=0;i<n;i++) t[i]=nums[i];\n priority_queue<pair<int,int>>pq;\n pq.push({t[0],0});\n int result=t[0];\n for...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\n/* Macros {{{ */\n/* A lot of this is from some of Benq's submissions\n [https://codeforces.com/profile/Benq]\n Ugly af to the eyes, but with vim fold its barable\n Hopefully c++20 concepts can make all this stuff must cleaner */\n\n/* Basics {{{ ...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "#define ll int\nclass Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n vector<int> v = nums;\n ll n = v.size();\n vector<int> dp = v;\n priority_queue<pair<ll, ll>> pq;\n pq.push({v[0], 0});\n ll ans = v[0];\n for (int i = 1; i...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n vector<int>t;\n for(int i=0;i<nums.size();i++){\n t.push_back(nums[i]);\n }\n int result=nums[0];\n priority_queue<pair<int,int>>pq;\n pq.push({t[0],0});\n for(i...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n int nNums = nums.size();\n int ans = -100000;\n int sum=0;\n deque<vector<int>> deq;\n\n for (int i=0; i<nNums; i++) {\n if (nums[i]>=0) {\n if (deq.size()>0) \...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "class Solution {\n priority_queue<pair<int, int>> pq;\n int maxt = 0, i = 0;\n\n public:\n Solution() {\n pq.push({0, -1});\n }\n int constrainedSubsetSum(const vector<int> &nums, const int k) {\n int maxte = INT_MIN;\n for (const int &i : nums)\n maxte = max...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "class Solution {\npublic:\n struct TreeNode {\n int begin;\n int end;\n int val;\n TreeNode* left;\n TreeNode* right;\n\n TreeNode(int begin, int end, int val) : begin(begin), end(end), val(val) {};\n };\n\n TreeNode* build(vector<int>& arr, int begin, int...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n /**\n vector to store maxSum, last index.\n if value can increase the sum, update index, if index is <=\n lastIndex + k else, flush the maxSum, start over\n **/\n vector<p...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n int n = nums.size();\n if (n == 1) {\n return nums[0];\n }\n vector<int> dp(n + 1, INT_MIN);\n priority_queue<pair<int, int>> pq;\n pq.push({nums[0], 0});\n for ...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int>dp(n+1,INT_MIN);\n priority_queue<pair<int,int>>pq;\n int maxi=INT_MIN;\n for(int i=0;i<n;i++){\n while(!pq.empty() && pq.top().second<i-k){\...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n vector<int>dp(nums.size());\n map<int,int>m;\n dp[0]=nums[0];\n m[dp[0]]++;\n int ans=max(INT_MIN,dp[0]);\n for(int i=1; i<nums.size(); i++){\n if(i>k){\n ...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n vector<int>dp(nums.size(), INT_MIN);\n map<int, int>mmp;\n int ans=INT_MIN;\n for(int i=nums.size()-1;i>=0;i--){\n if(i==nums.size()){\n dp[i]=nums[i];\n ...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "// class Solution {\n// public:\n// int constrainedSubsetSum(vector<int>& nums, int k) {\n// vector<int>dp(nums.size());\n// map<int,int>m;\n// dp[0]=nums[0];\n// m[dp[0]]++;\n// int ans=max(INT_MIN,dp[0]);\n// for(int i=1; i<nums.size(); i++){\n// ...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n int n=nums.size();\n vector<int> dp(n,0);\n // priority_queue<int> pq;\n map<int,int> mp; \n // pq.push(0);\n mp[0]=1;\n // int sz=1;\n for (int i=0;i<n;i++){\n ...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n int n=nums.size();\n vector<int> dp(n);\n for(int i=0;i<n;i++){\n dp[i]=nums[i];\n }\n map<int,int> mp;\n mp[dp[n-1]]++;\n for(int i=1;i<=min(k,n-1);i++){\n ...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "class Solution {\npublic:\n int solve1(vector<int>& nums, int k) {\n int n = nums.size();\n\n int ans = -1e9;\n deque<int> w;\n map<int, int> count;\n for (int i = 0; i < n; ++i) {\n if (w.size() == k + 1) {\n count[w.front()] -= 1;\n if (count[w.front()] == 0) {\n ...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "#define ll long long\nclass Solution {\n map<ll,ll> mp[100001];\n ll f(ll ind,ll prev,vector<int>& nums, int k){\n ll n=nums.size();\n if(ind==n){\n return 0;\n }\n if(mp[prev+1].find(ind)!=mp[prev+1].end()){\n return mp[prev+1][ind];\n }\n ...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n int i=0,j=0;\n vector<long>dp(nums.size(),0);\n map<int,int>mp;\n while(j<nums.size())\n {\n while(j>=i&&j-i>k)\n {\n mp[dp[i]]--;\n i...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n // multiset<int> st;\n map<int,int> mp;\n int sum = 0;\n int j = 0;\n // st.insert(nums[0]);\n mp[nums[0]]++;\n vector<int> v;\n int mn = -1e9;\n v.push_back(...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n int n = nums.size();\n list<int> l;\n vector<int> t(nums);\n\n int ans = nums[0];\n\n for(int i = 0 ; i<n ; i++)\n {\n while(l.size() && l.front() < (i - k))\n ...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n int n = nums.size();\n unordered_map<int,int> counter;\n counter[0] = 1e6;\n priority_queue<int> pq;\n vector<int> best;\n pq.push(0);\n int ans = nums[0];\n for (in...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "\n\nclass Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n\n int n=nums.size();\n \n vector<pair<int,int>>v;\n int l=-1;\n int count=0;\n for(int i=0;i<nums.size();i++)\n {\n if(nums[i]<0)\n {\n ...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n int n = nums.size() ;\n multiset<int> s ;\n s.insert(0) ;\n int dp[n+1] ;\n dp[0] = 0 ;\n int ans = -1e9 ;\n for(int i = 1;i<=n;i++){\n dp[i] = nums[i-1] ;\n ...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n int n = nums.size(); \n int dp[n]; \n dp[0] = nums[0]; \n\n multiset<int> mt; \n mt.insert(dp[0]); \n\n for(int i = 1; i < n; i++) {\n if(i-k > 0) {\n m...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n int n = nums.size(); \n int dp[n]; \n dp[0] = nums[0]; \n\n multiset<int> mt; \n mt.insert(dp[0]); \n\n for(int i = 1; i < n; i++) {\n if(i-k > 0) {\n m...
1,286
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, return the maximum sum of a <strong>non-empty</strong> subsequence of that array such that for every two <strong>consecutive</strong> integers in the subsequence, <code>nums[i]</code> and <code>nums[j]</code>, where <code>i &lt; j</code>, the co...
3
{ "code": "class Solution {\npublic:\n int constrainedSubsetSum(vector<int>& nums, int k) {\n int n = nums.size();\n vector<int>dp(n, -1e8);\n vector<int>a = nums;\n int cnt = 0;\n for (auto t : a) if (t > 0) cnt++;\n if (cnt <= 1) return *max_element(a.begin(), a.end());\n\n multiset<pair<int...