id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
3,195
<p>There are <code>n</code> balls on a table, each ball has a color black or white.</p> <p>You are given a <strong>0-indexed</strong> binary string <code>s</code> of length <code>n</code>, where <code>1</code> and <code>0</code> represent black and white balls, respectively.</p> <p>In each step, you can choose two ad...
3
{ "code": "class Solution {\npublic:\n long merge(vector<char> &arr,vector<char> &temp,long start,long mid,long end){\n \n long i= start,j=mid+1,k = start;\n long c = 0;\n while(i<=mid && j<=end ){\n if(arr[i]<=arr[j]){\n temp[k]= arr[i];\n i++;\n k++; \n ...
3,195
<p>There are <code>n</code> balls on a table, each ball has a color black or white.</p> <p>You are given a <strong>0-indexed</strong> binary string <code>s</code> of length <code>n</code>, where <code>1</code> and <code>0</code> represent black and white balls, respectively.</p> <p>In each step, you can choose two ad...
3
{ "code": "class Solution {\npublic:\n long long minimumSteps(string s) {\n vector<int>prefsum(s.size()+1,0);\n prefsum[0]=s[0]-'0';\n for(int i=1;i<s.size();i++)\n {\n prefsum[i]=prefsum[i-1] +(s[i]-'0');\n }\n long ans=0;\n for(int i=0;i<s.size();i++)\n...
3,195
<p>There are <code>n</code> balls on a table, each ball has a color black or white.</p> <p>You are given a <strong>0-indexed</strong> binary string <code>s</code> of length <code>n</code>, where <code>1</code> and <code>0</code> represent black and white balls, respectively.</p> <p>In each step, you can choose two ad...
3
{ "code": "class Solution {\npublic:\n long long minimumSteps(string s) {\n int n = s.size();\n vector<int> v(n, 0);\n int count = 0;\n long long p = 0;\n for (int i = 0; i < n; i++){\n if (s[i] == '0') {\n count++;\n p += i;\n ...
3,195
<p>There are <code>n</code> balls on a table, each ball has a color black or white.</p> <p>You are given a <strong>0-indexed</strong> binary string <code>s</code> of length <code>n</code>, where <code>1</code> and <code>0</code> represent black and white balls, respectively.</p> <p>In each step, you can choose two ad...
3
{ "code": "class Solution {\npublic:\n long long minimumSteps(string s) {\n vector<int> v;\n for(int i=0;i<s.size();i++){\n if(s[i]=='1') v.push_back(i);\n }\n if(v.size()==0) return 0;\n if(v.size()==1 && v[0]!=s.size()-1) return s.size()-1-v[0];\n int a=v[v.si...
3,192
<p>Given three integers <code>a</code>, <code>b</code>, and <code>n</code>, return <em>the <strong>maximum value</strong> of</em> <code>(a XOR x) * (b XOR x)</code> <em>where</em> <code>0 &lt;= x &lt; 2<sup>n</sup></code>.</p> <p>Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9 </sup...
0
{ "code": "class Solution {\npublic:\n const int mod = 1e9 + 7;\n int maximumXorProduct(long long a, long long b, int n) {\n long long p = (a >> n) << n, q = (b >> n) << n;\n for (int i = n - 1; i >= 0; i--) {\n long long abit = (a >> i & 1), bbit = (b >> i & 1);\n if (abit =...
3,192
<p>Given three integers <code>a</code>, <code>b</code>, and <code>n</code>, return <em>the <strong>maximum value</strong> of</em> <code>(a XOR x) * (b XOR x)</code> <em>where</em> <code>0 &lt;= x &lt; 2<sup>n</sup></code>.</p> <p>Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9 </sup...
0
{ "code": "class Solution {\npublic:\n int mod=1e9+7;\n typedef long long ll;\n int maximumXorProduct(long long a, long long b, int n) {\n ll aXORx=0;\n ll bXORx=0;\n //49 to n bit we are setting it same as a and b\n for(ll i=49;i>=n;i--){\n bool aset = ((a >> i) & 1) >...
3,192
<p>Given three integers <code>a</code>, <code>b</code>, and <code>n</code>, return <em>the <strong>maximum value</strong> of</em> <code>(a XOR x) * (b XOR x)</code> <em>where</em> <code>0 &lt;= x &lt; 2<sup>n</sup></code>.</p> <p>Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9 </sup...
0
{ "code": "class Solution {\npublic:\n int M = 1e9 + 7;\n typedef long long ll;\n int maximumXorProduct(long long a, long long b, int n) {\n ll xora = 0;\n ll xorb = 0;\n for (int i = 49; i >= n; i--) {\n bool ithbitofA = ((a >> i) & 1) > 0;\n bool ithbitofB = ((b >...
3,192
<p>Given three integers <code>a</code>, <code>b</code>, and <code>n</code>, return <em>the <strong>maximum value</strong> of</em> <code>(a XOR x) * (b XOR x)</code> <em>where</em> <code>0 &lt;= x &lt; 2<sup>n</sup></code>.</p> <p>Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9 </sup...
0
{ "code": "class Solution {\npublic:\n int mod=1e9+7;\n typedef long long ll;\n int maximumXorProduct(long long a, long long b, int n) {\n \n ll xXora=0;\n ll xXorb=0;\n\n // 49th bit to nth bit\n for(ll i=49;i>=n;i--){\n\n bool a_ith_bit=((a>>i) & 1) > 0; // Fin...
3,192
<p>Given three integers <code>a</code>, <code>b</code>, and <code>n</code>, return <em>the <strong>maximum value</strong> of</em> <code>(a XOR x) * (b XOR x)</code> <em>where</em> <code>0 &lt;= x &lt; 2<sup>n</sup></code>.</p> <p>Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9 </sup...
0
{ "code": "class Solution {\npublic:\n int maximumXorProduct(long long a, long long b, int n) {\n\n bitset<50>p(a), q(b);\n long long ax=0, bx=0;\n int mod=1e9+7;\n\n for(long long i=49;i>=n;i--)\n {\n if((a>>i)&1) ax^=((long long)1<<i);\n if((b>>i)&1) bx^=(...
3,192
<p>Given three integers <code>a</code>, <code>b</code>, and <code>n</code>, return <em>the <strong>maximum value</strong> of</em> <code>(a XOR x) * (b XOR x)</code> <em>where</em> <code>0 &lt;= x &lt; 2<sup>n</sup></code>.</p> <p>Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9 </sup...
2
{ "code": "class Solution {\npublic:\n int maximumXorProduct(long long a, long long b, int n) {\n\n bitset<50>p(a), q(b);\n long long ax=0, bx=0;\n int mod=1e9+7;\n\n for(long long i=49;i>=n;i--)\n {\n if((a>>i)&1) ax^=((long long)1<<i);\n if((b>>i)&1) bx^=(...
3,192
<p>Given three integers <code>a</code>, <code>b</code>, and <code>n</code>, return <em>the <strong>maximum value</strong> of</em> <code>(a XOR x) * (b XOR x)</code> <em>where</em> <code>0 &lt;= x &lt; 2<sup>n</sup></code>.</p> <p>Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9 </sup...
3
{ "code": "class Solution {\npublic:\n#define mod 1000000007\n int maximumXorProduct(long long a, long long b, int n) {\n long long x=0;\n for(int i=n-1;i>=0;i--){\n long long x1 = ((a>>i)&1);\n long long x2 = ((b>>i)&1);\n if(x1==x2){\n if(x1==0){\n ...
3,192
<p>Given three integers <code>a</code>, <code>b</code>, and <code>n</code>, return <em>the <strong>maximum value</strong> of</em> <code>(a XOR x) * (b XOR x)</code> <em>where</em> <code>0 &lt;= x &lt; 2<sup>n</sup></code>.</p> <p>Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9 </sup...
3
{ "code": "class Solution {\npublic:\n using ll = long long;\n const ll MOD = 1e9 + 7;\n int maximumXorProduct(long long a, long long b, int n) {\n bitset<50> tempA(a);\n bitset<50> tempB(b);\n bitset<50> x;\n \n for(int i=n-1; i >= 0; i--){\n if(tempA.to_ullong(...
3,192
<p>Given three integers <code>a</code>, <code>b</code>, and <code>n</code>, return <em>the <strong>maximum value</strong> of</em> <code>(a XOR x) * (b XOR x)</code> <em>where</em> <code>0 &lt;= x &lt; 2<sup>n</sup></code>.</p> <p>Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9 </sup...
3
{ "code": "class Solution {\npublic:\n using ll = long long;\n const ll MOD = 1e9 + 7;\n int maximumXorProduct(long long a, long long b, int n) {\n bitset<50> tempA(a);\n bitset<50> tempB(b);\n bitset<50> x;\n \n for(int i=n-1; i >= 0; i--){\n if(tempA.to_ullong(...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
0
{ "code": "class Solution {\npublic:\n #define MAXN 50000\n int N, H[MAXN + 1], T[4 * MAXN + 1];\n\n void Build(int lo, int hi, int v) {\n if (lo == hi) {\n T[v] = H[lo];\n return;\n }\n int mid = (lo + hi) / 2;\n Build(lo, mid, 2 * v);\n Build(mid + 1...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
0
{ "code": "class Solution {\npublic:\n #define MAXN 50000\n vector<int> H;\n int N, T[4 * MAXN];\n\n void Build(int lo, int hi, int v) {\n if (lo == hi) {\n T[v] = H[lo];\n return;\n }\n int mid = (lo + hi) / 2;\n Build(lo, mid, 2 * v + 1);\n Build(...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
0
{ "code": "struct SparseTable {\n\tint lg[50005], st[16][50005];\n\tvoid init(const vector<int>& a, int n) {\n\t\tfor(int i = 1; i <= n; ++i) st[0][i] = a[i - 1];\n\t\tfor(int j = 1; j <= 15; ++j) for(int i = 1; i <= n - (1 << j) + 1; ++i) st[j][i] = max(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);\n\t}\n\tint query...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
0
{ "code": "\nclass Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& h, vector<vector<int>>& q) {\n int n = h.size();\n vector<int> next(n,-1);\n stack<int> sk;\n for(int i=0; i<n; i++){\n while(!sk.empty() && h[sk.top()] < h[i]){\n next[sk...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
0
{ "code": "class Solution\n{\n using ll = long long;\n ll tree[20 * 10000 + 14];\n\n void build(int si, int ss, int se, vector<int> &arr)\n {\n if (ss == se)\n tree[si] = arr[ss - 1];\n else\n {\n int sm = (ss + se) >> 1;\n build(2 * si, ss, sm, arr);\...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
0
{ "code": "class Solution\n{\n using ll = long long;\n ll tree[20 * 10000 + 14];\n //building the range maximum segment tree\n void build(int si, int ss, int se, vector<int> &arr){\n if (ss == se)\n tree[si] = arr[ss - 1];\n else{\n int sm = (ss + se) >> 1;\n ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
0
{ "code": " class SegmentTree {\n vector<int> tree;\n\npublic:\n SegmentTree(int n) { tree.resize(4 * n + 1); }\n\n void buildTree(int idx, int l, int r, vector<int>& nums) {\n if (l == r) {\n tree[idx] = nums[l];\n return;\n }\n int mid = l + (r - l) / 2;\n ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
0
{ "code": "class SegmentTree {\npublic:\n SegmentTree(const vector<int>& data) {\n n = data.size();\n tree.resize(2 * n);\n build(data);\n }\n\n void update(int pos, int value) {\n pos += n; // Shift index to leaf\n tree[pos] = value;\n \n // Update parents\n...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
0
{ "code": "#define sz(x) ((int)(x).size())\n#define all(x) (x).begin(),(x).end()\n#define pb(a) push_back(a)\n#define mp(a,b) make_pair(a,b)\n#define put(a) cout<<a<<'\\n'\n#define ff first\n#define ss second\ntypedef long lo...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
0
{ "code": "class SegmentTree{\n int n;\n vector<int> arr;\n vector<int> tree;\npublic:\n SegmentTree(vector<int> arr){\n this->arr = arr;\n tree = vector<int>(arr.size()*4, -1);\n constructTree(0, 0, arr.size() - 1);\n }\n int constructTree(int node, int start, int end){\n ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
0
{ "code": "using pii = pair<int, int>;\nusing pip = pair<int, pii>;\n\nclass Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n // //// solution 1: use a priority queue and answer queries in non-standard order\n\n // const int qn = querie...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
0
{ "code": "class Solution {\npublic:\n\n struct A{\n int p;\n int h;\n int idx;\n A(int _p, int _h, int _idx) : p(_p), h(_h), idx(_idx) {}\n bool operator <(const A& that) const {\n return p > that.p;\n }\n };\n\n vector<int> leftmostBuildingQueries(vector...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
0
{ "code": "class SegTree{\npublic:\n vector<int>seg;\n vector<int>seg_min;\n SegTree(int n){\n seg.resize(4*n +1);\n seg_min.resize(4*n +1);\n }\n\n void build(vector<int>&arr,int low, int high, int ind){\n if(low==high){\n seg[ind]=arr[low];\n seg_min[ind]=ar...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
0
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& h, vector<vector<int>>& q) {\n \n \n int n = q.size();\n vector<int> ans(n,-1);\n \n vector<pair<int,int>> v;\n \n for(int i=0;i<n;i++){\n v.push_back({max(q[i]...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
0
{ "code": "class Solution {\npublic:\n int binary_search(vector<int>& heights, vector<int> &arr, int tar) {\n int ans = -1;\n int i = 0;\n int j = arr.size()-1;\n while(i <= j) {\n int m = (i+j)/2;\n int ht = heights[arr[m]];\n if(ht > tar) {\n ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
0
{ "code": "class Solution {\npublic:\n int binary_search(vector<int>& heights, vector<int> &arr, int tar) {\n int ans = -1;\n int i = 0;\n int j = arr.size()-1;\n while(i <= j) {\n int m = (i+j)/2;\n int ht = heights[arr[m]];\n if(ht > tar) {\n ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
0
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n int qsz = queries.size();\n for(int i=0;i<queries.size();i++){\n queries[i].push_back(i);\n if(queries[i][0]>queries[i][1]) swap(queries[i][0],queri...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
0
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n vector<int> ans(queries.size());\n vector<vector<pair<int, int>>> qs(heights.size());\n for (int i = 0; i < queries.size(); i++) {\n int a = queries[i][...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
0
{ "code": "class Solution {\npublic:\n int find(int a,int b,vector<int>& heights){\n if(heights[a]<heights[b] || a==b){\n return b;\n }\n for(int i = b+1;i<heights.size();i++){\n if(heights[i]>heights[a] && heights[i]>heights[b]){\n return i;\n }...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
0
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n // 尝试单调栈\n const int n = heights.size();\n const int m = queries.size();\n vector<int> ans(m);\n vector<vector<pair<int, int>>> qs(n);\n for (...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
1
{ "code": "class Solution \n{\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& h, vector<vector<int>>& q) \n {\n int n = h.size(), m = q.size(), r = n - 1;\n vector<int> ans(m);\n vector<pair<int, int>> st;\n\n auto insert = [&](int a) {\n while(st.size() && st....
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
1
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n int n = heights.size();\n int len = ceil(sqrt(n)); // size of the block and the number of blocks\n vector<int> sq (len,INT_MIN), ans;\n\n for (int i=0; i<n;...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
1
{ "code": "class Solution\n{\n using ll = long long;\n ll tree[20 * 10000 + 14];\n\n void build(int si, int ss, int se, vector<int> &arr)\n {\n if (ss == se)\n tree[si] = arr[ss - 1];\n else\n {\n int sm = (ss + se) >> 1;\n build(2 * si, ss, sm, arr);\...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
1
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n int n = heights.size();\n\n vector<array<int, 3>> vec;\n int q = queries.size();\n vector<int> ans(q, -2);\n for (int i = 0; i < q; i++) {\n ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
1
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n int n = queries.size(), m = heights.size();\n vector<int> res(n, -1);\n vector<vector<pair<int, int>>> qs(m);\n for (int i = 0; i < n; ++i) {\n i...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
1
{ "code": "class Solution {\npublic:\n struct node{\n int val;\n node(){\n val=0;\n }\n };\n node seg[800080];\n node merge(node a,node b){\n node c;\n c.val=max(a.val,b.val);\n return c;\n }\n void build(int v,int tl,int tr,vector<int> &h){\n ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
1
{ "code": "struct SegmentTree {\n vector<int> v;\n int n;\n \n SegmentTree(int _n, const vector<int>& arr) : n(_n) {\n v.resize(4*n, 0);\n Initialize(arr);\n } \n \n void Initialize (const vector<int>& arr, int l = 0, int r = -1, int i = 0) {\n if (r == -1) r += n;\n \...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
1
{ "code": "struct SegmentTree {\n vector<int> v;\n int n;\n \n SegmentTree(int _n, const vector<int>& arr) : n(_n) {\n v.resize(4*n, 0);\n Initialize(arr);\n } \n \n void Initialize (const vector<int>& arr, int l = 0, int r = -1, int i = 0) {\n if (r == -1) r += n;\n \...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
1
{ "code": "class Solution {\n int binarySearch(int val, vector<int> &arr, vector<int> &heights){\n int l=0;\n int r=arr.size()-1;\n while(l<r){\n int mid=(l+r)/2;\n if(heights[arr[mid]]<val){\n l=mid+1;\n }\n else{\n r=m...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "struct RMQ\n{\n int n;\n vector<int> log2;\n vector<vector<int>> rmq;\n void Build(vector<int> &vi)\n {\n n=vi.size();\n log2.assign(n+1,0);\n for(int i=2;i<=n;++i)\n {\n log2[i]=log2[i/2]+1;\n }\n rmq.assign(log2[n]+1,vector<int>(n));\n ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "class SegmentTree {\nprivate:\n std::vector<int> st, A;\n int n;\n int left(int p) { return p << 1; }\n int right(int p) { return (p << 1) + 1; }\n void build(int p, int L, int R) {\n if (L == R)\n st[p] = L;\n else {\n build(left(p), L, (L + R) / 2);\n ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "template<typename T, typename D>\nclass sparse_table {\nprivate:\n size_t n;\n size_t logn;\n vector<vector<D>> table;\n vector<size_t> lg;\n\n D (*convert)(const T &);\n\n T (*function)(const T &, const T &);\n\npublic:\n sparse_table(const vector<T> &arr, D (*conv)(const T &), T (*fu...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "class Solution {\n vector<pair<int, pair<int, int>>> vpp;\n multimap<int, int> m;\n vector<int> results;\n\npublic:\n Solution() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n }\n\n const vector<int>&\n leftmostBuildingQueries(const vect...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\n \n// using namespace std;\n// using namespace __gnu_pbds;\n\n// template<typename T>\n// using OST = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;\n\nconstexpr int INF = 0x3...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "#include <bits/stdc++.h>\n#include <ext/pb_ds/assoc_container.hpp>\n#include <ext/pb_ds/tree_policy.hpp>\n \n// using namespace std;\n// using namespace __gnu_pbds;\n\n// template<typename T>\n// using OST = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;\n\nconstexpr int INF = 0x3...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& h, vector<vector<int>>& q) {\n \n \n int n = q.size();\n vector<int> ans(n,-1);\n \n vector<pair<int,int>> v;\n \n for(int i=0;i<n;i++){\n v.push_back({max(q[i]...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "class Solution {\n\nprivate:\n struct Query\n {\n int x;\n int y;\n int index;\n\n bool operator< (const Query& other)\n {\n return y > other.y;\n }\n\n };\n\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<i...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "class Solution {\n vector<pair<int, pair<int, int>>> vpp;\n multimap<int, int> m;\n vector<int> results;\n\npublic:\n Solution() {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n }\n\n const vector<int>&\n leftmostBuildingQueries(const vect...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n int n = queries.size();\n vector<pair<int,pair<int,int>>>vec;\n for(int i = 0 ; i<n ; i++){\n if(queries[i][0] > queries[i][1]){\n swap(q...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights,\n vector<vector<int>>& queries) {\n int n = heights.size();\n vector<int> right(n);\n stack<int> st;\n st.push(n);\n for (int i = n - 1; i >= 0;...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n int n = queries.size();\n vector<int> ans(n , -1);\n\n for(auto &p : queries){\n if(p[0] < p[1]){\n swap(p[0] , p[1]);\n }\n ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "class Solution {\npublic:\n int queriesN, heightsN;\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n queriesN = queries.size();\n heightsN = heights.size();\n for (auto q: queries) {\n cout << q[0] << \", \" << q[1] << endl;...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "class Solution {\npublic:\n\n int t[200100];\n\n void build(int ind,int l,int r,vector<int>& heights){\n if(l==r){\n t[ind]=heights[l];\n return ;\n }\n int mid=(l+r)/2;\n build(2*ind,l,mid,heights);\n build(2*ind+1,mid+1,r,heights);\n t...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n vector<tuple<int,int,int>> sorted_queries;\n for (int i = 0; i < queries.size(); i++) {\n sorted_queries.push_back(make_tuple(max(queries[i][0],queries[i][1]),...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n int sz = queries.size();\n vector<int> ans(sz, -1);\n for(int i = 0; i < sz; i++) {\n \n int mv = max(heights[queries[i][0]], heights[queries...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "class Solution {\npublic:\n vector<pair<int, int>> st;\n int search(int x) {\n int l = 0;\n int r = st.size() - 1;\n int ans = -1;\n while (l <= r) {\n int m = (l + r) / 2;\n if (st[m].first > x) {\n ans = max(ans, m);\n ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "class Solution {\npublic:\n vector<pair<int, int>> st;\n int search(int x) {\n int l = 0;\n int r = st.size() - 1;\n int ans = -1;\n while (l <= r) {\n int m = (l + r) / 2;\n if (st[m].first > x) {\n ans = max(ans, m);\n ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n vector<pair<int, pair<int, int>>> q;\n vector<int> ans(queries.size(), -1);\n \n for (int i = 0; i < queries.size(); i++) {\n q.push_back({max(qu...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n int q = queries.size(), n = heights.size(), i, j, idx = 0;\n \n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;\n \n ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "class Solution {\n\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n int n = queries.size();\n for(int i=0; i<n; i++){\n sort(queries[i].begin(), queries[i].end());\n queries[i].push_back(i);\n }\n sor...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "class Solution {\n\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n int n = queries.size();\n for(int i=0; i<n; i++){\n sort(queries[i].begin(), queries[i].end());\n queries[i].push_back(i);\n }\n sor...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "class Solution {\n // Segment Tree - max range\n vector<int> st;\n void build_st(vector<int> &heights, int l, int r, int i) {\n if (l == r) {\n st[i] = heights[l];\n return;\n }\n int mid = (l+r) / 2;\n build_st(heights, l, mid, i*2 + 1);\n ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n int m = queries.size();\n int n = heights.size();\n vector<int> ans(m, -1);\n vector<vector<pair<int, int>>> qu(n);\n for (int i = 0; i < queries...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "class Solution {\npublic:\n\n#define pb push_back\n#define all(x) x.begin(), x.end()\n#define rall(x) x.rbegin(), x.rend()\n#define forn(i, x, n) for (int i = x; i < n; i++)\n#define vi vector<int>\n#define vpp vector<pair<int,int>>\n#define vs vector<string>\n int n ; \n vector<int>seg ; \n void bui...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "class Solution {\n int n;\n vector<int> seg;\n void build(int node,int st,int en,vector<int>& h){\n if(st==en){\n seg[node]=h[st];\n return;\n }\n int mid=(st+en)/2;\n build(2*node,st,mid,h);\n build(2*node+1,mid+1,en,h);\n seg[node]=...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
2
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;\n vector<int>ans(queries.size(),-1);\n unordered_map<int,vector<pair<int,int>>>mp;\...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n int n=heights.size(),m=queries.size();\n vector<int>ans(m,-1);\n unordered_map<int,vector<pair<int,int>>>mp;\n for(int id=0;id<m;id++)\n {\n ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n vector<int> qans(queries.size());\n map<int,vector<pair<int,int>>> mp;\n priority_queue<pair<int,int>,vector<pair<int,int>> ,greater<pair<int,int>>> pq;\n\n ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "typedef pair<int,int> pii;\n\nclass Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& height, vector<vector<int>>& queries) {\n \n priority_queue<pii, vector<pii>, greater<pii>>pq;\n\n map<int, vector<pair<int,int>>>mp;\n\n vector<int>res(queries.size(),-...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n int n = queries.size();\n vector<int>ans(n, -1);\n\n map<int, vector<pair<int, int>>>mp;\n\n for(int i=0;i<n;i++){\n int ind1 = queries[i][0];\n ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class segment{\npublic:\n vector<long long> seg;\n int n;\n segment(vector<int>v){\n n=v.size();\n seg.resize(4*n);\n build(0,0,n-1,v);\n }\n void build(int index,int low,int high,vector<int>&v){\n if(low==high){\n seg[index]=v[low];\n return...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class Solution {\npublic:\n int n;\n void update(vector<int>& tree, int node, int st, int end, int idx, int val) {\n if (st == end) {\n tree[node] = min(tree[node], val);\n return;\n }\n int mid = (st + end) / 2;\n if (idx <= mid) {\n updat...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class Solution {\npublic:\n int t[201000];\n int n;\n void build(int i,int l,int r){\n if(l==r){\n t[i]=n;\n return;\n }\n int mid=(l+r)>>1;\n int x=2*i;\n build(x,l,mid);\n build(x+1,mid+1,r);\n t[i]=min(t[x],t[x+1]);\n }\n...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class Solution {\npublic:\n int func(int l, int r, map<pair<int, int>, int>& dp, vector<int>& nge, vector<int>& h, int& n){\n if(r >= n) return -1;\n\n if(dp.find({l, r}) != dp.end()) return dp[{l, r}];\n if(r == l) return r;\n if((r > l) && (h[r] > h[l])) return dp[{l, r}] =...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n int n = heights.size();\n for (int i = 0; i < queries.size(); i++) {\n std::sort(queries[i].begin(), queries[i].end());\n queries[i].push_back(i);\n...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n int n = heights.size();\n for (int i = 0; i < queries.size(); i++) {\n std::sort(queries[i].begin(), queries[i].end());\n queries[i].push_back(i);\n...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n int m = heights.size(), n = queries.size();\n for (int i = 0; i < n; ++i) {\n queries[i].push_back(i);\n }\n // 按照query的右侧端点排序\n sort(quer...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class BIT {\n int n;\n vector<int> bit;\npublic:\n BIT(int n): n(n + 1), bit(vector<int>(n + 1, 1e5)) {}\n void update(int k, int val) {\n for (k++; k < n; k += k & -k) bit[k] = min(bit[k], val);\n return;\n }\n int query(int k, int val=1e5) {\n for ( ; k > 0; k -= k ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class BIT {\n int n;\n vector<int> bit;\npublic:\n BIT(int n): n(n + 1), bit(vector<int>(n + 1, 1e5)) {}\n void update(int k, int val) {\n for (k++; k < n; k += k & -k) bit[k] = min(bit[k], val);\n return;\n }\n int query(int k, int val=1e5) {\n for ( ; k > 0; k -= k ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "bool myfunc(const vector<int> & a, const vector<int> & b){\n if(a[1] == b[1]) return a[0] < b[0];\n return a[1] < b[1];\n}\nvoid printArr(const vector<int> & a){\n for(int i = 0 ; i < a.size() ; i ++) cout<<a[i]<<\" \";\n cout<<endl;\n}\nvoid printArr(const vector<vector<int>> & a){\n for(in...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "// Hay\nbool myfunc(const vector<int> & a, const vector<int> & b){\n if(a[1] == b[1]) return a[0] < b[0];\n return a[1] < b[1];\n}\nvoid printArr(const vector<int> & a){\n for(int i = 0 ; i < a.size() ; i ++) cout<<a[i]<<\" \";\n cout<<endl;\n}\nvoid printArr(const vector<vector<int>> & a){\n ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class Solution {\npublic:\nint go(vector<int>&st,vector<int>&v,int lim){\n int n=st.size(),l=0,r=n-1,ans=-1;\n\n while(l<=r){\n int mid=(l+r)/2;\n if(v[st[mid]]>lim)ans=st[mid],l=mid+1;\n else r=mid-1;\n }\n return ans;\n}\n vector<int> leftmostBuildingQueries(vector<int...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n int n=heights.size();\n unordered_map<int,vector<pair<int,int>>>mp;\n vector<int>ans(queries.size(),-1);\n for(int k=0;k<queries.size();k++){\n i...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class Solution {\npublic:\nstruct Node{\n int ans;\n pair<int, int> range;\n int index;\n Node* left;\n Node* right;\n};\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n for (int i = 0; i < queries.size(); i++) {\n if (queries...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "/* Approach : Priority Queue and hashing\n1. First we can answer below basic queries for (i, j):\nif i < j and heights[i] < heights[j], then Alice and Bob meet at j.\nif i > j and heights[i] > heights[j], then Alice and Bob meet at i.\nif i == j, then Alice and Bob already meet at i.\n\n2. For the remainin...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n int n = heights.size();\n int totalQueries = queries.size();\n\n // to store queries ans\n vector<int> ans(totalQueries, -1);\n\n // Max query index ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n map< int , vector <pair <int,int> > > m;\n int n=heights.size(),n1=queries.size();\n vector <int > ans(n1,-1);\n for (int i=0;i<queries.size();i++){\n ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n int n = heights.size(), m = queries.size();\n vector<int> ans(m);\n \n vector<int> nge(n);\n stack<int> stk;\n for(int i = n-1; i>=0; i--){\n ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& h, vector<vector<int>>& q) {\n int N=q.size();\n vector<vector<int>>v(N);\n for(int i=0;i<N;i++){\n int j=min(q[i][0],q[i][1]);\n v[i]={h[j],i};\n }\n priority_queue<vect...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n vector<pair<vector<int>,int>> q;\n int n = queries.size();\n for(int i = 0; i<queries.size(); i++){\n if(queries[i][0] > queries[i][1])\n sw...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& queries) {\n vector<pair<vector<int>,int>> q;\n int n = queries.size();\n for(int i = 0; i<queries.size(); i++){\n if(queries[i][0] > queries[i][1])\n sw...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& h, vector<vector<int>>& queries) {\n int n = h.size(), qn = queries.size();\n vector<vector<vector<int>>> q(n);\n priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> hp;\n vector<i...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "struct Entry{\n int val, idx;\n bool operator<(const auto& other) const {\n return val >= other.val;\n }\n};\n\nstruct QE{\n vector<int> qp;\n int idx;\n};\n\nclass Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& hs, vector<vector<int>>& qs) {\n int n ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& heights, vector<vector<int>>& q) {\n stack<int> st;\n int n = heights.size();\n vector<int> next(n,-1);\n for(int i = n-1;i>=0;i--){\n while(!st.empty() && heights[st.top()]<=heights[i]){\...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class Solution {\npublic:\n class node {\n public:\n int s, e;\n node* l;\n node* r;\n int maxi;\n\n node(int s, int e, node* l, node* r, int maxi) {\n this->s = s;\n this->e = e;\n this->l = l;\n this->r = r;\n ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "template <typename T> class min_segtree {\n const int inf = 0x3f3f3f3f;\n vector<T> st;\n int n;\n#define lc (rt << 1)\n#define rc (rt << 1 | 1)\n void push_up(int rt) {\n st[rt] = min(st[lc], st[rc]);\n }\n void build(int rt, int l, int r, vector<T>& a) {\n if(l == r) {\n ...
3,181
<p>You are given a <strong>0-indexed</strong> array <code>heights</code> of positive integers, where <code>heights[i]</code> represents the height of the <code>i<sup>th</sup></code> building.</p> <p>If a person is in building <code>i</code>, they can move to any other building <code>j</code> if and only if <code>i &lt...
3
{ "code": "class Solution {\npublic:\n vector<int> leftmostBuildingQueries(vector<int>& A, vector<vector<int>>& queries) {\n int n = A.size(), qn = queries.size();\n vector<vector<vector<int>>> que(n);\n priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> h;\n vec...