LeetCode 2024/7 每日一题 合集

2024-7-1 2065. 最大化一张图中的路径价值

分析

注意观察到至多走十条边,因此直接爆搜即可。

代码实现

class Solution {
public:
    int maximalPathQuality(vector<int>& values, vector<vector<int>>& edges, int maxTime) {
        int n = size(values), m = size(edges);
        std::vector<std::vector<std::pair<int, int>>> g(n);
        for (auto &edge : edges) {
            int u = edge[0], v = edge[1], w = edge[2];
            g[u].emplace_back(v, w);
            g[v].emplace_back(u, w);
        }
        std::vector<bool> vis(n);
        vis[0] = true;
        int ans = 0;
        auto dfs = [&](auto &&self, int u, int times, int cost)->void {
            if (u == 0) {
                ans = std::max(ans, cost);
            }
            for (auto [v, w] : g[u]) if (times + w <= maxTime) {
                if (!vis[v]) {
                    vis[v] = true;
                    self(self, v, times + w, cost + values[v]);
                    vis[v] = false;
                } else {
                    self(self, v, times + w, cost);
                }
            }
        };
        dfs(dfs, 0, 0, values[0]);
        return ans;
    }
};

2024-7-2 3115. 质数的最大距离

代码实现

std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
namespace Factorizer {
    std::vector<int> primes;
    std::vector<int> least;
    void sieve(int n) {
        std::vector<int> nums;
        least.assign(n + 1, 0);
        for (int i = 2; i <= n; ++i) {
            if (least[i] == 0) {
                least[i] = i;
                nums.push_back(i);
            }
            int now = 0;
            while (now < (int)nums.size()
                && nums[now] <= least[i] && i * nums[now] <= n) {
                least[i * nums[now]] = nums[now];
                now += 1;
            }
        }
        primes = nums;
    }
    constexpr bool miller_rabin(long long n) {
        if (n <= 1 || (n != 2 && n % 2 == 0)) return false;
        for (auto a : {3, 5, 7, 11, 13, 17, 19, 23, 29}) {
            if (n % a == 0) return n == a;
        }
        if (n < 31 * 31) return true;
        long long d = n - 1;
        while (d % 2 == 0) d /= 2;
        constexpr long long bases[] = {
            2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37
        };
        for (long long a : bases) {
            if (n == a) return true;
            long long t = d;
            long long y = 1 % n;
            for (long long _t = t; _t != 0; _t >>= 1) {
                if (_t & 1) y = (__int128) y * a % n;
                a = (__int128) a * a % n;
            }
            while (t != n - 1 && y != 1 && y != n - 1) {
                y = (__int128) y * y % n;
                t <<= 1;
            }
            if (y != n - 1 && t % 2 == 0) return false;
        }
        return true;
    }
    long long pollard_rho(long long n) {
        if (n == 1 || miller_rabin(n)) return n;
        long long now = 0;
        do {
            long long t = std::gcd(++now, n), r = t;
            if (t != 1 && t != n) return t;
            long long g = 1;
            do {
                t = ((__int128) t * t % n + now) % n;
                r = ((__int128) r * r % n + now) % n;
                r = ((__int128) r * r % n + now) % n;
            } while ((g = std::gcd(abs(t - r), n)) == 1);
            if (g != n) return g;
        } while (now < n / now);
        return 0;
    }
    std::vector<long long> factor(long long n) {
        if (n == 1) return {};
        std::vector<long long> g, d;
        d.push_back(n);
        while (!d.empty()) {
            auto v = d.back();
            d.pop_back();
            auto rho = pollard_rho(v);
            if (rho == v) {
                g.push_back(rho);
            } else {
                d.push_back(rho);
                d.push_back(v / rho);
            }
        }
        std::sort(g.begin(), g.end());
        return g;
    }
}
class Solution {
public:
    int maximumPrimeDifference(vector<int>& nums) {
        int n = size(nums);
        int l = 0, r = n - 1;
        while (Factorizer::factor(nums[l]).size() != 1 || nums[l] == 1) {
            l += 1;
        }
        while (Factorizer::factor(nums[r]).size() != 1 || nums[r] == 1) {
            r -= 1;
        }        
        return r - l;
    }
};

2024-7-3 3099. 哈沙德数

代码实现

class Solution {
public:
    int sumOfTheDigitsOfHarshadNumber(int x) {
        int ans = 0, y = x;
        while (x) {
            ans += x % 10;
            x /= 10;
        }
        return y % ans ? -1 : ans;
    }
};

2024-7-4 3086. 拾起 K 个 1 需要的最少行动次数

分析

(难度在于读题的题目)贪心+枚举,可以发现如果先执行操作1再执行操作2,一定可以得到一个k并且如果此时选择的位置为1,可以直接用代价1收集到1,因此可以对于maxChanges >= k - c的情况可以直接特判,最后只要枚举一下在所有位置的aliceIndex的代价即可,用双指针维护。

代码实现

class Solution {
public:
    long long minimumMoves(vector<int>& nums, int k, int maxChanges) {
        int n = size(nums);
        std::vector<int> pos;
        for (int i = 0; i < n; ++i) if (nums[i]) {
            pos.emplace_back(i);
        }
        int maxl = 0;
        for (int i = 0; i < (int)size(pos); ++i) {
            int len = 1;
            while (i + 1 < (int)size(pos) && pos[i + 1] - pos[i] == 1) {
                i += 1, len += 1;
            }   
            maxl = std::max(maxl, len);
        }
        int c = std::min(3, maxl);
        if (size(pos) == 0) {
            return k * 2;
        } else if (k <= c) {
            return k - 1;
        } else if (c + maxChanges >= k) {
            return c - 1 + (k - c) * 2;
        } else {
            k -= maxChanges;
            std::vector<int> pre(n + 1);
            for (int i = 0; i < n; ++i) {
                pre[i + 1] = pre[i] + nums[i];
            }
            long long ans = 0;
            for (int i = 1; i < k; ++i) {
                ans += pos[i] - pos[0];
            }
            int l = 0, r = k - 1;
            long long last = ans;
            for (int i = pos[0] + 1; i < n; ++i) {
                long long sum = last;
                sum += (long long)(pre[i] - pre[pos[l]]) - (pre[pos[r] + 1] - pre[i]);
                while (r + 1 < size(pos) && i - pos[l] >= pos[r + 1] - i) {
                    sum = sum - (i - pos[l]) + (pos[r + 1] - i);
                    l += 1, r += 1;
                }
                ans = std::min(ans, sum);
                last = sum;
            }
            return ans + 2 * maxChanges;    
        }
    }
};

2024-7-5 3033. 修改矩阵

代码实现

class Solution {
public:
    vector<vector<int>> modifiedMatrix(vector<vector<int>>& matrix) {
        int n = size(matrix), m = size(matrix[0]);
        for (int i = 0; i < m; ++i) {
            int max = 0;
            for (int j = 0; j < n; ++j) {
                max = std::max(max, matrix[j][i]);
            }
            for (int j = 0; j < n; ++j) {
                if (matrix[j][i] == -1) {
                    matrix[j][i] = max;
                }
            }
        }
        return matrix;
    }
};

2024-7-6 3101. 交替子数组计数

代码实现

class Solution {
public:
    long long countAlternatingSubarrays(vector<int>& nums) {
        int n = size(nums);    
        long long ans = 0;
        for (int i = 1; i < n; ++i) {
            int len = 1;
            while (i < n && nums[i] != nums[i - 1]) {
                i += 1, len += 1;
            }
            ans += (long long)len * (len - 1) / 2;
        }
        return ans + n;
    }
};

2024-7-7 1958. 检查操作是否合法

代码实现

class Solution {
public:
    bool checkMove(vector<vector<char>>& board, int rMove, int cMove, char color) {
        int n = size(board), m = size(board[0]);
        for (auto [x, y] : {std::pair{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}}) {
            int fx = rMove + x, fy = cMove + y; 
            if (fx >= 0 && fx < n && fy >= 0 && fy < m && board[fx][fy] == (color ^ 'B' ^ 'W')) {
                while (true) {
                    fx += x, fy += y;
                        if (fx < 0 || fx >= n || fy < 0 || fy >= m || board[fx][fy] == '.') {
                        break;
                    }
                    if (board[fx][fy] == color) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
};

2024-7-8 724. 寻找数组的中心下标

代码实现

class Solution {
public:
    int pivotIndex(vector<int>& nums) {
        int n = size(nums);
        std::vector<int> pre(n + 1);
        for (int i = 0; i < n; ++i) {
            pre[i + 1] = pre[i] + nums[i];
        }
        for (int i = 1; i <= n; ++i) {
            if (pre[i - 1] == pre[n] - pre[i]) {
                return i - 1;
            }
        }
        return -1;
    }
};

2024-7-9 3102. 最小化曼哈顿距离

代码实现

class Solution {
public:
    int minimumDistance(vector<vector<int>>& points) {
        std::multiset<int> xs, ys;
        for (auto &p : points) {
            xs.emplace(p[0] + p[1]);
            ys.emplace(p[0] - p[1]);
        }
        int ans = 2e9;
        for (auto &p : points) {
            int x = p[0] + p[1], y = p[0] - p[1];
            xs.erase(xs.lower_bound(x));
            ys.erase(ys.lower_bound(y));
            ans = std::min(ans, std::max<int>(*xs.rbegin() - *xs.begin(), *ys.rbegin() - *ys.begin()));
            xs.emplace(x);
            ys.emplace(y);
        }
        return ans;
    }
};

2024-7-10 2970. 统计移除递增子数组的数目 I

代码实现

class Solution {
public:
    int incremovableSubarrayCount(vector<int>& nums) {
        int n = size(nums);
        int ans = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = i; j < n; ++j) {
                std::vector<int> ver;
                ver.insert(ver.end(), nums.begin(), nums.begin() + i);
                ver.insert(ver.end(), nums.begin() + j + 1, nums.end());
                bool ok = true;
                for (int k = 1; k < size(ver); ++k) {
                    ok &= (ver[k] > ver[k - 1]);
                }
                ans += ok;
            }
        }
        return ans;
    }
};

2024-7-11 2972. 统计移除递增子数组的数目 II

代码实现

class Solution {
public:
    long long incremovableSubarrayCount(vector<int>& nums) {
        int n = size(nums);
        int idx = 0;
        while (idx < n - 1 && nums[idx] < nums[idx + 1]) {
            idx += 1;
        }
        if (idx == n - 1) {
            return (long long) n * (n + 1) / 2;
        }
        long long ans = idx + 2;
        for (int i = n - 1; i == n - 1 || nums[i] < nums[i + 1]; --i) {
            while (idx >= 0 && nums[idx] >= nums[i]) {
                idx -= 1;
            }
            ans += idx + 2;
        }
        return ans;
    }
};

2024-7-12 2974. 最小数字游戏

代码实现

class Solution {
public:
    vector<int> numberGame(vector<int>& nums) {
        std::ranges::sort(nums);
        for (int i = 1; i < (int)size(nums); i += 2) {
            std::swap(nums[i], nums[i - 1]);
        }
        return nums;
    }
};

2024-7-13 3011. 判断一个数组是否可以变为有序

代码实现

class Solution {
public:
    bool canSortArray(vector<int>& nums) {
        int n = size(nums);
        for (int i = 0; i < n; ++i) {
            int x = __builtin_popcount(nums[i]), j = i;
            while (j < n && __builtin_popcount(nums[j]) == x) {
                j += 1;
            }
            std::sort(nums.begin() + i, nums.begin() + j);
        } 
        return std::is_sorted(nums.begin(), nums.end());
    }
};

2024-7-14 807. 保持城市天际线

代码实现

class Solution {
public:
    int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
        int n = size(grid);
        std::vector<int> maxr(n), maxc(n);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                maxr[i] = std::max(maxr[i], grid[i][j]);
                maxc[j] = std::max(maxc[j], grid[i][j]);
            }
        }
        int ans = 0;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                ans += std::min(maxr[i], maxc[j]) - grid[i][j];
            }
        }
        return ans;
    }
};

2024-7-15 721. 账户合并

代码实现

struct DSU {
    std::vector<int> p, siz;
    DSU(int n) : p(n), siz(n, 1) { std::iota(p.begin(), p.end(), 0); }
    int leader(int x) {
        while (x != p[x]) x = p[x] = p[p[x]];
        return x;
    }
    bool same(int x, int y) { return leader(x) == leader(y); }
    bool merge(int x, int y) {
        x = leader(x), y = leader(y);
        if (x == y) return false;
        siz[x] += siz[y], p[y] = x;
        return true;
    }
    int size(int x) { return siz[leader(x)]; }
};
class Solution {
public:
    vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
        int n = size(accounts);
        DSU dsu(n);
        auto isEdge = [&](int x, int y) {
            for (int i = 1; i < size(accounts[x]); ++i) {
                for (int j = 1; j < size(accounts[y]); ++j) {
                    if (accounts[x][i] == accounts[y][j]) {
                        return true;
                    }
                }
            }
            return false;
        };
        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                if (isEdge(i, j)) {
                    dsu.merge(i, j);
                }
            }
        }
        std::vector<std::vector<std::string>> ans;
        std::vector<std::vector<int>> set(n);
        for (int i = 0; i < n; ++i) {
            set[dsu.leader(i)].emplace_back(i);
        }
        for (int i = 0; i < n; ++i) if (size(set[i])) {
            std::vector<std::string> add = {accounts[i][0]};
            for (auto t : set[i]) {
                for (int j = 1; j < (int)size(accounts[t]); ++j) {
                    add.emplace_back(accounts[t][j]);
                }
            }
            std::sort(add.begin() + 1, add.end());
            add.erase(std::unique(add.begin(), add.end()), add.end());
            ans.emplace_back(add);
        }
        return ans;
    }
};

2024-7-16 2956. 找到两个数组中的公共元素

代码实现

class Solution {
public:
    vector<int> findIntersectionValues(vector<int>& nums1, vector<int>& nums2) {
        int n = size(nums1), m = size(nums2);
        std::vector<int> ans{0, 0};
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (nums1[i] == nums2[j]) {
                    ans[0] += 1;
                    break;
                }
            }
        }
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (nums1[j] == nums2[i]) {
                    ans[1] += 1;
                    break;
                }
            }
        }
        return ans;
    }
};  

2024-7-17 2959. 关闭分部的可行集合数目

分析

可以发现\(n \leq 10\),显然可以直接进行二进制枚举选择是否关闭分部。然后进行连边,floyed暴力跑看图是否联通和最长路径即可。

代码实现

class Solution {
public:
    int numberOfSets(int n, int maxDistance, vector<vector<int>>& roads) {
        auto work = [&](int choice) {
            std::vector g(n, std::vector<int>(n, 1e9));
            for (int i = 0; i < n; ++i) {
                g[i][i] = 0;
            }
            for (auto road : roads) {
                int u = road[0], v = road[1], w = road[2];
                if ((choice >> u & 1) && (choice >> v & 1)) {
                    g[u][v] = g[v][u] = std::min(g[u][v], w);
                }
            }
            for (int k = 0; k < n; ++k) {
                for (int i = 0; i < n; ++i) {
                    for (int j = 0; j < n; ++j) {
                        g[i][j] = std::min(g[i][j], g[i][k] + g[k][j]);
                    }
                }
            }
            int max = 0;
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < n; ++j) {
                    if (g[i][j] < 1e9) {
                        max = std::max(max, g[i][j]);
                    }
                }
            }
            bool ok = true;
            for (int i = 0; i < n; ++i) if (choice >> i & 1) {
                for (int j = 0; j < n; ++j) if (choice >> j & 1) {
                    ok &= (g[i][j] < 1e9);
                }
            }
            return max <= maxDistance && ok;
        };
        int ans = 0;
        for (int i = 0; i < (1 << n); ++i) {
            ans += work(i);
        }   
        return ans;
    }
};

2024-7-18 3112. 访问消失节点的最少时间

代码实现

class Solution {
public:
    vector<int> minimumTime(int n, vector<vector<int>>& edges, vector<int>& disappear) {
        std::vector<std::vector<std::pair<int, int>>> g(n);
        for (auto edge : edges) {
            int u = edge[0], v = edge[1], w = edge[2];
            g[u].emplace_back(v, w);
            g[v].emplace_back(u, w);
        }
        std::vector<int> dist(n, 1e9);
        std::vector<bool> st(n);
        std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<>> heap;
        heap.emplace(0, 0);
        dist[0] = 0;
        while (size(heap)) {
            auto [d, u] = heap.top();
            heap.pop();
            if (st[u]) continue;
            st[u] = true;
            for (auto [v, w] : g[u]) {
                if (d + w < dist[v] && disappear[v] > d + w) {
                    dist[v] = dist[u] + w;
                    heap.emplace(dist[v], v);
                }
            }
        }
        for (int i = 0; i < n; ++i) {
            if (dist[i] >= 1e9) {
                dist[i] = -1;
            }
        }
        return dist;
    }
};

2024-7-19 3096. 得到更多分数的最少关卡数目

代码实现

class Solution {
public:
    int minimumLevels(vector<int>& possible) {
        int n = size(possible);
        int sum = std::accumulate(possible.begin(), possible.end(), 0) * 2 - n;
        int pre = 0;
        for (int i = 0; i < n - 1; ++i) {
            pre += possible[i] == 1 ? 1 : -1;
            if (2 * pre > sum) return i + 1;
        }
        return -1;
    }
};

2024-7-20 2850. 将石头分散到网格图的最少移动次数

代码实现

class Solution {
public:
    int minimumMoves(vector<vector<int>>& grid) {
        std::vector<std::pair<int, int>> from, to;
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 3; ++j) {
                if (grid[i][j]) {
                    for (int k = 1; k < grid[i][j]; ++k) {
                        from.emplace_back(i, j);
                    }
                } else {
                    to.emplace_back(i, j);
                }
            }
        }
        int ans = 1e9;
        do {
            int sum = 0;
            for (int i = 0; i < (int)size(from); ++i) {
                sum += std::abs(from[i].first - to[i].first) + std::abs(from[i].second - to[i].second);
            }
            ans = std::min(ans, sum);
        } while (std::next_permutation(from.begin(), from.end()));
        return ans;
    }
};

2024-7-21 2850. 将石头分散到网格图的最少移动次数

代码实现

class Solution {
public:
    int maximumSum(vector<int>& arr) {
        int dp0 = arr[0], dp1 = 0, res = arr[0];
        for (int i = 1; i < arr.size(); ++i) {
            dp1 = max(dp0, dp1 + arr[i]);
            dp0 = max(dp0, 0) + arr[i];
            res = max(res, max(dp0, dp1));
        }
        return res;
    }
};

2024-7-22 2101. 引爆最多的炸弹

代码实现

class Solution {
public:
    int maximumDetonation(vector<vector<int>>& bombs) {
        int n = size(bombs);
        auto get = [&](int a1, int b1, int a2, int b2) {
            return std::hypot(a1 - a2, b1 - b2);
        };
        std::vector<std::vector<int>> g(n);
        for (int i = 0; i < n; ++i) {
            for (int j = i + 1; j < n; ++j) {
                double dist = get(bombs[i][0], bombs[i][1], bombs[j][0], bombs[j][1]);
                if (1. * bombs[i][2] >= dist) {
                    g[i].push_back(j);
                }
                if (1. * bombs[j][2] >= dist) {
                    g[j].push_back(i);
                }
            }
        }
        std::vector<bool> vis(n);
        auto dfs = [&](auto &&self, int u)->int {
            int cnt = 1;
            vis[u] = true;
            for (auto v : g[u]) if (!vis[v]) {
                cnt += self(self, v);
            }
            return cnt;
        };
        int ans = 0;
        for (int i = 0; i < n; ++i) {
            vis.assign(n, false);
            ans = std::max(ans, dfs(dfs, i));
        }
        return ans;
    }
};

2024-7-23 3098. 求出所有子序列的能量和

代码实现

constexpr int P = 1e9 + 7;
class Solution {
public:
    int sumOfPowers(vector<int>& nums, int k) {
        std::unordered_map<long long, int> f;
        int n = size(nums);
        std::sort(nums.begin(), nums.end());
        auto dfs = [&](auto&& dfs, int i, int j, int k, int mi) -> int {
            if (i >= n) {
                return k == 0 ? mi : 0;
            }
            if (n - i < k) {
                return 0;
            }
            long long key = (1LL * mi) << 18 | (i << 12) | (j << 6) | k;
            if (f.contains(key)) return f[key];
            long long ans = dfs(dfs, i + 1, j, k, mi);
            if (j == n) {
                ans += dfs(dfs, i + 1, i, k - 1, mi);
            } else {
                ans += dfs(dfs, i + 1, i, k - 1, min(mi, nums[i] - nums[j]));
            }
            ans %= P;
            return f[key] = ans;
        };
        return dfs(dfs, 0, n, k, 1e9);
    }
};

2024-7-24 2766. 重新放置石块

代码实现

class Solution {
public:
    vector<int> relocateMarbles(vector<int>& nums, vector<int>& moveFrom, vector<int>& moveTo) {
        std::map<int, int> mp;
        int n = size(nums), m = size(moveFrom);
        for (int i = 0; i < n; ++i) {
            mp[nums[i]] += 1;
        }
        for (int i = 0; i < m; ++i) {
            int u = moveFrom[i], v = moveTo[i];
            if (mp.count(u)) {
                mp[v];
                if (u != v) {
                    mp.erase(u);
                }
            }
        }
        std::vector<int> ans;
        for (auto [x, _] : mp) {
            ans.push_back(x);
        }
        return ans;
    }
};

2024-7-25 2844. 生成特殊数字的最少操作

代码实现

class Solution {
public:
    int minimumOperations(string num) {
        int ans = size(num) - 1;
        int suc = 0;
        {
            std::string s = num;
            int p5 = s.rfind('5');
            if (p5 != -1) {
                int cnt = (int)size(num) - p5 - 1;
                s.erase(s.begin() + p5, s.end());
                int p2 = s.rfind('2'), p7 = s.rfind('7');
                if (p2 != -1) {
                    ans = std::min(ans, cnt + (int)size(s) - p2 - 1);
                    suc += 1;
                }
                if (p7 != -1) {
                    ans = std::min(ans, cnt + (int)size(s) - p7 - 1);
                    suc += 1;
                }
            }
        }
        {
            std::string s = num;
            int p0 = s.rfind('0');
            if (p0 != -1) {
                int cnt = (int)size(num) - p0 - 1;
                s.erase(s.begin() + p0, s.end());
                int p00 = s.rfind('0'), p5 = s.rfind('5');
                if (p00 != -1) {
                    ans = std::min(ans, cnt + (int)size(s) - p00 - 1);
                    suc += 1;
                }
                if (p5 != -1) {
                    ans = std::min(ans, cnt + (int)size(s) - p5 - 1);
                    suc += 1;
                }
            }
        }
        if (suc == 0 && std::count(num.begin(), num.end(), '0') == 0) {
            return size(num);
        }
        return ans;
    }
};

2024-7-26 2740. 找出分区值

代码实现

class Solution {
public:
    int findValueOfPartition(vector<int>& nums) {
        int n = size(nums);
        std::sort(nums.begin(), nums.end());
        int ans = 2e9;
        for (int i = 1; i < n; ++i) {
            ans = std::min(ans, nums[i] - nums[i - 1]);
        }
        return ans;
    }
};

2024-7-27 3106. 满足距离约束且字典序最小的字符串

代码实现

class Solution {
public:
    string getSmallestString(string s, int k) {
        for (auto &c : s) {
            int x = std::min(c - 'a', 'z' - c + 1);
            if (x > k) {
                c -= k;
                return s;
            }
            c = 'a', k -= x;
        }
        return s;
    }
};

2024-7-28 699. 掉落的方块

代码实现

class Solution {
public:
    vector<int> fallingSquares(vector<vector<int>>& positions) {
        int n = size(positions);
        std::vector<int> h(n);
        for (int i = 0; i < n; ++i) {
            int l1 = positions[i][0], r1 = positions[i][0] + positions[i][1] - 1;
            h[i] = positions[i][1];
            for (int j = 0; j < i; ++j) {
                int l2 = positions[j][0], r2 = positions[j][0] + positions[j][1] - 1;
                if (r1 >= l2 && r2 >= l1) {
                    h[i] = std::max(h[i], h[j] + positions[i][1]);
                }
            }
        }
        for (int i = 1; i < n; ++i) {
            h[i] = std::max(h[i], h[i - 1]);
        }
        return h;
    }
};

2024-7-29 682. 棒球比赛

代码实现

class Solution {
public:
    int calPoints(vector<string>& operations) {
        int n = size(operations);
        std::vector<int> ans;
        for (auto op : operations) {
            if (op == "+") {
                ans.push_back(ans.rbegin()[0] + ans.rbegin()[1]);
            } else if (op == "D") {
                ans.push_back(ans.rbegin()[0] * 2);
            } else if (op == "C") {
                ans.pop_back();
            } else {
                ans.push_back(std::stoi(op));
            }
        }
        return std::accumulate(ans.begin(), ans.end(), 0LL);
    }
};

2024-7-30 682. 棒球比赛

代码实现

class Solution {
public:
    vector<int> getGoodIndices(vector<vector<int>>& variables, int target) {
        auto power = [&](int a, int b, int p) {
            int c = 1;
            while (b) {
                if (b & 1) c = c * a % p;
                a = a * a % p;
                b >>= 1;
            }
            return c;
        };
        std::vector<int> ans;
        for (int i = 0; i < size(variables); ++i) {
            int a = variables[i][0];
            int b = variables[i][1];
            int c = variables[i][2];
            int m = variables[i][3];
            if (power(power(a, b, 10), c, m) == target) {
                ans.push_back(i);
            }
        }
        return ans;
    }
};

2024-7-31 3111. 覆盖所有点的最少矩形数目

分析

显然的发现y轴对于题目并没有影响,因此直接记录x的坐标并对其排序去重,最后贪心的放置即可。

代码实现

class Solution {
public:
    int minRectanglesToCoverPoints(vector<vector<int>>& points, int w) {
        int n = size(points);
        std::vector<int> px(n);
        for (int i = 0; i < n; ++i) {
            px[i] = points[i][0];
        }
        std::sort(px.begin(), px.end());
        px.erase(std::unique(px.begin(), px.end()), px.end());
        n = size(px);
        int cnt = 0;
        for (int i = 0; i < n; ++i) {
            int first = px[i], j = i + 1;
            while (j < n && px[j] - first <= w) {
                j += 1;
            }
            i = j - 1;
            cnt += 1;
        }
        return cnt;
    }
};
posted @ 2024-07-01 11:06  sleeeeeping  阅读(9)  评论(0编辑  收藏  举报