代码拉取完成,页面将自动刷新
// Time: ctor: O(1)
// flip: O(1)
// reset: O(min(f, r * c))
// Space: O(min(f, r * c))
class Solution {
public:
Solution(int n_rows, int n_cols) :
n_rows_(n_rows),
n_cols_(n_cols),
n_(n_rows * n_cols),
gen_{(random_device())()} {
}
vector<int> flip() {
uniform_int_distribution<int> uni(0, --n_);
const auto target = uni(gen_);
int x = get(target, target);
lookup_[target] = get(n_, n_);
return {x / n_cols_, x % n_cols_};
}
void reset() {
lookup_.clear();
n_ = n_rows_ * n_cols_;
}
private:
int get(int key, int default_value) {
return lookup_.count(key) ? lookup_[key] : default_value;
}
int n_rows_;
int n_cols_;
int n_;
unordered_map<int, int> lookup_;
default_random_engine gen_;
};
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(n_rows, n_cols);
* vector<int> param_1 = obj.flip();
* obj.reset();
*/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。