LeetCode 2024/8 每日一题合集

2024-7-1 LCP 40. 心算挑战

代码实现

class Solution {
public:
    int maxmiumScore(vector<int>& cards, int cnt) {
        int n = size(cards);
        std::sort(cards.rbegin(), cards.rend());
        int sum = std::accumulate(cards.begin(), cards.begin() + cnt, 0);
        int odd = -1, even = -1;
        for (int i = 0; i < cnt; ++i) {
            if (cards[i] % 2) {
                odd = i;
            } else {
                even = i;
            }
        }
        int ans = 0;
        if (cnt == n || sum % 2 == 0) {
            ans = sum;
        } else {
            {
                int now = -1;
                for (int i = cnt; i < n; ++i) {
                    if (cards[i] % 2 == 0) {
                        now = i;
                        break;
                    }
                }
                if (now == -1) {
                    ans = std::max(ans, sum - cards[odd]);
                } else {
                    ans = std::max(ans, sum - cards[odd] + cards[now]);
                }
            }
            {
                int now = -1;
                for (int i = cnt; i < n; ++i) {
                    if (cards[i] % 2) {
                        now = i;
                        break;
                    }
                }
                if (now == -1) {
                    ans = std::max(ans, sum - cards[odd]);
                } else if (even != -1) {
                    ans = std::max(ans, sum - cards[even] + cards[now]);
                }
            }
        }
        return ans % 2 == 0 ? ans : 0;
    }
};

2024-8-2 3128. 直角三角形

代码实现

class Solution {
public:
    long long numberOfRightTriangles(vector<vector<int>>& grid) {
        int n = size(grid), m = size(grid[0]);
        std::vector<std::vector<int>> p1(n + 1, std::vector<int>(m + 1));
        std::vector<std::vector<int>> p2(n + 1, std::vector<int>(m + 1));
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                p1[i + 1][j + 1] = p1[i + 1][j] + grid[i][j]; 
            }
        }
        for (int j = 0; j < m; ++j) {
            for (int i = 0; i < n; ++i) {
                p2[i + 1][j + 1] = p2[i][j + 1] + grid[i][j];
            }
        }
        using i64 = long long;
        i64 ans = 0;
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= m; ++j) if (grid[i - 1][j - 1]) {
                int left = p1[i][j - 1] - p1[i][0], right = p1[i][m] - p1[i][j];
                int up = p2[i - 1][j] - p2[0][j], down = p2[n][j] - p2[i][j];
                ans += up * right + right * down + down * left + left * up;
            }
        }
        return ans;
    }
};

2024-8-3 3143. 正方形中的最多点数

代码实现

class Solution {
public:
    int maxPointsInsideSquare(vector<vector<int>>& points, string s) {
        int n = size(points), ans = 0;
        auto check = [&](int size) {
            int vis = 0;
            for (int i = 0; i < n; ++i) {
                if (abs(points[i][0]) <= size && abs(points[i][1]) <= size) {
                    char c = s[i] - 'a';
                    if (vis >> c & 1) {
                        return false;
                    }
                    vis |= 1 << c;
                }
            }
            ans = __builtin_popcount(vis);
            return true;
        };
        int l = 0, r = 1e9 + 1;
        while (l < r) {
            int mid = l + r >> 1;
            if (check(mid)) {
                l = mid + 1;
            } else {
                r = mid;
            }
        }
        return ans;
    }
};

2024-8-4 572. 另一棵树的子树

补签,但是连胜断了QWQ

代码实现

class Solution {
public:
    bool isSubtree(TreeNode* A, TreeNode* B) {
        if (B == NULL) return false;
        auto check = [&](auto &&self, TreeNode *a, TreeNode *b)->bool {
            if (a == nullptr && b == nullptr) return true;
            if (b == nullptr) return false;
            if (a == nullptr) return false;
            if (a->val != b->val) return false;
            return self(self, a->left, b->left) && self(self, a->right, b->right);
        };
        auto dfs = [&](auto &&self, TreeNode *root)->bool {
            if (root == NULL) return false;
            if (root->val == B->val) {
                if (check(check, root, B)) return true;
            }
            if (self(self, root->right)) return true;
            if (self(self, root->left)) return true;
            return false;
        };
        return dfs(dfs, A);
    }
};

*2024-8-5 600. 不含连续1的非负整数

代码实现

class Solution {
public:
    int findIntegers(int n) {
        std::vector<int> f(31, 1);
        for (int i = 2; i < 31; ++i) {
            f[i] = f[i - 1] + f[i - 2];
        }
        int pre = 0, ans = 0;
        for (int i = 29; i >= 0; --i) {
            if ((n & (1 << i))) {
                ans += f[i + 1];
                if (pre == 1) break;
                pre = 1;
            } else pre = 0;
            ans += i == 0;
        } 
        return ans;

    }
};

*2024-8-6 3129. 找出所有稳定的二进制数组 I

代码实现

using i64 = long long;

template <class T>
constexpr T power(T a, i64 b) {
    T res = 1;
    for (; b; b /= 2, a *= a)
        if (b % 2) res *= a;
    return res;
}
template <int P>
struct MInt {
    int x;
    constexpr MInt() : x{} {}
    constexpr MInt(i64 x) : x{norm(x % P)} {}
    constexpr int norm(int x) const {
        if (x < 0) x += P;
        if (x >= P) x -= P;
        return x;
    }
    constexpr int val() const { return x; }
    explicit constexpr operator int() const { return x; }
    constexpr MInt operator-() const {
        MInt res;
        res.x = norm(P - x);
        return res;
    }
    constexpr MInt inv() const {
        assert(x != 0);
        return power(*this, P - 2);
    }
    constexpr MInt &operator*=(MInt rhs) {
        x = 1ll * x * rhs.x % P;
        return *this;
    }
    constexpr MInt &operator+=(MInt rhs) {
        x = norm(x + rhs.x);
        return *this;
    }
    constexpr MInt &operator-=(MInt rhs) {
        x = norm(x - rhs.x);
        return *this;
    }
    constexpr MInt &operator/=(MInt rhs) { return *this *= rhs.inv(); }
    friend constexpr MInt operator*(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res *= rhs;
        return res;
    }
    friend constexpr MInt operator+(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res += rhs;
        return res;
    }
    friend constexpr MInt operator-(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res -= rhs;
        return res;
    }
    friend constexpr MInt operator/(MInt lhs, MInt rhs) {
        MInt res = lhs;
        res /= rhs;
        return res;
    }
    friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
        i64 v;
        is >> v;
        a = MInt(v);
        return is;
    }
    friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) { return os << a.val(); }
    friend constexpr bool operator==(MInt lhs, MInt rhs) { return lhs.val() == rhs.val(); }
    friend constexpr bool operator!=(MInt lhs, MInt rhs) { return lhs.val() != rhs.val(); }
};

template <int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();

// constexpr int P = 998244353;
constexpr int P = 1e9 + 7;
using Z = MInt<P>;

struct Comb {
    int n;
    std::vector<Z> _fac, _invfac, _inv;
    Comb() : n{0}, _fac{1}, _invfac{1}, _inv{0} {}
    Comb(int n) : Comb() { init(n); }
    void init(int m) {
        if (m <= n) return;
        _fac.resize(m + 1), _invfac.resize(m + 1), _inv.resize(m + 1);
        for (int i = n + 1; i <= m; ++ i) _fac[i] = _fac[i - 1] * i;
        _invfac[m] = _fac[m].inv();
        for (int i = m; i > n; -- i) _invfac[i - 1] = _invfac[i] * i, _inv[i] = _invfac[i] * _fac[i - 1];
        n = m;
    }
    Z fac(int m) {
        if (m > n) init(2 * m);
        return _fac[m];
    }
    Z invfac(int m) {
        if (m > n) init(2 * m);
        return _invfac[m];
    }
    Z inv(int m) {
        if (m > n) init(2 * m);
        return _inv[m];
    }
    Z binom(int n, int m) {
        if (n < m || m < 0) return 0;
        return fac(n) * invfac(m) * invfac(n - m);
    }
} comb;

class Solution {
public:
    int numberOfStableArrays(int zero, int one, int limit) {
        int n = zero + one;
        if (zero > one) {
            std::swap(zero, one);
        }
        std::vector<Z> f0(zero + 3);
        for (int i = (zero - 1) / limit + 1; i <= zero; ++i) {
            f0[i] = comb.binom(zero - 1, i - 1);
            for (int j = 1; j <= (zero - i) / limit; ++j) {
                f0[i] += (j % 2 ? -1 : 1) * comb.binom(i, j) * comb.binom(zero - j * limit - 1, i - 1);
            }
        } 
        std::vector<Z> f1(one + 3);
        for (int i = (one - 1) / limit + 1; i <= one; ++i) {
            f1[i] = comb.binom(one - 1, i - 1);
            for (int j = 1; j <= (one - i) / limit; ++j) {
                f1[i] += (j % 2 ? -1 : 1) * comb.binom(i, j) * comb.binom(one - j * limit - 1, i - 1);
            }
        }
        Z ans = 0;
        for (int i = (one - 1) / limit + 1; i <= std::min(zero + 1, one); ++i) {
            ans += (f0[i - 1] + 2 * f0[i] + f0[i + 1]) * f1[i];
        }
        return (int)ans;
    }
};

*2024-8-7 3130. 找出所有稳定的二进制数组 II

代码实现

同前一天

*2024-8-8 3131. 找出与数组相加的整数 I

代码实现

class Solution {
public:
    int addedInteger(vector<int>& nums1, vector<int>& nums2) {
        return std::ranges::max(nums2) - std::ranges::max(nums1);
    }
};

*2024-8-9 3132. 找出与数组相加的整数 II

代码实现

class Solution {
public:
    int minimumAddedInteger(vector<int>& nums1, vector<int>& nums2) {
        std::ranges::sort(nums1);
        std::ranges::sort(nums2);
        for (int i = 2; i > 0; --i) {
            int x = nums2[0] - nums1[i];
            int j = 0;
            for (int k = i; k < size(nums1); k++) {
                if (nums2[j] == nums1[k] + x && ++j == size(nums2)) {
                    return x;
                }
            }
        }
        return nums2[0] - nums1[0];
    }
};
posted @ 2024-08-01 09:52  sleeeeeping  阅读(26)  评论(0)    收藏  举报