File tree 1 file changed +41
-16
lines changed
scripts/algorithms/M/Most Beautiful Item for Each Query
1 file changed +41
-16
lines changed Original file line number Diff line number Diff line change
1
+ // Runtime: 321 ms (Top 69.62%) | Memory: 89.00 MB (Top 83.54%)
2
+
1
3
class Solution {
2
4
public:
5
+
3
6
vector<int > maximumBeauty (vector<vector<int >>& items, vector<int >& queries) {
4
- vector<vector<int >> v;
5
- int n = queries.size ();
6
- for (int i = 0 ; i < n; i++){
7
- v.push_back ({queries[i],i});
8
- }
9
- sort (v.begin (),v.end ());
10
7
sort (items.begin (),items.end ());
11
- vector<int > ans (n);
12
- int j=0 ;
13
- n = items.size ();
14
- int mx = 0 ;
15
- for (auto &i: v){
16
- while (j<n && items[j][0 ]<=i[0 ]){
17
- mx = max (mx,items[j][1 ]);
18
- j++;
8
+ int maxi = items[0 ][1 ];
9
+ // for(auto xt : items)
10
+ // {
11
+ // cout<<xt[0]<<" "<<xt[1]<<endl;
12
+ // }
13
+ for (auto &xt : items)
14
+ {
15
+ maxi = max (maxi , xt[1 ]);
16
+ xt[1 ] = maxi;
17
+ }
18
+ // for(auto xt : items)
19
+ // {
20
+ // cout<<xt[0]<<" "<<xt[1]<<endl;
21
+ // }
22
+ vector<int >ans;
23
+ int n = items.size ();
24
+
25
+ for (int key : queries){
26
+ int left = 0 ;
27
+ int right = n - 1 ;
28
+
29
+ int count = 0 ;
30
+
31
+ while (left <= right) {
32
+ int mid = (right + left) / 2 ;
33
+ if (items[mid][0 ] <= key) {
34
+ count = mid + 1 ;
35
+ left = mid + 1 ;
36
+ }
37
+ else
38
+ right = mid - 1 ;
19
39
}
20
- ans[i[1 ]] = mx;
40
+
41
+ if (count==0 )
42
+ ans.push_back (0 );
43
+ else
44
+ ans.push_back (items[count-1 ][1 ]);
21
45
}
22
46
return ans;
23
47
}
24
- };
48
+ };
49
+
You can’t perform that action at this time.
0 commit comments