문제 링크
요약
- Histogram 정렬
최종
결과
- LeetCode 3014 의 후속 문제인데, 3014에 중복문자 조건이 추가된 형태다.
- 이건 많이 등장하는 character 는 적게 push 하도록 만들면 된다.
- 각 character 에 대한 histogram 을 만든다.
- Histogram 을 내림차순으로 정렬한다.
- Histogram index
i에 대해,(i >> 3) + 1를histogram[i]곱해서 다 더한다.(i >> 3) + 1는 8개씩 끊어서 push 수를 1, 2, 3, … 씩 할당해주는 것과 동일하다.
- 그래서 코드는 아래와 같다.
- 한가지 트릭은 마지막 loop 에서
histogram[i] = 0로 초기화를 해주면histogram을 재사용할 수 있다 (LeetCode 3517 과 동일).
- 한가지 트릭은 마지막 loop 에서
class Solution {
array<int, 26> histogram{};
public:
int minimumPushes(string word) {
int acc = 0;
for (char c : word) {
histogram[c - 'a']++;
}
sort(histogram.begin(), histogram.end(), [](int a, int b) {
return a > b;
});
for (int i = 0; i < 26; i++) {
acc += histogram[i] * ((i >> 3) + 1);
histogram[i] = 0;
}
return acc;
}
};