File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
solutions/2070. Most Beautiful Item for Each Query Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ public class Solution
2
+ {
3
+ public int [ ] MaximumBeauty ( int [ ] [ ] items , int [ ] queries )
4
+ {
5
+ int [ ] ans = new int [ queries . Length ] ;
6
+ int [ ] maxBeautySoFar = new int [ items . Length + 1 ] ;
7
+
8
+ Array . Sort ( items , ( a , b ) => a [ 0 ] . CompareTo ( b [ 0 ] ) ) ;
9
+
10
+ for ( int i = 0 ; i < items . Length ; ++ i )
11
+ maxBeautySoFar [ i + 1 ] = Math . Max ( maxBeautySoFar [ i ] , items [ i ] [ 1 ] ) ;
12
+
13
+ for ( int i = 0 ; i < queries . Length ; ++ i )
14
+ {
15
+ int index = FirstGreater ( items , queries [ i ] ) ;
16
+ ans [ i ] = maxBeautySoFar [ index ] ;
17
+ }
18
+
19
+ return ans ;
20
+ }
21
+
22
+ private int FirstGreater ( int [ ] [ ] items , int q )
23
+ {
24
+ int l = 0 ;
25
+ int r = items . Length ;
26
+ while ( l < r )
27
+ {
28
+ int m = ( l + r ) / 2 ;
29
+ if ( items [ m ] [ 0 ] > q )
30
+ r = m ;
31
+ else
32
+ l = m + 1 ;
33
+ }
34
+ return l ;
35
+ }
36
+ }
You can’t perform that action at this time.
0 commit comments