id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,535
<p>You are given three integers <code>n</code>, <code>m</code> and <code>k</code>. Consider the following algorithm to find the maximum element of an array of positive integers:</p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/02/e.png" style="width: 424px; height: 372px;" /> <p>You should build the arr...
3
{ "code": "#define MOD 1000000007\n#define ll long long\nclass Solution {\npublic:\n vector<vector<vector<ll>>>dp;\n ll solve(int index, int curr, int score, int n, int m, int k) {\n if (index >= n) {\n if (score == k) return 1;\n else return 0;\n }\n if (score > k) re...
1,535
<p>You are given three integers <code>n</code>, <code>m</code> and <code>k</code>. Consider the following algorithm to find the maximum element of an array of positive integers:</p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/02/e.png" style="width: 424px; height: 372px;" /> <p>You should build the arr...
3
{ "code": "// class Solution {\n// public:\n// vector<vector<vector<int>>>memo;\n// int mod=1e9+7;\n// int numOfArraysfun(int ind, int searchspace, int mx,int n,int m,int k){\n// if(ind==n){\n// return (searchspace==k);\n// }\n// if(memo[ind][searchspace][mx+1]!=-1){\n/...
1,535
<p>You are given three integers <code>n</code>, <code>m</code> and <code>k</code>. Consider the following algorithm to find the maximum element of an array of positive integers:</p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/02/e.png" style="width: 424px; height: 372px;" /> <p>You should build the arr...
3
{ "code": "class Solution {\npublic:\n const int MOD = 1e9 + 7;\n\n long long f(int curr_idx, int n, int m, int k, int curr_k, \n int maxSoFar, vector<vector<vector<long long>>> &dp){\n // Base case\n if(curr_idx == n){\n if(curr_k == k){\n return 1;\n ...
1,535
<p>You are given three integers <code>n</code>, <code>m</code> and <code>k</code>. Consider the following algorithm to find the maximum element of an array of positive integers:</p> <img alt="" src="https://assets.leetcode.com/uploads/2020/04/02/e.png" style="width: 424px; height: 372px;" /> <p>You should build the arr...
3
{ "code": "class Solution {\npublic:\n const int MOD = 1e9 + 7;\n\n long long f(int curr_idx, int n, int m, int k, int curr_k, \n int maxSoFar, vector<vector<vector<long long>>> &dp){\n // Base case\n if(curr_idx == n){\n if(curr_k == k){\n return 1;\n ...
1,537
<p>Given a&nbsp;string <code>s</code>&nbsp;of zeros and ones, <em>return the maximum score after splitting the string into two <strong>non-empty</strong> substrings</em> (i.e. <strong>left</strong> substring and <strong>right</strong> substring).</p> <p>The score after splitting a string is the number of <strong>zeros...
0
{ "code": "class Solution {\npublic:\n int maxScore(const std::string& s) {\n int max_score = 0;\n int count_zeros_left = 0;\n int count_ones_right = std::count(s.begin(), s.end(), '1');\n\n for (int i = 0; i < s.size() - 1; ++i) {\n count_zeros_left += (s[i] == '0');\n ...
1,537
<p>Given a&nbsp;string <code>s</code>&nbsp;of zeros and ones, <em>return the maximum score after splitting the string into two <strong>non-empty</strong> substrings</em> (i.e. <strong>left</strong> substring and <strong>right</strong> substring).</p> <p>The score after splitting a string is the number of <strong>zeros...
0
{ "code": "class Solution {\npublic:\n int maxScore(string s) {\n int n=s.size();\n int sum=0;\n for(int i=0;i<n;i++)\n {\n if(s[i]=='1')\n {\n sum +=1;\n }\n }\n int cnt=0;\n if(s[0]=='0')\n {\n cnt++...
1,537
<p>Given a&nbsp;string <code>s</code>&nbsp;of zeros and ones, <em>return the maximum score after splitting the string into two <strong>non-empty</strong> substrings</em> (i.e. <strong>left</strong> substring and <strong>right</strong> substring).</p> <p>The score after splitting a string is the number of <strong>zeros...
3
{ "code": "class Solution {\npublic:\n int maxScore(string s) {\n int result = 0, zero_cnt = 0, current_score = 0;\n vector<int> arr(s.size());\n for(int i = 0; i < s.size(); i++) {\n if(s[i] == '0') {\n zero_cnt++;\n }\n arr[i] = zero_cnt;\n ...
1,537
<p>Given a&nbsp;string <code>s</code>&nbsp;of zeros and ones, <em>return the maximum score after splitting the string into two <strong>non-empty</strong> substrings</em> (i.e. <strong>left</strong> substring and <strong>right</strong> substring).</p> <p>The score after splitting a string is the number of <strong>zeros...
3
{ "code": "class Solution {\npublic:\n int maxScore(string s) {\n int n = s.length(), sum = 0, temp, ans = 0;\n\n vector<int> vec(n);\n for(int i=0;i<n;i++){\n if(s[i] == '0') sum++;\n vec[i] = sum;\n }\n\n for(int i=0;i<n-1;i++){\n temp = vec[i];...
1,537
<p>Given a&nbsp;string <code>s</code>&nbsp;of zeros and ones, <em>return the maximum score after splitting the string into two <strong>non-empty</strong> substrings</em> (i.e. <strong>left</strong> substring and <strong>right</strong> substring).</p> <p>The score after splitting a string is the number of <strong>zeros...
3
{ "code": "class Solution {\npublic:\n int maxScore(string s) {\n vector<int> right(s.size());\n int n= s.size();\n if(s[n-1]=='1')\n right[n-1]=1;\n\n for(int i=s.size()-2;i>0;i--){\n if(s[i]=='1')\n right[i]=1+right[i+1];\n else\n rig...
1,537
<p>Given a&nbsp;string <code>s</code>&nbsp;of zeros and ones, <em>return the maximum score after splitting the string into two <strong>non-empty</strong> substrings</em> (i.e. <strong>left</strong> substring and <strong>right</strong> substring).</p> <p>The score after splitting a string is the number of <strong>zeros...
3
{ "code": "class Solution {\npublic:\n int maxScore(string s) {\n std::vector<int> count_zeros(s.size(), 0);\n std::vector<int> count_ones(s.size(), 0);\n \n int count = 0;\n for (int i = 0; i < s.size(); i++) {\n if (s[i] == '0'){\n count++;\n ...
1,537
<p>Given a&nbsp;string <code>s</code>&nbsp;of zeros and ones, <em>return the maximum score after splitting the string into two <strong>non-empty</strong> substrings</em> (i.e. <strong>left</strong> substring and <strong>right</strong> substring).</p> <p>The score after splitting a string is the number of <strong>zeros...
3
{ "code": "class Solution {\npublic:\n int maxScore(string s) {\n if(s.size()==2 && s[0]=='1' && s[1]=='0'){\n return 0;\n }\n if(s.size()==2 && s[0]=='0' && s[1]=='1'){\n return 2;\n }\n if(s.size()<3){\n return 1;\n }\n vector<int>...
1,537
<p>Given a&nbsp;string <code>s</code>&nbsp;of zeros and ones, <em>return the maximum score after splitting the string into two <strong>non-empty</strong> substrings</em> (i.e. <strong>left</strong> substring and <strong>right</strong> substring).</p> <p>The score after splitting a string is the number of <strong>zeros...
3
{ "code": "class Solution {\npublic:\n int maxScore(string s) {\n unordered_map<char,int>m;\n int zero=0;\n int one=0;\n for(int i=0;i<s.size();i++){\n m[s[i]]++;\n }\n int sum=0;\n int maxm=0;\n one=m['1'];\n cout<<one;\n for(int i=0...
1,537
<p>Given a&nbsp;string <code>s</code>&nbsp;of zeros and ones, <em>return the maximum score after splitting the string into two <strong>non-empty</strong> substrings</em> (i.e. <strong>left</strong> substring and <strong>right</strong> substring).</p> <p>The score after splitting a string is the number of <strong>zeros...
3
{ "code": "class Solution {\npublic:\n int maxScore(string s) {\n unordered_map<char,int>mp;\n for(int i=0;i<s.size();i++){\n mp[s[i]]++;\n }\n int one = mp['1'];\n int zero = 0;\n int res = 0;\n\n for(int i=0;i<s.size()-1;i++){\n if(s[i] == '0...
1,537
<p>Given a&nbsp;string <code>s</code>&nbsp;of zeros and ones, <em>return the maximum score after splitting the string into two <strong>non-empty</strong> substrings</em> (i.e. <strong>left</strong> substring and <strong>right</strong> substring).</p> <p>The score after splitting a string is the number of <strong>zeros...
3
{ "code": "class Solution {\npublic:\n int maxScore(string s) \n {\n int prefixSum[505] = {0};\n int size = s.size();\n int intnum;\n int result[500]={0};\n for(int i=0;i<size;i++)\n {\n if(i==0)\n {\n intnum = s[i] -'0';\n ...
1,537
<p>Given a&nbsp;string <code>s</code>&nbsp;of zeros and ones, <em>return the maximum score after splitting the string into two <strong>non-empty</strong> substrings</em> (i.e. <strong>left</strong> substring and <strong>right</strong> substring).</p> <p>The score after splitting a string is the number of <strong>zeros...
3
{ "code": "class Solution {\npublic:\n int maxScore(string s) {\n map<int, int> m1;\n map<int, int> m2;\n int size = s.length();\n \n for(int i = 0; i < s.length(); i++)\n {\n if(s[i] == '0')\n {\n m1[i] = m1[i-1]+1;\n }\n ...
1,537
<p>Given a&nbsp;string <code>s</code>&nbsp;of zeros and ones, <em>return the maximum score after splitting the string into two <strong>non-empty</strong> substrings</em> (i.e. <strong>left</strong> substring and <strong>right</strong> substring).</p> <p>The score after splitting a string is the number of <strong>zeros...
3
{ "code": "class Solution {\npublic:\n int getScore(int right_begin_at, string s){\n int score = 0;\n for(int i=0; i<s.length(); i++){\n if(i < right_begin_at && s[i] == '0')\n score++;\n else if(i >= right_begin_at && s[i] == '1')\n score++;\n ...
1,537
<p>Given a&nbsp;string <code>s</code>&nbsp;of zeros and ones, <em>return the maximum score after splitting the string into two <strong>non-empty</strong> substrings</em> (i.e. <strong>left</strong> substring and <strong>right</strong> substring).</p> <p>The score after splitting a string is the number of <strong>zeros...
3
{ "code": "#include<iostream>\n#include <vector>\n#include <algorithm>\nclass Solution {\npublic:\nint max(vector<int>& v){\n int maxi = -763;\n\nfor(int i = 0 ; i<v.size();i++){\nif(v[i]>maxi){\n maxi=v[i];\n}\n\n}\nreturn maxi;\n}\nint zerocount(const string& s,int j){\n int count = 0;\n for(int i = 0 ;...
1,537
<p>Given a&nbsp;string <code>s</code>&nbsp;of zeros and ones, <em>return the maximum score after splitting the string into two <strong>non-empty</strong> substrings</em> (i.e. <strong>left</strong> substring and <strong>right</strong> substring).</p> <p>The score after splitting a string is the number of <strong>zeros...
3
{ "code": "#include<iostream>\n#include <vector>\n#include <algorithm>\nclass Solution {\npublic:\nint max(vector<int>& v){\n int maxi = -763;\n\nfor(int i = 0 ; i<v.size();i++){\nif(v[i]>maxi){\n maxi=v[i];\n}\n\n}\nreturn maxi;\n}\nint zerocount(const string& s,int j){\n int count = 0;\n for(int i = 0 ;...
1,537
<p>Given a&nbsp;string <code>s</code>&nbsp;of zeros and ones, <em>return the maximum score after splitting the string into two <strong>non-empty</strong> substrings</em> (i.e. <strong>left</strong> substring and <strong>right</strong> substring).</p> <p>The score after splitting a string is the number of <strong>zeros...
3
{ "code": "class Solution {\npublic:\n int maxScore(string s) {\n int n=s.length();\n int i=1;\n int max_score = INT_MIN;\n while(i<n){\n string temp1 = s.substr(0,i);\n string temp2 = s.substr(i);\n int zeros = 0,ones = 0;\n for(auto k:temp1)...
1,537
<p>Given a&nbsp;string <code>s</code>&nbsp;of zeros and ones, <em>return the maximum score after splitting the string into two <strong>non-empty</strong> substrings</em> (i.e. <strong>left</strong> substring and <strong>right</strong> substring).</p> <p>The score after splitting a string is the number of <strong>zeros...
3
{ "code": "#include <stdio.h>\n#include <string.h>\nclass Solution {\npublic:\n int maxScore(string s) {\n int len = s.length();\n int sum = 0;\n int ret = 0;\n string word1 = \"\";\n string word2 = \"\";\n\n\n for (int i = 1; i < len; i++) {\n word1 = s.substr(...
1,538
<p>There are several cards <strong>arranged in a row</strong>, and each card has an associated number of points. The points are given in the integer array <code>cardPoints</code>.</p> <p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly <code>k</code> cards.</p>...
0
{ "code": "#pragma GCC optimize(\"O3\")\n#pragma GCC target(\"avx2, bmi, bmi2, lzcnt, popcnt\")\nstatic const bool __boost = [](){\n cin.tie(nullptr);\n cout.tie(nullptr);\n return ios_base::sync_with_stdio(false);\n}();\nclass Solution {\npublic:\n int maxScore(vector<int>& cp, int k) {\n \n ...
1,538
<p>There are several cards <strong>arranged in a row</strong>, and each card has an associated number of points. The points are given in the integer array <code>cardPoints</code>.</p> <p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly <code>k</code> cards.</p>...
0
{ "code": "#pragma GCC optimize(\"O3\")\n#pragma GCC target(\"avx2, bmi, bmi2, lzcnt, popcnt\")\nstatic const bool __boost = [](){\n cin.tie(nullptr);\n cout.tie(nullptr);\n return ios_base::sync_with_stdio(false);\n}();\n\nclass Solution {\npublic:\n int maxScore(vector<int>& cardPoints, int k) {\n ...
1,538
<p>There are several cards <strong>arranged in a row</strong>, and each card has an associated number of points. The points are given in the integer array <code>cardPoints</code>.</p> <p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly <code>k</code> cards.</p>...
0
{ "code": "class Solution {\npublic:\n int maxScore(vector<int>& cardPoints, int k) {\n int lsum=0;\n int rsum=0;\n int tsum=0;\n int n=cardPoints.size();\n\n for(int i=0;i<k;i++){lsum=lsum+cardPoints[i];}\n tsum =lsum;\n\n\n for(int j=0;j<k;j++){\n lsum ...
1,538
<p>There are several cards <strong>arranged in a row</strong>, and each card has an associated number of points. The points are given in the integer array <code>cardPoints</code>.</p> <p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly <code>k</code> cards.</p>...
0
{ "code": "class Solution {\npublic:\n int maxScore(vector<int>& cardPoints, int k) {\n int leftSum = 0,rightSum = 0;\n int n = cardPoints.size();\n int maxSum = 0;\n\n for(int i=0; i<k; i++){\n leftSum+=cardPoints[i];\n }\n int right = n-1;\n maxSum = le...
1,538
<p>There are several cards <strong>arranged in a row</strong>, and each card has an associated number of points. The points are given in the integer array <code>cardPoints</code>.</p> <p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly <code>k</code> cards.</p>...
0
{ "code": "class Solution {\npublic:\n int maxScore(vector<int>& cardPoints, int k) {\n int n = cardPoints.size();\n int j = n - 1, suffixSum = 0, totalTaken = 0;\n while(totalTaken < k) {\n suffixSum += cardPoints[j];\n j--;\n totalTaken++;\n }\n\n ...
1,538
<p>There are several cards <strong>arranged in a row</strong>, and each card has an associated number of points. The points are given in the integer array <code>cardPoints</code>.</p> <p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly <code>k</code> cards.</p>...
0
{ "code": "class Solution {\npublic:\n int maxScore(vector<int>& a, int k) {\n int l=0,r=0,ans=0,s=0;\n int n=a.size();\n for(int i=0;i<k;i++){\n s+=a[i];\n ans=max(ans,s);\n }\n int j=n-1;\n for(int i=k-1;i>=0;i--){\n s-=a[i];\n s+=a[j];...
1,538
<p>There are several cards <strong>arranged in a row</strong>, and each card has an associated number of points. The points are given in the integer array <code>cardPoints</code>.</p> <p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly <code>k</code> cards.</p>...
0
{ "code": "class Solution {\npublic:\n int maxScore(vector<int>& cardPoints, int k) {\n int lsum=0;\n int rsum=0;\n int tsum=0;\n int n=cardPoints.size();\n\n for(int i=0;i<k;i++){lsum=lsum+cardPoints[i];}\n tsum =lsum;\n\n\n for(int j=0;j<k;j++){\n lsum ...
1,538
<p>There are several cards <strong>arranged in a row</strong>, and each card has an associated number of points. The points are given in the integer array <code>cardPoints</code>.</p> <p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly <code>k</code> cards.</p>...
0
{ "code": "class Solution {\npublic:\n int maxScore(vector<int>& cardPoints, int k) {\n int sum = 0;\n for(int i=0; i<k ;i++){\n sum += cardPoints[i];\n }\n int ans = sum;\n int n = cardPoints.size()-1;\n for(int i=k-1; i>=0; i--){\n sum -= cardPoints...
1,538
<p>There are several cards <strong>arranged in a row</strong>, and each card has an associated number of points. The points are given in the integer array <code>cardPoints</code>.</p> <p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly <code>k</code> cards.</p>...
2
{ "code": "class Solution {\npublic:\n int maxScore(vector<int>& cardPoints, int k) {\n int n=cardPoints.size(),i,l=n-1,r=k-1,sum=0,ans=0;\n for(i=0;i<k;i++) sum+=cardPoints[i];\n ans=sum;\n while(r>=0)\n {\n sum-=cardPoints[r];\n r--;\n sum+=card...
1,538
<p>There are several cards <strong>arranged in a row</strong>, and each card has an associated number of points. The points are given in the integer array <code>cardPoints</code>.</p> <p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly <code>k</code> cards.</p>...
2
{ "code": "class Solution {\npublic:\n int maxScore(vector<int>& cardPoints, int k) {\n int lsum=0;\n int rsum=0;\n int tsum=0;\n int n=cardPoints.size();\n\n for(int i=0;i<k;i++){lsum=lsum+cardPoints[i];}\n tsum =lsum;\n\n\n for(int j=0;j<k;j++){\n lsum ...
1,538
<p>There are several cards <strong>arranged in a row</strong>, and each card has an associated number of points. The points are given in the integer array <code>cardPoints</code>.</p> <p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly <code>k</code> cards.</p>...
3
{ "code": "class Solution {\npublic:\n int maxScore(vector<int>& cardPoints, int k) \n {\n int leftsum=0;\n for(int i=0;i<k;i++)\n {\n leftsum += cardPoints[i];\n }\n int rightsum=0;\n int maxsum=leftsum;\n int rightindex=cardPoints.size()-1;\n ...
1,538
<p>There are several cards <strong>arranged in a row</strong>, and each card has an associated number of points. The points are given in the integer array <code>cardPoints</code>.</p> <p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly <code>k</code> cards.</p>...
3
{ "code": "class Solution {\npublic:\n int maxScore(vector<int>& cardPoints, int k) {\n int n=cardPoints.size();\n int sum[n+1];\n sum[0]=0;\n for(int i=0;i<n;i++){\n sum[i+1]=sum[i]+cardPoints[i];\n }\n int window=n-k;\n // cout<<\"window:<\"<<window;\n int l=0,r=window;\n ...
1,538
<p>There are several cards <strong>arranged in a row</strong>, and each card has an associated number of points. The points are given in the integer array <code>cardPoints</code>.</p> <p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly <code>k</code> cards.</p>...
3
{ "code": "class Solution {\npublic:\n int maxScore(vector<int>& cardPoints, int k) {\n int n = cardPoints.size();\n ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);\n int arr1[n];\n memset(arr1, 0, sizeof(arr1));\n arr1[0] = cardPoints[0];\n for(int i=1;i<n;i++)\n {...
1,538
<p>There are several cards <strong>arranged in a row</strong>, and each card has an associated number of points. The points are given in the integer array <code>cardPoints</code>.</p> <p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly <code>k</code> cards.</p>...
3
{ "code": "class Solution {\npublic:\n int maxScore(vector<int>& cardPoints, int k) {\n int n=cardPoints.size();\n int sum[n+1];\n sum[0]=0;\n for(int i=0;i<n;i++){\n sum[i+1]=sum[i]+cardPoints[i];\n }\n int window=n-k;\n // cout<<\"window:<\"<<window;\n int l=1,r=window;\n ...
1,538
<p>There are several cards <strong>arranged in a row</strong>, and each card has an associated number of points. The points are given in the integer array <code>cardPoints</code>.</p> <p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly <code>k</code> cards.</p>...
3
{ "code": "class Solution {\npublic:\n int maxScore(vector<int>& cardPoints, int k) {\n int n=cardPoints.size(),l[k+1],r[k+1];\n l[0]=r[0]=0;\n for (int i=1;i<k+1;i++){\n l[i]=l[i-1]+cardPoints[i-1];\n r[i]=r[i-1]+cardPoints[n-i];\n }\n int max=0;\n f...
1,538
<p>There are several cards <strong>arranged in a row</strong>, and each card has an associated number of points. The points are given in the integer array <code>cardPoints</code>.</p> <p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly <code>k</code> cards.</p>...
3
{ "code": "class Solution {\npublic:\n int maxScore(vector<int>& arr, int k) {\n int n=arr.size();\n int left[k],right[k];\n left[0]=arr[0],right[k-1]=arr[n-1];\n \n for(int i=1;i<k;i++){\n left[i]=left[i-1]+arr[i];\n right[k-i-1]=right[k-i]+arr[n-i-1];\n ...
1,538
<p>There are several cards <strong>arranged in a row</strong>, and each card has an associated number of points. The points are given in the integer array <code>cardPoints</code>.</p> <p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly <code>k</code> cards.</p>...
3
{ "code": "class Solution {\npublic:\n int maxScore(vector<int>& cardPoints, int k) {\n int n = cardPoints.size();\n int arr1[n];\n memset(arr1, 0, sizeof(arr1));\n arr1[0] = cardPoints[0];\n for(int i=1;i<cardPoints.size();i++)\n {\n arr1[i] = cardPoints[i]+arr...
1,538
<p>There are several cards <strong>arranged in a row</strong>, and each card has an associated number of points. The points are given in the integer array <code>cardPoints</code>.</p> <p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly <code>k</code> cards.</p>...
3
{ "code": "class Solution {\npublic:\n int maxScore(vector<int>& cardPoints, int k) {\n int n = cardPoints.size();\n int arr1[n];\n memset(arr1, 0, sizeof(arr1));\n arr1[0] = cardPoints[0];\n for(int i=1;i<n;i++)\n {\n arr1[i] = cardPoints[i]+arr1[i-1];\n ...
1,538
<p>There are several cards <strong>arranged in a row</strong>, and each card has an associated number of points. The points are given in the integer array <code>cardPoints</code>.</p> <p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly <code>k</code> cards.</p>...
3
{ "code": "#define ll long long \n#include <algorithm>\n \nclass Solution {\npublic:\n int maxScore(vector<int>& a, int k) {\n int pre[a.size()]; \n int post[a.size()]; \n\n pre[0] = a[0];\n post[a.size() -1 ] = a[a.size()-1 ];\n for ( ll i = 01 ; i < a.size() ; i++ ){\n ...
1,538
<p>There are several cards <strong>arranged in a row</strong>, and each card has an associated number of points. The points are given in the integer array <code>cardPoints</code>.</p> <p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly <code>k</code> cards.</p>...
3
{ "code": "class Solution {\npublic:\n int maxScore(vector<int>& cardPoints, int k) {\n int size = cardPoints.size();\n int suffix[size];\n int prefix[size];\n prefix[0] = cardPoints[0];\n for(int i = 1;i<size;i++){\n prefix[i] = prefix[i-1]+cardPoints[i];\n }\n...
1,538
<p>There are several cards <strong>arranged in a row</strong>, and each card has an associated number of points. The points are given in the integer array <code>cardPoints</code>.</p> <p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly <code>k</code> cards.</p>...
3
{ "code": "class Solution {\npublic:\n int maxScore(vector<int>& cardPoints, int k) {\n int n = cardPoints.size();\n\n vector<int> prefix(n);\n prefix[0] = cardPoints[0];\n for (int i=1; i<n; i++) {\n prefix[i] = prefix[i-1] + cardPoints[i];\n }\n\n int ans = 0;...
1,538
<p>There are several cards <strong>arranged in a row</strong>, and each card has an associated number of points. The points are given in the integer array <code>cardPoints</code>.</p> <p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly <code>k</code> cards.</p>...
3
{ "code": "class Solution {\npublic:\n int maxScore(vector<int>& cardPoints, int k) {\n int n = cardPoints.size();\n vector<int> vec(2*k);\n for(int i=0;i<k;i++){\n vec[i] = cardPoints[k-i-1];\n vec[k+i] = cardPoints[n-i-1];\n }\n\n int l = 0, r = k-1, sum =...
1,538
<p>There are several cards <strong>arranged in a row</strong>, and each card has an associated number of points. The points are given in the integer array <code>cardPoints</code>.</p> <p>In one step, you can take one card from the beginning or from the end of the row. You have to take exactly <code>k</code> cards.</p>...
3
{ "code": "class Solution {\npublic:\n int maxScore(vector<int>& cardPoints, int k) {\n int n = cardPoints.size();\n vector<int> leftSum(k + 1, 0), rightSum(k + 1, 0);\n int lSum = 0, rSum = 0;\n for(int i = 0; i < k; i++) {\n int rightPtr = n - i - 1;\n lSum += ca...
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:...
0
{ "code": "class Solution {\nprivate:\n typedef unsigned short idx_t;\n\n static unsigned pack(const idx_t i, const idx_t j) __attribute__((always_inline)) {\n return (i << 16) + j;\n }\n\n static idx_t unpack_i(const unsigned p) __attribute__((always_inline)) {\n return p >> 16;\n }\n\n s...
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:...
0
{ "code": "static int io_opt = []() {\n std::ios::sync_with_stdio(false);\n return 0;\n}();\n\nclass Solution {\n public:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n vector<int> diagonal_count(1, 0);\n int rsize = static_cast<int>(nums.size());\n for (int i = 0; i < rsize; ++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:...
0
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n vector<pair<int, int>> queue;\n queue.push_back({0, 0});\n vector<int> ans;\n\n while (!queue.empty()) {\n auto [row, col] = queue[0];\n queue.erase(queue.begin());\...
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:...
0
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n auto diagToSize = std::vector<int>{};\n\n for (int i=0; i<nums.size(); ++i) {\n for (int j=0; j<nums[i].size(); ++j) {\n auto diagId = i + j;\n if (diagId >= di...
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:...
0
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n \n queue<pair<int,int>>q;\n q.push({0,0});\n vector<int>result;\n\n while(!q.empty())\n {\n\n auto [row,col]=q.front();\n q.pop();\n\n result.push_b...
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:...
0
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n queue<pair<int, int>> q;\n q.push({0,0});\n vector<int> res;\n \n \n\n while(!q.empty()){\n auto [r,c] = q.front();\n q.pop(); \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:...
0
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n queue<pair<int,int>> queue;\n queue.push({0,0});\n vector<int> ans;\n\n while(!queue.empty()){\n auto [row , col] = queue.front();\n queue.pop();\n ans.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:...
0
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n queue<pair<int, int>> queue;\n queue.push({0, 0});\n vector<int> ans;\n int x, y;\n while (!queue.empty()) {\n tie(x, y) = queue.front();\n queue.pop();\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:...
0
{ "code": "class Solution {\npublic:\n/*\n[1,2,3,4,5],\n[6,7],\n[8],\n[9,10,11],\n[12,13,14,15,16]\nq= (0,1)(2,0)\nrow= 1\ncol=0\nans = 1, 6, 2\n*/\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n queue<pair<int,int>> q;\n q.push({0,0});\n vector<int> ans;\n while(!q.empty...
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:...
0
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& arr) {\n \n int n = arr.size();\n\n vector<int> res;\n\n queue< pair<int,int> > curr;\n queue< pair<int,int> > next;\n\n curr.push({ 0, 0});\n\n while(!curr.empty()) {\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:...
0
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n vector<int> ans;\n queue<pair<int,int>> Q;\n Q.push({0,0});\n while(!Q.empty()){\n auto cur=Q.front();\n Q.pop();\n int i=cur.first,j=cur.second;\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:...
0
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n auto cmp = [&](const pair<int, int>& a, const pair<int, int>& b) {\n auto [ax, ay] = a;\n auto [bx, by] = b;\n if (ax + ay == bx + by) {\n return ax > bx;\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:...
0
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n queue<pair<int,pair<int,int>>> traverseQueue;\n traverseQueue.push({nums[0][0],{0,0}});\n vector<int> ans;\n while(!traverseQueue.empty()){\n ans.push_back(traverseQueue.front(...
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:...
0
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n vector<pair<int, int>> v;\n v.reserve(100000);\n for (int i = 0; i < nums.size(); ++i) {\n for (int j = 0; j < nums[i].size(); ++j) {\n v.push_back({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:...
0
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n priority_queue<pair<int, int>> pq;\n for (unsigned int i = 0; i < nums.size(); i++){\n for (unsigned int j = 0; j < nums[i].size(); j++){\n pq.push({-(i+j), 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:...
0
{ "code": "class Solution {\npublic:\n\n struct comparisonF {\n constexpr bool operator() (pair<int, int>& a, pair<int, int>& b) const noexcept {\n if ( (a.first + a.second) != (b.first + b.second) ) {\n return (a.first + a.second) > (b.first + b.second);\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:...
0
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n const int n = nums.size();\n list<pair<int, int>> l;\n for (int i = n-1; i >= 0; i--) {\n l.push_back({i, 0});\n }\n vector<int> res;\n int pl = 0;\n auto ...
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:...
0
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n vector<tuple<int, int, int>> numTuples;\n for (int i = 0; i < nums.size(); i++) {\n for (int j = 0; j < nums[i].size(); j++) {\n numTuples.push_back({ i + j, nums.size() - 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:...
0
{ "code": "class Solution {\npublic:\n //好題目,學習讓預設sort函數幫我們自動排序(按照參數順序)\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n ios_base::sync_with_stdio(0);\n cin.tie(NULL);\n cout.tie(NULL);\n\n int n = nums.size(), limit;\n vector<tuple<int,int,int>> data;\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:...
0
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n std::vector<std::tuple<int, int, int>> res;\n\n for(int i = 0; i < int(nums.size()); i++)\n {\n for(int j = 0; j < int(nums[i].size()); j++)\n {\n res.push_b...
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:...
0
{ "code": "\nclass Solution {\npublic:\n struct sumRowNum{\n int sum;\n int col;\n int val;\n\n bool operator>(const sumRowNum& other) const\n {\n if(sum == other.sum)\n return col > other.col;\n return sum > other.sum;\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:...
0
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n vector<int>ans;\n \n priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>>pq;\n for(int i=0;i<nums.size();i++){\n for(int j=0...
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:...
0
{ "code": "class Solution {\npublic:\n static bool comp(const pair<int, pair<int, int>>& p1, const pair<int, pair<int, int>>& p2){\n if(p1.first != p2.first){\n return p1.first < p2.first;\n }else{\n return p1.second.first > p2.second.first;\n }\n\n }\n vector<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:...
0
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n \n vector<pair<int, pair<int,int>>> tupl; \n vector<int> ans;\n\n for(int i = 0; i < nums.size(); ++i) {\n for(int j = 0; j < nums[i].size(); ++j) {\n int sum = ...
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:...
0
{ "code": "#define PIII pair<pair<int, int>, int>\nclass Solution {\npublic:\n static bool compare(const PIII &a, const PIII &b) {\n if(a.first.first == b.first.first) {\n return a.first.second < b.first.second;\n }\n return a.first.first <= b.first.first;\n }\n \n vector<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:...
1
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n vector<int> ans;\n size_t maxT = 0;\n for (int i = 0 ;i < nums.size(); i++) {\n maxT = max(maxT, i + nums[i].size() -1);\n }\n int m =nums.size();\n vector<int> 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 struct Entry\n {\n int val;\n long long position;\n \n// void computePosition()\n// {\n// position = static_cast<long long>(x + y + 1) * (x + y + 2) / 2 - y;\n// }\n bool operator<(Entry const& arg) const\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:...
1
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n vector<int> ans;\n priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>, greater<tuple<int, int, int>>> q;\n q.push({0, 0, 0});\n int n = nums.size();\n vector<int> m(...
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 vector<int> ans ;\n queue<pair<int, int>> st ;\n vector<vector<int>> vis(nums.size()) ; \n for(int i = 0 ; i < nums.size() ; i++) vis[i].resize(nums[i].size() , 0) ; \n pair<int ,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:...
1
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n vector < pair <long long,int> > R;\n\n for (int i = 0; i < nums.size();i++) {\n for (int j = 0; j < nums[i].size();j++){\n R.push_back({(long long)(i+j)*100005 - i,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:...
1
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n int n=nums.size();\n vector<int>ans;\n int mx=0;\n for(int i=0;i<n;i++){\n mx=max(mx,(int)nums[i].size());\n }\n int total_size=n+mx-1;\n vector<int>adj[to...
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 n = nums.size();\n vector<int>ans;\n int len = -1;\n for(int i= 0; i<n; i++){\n len = max(len, static_cast<int>(nums[i].size()));\n }\n int m = len;\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:...
1
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) \n {\n int nrow = static_cast<int>(nums.size());\n int ncol=0;\n for(int i=0;i < nrow;i++)\n { ncol = max(ncol, static_cast<int>(nums[i].size()));}\n\n vector<vector<int>>result(nro...
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 vector<int> result;\n if (nums.empty()) return result;\n int maxRow = nums.size();\n int maxCol = 0;\n for (const auto& row : nums) {\n maxCol = max(maxCol, (int)row.siz...
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 vector<vector<int>> buckets;\n int m = nums.size();\n for(int i = m-1;~i;--i)\n {\n int n = nums[i].size();\n for(int j = n-1;~j;--j)\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:...
1
{ "code": "class Solution {\npublic:\n struct Entry\n {\n int x;\n int y;\n int val;\n long long position;\n \n void computePosition()\n {\n position = static_cast<long long>(x + y + 1) * (x + y + 2) / 2 - y;\n }\n bool operator<(Entry const&...
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 vector<tuple<int,int,int>>vc;\n for (int i = 0; i <nums.size() ; ++i) {\n for (int j = 0; j < nums.at(i).size(); ++j) {\n vc.push_back({i+j,-i,nums.at(i).at(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 R = nums.size();\n int C = INT_MIN; for(auto & v : nums) C = max(C, (int)v.size());\n\n vector<vector<pair<int,int>>> temp(2*(max(R, C)) + 1);\n for(int i = 0; i < R; ++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:...
1
{ "code": "/*\n\nidk y my code is not able to transpose [[1,2,3],[4],[5,6,7],[8],[9,10,11]] this matrix\ncoz this code is able to do transpose when I checked it manually \n\n#include <algorithm>\nclass Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n\n int maxi=0;\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 maxColSize = 0;\n int rows = nums.size();\n for(int i=0;i<rows;i++){\n maxColSize = max((int)nums[i].size(),maxColSize);\n }\n int sz = rows+maxColSize;\n vec...
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 //For an infinite square grid with an upper left corner,\n //we have a function that takes 2D position to ordinal number:\n //(0,0) -> 1\n //(0,1) -> 2\n //(1,0) -> 3\n //(0...
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 //For an infinite square grid with an upper left corner,\n //we have a function that takes 2D position to ordinal number:\n //(0,0) -> 1\n //(0,1) -> 2\n //(1,0) -> 3\n //(0...
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 //For an infinite square grid with an upper left corner,\n //we have a function that takes 2D position to ordinal number:\n //(0,0) -> 1\n //(0,1) -> 2\n //(1,0) -> 3\n //(0...
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 std::vector<std::vector<int>> ordered_nums;\n\n for (int i=0; i<nums.size(); ++i) {\n for (int j=0; j<nums[i].size(); ++j) {\n if (i+j >= ordered_nums.size()) {\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 i, j, ind;\n vector<vector<int>> inter;\n vector<int> res;\n\n for(i = nums.size() - 1; i >= 0; i--) {\n for(j = 0; j < nums[i].size(); j++) {\n ind = 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:...
1
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) \n {\n vector<vector<int>> arr;\n\n int m = nums.size();\n for(int i=0;i<m;i++)\n {\n for(int j=0;j<nums[i].size();j++)\n {\n if(arr.size() <= 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:...
1
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n vector<int> result;\n int max_col_index = 0;\n int si;\n for(auto i:nums){\n si=i.size();\n max_col_index = max(max_col_index,si);\n }\n vector<vector<...
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 vector<vector<int>> A;\n int m=nums.size(), i, j;\n for(i=0; i<m; i++) {\n int n=nums[i].size();\n for(j=0; j<n; j++) {\n int k=i+j;\n if (k==...
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 // 10:03 PM - 10:13 PM\n vector<int> ans;\n queue<vector<int>> cells;\n cells.push({0, 0});\n while (cells.size()){\n int row = cells.front()[0];\n int col = ...
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 unsigned long long id(int i,int j,const int col){\n unsigned long long len = col*2;\n unsigned long long bottom = i+j;\n bottom *= len;\n bottom += j;\n return bottom;\n }\n vector<int> findDiagonalOrder(vector<vector<int>>& m) {\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": "# include <bits/stdc++.h>\n\nclass Solution {\npublic:\n\n void printAns(vector<int> &ans) {\n for (auto num : ans) {\n cout << num << \" \";\n }\n cout << endl;\n }\n\n void printRows(set<int> &rows) {\n for (auto row : views::reverse(rows)) {\n c...
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 vector<int> out;\n int maxCol=0;\n for(int i=0; i< nums.size();i++){\n maxCol = max(maxCol, (int)nums[i].size());\n }\n vector<vector<int>> inter(maxCol + nums.size(),ve...
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 unordered_set<int> visited;\n q.emplace(0, 0);\n visited.insert(0);\n \n vector<int> result;\n while (!q.empty()) {\n auto [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:...
1
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n int n = nums.size(), mx = 0;\n long long mul = 1e5+1;\n vector<int> res;\n queue<pair<int, int>> q;\n q.push({0, 0});\n unordered_set<long long> st;\n st.insert(0);\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 n = nums.size();\n int m = -1;\n for(int i=0;i<n;i++){\n int size = nums[i].size();\n m = max(m,size);\n }\n\n // vector<vector<int>> input(n,vector<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:...
1
{ "code": "class Solution {\npublic:\n vector<int> findDiagonalOrder(vector<vector<int>>& nums) {\n int n = nums.size();\n int m = -1;\n\n vector<vector<bool>> visited;\n\n for(int i=0;i<n;i++){\n int m = nums[i].size();\n vector<bool> temp(m,false);\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 {\n struct SparseMatrixCellInfo {\n size_t x;\n size_t y;\n size_t rowColSum;\n int value;\n };\n\npublic:\n std::vector<int> findDiagonalOrder(std::vector<std::vector<int>>& nums) {\n std::vector<SparseMatrixCellInfo> processedNums{};\n\n f...