We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
签个到溜了 1812. 判断国际象棋棋盘中一个格子的颜色
class Solution { public: bool squareIsWhite(string coordinates) { return (coordinates[0] + coordinates[1]) % 2; } };
935. 骑士拨号器 dp,mid
class Solution { static constexpr int mod = 1e9 + 7; public: int knightDialer(int n) { vector<vector<int>> moves = { {4, 6}, // 0 {6, 8}, {7, 9}, {4, 8}, // 1 2 3 {3, 9, 0}, {}, {1, 7, 0}, // 4 5 6 {2, 6}, {1, 3}, {2, 4} // 7 8 9 }; vector<vector<int>> d{vector<int>(10, 0), vector<int>(10, 1)}; for (int i = 2; i <= n; i++) { int x = i & 1; for (int j = 0; j < 10; j++) { d[x][j] = 0; for (int k : moves[j]) { d[x][j] = (d[x][j] + d[x ^ 1][k]) % mod; } } } int res = 0; for (auto x : d[n % 2]) { res = (res + x) % mod; } return res; } };
2717. 半有序排列 签个到,记一下这个std::minmax_element
std::minmax_element
class Solution { public: int semiOrderedPermutation(vector<int>& nums) { auto [first, last] = minmax_element(nums.begin(), nums.end()); return first + nums.size() - 1 - last - (last < first); } };
2931. 购买物品的最大开销 hard,但感觉难度只是普通mid,吐槽一下std::priority_queue的api好丑啊。。。
std::priority_queue
class Solution { public: using ll = long long; long long maxSpending(vector<vector<int>>& values) { priority_queue<array<ll, 2>, std::vector<array<ll, 2>>, greater<array<ll, 2>>>q; ll m = values.size(), n = values[0].size(); vector<ll>idxs(m, n - 1); for(ll i = 0; i < m; i++){ q.push({values[i][idxs[i]], i}); } ll ans = 0, d = 1; while(!q.empty()){ auto [value, i] = q.top(); ans += d * value; d++, q.pop() , idxs[i]--; if(idxs[i] >= 0){ q.push({values[i][idxs[i]], i}); } } return ans; } };
3264. K 次乘运算后的最终数组 I 感觉std::make_heap比std::priority_queue好用
std::make_heap
class Solution { public: vector<int> getFinalState(vector<int>& nums, int k, int multiplier) { if(multiplier == 1){ return nums; } int len = nums.size(); vector<int>h(len); iota(h.begin(), h.end(), 0); auto cmp = [&nums](int a, int b){ return nums[a] > nums[b] || (nums[a] == nums[b] && a > b); }; for (auto _{k}; _--;) { make_heap(h.begin(), h.end(), cmp); pop_heap(h.begin(), h.end(), cmp); nums[h[len - 1]] *= multiplier; } return nums; } };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
周一
签个到溜了
1812. 判断国际象棋棋盘中一个格子的颜色
周二
935. 骑士拨号器
dp,mid
周三
2717. 半有序排列
签个到,记一下这个
std::minmax_element
周四
2931. 购买物品的最大开销
hard,但感觉难度只是普通mid,吐槽一下
std::priority_queue
的api好丑啊。。。周五
3264. K 次乘运算后的最终数组 I
感觉
std::make_heap
比std::priority_queue
好用The text was updated successfully, but these errors were encountered: