Skip to content

Commit 1039fcd

Browse files
authored
Update minimum-number-of-pushes-to-type-word-i.cpp
1 parent cecd0a4 commit 1039fcd

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

C++/minimum-number-of-pushes-to-type-word-i.cpp

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
1+
// Time: O(4)
2+
// Space: O(1)
3+
4+
// greedy
5+
class Solution {
6+
public:
7+
int minimumPushes(string word) {
8+
const auto& ceil_divide = [](int a, int b) {
9+
return (a + b - 1) / b;
10+
};
11+
12+
int result = 0;
13+
const int l = ceil_divide(size(word), (9 - 2 + 1));
14+
for (int i = 1; i <= l; ++i) {
15+
result += i * min(static_cast<int>(size(word)) - (i - 1) * (9 - 2 + 1), (9 - 2 + 1));
16+
}
17+
return result;
18+
}
19+
};
20+
121
// Time: O(26)
222
// Space: O(26)
3-
423
// freq table, greedy
5-
class Solution {
24+
class Solution2 {
625
public:
726
int minimumPushes(string word) {
827
vector<int> cnt(26);

0 commit comments

Comments
 (0)