Leetcode 第102场双周赛

Q1

循环

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
const int mod = 1e9 + 7;
class Solution {
   public:
    vector<int> findColumnWidth(vector<vector<int>>& grid) {
        int n = grid.size(), m = grid[0].size();
        // int ans = 0;
        vector<int> ans(m, 0);
        for (int i = 0; i < m; i++) {
            int cnt = 0;
            for (int j = 0; j < n; j++) {
                // cout<<to_string(grid[j][i])<<" ";
                cnt = max(cnt, (int)to_string(grid[j][i]).size());
            }
            ans[i] = cnt;
        }
        return ans;
    }
};

Q2

预处理前缀

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
const int mod = 1e9 + 7;

class Solution {
   public:
    vector<long long> findPrefixScore(vector<int>& nums) {
        int n = nums.size();
        vector<long long> ans(n, 0), LMAX(n, 0);
        LMAX[0] = nums[0];
        for (int i = 1; i < n; i++) {
            LMAX[i] = max(LMAX[i - 1], 1ll * nums[i]);
        }
        vector<ll> conver(n, 0);
        for (int i = 0; i < n; i++) {
            conver[i] = nums[i] + LMAX[i];
        }
        ans[0]=conver[0];
        for (int i = 1; i < n; i++) {
            ans[i] = ans[i - 1] + conver[i];
        }
        return ans;
    }
};

Q3

二叉树的遍历
先把每一层的兄弟节点val之和处理出来。
那么对于每一个节点,新的val=这一层的val之和-自己的val-兄弟节点val。
每一次遍历把兄弟节点的val穿过去。
算是leetcode的特色题目。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
const int mod = 1e9 + 7;
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
   public:
    map<int, int> mp;
    void pre(TreeNode* root, int depth) {
        if (root == nullptr) return;
        mp[depth] += root->val;
        pre(root->left, depth + 1);
        pre(root->right, depth + 1);
    }
    void dfs(TreeNode* root, int depth,int t) {
        if(root == nullptr) return;
        root->val = mp[depth]-root->val-t;
        int tl=root->left==nullptr?0:root->left->val;
        int tr=root->right==nullptr?0:root->right->val;
        dfs(root->left, depth + 1,tr);
        dfs(root->right, depth + 1,tl);
    }
    TreeNode* replaceValueInTree(TreeNode* root) {
        pre(root, 0);
        dfs(root, 0,0);
        return root;
    }
};

Q4

地杰斯特拉模板题
注意是单向边。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
const int mod = 1e9 + 7;

class Graph {
   public:
    vector<pair<int, int>> g[200];

    int Dijkstra(int st, int n, int ed) {
        vector<int> dis(n + 1, INT_MAX);
        dis[st] = 0;
        priority_queue<pair<int, int>, vector<pair<int, int>>,
                       greater<pair<int, int>>>
            q;
        q.push({0, st});
        while (!q.empty()) {
            auto t = q.top();
            q.pop();
            int u = t.second, d = t.first;
            if (d > dis[u]) continue;
            for (auto& e : g[u]) {
                int v = e.first, w = e.second;
                if (dis[v] > dis[u] + w) {
                    dis[v] = dis[u] + w;
                    q.push({dis[v], v});
                }
            }
        }
        return dis[ed] == INT_MAX ? -1 : dis[ed];
    }

    Graph(int n, vector<vector<int>>& edges) {
        for (auto& arr : edges) {
            g[arr[0]].push_back({arr[1], arr[2]});
        }
    }

    void addEdge(vector<int> edge) {
        g[edge[0]].push_back({edge[1], edge[2]});
    }

    int shortestPath(int node1, int node2) {
        return Dijkstra(node1, 200, node2);
    }
};

/**
 * Your Graph object will be instantiated and called as such:
 * Graph* obj = new Graph(n, edges);
 * obj->addEdge(edge);
 * int param_2 = obj->shortestPath(node1,node2);
 */
### LeetCode概述 LeetCode是一项在线编程竞活动,旨在帮助程序员提升算法能力并准备技术面试。参与者可以在规定的时间内解决一系列具有挑战性的编程问题[^1]。 ### 参加方法 为了参与LeetCode,用户需先注册一个LeetCode账号,并定期关注官方公告获取最新的事通知。比通常会在周末举行,在比当天登录网站进入指定的比页面即可开始作答。对于初次参者来说,可能会遇到一些操作上的不熟悉,比如不清楚在哪里提交解答等问题,但随着经验积累这些问题都会迎刃而解[^2]。 ### 比时间安排 LeetCode一般固定在北京时间每周日凌晨01:00(UTC/GMT+8)准时开,持续时间为两小时。在此期间,选手可以自由选择任意连续的两个小时完成比中的题目。需要注意的是,具体的比日期和时间可能因节假日等因素有所调整,请务必留意官网发布的最新消息[^4]。 ### 题目类型分析 比中涉及的题目种类繁多,涵盖了数据结构、算法设计等多个方面。常见的题目形式包括但不限于: - 数组与字符串处理 - 动态规划 - 图论及其应用 - 排序与查找技巧 - 栈队列等高级数据结构的应用 这些题目往往要求较高的逻辑思维能力和扎实的基础知识掌握程度。例如,在某些情况下,一道看似简单的数组运算题也可能隐藏着深层次的数据结构优化思路;而在另一些景下,则需要运用到复杂的动态规划策略来解决问题[^5]。 ```python def example_function(input_data): """ 这是一个示例函数,用于展示如何编写Python代码。 参数: input_data (list): 输入的数据列表 返回: result (int): 计算后的结果值 """ # 处理输入数据... processed_data = sorted(input_data) # 执行核心计算逻辑... result = sum(processed_data[:3]) return result ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

万伏小太阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值