//java import java.util.*; import java.lang.Math; class Solution { public int[] leftmostBuildingQueries(int[] heights, int[][] queries) { int q=queries.length; int n=heights.length; int[] ans=new int[q]; Arrays.fill(ans,-1); List queryStore=new ArrayList(n); for(int i=0;ia.get(0)-b.get(0)); for(int i=0;i
#Python #TC=O(n+q+qlogq)=O(n+qlogq)=O(qlogq) #SC=O(n+q) import heapq class Solution: def leftmostBuildingQueries(self, heights: List[int], queries: List[List[int]]) -> List[int]: q=len(queries) n=len(heights) ans=[-1 for _ in range(q)] #O(q) queryStore=[[] for _ in range(n)] #O(n+q) minHeap=[] #O(q) for i in range(q): #O(q) a,b=min(queries[i]),max(queries[i]) if(a==b or heights[b]>heights[a]): ans[i]=b else: queryStore[b].append((max(heights[b],heights[a]),i)) print(ans) print(queryStore)
for i in range(n): #O(n) while(minHeap and minHeap[0][0]
//java
import java.util.*;
import java.lang.Math;
class Solution {
public int[] leftmostBuildingQueries(int[] heights, int[][] queries) {
int q=queries.length;
int n=heights.length;
int[] ans=new int[q];
Arrays.fill(ans,-1);
List queryStore=new ArrayList(n);
for(int i=0;ia.get(0)-b.get(0));
for(int i=0;i
Thanku for the explanation
Glad it helped!
Please comment here, if you need Hindi Explanation of This Solution
#Python
#TC=O(n+q+qlogq)=O(n+qlogq)=O(qlogq)
#SC=O(n+q)
import heapq
class Solution:
def leftmostBuildingQueries(self, heights: List[int], queries: List[List[int]]) -> List[int]:
q=len(queries)
n=len(heights)
ans=[-1 for _ in range(q)] #O(q)
queryStore=[[] for _ in range(n)] #O(n+q)
minHeap=[] #O(q)
for i in range(q): #O(q)
a,b=min(queries[i]),max(queries[i])
if(a==b or heights[b]>heights[a]):
ans[i]=b
else:
queryStore[b].append((max(heights[b],heights[a]),i))
print(ans)
print(queryStore)
for i in range(n): #O(n)
while(minHeap and minHeap[0][0]