id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
2,366
<p>You have <code>n</code> bags numbered from <code>0</code> to <code>n - 1</code>. You are given two <strong>0-indexed</strong> integer arrays <code>capacity</code> and <code>rocks</code>. The <code>i<sup>th</sup></code> bag can hold a maximum of <code>capacity[i]</code> rocks and currently contains <code>rocks[i]</co...
3
{ "code": "class Solution {\npublic:\n \n bool chk(vector<int> c, int rocks, int mid){\n int n = c.size(), count = 0;\n for(int i=0; i<n; i++){\n if(rocks==0) break;\n else if(c[i]==0) count++;\n else if (c[i]>0 and rocks>=c[i]) {\n rocks-=c[i];\n ...
2,366
<p>You have <code>n</code> bags numbered from <code>0</code> to <code>n - 1</code>. You are given two <strong>0-indexed</strong> integer arrays <code>capacity</code> and <code>rocks</code>. The <code>i<sup>th</sup></code> bag can hold a maximum of <code>capacity[i]</code> rocks and currently contains <code>rocks[i]</co...
3
{ "code": "class Solution {\npublic:\n \n bool chk(vector<int> c, int rocks, int mid){\n int n = c.size(), count = 0;\n for(int i=0; i<n; i++){\n if(rocks==0) break;\n else if(c[i]==0) count++;\n else if (c[i]>0 and rocks>=c[i]) {\n rocks-=c[i];\n ...
2,366
<p>You have <code>n</code> bags numbered from <code>0</code> to <code>n - 1</code>. You are given two <strong>0-indexed</strong> integer arrays <code>capacity</code> and <code>rocks</code>. The <code>i<sup>th</sup></code> bag can hold a maximum of <code>capacity[i]</code> rocks and currently contains <code>rocks[i]</co...
3
{ "code": "class Solution {\npublic:\n int maximumBags(vector<int>& capacity, vector<int>& rocks, int additionalRocks) {\n vector<vector<int>> data(rocks.size(), vector<int>(2));\n for (int i = 0; i < rocks.size(); i++) {\n data[i][0] = capacity[i];\n data[i][1] = rocks[i];\n ...
2,366
<p>You have <code>n</code> bags numbered from <code>0</code> to <code>n - 1</code>. You are given two <strong>0-indexed</strong> integer arrays <code>capacity</code> and <code>rocks</code>. The <code>i<sup>th</sup></code> bag can hold a maximum of <code>capacity[i]</code> rocks and currently contains <code>rocks[i]</co...
3
{ "code": "class Solution {\npublic:\n int maximumBags(vector<int>& cap, vector<int>& rocks, int extra) {\n int n = cap.size();\n vector<vector<int>> arr(n);\n for (int i=0;i<n;i++) {\n arr[i] = {cap[i], rocks[i]};\n }\n\n sort(begin(arr), end(arr), [](const vector<int...
2,366
<p>You have <code>n</code> bags numbered from <code>0</code> to <code>n - 1</code>. You are given two <strong>0-indexed</strong> integer arrays <code>capacity</code> and <code>rocks</code>. The <code>i<sup>th</sup></code> bag can hold a maximum of <code>capacity[i]</code> rocks and currently contains <code>rocks[i]</co...
3
{ "code": "class Solution {\npublic:\n int maximumBags(vector<int>& nums, vector<int>& arr, int k) {\n \n \n multiset<int> st;\n int n=nums.size();\n \n for(int i=0;i<n;i++){\n st.insert(nums[i]-arr[i]);\n }\n \n int ans=0;\n \n ...
2,366
<p>You have <code>n</code> bags numbered from <code>0</code> to <code>n - 1</code>. You are given two <strong>0-indexed</strong> integer arrays <code>capacity</code> and <code>rocks</code>. The <code>i<sup>th</sup></code> bag can hold a maximum of <code>capacity[i]</code> rocks and currently contains <code>rocks[i]</co...
3
{ "code": "class Solution {\npublic:\n int maximumBags(vector<int>& capacity, vector<int>& rocks, int additionalRocks) {\n multiset<int> diffs;\n for (int i = 0; i < rocks.size(); i++) {\n diffs.insert(capacity[i] - rocks[i]);\n }\n\n int count = 0;\n for (auto diff: d...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "const int mod = 1e9+7;\nconst int maxn = 1e5+5;\nint l[maxn], r[maxn];\nlong long csum[maxn];\nclass Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n long long tsum = 0, ttsum = 0;\n int n = strength.size();\n for(int i = 1; i <= n; i++){\n tsum = (tsum+...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "using LL = long long;\nclass Solution {\n int mod = 1e9+7;\npublic:\n int totalStrength(vector<int>& strength) {\n /*\n total strength = min strength * sum(strength)\n\n l mid r\n 6 4 3 2 1 2 3 4 5 6 7\n \n start from mid...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n int n = strength.size();\n long long res = 0, pps[n + 1];\n pps[0] = 0;\n stack<int> st;\n constexpr static int MOD = 1e9 + 7;\n\n\n partial_sum(strength.begin(), strength.end(), pps + 1,\n...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "class Solution {\n int mod = 1e9+7;\npublic:\n int totalStrength(vector<int>& strength) {\n /*\n total strength = min(strength in subset) * sum(strength in subset)\n\n [1 3 10 32 7 23 12 5]\n\n we focus on 7, \n l mid r\n 1 3 [...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n int n = strength.size();\n stack <int> s;\n int left[n], right[n], sum[n];\n long long M = 1000000007;\n\n for(int i = 0; i < n; i++) {\n while(!s.empty() && strength[s.top()] > strengt...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "class Solution {\n int mod = 1e9+7;\n int p[100005];\n int prev[100005];\n int nxt[100005];\n bool ok[100005];\n int get_prev(int x){\n return prev[x] == -1?x:prev[x]=get_prev(prev[x]);\n }\n int get_nxt(int x){\n return nxt[x] == -1?x:nxt[x]=get_nxt(nxt[x]);\n }\n ...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "class Solution {\npublic:\n int mod=1e9+7;\n int totalStrength(vector<int>& strength) {\n int n=strength.size(); \n long long ps[n+1], pps[n+1], left[n], right[n], sum=0, ans=0;\n for(int i=0;i<n;i++) {\n sum += strength[i];\n ps[i] = sum;\n pps[i...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "class Solution {\npublic:\n #define ll long long\n int totalStrength(vector<int>& A) {\n int res = 0, ac = 0, mod = 1e9 + 7, n = A.size();\n vector<int> stack = {}, acc(n + 2);\n for (int r = 0; r <= n; ++r) {\n int a = r < n ? A[r] : 0;\n ac = (ac + a) % mo...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n int n = strength.size(), mod = 1e9+7;\n\n long long prefixSum = 0;\n vector<long long> prefixAccu(n+1, 0LL);\n for (int i = 0; i < n; i++) {\n prefixSum = (prefixSum+strength[i])%mod;\n ...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n int n = strength.size(), mod = 1e9+7;\n\n vector<long long> prefixAccu(n+1, 0LL);\n partial_sum(strength.begin(), strength.end(), prefixAccu.begin()+1, [&](int a, int b) {\n return (a+b) % mod;\n ...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& s) {\n long long res = 0, sz = s.size(), mod = 1000000007;\n vector<int> st;\n vector<long long> pps(s.size() + 1);\n partial_sum(begin(s), end(s), begin(pps) + 1, [&](int s, int n){ return (s + n) % mod; });\n partial_sum(begin(p...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& str) {\n int sz = str.size();\n long long res = 0, bn7 = 1E9+7, sm = 0;\n stack<int> stk;\n vector<long long> pSum(sz+2, 0);\n\n for(int r = 0; r <= sz; r++) {\n int s = r<sz ? str[r] : 0;\n ...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& arr) {\n int size = arr.size();\n const int mod = 1e9 + 7;\n \n vector<int> prefix(size + 1, 0);\n for(int i=1;i<=size;i++) {\n prefix[i] = (arr[i-1]%mod + prefix[i-1]%mod)%mod;\n }\n\n ...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n const int MOD = 1e9 + 7;\n int n = strength.size();\n \n // To store the final result\n long long totalSum = 0;\n \n // To keep track of prefix sums and cumulative sums\n long...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n const int MOD = 1e9 + 7;\n int n = strength.size();\n \n // To store the final result\n long long totalSum = 0;\n \n // To keep track of prefix sums and cumulative sums\n long...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "class Solution {\npublic:\n int modulo = 1000000007;\n void populateJustSmallestOnNearest(vector<int>& strength, int justSmallerOnNearestLeft[], int justSmallerOnNearestRight[]){\n int n = strength.size();\n stack<pair<int, int>> st;\n for(int i=0; i<n; i++){\n while(s...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n const int M = 1e9 + 7;\n int n = strength.size();\n vector<int> preSmallerOrEqual(n, -1), nextSmaller(n, n);\n stack<int> stk;\n for (int i = 0; i < n; ++i) {\n while (!stk.empty() && s...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n int n = strength.size();\n const int mod = 1000000007;\n vector<int> right(n + 1, n + 1);\n vector<int> left(n + 1, 0);\n stack<int> stk;\n for(int i = 0; i < n; i++) {\n while(!...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n int n = strength.size();\n const int mod = 1000000007;\n vector<int> right(n + 1, n + 1);\n vector<int> left(n + 1, 0);\n stack<int> stk;\n for(int i = 0; i < n; i++) {\n while(!...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n // left[0..n - 1]\n // right[0....n-1]\n // prefix[1...n]\n // sumofsum[1...n]\n int n = strength.size();\n const int mod = 1000000007;\n vector<int> right(n + 1, n + 1);\n ve...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "#pragma GCC optimize(\"O3\", \"unroll-loops\")\nclass Solution {\npublic:\n void print(auto& c){\n int sz=c.size();\n cout<<\"[\";\n for(int i=0; i<sz-1; i++) cout<<c[i]<<\", \";\n cout<<c[sz-1]<<\"]\\n\";\n }\n int totalStrength(vector<int>& s) {\n int n=s.size(...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "#pragma GCC optimize(\"O3\", \"unroll-loops\")\nclass Solution {\npublic:\n void print(auto& c){\n int sz=c.size();\n cout<<\"[\";\n for(int i=0; i<sz-1; i++) cout<<c[i]<<\", \";\n cout<<c[sz-1]<<\"]\\n\";\n }\n int totalStrength(vector<int>& s) {\n int n=s.size(...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "#pragma GCC optimize(\"O3\", \"unroll-loops\")\nclass Solution {\npublic:\n void print(auto& c){\n int sz=c.size();\n cout<<\"[\";\n for(int i=0; i<sz-1; i++) cout<<c[i]<<\", \";\n cout<<c[sz-1]<<\"]\\n\";\n }\n int totalStrength(vector<int>& s) {\n int n=s.size(...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "typedef long long ll;\n\nclass Solution {\npublic:\n int totalStrength(vector<int>& a) {\n n = a.size();\n vector<ll> pp = getPrefixOfPrefix(a);\n\n ll res = 0;\n stack<int> st;\n for(int r = 0; r <= n; r++) {\n while(!st.empty() && (r == n || a[r] <= a[st.t...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "using ll = long long;\n\nclass Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n if (strength.empty()) {\n return 0;\n }\n\n strength.push_back(0);\n\n int n = strength.size() , MOD = 1'000'000'007;\n vector<ll> prefixSumA(n, 0);\n ve...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "using ll = long long;\n\nclass Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n if (strength.empty()) {\n return 0;\n }\n\n strength.insert(strength.begin(), 0);\n strength.push_back(0);\n\n int n = strength.size() , MOD = 1'000'000'007;\n ...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n strength.push_back(0);\n\n const int n = strength.size();\n const int64_t MOD = 1e9 + 7;\n\n int64_t total = 0;\n\n // 1-indexed\n vector<int64_t> p(n+1, 0);\n for (int i = 0; i < n;...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n strength.push_back(0);\n\n const int n = strength.size();\n const int64_t MOD = 1e9 + 7;\n\n int64_t total = 0;\n\n // 1-indexed\n vector<int64_t> p(n+1, 0);\n for (int i = 0; i < n;...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n int n = strength.size();\n constexpr int mod = 1e9 + 7;\n\n vector<int> rf(n, n);\n vector<int> lf(n, -1);\n vector<int> pref(n + 1);\n\n for (int i = 1; i <= n; ++i) {\n pref[i]...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "using ll = long long;\n\nclass Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n if (strength.empty()) {\n return 0;\n }\n\n strength.insert(strength.begin(), 0);\n strength.push_back(0);\n\n int n = strength.size() , MOD = 1'000'000'007;\n ...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
0
{ "code": "class Solution {\npublic:\n //https://leetcode.com/problems/sum-of-total-strength-of-wizards/discuss/2530049/JAVA-Easy-to-understand-Time-%3A-O(n)-Space-O(n)\n const long long mod = 1000000007;\n int totalStrength(vector<int>& arr) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
1
{ "code": "class Solution {\n const int P=1e9+7;\n int i,j,n,x,y;\n long long tmp,ans;\npublic:\n int totalStrength(vector<int>&a) {\n n=a.size();\n ans=0;\n int*l=new int [n];\n int*r=new int [n];\n int*s=new int [n+1];\n int*sl=new int [n+1];\n int*sr=new...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
1
{ "code": "class Solution {\n const int P=1e9+7;\n int i,j,n,x,y;\n long long tmp,ans;\npublic:\n int totalStrength(vector<int>&a) {\n n=a.size();\n ans=0;\n int*l=new int [n];\n int*r=new int [n];\n int*s=new int [n+1];\n int*sl=new int [n+1];\n int*sr=new...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
1
{ "code": "const int MOD = 1e9 + 7;\nclass Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n int n = strength.size();\n vector<int> left(n, -1), right(n, n);\n stack<int> st;\n for (int i = 0; i < n; ++i) {\n while (!st.empty() && strength[st.top()] >= strength[i])\n st.pop()...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
1
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& nums) {\n const int MOD = 1000000007;\n int n = nums.size();\n\n vector<int> pre(n + 1, 0);\n vector<int> presum(n + 2, 0);\n\n for (int i = 0; i < n; i++) {\n pre[i + 1] = (pre[i] + nums[i]) % MOD;\...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
1
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n const int mod = 1e9 + 7;\n int n = strength.size();\n vector<int> left(n), right(n);\n stack<int> s;\n\n // finding index of first element to the the left that is smaller\n // than or equal...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
1
{ "code": "class Solution {\npublic:\n\n int MOD = (1e9+7);\n\n int totalStrength(vector<int>& strength) {\n\n int N = strength.size();\n\n\n vector<long long> prefix(N + 1, 0L);\n for (int i = 0; i < N; ++i) {\n prefix[i + 1] = (prefix[i] + strength[i]) % MOD;\n }\n // sum of ...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
1
{ "code": "class Solution {\npublic:\n using ll = long long;\n const static int MOD = 1e9 + 7;\n int totalStrength(vector<int>& st) {\n int size = st.size();\n vector<int> left(size, 0);\n vector<int> right(size, 0);\n vector<ll> diff(size + 2, 0);\n vector<int> v;\n ...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
1
{ "code": "using ll = long long;\nclass Solution {\n const static ll mod = 1e9 + 7;\npublic:\n int totalStrength(vector<int>& a) {\n int n = a.size();\n vector<int> L(n, -1), R(n, n);\n vector<int> stk;\n for (int i = 0; i < n; ++i) {\n int num = a[i];\n while (...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
1
{ "code": "class Solution {\npublic:\n const int mod = 1e9+7 ; \n #define ll long long\n int totalStrength(vector<int>& nums) {\n int n = nums.size() ;\n vector<int>nsl(n, 0 ) , nsr(n,0) ; \n for(int i = 0 ; i<n;i++){\n nsl[i] = i-1 ; \n while(nsl[i]!=-1 and num...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
1
{ "code": "class Solution {\npublic:\n int mod=1e9+7;\n int Value(vector<int> a, int n){\n vector<long long>pre(n+2,0);\n for(int i=1;i<=n;i++)\n pre[i]=(pre[i-1]+a[i-1])%mod;\n for(int i=1;i<=n+1;i++)\n pre[i]=(pre[i-1]+pre[i])%mod;\n for(int i=n+1;i>0;i--)\n ...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
1
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n int n = strength.size();\n vector<int> pre(n, 0);\n vector<long long> abc(n, 0);\n vector<int> bef(n, 0);\n vector<int> aft(n, 0);\n stack<int> nums;\n int mod = 1e9 + 7;\n fo...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
1
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n vector<int> left_small(strength.size(), 0);\n \n long int mod = 1e9 + 7;\n stack<int> left_mins;\n for (int i = 0; i < strength.size(); i++) {\n while (left_mins.size() > 0 && strength[...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
1
{ "code": "class Solution {\npublic:\n int totalStrength(std::vector<int> &strength) {\n const auto MOD = static_cast<int>(1e9) + 7;\n const auto sz = strength.size();\n std::vector<int> lessL(sz, 0);\n std::vector<int> leqR(sz, 0);\n std::vector<int> pref(sz, 0);\n std::vector<int> pref2(sz, 0);\n...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
1
{ "code": "long long mod = 1e9+7;\nclass Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n int len = strength.size();\n vector<int> l_bound(len);\n vector<int> r_bound(len);\n\n stack<pair<int/*val*/,int/*pos*/>> inc;\n for(int i = 0; i < len; ++i) {\n ...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
1
{ "code": "#define ll long long\nclass Solution {\npublic:\n ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}\n ll mod_sub(ll a, ll b, ll m){a = a % m;b = b % m;return (((a - b) % m) + m) % m;}\n ll mod_mul(ll a, ll b, ll m){a = a % m;b = b % m;return (((a * b) % m) + m) %...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
1
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n stack<int> st;\n int n = strength.size();\n int mod = 1e9 + 7;\n vector<int> right(n, n), left(n, -1);\n for(int i=0;i<n;i++){\n while(st.empty() == false && strength[st.top()] >= stren...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
1
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n int n = strength.size();\n vector<int> nse(n, n);\n vector<int> st;\n for (int i = n - 1; i >= 0; i--) {\n while (st.size() && strength[st.back()] > strength[i])\n st.pop_back()...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
1
{ "code": "class Solution {\n using LL = long long;\n LL M = 1e9+7;\npublic:\n int totalStrength(vector<int>& nums) {\n int n = nums.size();\n nums.insert(nums.begin(), 0);\n\n vector<LL>presum(n+2, 0);\n for (int i=1; i<=n; i++)\n presum[i] = (presum[i-1]+(LL)nums[i]) ...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
1
{ "code": "const long mod=1e9+7;\nclass Solution {\npublic:\n int totalStrength(vector<int>& a1) {\n \n vector<long long> a;\n for(int i=0;i<a1.size();i++) a.push_back(a1[i]);\n long long n=a.size();\n stack<long long> st;\n stack<long long> v;\n st.push(-1);\n ...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
1
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n int n = strength.size();\n vector<int> left(n), right(n);\n stack<int> stl, str;\n for(int i=0; i<n; ++i){\n while(!stl.empty() && strength[stl.top()] >= strength[i])\n stl.pop(...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
1
{ "code": "using ll = long long;\nconst int MOD = 1e9+7;\nclass Solution {\npublic:\n int totalStrength(vector<int>& v) {\n int n = (int)v.size();\n \n vector<long long> leftdp(n);\n vector<long long> rightdp(n);\n vector<long long> pref(n);\n long long ans =0;\n le...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
1
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int> &strength) {\n int n = strength.size();\n vector<int> rightMin(n,n);\n stack<int> st;\n for(int i = n-1; i >= 0; i--) {\n while(!st.empty() and strength[st.top()] >= strength[i])\n st.pop();\n...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
1
{ "code": "class Solution {\npublic:\n const int mod=(1e9+7);\n int totalStrength(vector<int>& s) {\n int n=s.size();\n vector<long long>pre(n+1,0ll);\n for(int i=0;i<n;i++) pre[i+1]=(pre[i]+s[i])%mod;\n vector<long long>ppre(n+2,0ll);\n for(int i=0;i<n+1;i++) ppre[i+1]=(ppre[...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
1
{ "code": "\nclass Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n const long mod = 1e9 + 7;\n const int n = strength.size();\n std::vector<int> left(n);\n std::vector<int> right(n);\n std::stack<int> nextKeeper;\n for(int i = 0; i < n; i++){\n ...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
2
{ "code": "typedef long long int ll;\nconst int mod = 1e9+7;\n\nclass Solution {\npublic:\n int totalStrength(vector<int>& str) {\n int n = str.size();\n vector<int> prev (n);\n vector<int> next (n);\n \n stack<int> s;\n for (int j = 0; j < n; j ++) {\n while (!...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
2
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n vector<int> left_small(strength.size(), 0);\n \n long int mod = 1e9 + 7;\n stack<int> left_mins;\n for (int i = 0; i < strength.size(); i++) {\n while (left_mins.size() > 0 && strength[...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
2
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n constexpr uint64_t mod = 1'000'000'007;\n\n vector<uint64_t> psums(strength.size(), 0);\n vector<uint64_t> ppsums(strength.size() + 1, 0);\n\n const auto safeAccum = [mod](const uint64_t acc, const uint6...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
2
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n constexpr uint64_t mod = 1'000'000'007;\n const auto safeSub = [mod](const uint64_t lhs, const uint64_t rhs) {\n return (lhs % mod + mod - rhs % mod) % mod;\n };\n const auto safeSum = [mod](c...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
2
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n constexpr uint64_t mod = 1'000'000'007;\n\n vector<uint64_t> psums(strength.size(), 0);\n vector<uint64_t> ppsums(strength.size() + 1, 0);\n\n const auto safeAccum = [mod](const uint64_t acc, const uint6...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
2
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n constexpr uint64_t mod = 1'000'000'007;\n const auto safeSub = [mod](const uint64_t lhs, const uint64_t rhs) {\n return (lhs % mod + mod - rhs % mod) % mod;\n };\n const auto safeSum = [mod](c...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
3
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& st) {\n long long MOD = 1'000'000'007;\n const int N = st.size();\n // sum of first k elements\n vector<long long> prefix(N + 1, 0L);\n for (int i = 0; i < N; ++i) {\n prefix[i + 1] = (prefix[i] + st[i]) % MOD;\n }\n ...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
3
{ "code": "class Solution {\npublic:\n #define ll long long\n ll mod = 1e9 +7;\n int totalStrength(vector<int>& strength) {\n vector<int> str = strength;\n int n = str.size();\n vector<int> prev (n);\n vector<int> next (n);\n \n stack<int> s;\n for (int j = 0;...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
3
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& arr) {\n stack<pair<int,int>>s;\n long long int sum=0,n=arr.size();\n vector<int>psi(n), nsi(n);\n for(int i=0;i<n;i++){\n while(!s.empty() and s.top().first>=arr[i])s.pop();\n if(s.empty())psi[i]...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
3
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& arr) {\n stack<pair<int,int>>s;\n long long int sum=0,n=arr.size();\n vector<int>psi(n), nsi(n);\n for(int i=0;i<n;i++){\n while(!s.empty() and s.top().first>=arr[i])s.pop();\n if(s.empty())psi[i]...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
3
{ "code": "using LL = long long;\nLL M = 1e9+7;\nclass Solution \n{\npublic:\n int totalStrength(vector<int>& nums) \n {\n int n = nums.size();\n vector<LL> presum1(n+2, 0);\n vector<LL> presum2(n+2, 0);\n nums.insert(nums.begin(), 0);\n for(LL i=1;i<=n;i++)\n {\n ...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
3
{ "code": "using ll = long long;\nconst int MOD = 1e9 + 7;\n\nll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}\nll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}\nll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
3
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n int n = strength.size();\n long long ans = 0, mod = 1e9 + 7;\n vector<long long> prefix(n + 1, 0), iprefix(n + 1, 0), isuffix(n + 1, 0);\n for (int i = 1; i <= n; i++)\n {\n prefix[i] =...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
3
{ "code": "#define P ((int)1e9 + 7)\nclass Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n int n = strength.size();\n vector<long long> prefix_sum(n+1, 0);\n vector<long long> min_sum(n+1, 0);\n vector<long long> subarray_sum(n+1, 0);\n int m = INT_MAX;\n ...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
3
{ "code": "class Solution {\npublic:\n const int mod = 1e9 + 7;\n int totalStrength(vector<int>& strength) {\n int n = strength.size();\n auto right = nextSmallerRight(strength), left = nextSmallerLeft(strength);\n vector<long long> prefix(n + 1, 0), ps(n + 1, 0);\n for (int i = 0; i...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
3
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n int n=strength.size(), mod = 1e9 + 7, res = 0;\n vector<long long> Left2Right(n), right2Left(n), Prefix(n);\n \n for (int i=0, j=n-1, k=1; i<n; ++i, --j, ++k) {\n Prefix[i] = strength[i];\n ...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
3
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& str) {\n \n stack<int> s;\n int n=str.size();\n vector<int> l,r;\n int mod=pow(10,9)+7;\n \n for(int i=0;i<str.size();i++){\n while(!s.empty() && str[s.top()]>str[i]){\n ...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
3
{ "code": "// Whenever revising this question also see -> \"907. Sum of Subarray Minimums\"\n\n// https://www.youtube.com/watch?v=K31VME56L3o\n\nclass Solution {\n\t// TC : O(n)\n\t// SC : O(n)\n\tpublic:\n int totalStrength(vector<int> &strength) {\n\t\tlong long n = strength.size();\n\n\t\tlong long MOD = 1e...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
3
{ "code": "class Solution {\n\t// TC : O(n)\n\t// SC : O(n)\n\tpublic:\n int totalStrength(vector<int> &strength) {\n\t\tlong long n = strength.size();\n\n\t\tlong long MOD = 1e9 + 7;\n\n\t\tvector<long long>preSum(n + 1);\n\n\t\tvector<long long>prePrefix(n + 2);\n\n\t\tfor (long long i = 0; i < n; i++) {\n\t...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
3
{ "code": "// https://www.youtube.com/watch?v=K31VME56L3o\n\nclass Solution {\n\t// TC : O(n)\n\t// SC : O(n)\n\tpublic:\n int totalStrength(vector<int> &strength) {\n\t\tlong long n = strength.size();\n\n\t\tlong long MOD = 1e9 + 7;\n\n\t\tvector<long long>preSum(n + 1);\n\n\t\tvector<long long>prePrefix(n + ...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
3
{ "code": "class Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n const long long M = 1e9 + 7;\n int n = strength.size();\n \n vector<int> prevSmaller;\n vector<int> nextSmaller;\n \n vector<int> stk;\n for(int i = 0; i < n; ++i) {\n ...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
3
{ "code": "#define ll long long\nconst int mod = 1e9+7;\nclass Solution {\npublic:\n int totalStrength(vector<int>& s) {\n int n = s.size();\n vector<ll> pref(n, 0), prefPref(n, 0);\n pref[0] = s[0], prefPref[0] = pref[0];\n for(ll i=1; i<n; ++i)\n pref[i] = (pref[i-1] + s[i]...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
3
{ "code": "using ll = long long;\nclass Solution {\npublic:\n int n;\n int MOD = 1e9 + 7;\n vector<ll> prefix, prefix_sum;\n vector<ll> suffix, suffix_sum;\n\n ll get_total(int prev, int i, int next){\n int l = i - prev, r = next - i;\n // ll total = pow(prefix[n - 1], l * r);\n ll...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
3
{ "code": "class Solution {\npublic:\n // Time Complexity:- O(N)\n // Space Complexity:- O(N)\n \n #define MOD 1000000007\n \n int totalStrength(vector<int>& strength) {\n int n = strength.size();\n \n vector<long long> prefsum(n+2),suffsum(n+2);\n vector<long long> double_prefsum(n+2...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
3
{ "code": "class Solution {\npublic:\n #define MOD 1000000007\n \n int totalStrength(vector<int>& strength) {\n int n = strength.size();\n \n vector<long long> prefsum(n+2),suffsum(n+2);\n vector<long long> double_prefsum(n+2),double_suffsum(n+2);\n \n for(int i=1;i<=n;i++){\n ...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
3
{ "code": "using LL = long long;\nclass Solution {\n LL M = 1e9+7;\npublic:\n int totalStrength(vector<int>& strength) \n {\n int n = strength.size();\n vector<LL> presum(n+2, 0);\n vector<LL> presum2(n+2, 0);\n vector<int> arr = {0};\n for(auto x:strength)\n arr...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
3
{ "code": "\n\nclass Solution {\npublic:\n int n;\n vector<int> prevSmaller, nextSmaller;\n vector<long long> prefSum, prefSumSum, suffSum, suffSumSum;\n long long MOD = (long long)1e9 + 7LL;\n\n void monotonicStack(vector<int>& strength, vector<int>& prev, bool inclusive) {\n prev.resize(n);\n\...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
3
{ "code": "#define ll long long\nclass Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n const int MOD = 1e9 + 7;\n int N = strength.size();\n vector<ll> prefix = {0};\n vector<ll> accum = {0};\n \n vector<pair<int, int>> stack;\n ll res = 0;\n\n ...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
3
{ "code": "#define ll long long\nclass Solution {\npublic:\n int totalStrength(vector<int>& strength) {\n const int MOD = 1e9 + 7;\n int N = strength.size();\n vector<ll> prefix = {0};\n vector<ll> accum = {0};\n \n vector<pair<int, int>> stack;\n ll res = 0;\n\n ...
2,368
<p>As the ruler of a kingdom, you have an army of wizards at your command.</p> <p>You are given a <strong>0-indexed</strong> integer array <code>strength</code>, where <code>strength[i]</code> denotes the strength of the <code>i<sup>th</sup></code> wizard. For a <strong>contiguous</strong> group of wizards (i.e. the w...
3
{ "code": "/* An amazing problem invloving data structures, logic, previous\n implementation practice and dragon level mathematics and deduction */\nclass Solution {\nprivate:\n #define ll long long\n #define mod (ll)(pow(10, 9) + 7)\npublic:\n int totalStrength(vector<int>& strength) {\n ios::sync_wi...
2,379
<p>You are given an integer <code>n</code> denoting the number of cities in a country. The cities are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are also given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidi...
0
{ "code": "class Solution {\npublic:\n long long maximumImportance(int n, vector<vector<int>>& roads) {\n // vector<long long> graph(n);\n // for(auto& road:roads){\n // graph[road[0]]++;\n // graph[road[1]]++;\n // }\n // sort(graph.begin(),graph.end());\n ...
2,379
<p>You are given an integer <code>n</code> denoting the number of cities in a country. The cities are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are also given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidi...
0
{ "code": "int init = [] {\n ios_base::sync_with_stdio(false);\n\tcin.tie(nullptr);\n\tofstream out(\"user.out\");\n\tcout.rdbuf(out.rdbuf());\n string s;\n vector<long long> v;\n while(getline(cin,s)){\n stringstream ss(s);\n int n;\n if(s[0]!='['){\n ss>>n;\n v...
2,379
<p>You are given an integer <code>n</code> denoting the number of cities in a country. The cities are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are also given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidi...
0
{ "code": "class Solution {\npublic:\n long long maximumImportance(int n, vector<vector<int>>& roads) {\n int deg[n];\n memset(deg,0,sizeof(deg));\n for(int i=0; i<roads.size(); i++){\n deg[roads[i][0]]++;\n deg[roads[i][1]]++;\n }\n sort(deg,deg+n);\n ...
2,379
<p>You are given an integer <code>n</code> denoting the number of cities in a country. The cities are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are also given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidi...
0
{ "code": "// class Solution {\n// private:\n// void dfs(vector<vector<int>>& adj, vector<vector<int>>& vis, vector<int>& vis1, vector<int>& importance, long long& ans, int p, int c){\n// if(p!=c) ans+=importance[p]+importance[c];\n// vis1[p]=1; vis[p][c]=1; vis[c][p]=1;\n// p=c;\n// ...
2,379
<p>You are given an integer <code>n</code> denoting the number of cities in a country. The cities are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are also given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidi...
0
{ "code": "// class Solution {\n// private:\n// void dfs(vector<vector<int>>& adj, vector<vector<int>>& vis, vector<int>& vis1, vector<int>& importance, long long& ans, int p, int c){\n// if(p!=c) ans+=importance[p]+importance[c];\n// vis1[p]=1; vis[p][c]=1; vis[c][p]=1;\n// p=c;\n// ...
2,379
<p>You are given an integer <code>n</code> denoting the number of cities in a country. The cities are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are also given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidi...
0
{ "code": "class Solution {\npublic:\n long long maximumImportance(int n, vector<vector<int>>& roads) {\n int deg[n];\n memset(deg,0,sizeof(deg));\n for(int i=0; i<roads.size(); i++){\n deg[roads[i][0]]++;\n deg[roads[i][1]]++;\n }\n sort(deg,deg+n);\n ...
2,379
<p>You are given an integer <code>n</code> denoting the number of cities in a country. The cities are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are also given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidi...
0
{ "code": "std::vector<int> outgoing_edges(50001);\nstd::vector<int> counts(50001);\n\nclass Solution {\npublic:\n long long maximumImportance(int n, vector<vector<int>>& roads) {\n for (int i = 0; i < n + 1; i++) {\n outgoing_edges[i] = 0;\n counts[i] = 0;\n }\n for (int...
2,379
<p>You are given an integer <code>n</code> denoting the number of cities in a country. The cities are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are also given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidi...
0
{ "code": "class Solution {\npublic:\nlong long maximumImportance(int n, vector<vector<int>>& roads) {\n long long cnt[50001] = {}, res = 0;\n for (auto &r : roads) {\n ++cnt[r[0]];\n ++cnt[r[1]];\n }\n sort(begin(cnt), begin(cnt) + n);\n for (int i = 0; i < n; ++i)\n res += cnt[i]...
2,379
<p>You are given an integer <code>n</code> denoting the number of cities in a country. The cities are numbered from <code>0</code> to <code>n - 1</code>.</p> <p>You are also given a 2D integer array <code>roads</code> where <code>roads[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> denotes that there exists a <strong>bidi...
0
{ "code": "class Solution {\npublic:\nlong long maximumImportance(int n, vector<vector<int>>& roads) {\n long long cnt[50001] = {}, res = 0;\n for (auto &r : roads) {\n cnt[r[0]]++;\n cnt[r[1]]++;\n }\n sort(cnt, cnt + n);\n for (int i = 0; i < n; ++i)\n res += cnt[i] * (i + 1);\n ...