Skip to content
New issue

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

2024年第50周力扣打卡 #28

Open
XLXXLXXLX opened this issue Dec 9, 2024 · 0 comments
Open

2024年第50周力扣打卡 #28

XLXXLXXLX opened this issue Dec 9, 2024 · 0 comments
Labels

Comments

@XLXXLXXLX
Copy link
Owner

XLXXLXXLX commented Dec 9, 2024

周一

签个到溜了
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

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好丑啊。。。

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_heapstd::priority_queue好用

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;
    }
};
@XLXXLXXLX XLXXLXXLX changed the title 2024年第49周力扣打卡 2024年第50周力扣打卡 Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant