LeetCode 第 295 场周赛

总结

总结就是自己太菜了哇,单调栈要去多练一练,很多时候单调栈可能不是简单维护最值了。第二题主要考察了库函数,第三题就是单调栈的简单运用叭,赛后感觉确实不难,但是不知道为什么会被比赛时的自己蠢哭~呜呜呜

6079. 价格减免

对库函数不太熟叭hh,不过正好学到了。因为没有特判然后就又寄了一发

class Solution {
public:
    long long check(string s) {
        if (s.empty()) return false;
        for (auto & c : s)
            if (!isdigit(c))
                return false;
        return stoll(s);
    }
    string discountPrices(string s, int d) {
        stringstream ss(s);
        string str;
        string ans;
        while (ss >> str) {
            ans += " ";
            if (str[0] != '$') {
                ans += str;
                continue;
            } else {
                long long t = check(str.substr(1));
                if (t) {
                    char c[20] = {};
                    cout << t << endl;
                    sprintf(c, "$%.2lf", (double)t * 1.0 * (100 - d) / 100);
                    ans += c;
                }
                else ans += str;
            }
        }
    return ans.substr(1);
    }
};

6080. 使数组按非递减顺序排列

class Solution {
public:
    int totalSteps(vector<int> &nums) {
        int ans = 0;
        stack<pair<int, int>> st;
        for (int num : nums) {
            int maxT = 0;
            while (!st.empty() && st.top().first <= num) {
                maxT = max(maxT, st.top().second);
                st.pop();
            }
            if (!st.empty()) ++maxT;
            ans = max(ans, maxT);
            st.emplace(num, maxT);
        }
        return ans;
    }
};

6081. 到达角落需要移除障碍物的最小数目

// shiran
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, n, a) for (int i = n - 1; i >= a; i--)
#define sz(x) (int)size(x)
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define pb push_back
#define mk make_mair
typedef long long ll;
typedef pair<int, int> PII;
const int INF = 1e9+7;
const int N = 200010, M = 300010;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

struct Node
{
    int x, y, d;
    bool operator< (const Node &t) const
    {
        return d > t.d;
    }
};

class Solution {
public:
    int minimumObstacles(vector<vector<int>>& grid) {
        int n = grid.size(), m = grid[0].size();
        vector<vector<int>> f(n, vector<int>(m, INF));
        priority_queue<Node> q;
        q.push({0, 0, 0});
        f[0][0] = 0;
        
        while (q.size())
        {
            auto t = q.top(); q.pop();
            
            if (t.x == n - 1 && t.y == m - 1) break;
            
            rep(i, 0, 4)
            {
                int x = t.x + dx[i], y = t.y + dy[i];
                if (x < 0 || x >= n || y < 0 || y >= m) continue;
                if (f[x][y] > f[t.x][t.y] + grid[x][y])
                {
                    f[x][y] = f[t.x][t.y] + grid[x][y];
                    q.push({x, y, f[x][y]});
                }
            }
        }
        return f[n - 1][m - 1];
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Shirandexiaowo

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

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

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

打赏作者

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

抵扣说明:

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

余额充值