casyup.me@outlook.com

0%

leetcode/onMy...

on my …

my solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution {
public:
bool isNStraightHand(vector<int>& hand, int W) {
div_t di = div(hand.size(), W);
if (di.rem) {
return false;
}

map<int, int> hds;
for (auto v : hand) {
++hds[v];
}

while (!hds.empty()) {
auto it = *hds.begin();
for (int l = it.second, b = it.first, e = it.first + W;
b < e; ++b) {
hds[b] -= l;
if (hds[b] < 0) {
return false;
} else if (hds[b] == 0) {
hds.erase(b);
}
}
}

return true;
}
};

什么也不想说 :(

the best solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
bool isNStraightHand(vector<int>& hand, int W) {
vector<int> v(W);

if (W == 3 && hand[0] == 2 && hand[1] == 4) {
return false;
}

for(int h: hand) {
v[h%W]++;
}

int cnt = v[0];
for(int i=0; i<W; i++) {
if(v[i] != cnt) return false;
}

return true;
}
};

虽然这个算法偷鸡(最开始那个 if 判断明显具有针对性, 在输入是 [2,4,3] 的情况下会不正确)

但是依旧对我造成了很大打击…

最开始我的算法是想了一段时间的, 怎么简单, 怎么高效.

啊… 我为何总是欠缺这种看透问题本质的能力呢…

我的算法不止返回 bool 还可以得到最后的集, 我只能如此安慰自己