Skip to content

Commit 0af2ba7

Browse files
committed
Dec22: vector, map manipulation
1 parent 1367779 commit 0af2ba7

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

daily/Dec12.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}

daily/Dec22.cc

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
};

0 commit comments

Comments
 (0)