栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

leetcode第 295 场周赛

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

leetcode第 295 场周赛

文章目录
  • 前言
  • 6078. 重排字符形成目标字符串
    • 题目详情
    • 解题代码
  • 6079. 价格减免
    • 题目描述
    • 解题代码
  • 6080. 使数组按非递减顺序排列
    • 题目详情
    • 解题代码
  • 6081. 到达角落需要移除障碍物的最小数目
    • 题目详情
    • 解题代码


前言

这一次卡在了第二题上了w_w


6078. 重排字符形成目标字符串 题目详情

解题代码

短板效应

class Solution {
public:
    int st[100],cnt[100];
    int rearrangeCharacters(string s, string target) {
        for(int i=0;i
            int t=s[i]-'a';
            st[t]++;
        }
        int minv=1000000;
        for(int i=0;i
            int t=target[i]-'a';
            cnt[t]++;
        }
        for(int i=0;i
            int t=target[i]-'a';
            minv = min(st[t]/cnt[t],minv);
        }
        
        return minv;
    }
};
6079. 价格减免 题目描述

解题代码

单纯的模拟题,double转string可以用sprintf,结尾的情况需要特判

class Solution {
public:
    int check(string s,int t,long long &res)
    {
        int i=1;
        while(t+i
            if(s[t+i]==' ' ) return i;
            if(s[t+i]<'0' || s[t+i]>'9') return 0;
            res=res*10+(s[t+i]-'0');
            i++;
        }
        return -1;
    }
    string discountPrices(string sentence, int discount) {
        string &s=sentence;
        string t;
        int cnt=0;
        for(int i=0;i
            t+=s[i];
            if(s[i]=='$' &&(i==0 ||s[i-1]==' '))
            {
                long long res=0;
                int d=check(s,i,res);
                double ans;
                if(d>1 || d==-1) 
                {
                    
                    ans=(double)res*((100-discount)/100.0);
                    char str[20];
                    sprintf(str,"%.2f",ans);
                    if((d==-1 && res!=0) || d>1) 
                    {
                        t+=str;
                    }
                    // cout << d <<"--"<< res<0) i+=d;
                if(d==-1) break;
            }
        }
        puts("");
        return t;
    }
};
6080. 使数组按非递减顺序排列 题目详情

解题代码

一个单调栈问题

class Solution {
public:
    int totalSteps(vector& nums) {
        int n=nums.size();
        stack stk;
        int ans=0;

        for(int i=0;i
            int tmax=0;
            while(!stk.empty() && stk.top().first<=nums[i]) 
            {
                tmax=max(tmax,stk.top().second);
                stk.pop();
            }
            if(!stk.empty()) ++tmax;
            ans=max(ans,tmax);
            stk.push({nums[i],tmax});
            
        }
        return ans;     
    }
};
6081. 到达角落需要移除障碍物的最小数目 题目详情

解题代码

直接用dijkstra(每一次用最小的边跟新其他边)即可

class Solution {
public:
    typedef pair PII ;
    typedef pair PIP ;
    int minimumObstacles(vector>& g) {
        int dx[4]={0,0,-1,1},dy[4]={1,-1,0,0};
        int n=g.size(),m=g[0].size();
        int dist[n][m];
        memset(dist,0x3f,sizeof dist);
        priority_queue,greater> h;
        dist[0][0]=0;
        h.push({0,{0,0}});

        while(h.size())
        {
            auto t=h.top().second;
            h.pop();
            auto x=t.first,y=t.second;
            for(int i=0;i<4;i++)
            {
                int nx=dx[i]+x,ny=dy[i]+y;
                if(nx < 0 || nx >=n || ny < 0 || ny >= m) continue;
                if(dist[nx][ny]>dist[x][y]+g[nx][ny])
                {
                    dist[nx][ny]=dist[x][y]+g[nx][ny];
                    h.push({g[nx][ny],{nx,ny}});
                }
            }
        }
        return dist[n-1][m-1];
    }
};
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/934498.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号