File tree 2 files changed +55
-0
lines changed
2 files changed +55
-0
lines changed Original file line number Diff line number Diff line change
1
+ func maxSpending (values [][]int ) (ans int64 ) {
2
+ m , n := len (values ), len (values [0 ])
3
+ a := make ([]int , 0 , m * n ) // reserve space
4
+ for _ , row := range values {
5
+ a = append (a , row ... )
6
+ }
7
+ slices .Sort (a )
8
+
9
+ for i , x := range a {
10
+ ans += int64 (x ) * int64 (i + 1 )
11
+ }
12
+ return
13
+ }
Original file line number Diff line number Diff line change
1
+ #include < unordered_map>
2
+ #include < vector>
3
+ using namespace std ;
4
+
5
+ class Solution {
6
+ public:
7
+ int stepToOne (int n) {
8
+ int res = 0 ;
9
+ if (n == 1 ) return 0 ;
10
+ while (n != 1 ) {
11
+ if (n % 2 == 0 ) {
12
+ n /= 2 ;
13
+ } else {
14
+ n = n * 3 + 1 ;
15
+ }
16
+ res++;
17
+ }
18
+ return res;
19
+ }
20
+
21
+ /* *
22
+ * LC1387: Sort Integers by the power value [E]
23
+ * Basic pair, map
24
+ */
25
+ int getKth (int lo, int hi, int k) {
26
+ unordered_map<int , int > nmap;
27
+ for (int i = lo; i <= hi; i++) {
28
+ nmap[i] = stepToOne (i);
29
+ }
30
+ vector<pair<int , int >> sortedVector (nmap.begin (), nmap.end ());
31
+
32
+ sort (sortedVector.begin (), sortedVector.end (),
33
+ [](const pair<int , int >& le, const pair<int , int >& ri) {
34
+ if (le.second == ri.second ) {
35
+ return le.first < ri.first ;
36
+ }
37
+ return le.second < ri.second ;
38
+ });
39
+
40
+ return sortedVector[k - 1 ].first ;
41
+ }
42
+ };
You can’t perform that action at this time.
0 commit comments