leetcode第320周赛及acwing第78场周赛补题

acwing:

4720. 字符串 - AcWing题库

思路:类似于消消乐,可以用桟来模拟消除。y总的代码可以看作类似桟的处理,不同则压入,相同则将栈顶弹出即可

#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;

int main()
{
    string x;
    cin >> x;
    string res;
    for(auto k : x)
    {
        if(res.size() && res.back() == k) res.pop_back();
        else res += k;
    }
    cout << res;
    return 0;
}

4721. 排队 - AcWing题库

思路:用暴力来做会超时,我们用单调桟来保证有序性,再通过二分查找定位目标值即可

#include<iostream>
#include<algorithm>

using namespace std;

const int N = 100010;

int n;
int w[N], stk[N];
int res[N];

int main()
{
    cin >> n;
    for(int i = 0; i < n; i++) cin >> w[i];
    
    int top = 0;
    for(int i = n - 1; i >= 0; i--)
    {
        if(!top || w[i] <= w[stk[top]]) res[i] = -1;
        else
        {
            int l = 1, r = top;
            while(l < r)
            {
                int mid = l + r >> 1;
                if(w[stk[mid]] < w[i]) r = mid;
                else l = mid + 1;
            }
            res[i] = stk[r] - i - 1;
        }
        if(!top || w[i] < w[stk[top]]) stk[++top] = i;
    }
    
    for(int i = 0; i < n; i++) cout << res[i] << " ";
    return 0;
}

leetcode:

6241. 数组中不等三元组的数目 - 力扣(LeetCode)

思路:简单遍历一遍即可

class Solution {
public:
    int unequalTriplets(vector<int>& nums) {
        int n = nums.size();
        int res = 0;
        for(int i = 0; i < n; i++)
        {
            for(int j = i + 1; j < n; j++)
            {
                for(int k = j + 1; k < n; k++)
                {
                    if(nums[i] != nums[j] && nums[i] != nums[k] && nums[j] != nums[k]) res++;
                }
            }
        }
        return res;
    }
};

6242. 二叉搜索树最近节点查询 - 力扣(LeetCode)

思路:将二叉树节点遍历到数组里排序,然后用二分来找符合条件的值,注意条件即可,比赛时漏考虑了二分找不到元素的话不一定该值就是最大的小于目标值的数,需要倒退一个判断一下,找最小的大于目标值的数也是如此

/**
 * 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:
    vector<vector<int>> closestNodes(TreeNode* root, vector<int>& queries) {
        queue<TreeNode*> que;
        if (root != NULL) que.push(root);
        vector<int> result;
        while (!que.empty()) {
            int size = que.size();
            // 这里一定要使用固定大小size,不要使用que.size(),因为que.size是不断变化的
            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();
                result.push_back(node->val);
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
            }
        }
        int n = queries.size();
        sort(result.begin(), result.end());
        vector<vector<int>> res(n, vector<int>(2));
        for(int i = 0; i < n; i++)
        {
            res[i][0] = -1;
            res[i][1] = 1e9;
        }
        for(int i = 0; i < queries.size(); i++)
        {
            int l = 0, r = result.size() - 1;
            while(l < r)
            {
                int mid = (l + r) / 2 ;
                if(result[mid] >= queries[i]) r = mid;
                else l = mid + 1;
            }
            if(result[l] == queries[i])
            {
                res[i][0] = res[i][1] = result[l];
                continue;
            }
            if(result[l] <= queries[i]) res[i][0] = max(res[i][0], result[l]);
            else if(l > 0 && result[l-1] < queries[i]) res[i][0] = max(res[i][0], result[l - 1]);
            l = 0, r = result.size() - 1;
            while(l < r)
            {
                int mid = (l + r + 1) / 2;
                if(result[mid] <= queries[i]) l = mid;
                else r = mid - 1;
            }
            if(result[l] >= queries[i])res[i][1] = min(res[i][1], result[l]);
            else if(l < result.size() - 1 && result[l+1] > queries[i]) res[i][1] = min(res[i][1], result[l+1]);
            /*
            for(int j = 0; j < result.size(); j++)
            {
                if(result[j] == queries[i])
                {
                    res[i][0] = res[i][1] = result[j];
                    break;
                }
                else if(result[j] < queries[i]) res[i][0] = max(res[i][0], result[j]);
                else res[i][1] = min(res[i][1], result[j]);
            }
            */
        }
        for(int i = 0; i < n; i++)
        {
            if(res[i][1] == 1e9) res[i][1] = -1;
        }
        return res;
    }
};

6243. 到达首都的最少油耗 - 力扣(LeetCode)

思路:贪心,用dfs来搜索遍历,计算每条边的贡献值即可

class Solution {
private:
public:
    typedef long long LL;
    int seats;
    vector<vector<int>> edges;
    vector<bool> b;
    vector<LL> cnt;
    vector<LL> vres;
    //每个节点向父节点返回(val/seats + (val%seats!=0))
    long long minimumFuelCost(vector<vector<int>>& roads, int seats){
        this->seats = seats;
        int n = roads.size();
        edges.resize(n+1);
        b.resize(n+1);
        vres.resize(n+1);
        cnt.resize(n+1);
        for(auto &i : cnt) i = 1;
        //构建无向图
        for(int i = 0; i < n; i ++)
        {
            int x = roads[i][0], y = roads[i][1];
            edges[x].push_back(y);
            edges[y].push_back(x);
        }
        return dfs(0); //0作为父节点,他的子节点返回的值
    }
    
    LL dfs(int u)
    {
        if (b[u]) return 0;
        b[u] = true;
        LL res = 0;
        for (int i : edges[u])
        {
            if(b[i]) continue;
            dfs(i);
            cnt[u] += cnt[i];
            res += cnt[i]/seats + (cnt[i]%seats!=0);
            res += vres[i];
        }
        vres[u] += res;
        // cout << u << ' ' << cnt[u]  << ' ' << res << endl;
        return vres[u];
    }
};
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值