当前位置: 首页 > news >正文

27.贪心算法5

合并区间

class Solution {
public:
    static bool cmp(const vector<int> & a,const vector<int> & b){
        return a[0]<b[0];
    }
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        sort(intervals.begin(),intervals.end(),cmp);
        vector<int> tmp=intervals[0];
        vector<vector<int>> res;
        int n=intervals.size();
        for(int i=1;i<n;i++){
            if(tmp[1]>=intervals[i][0]){
                tmp[1]=tmp[1]>intervals[i][1]?tmp[1]:intervals[i][1];
            }else{
                res.push_back(tmp);
                tmp=intervals[i];
            }
        }
        res.push_back(tmp);
        return res;
    }
};

和分割字符串那个一样

单调递增的数字

class Solution {
public:
    int monotoneIncreasingDigits(int k) {
        string s=to_string(k);
        int index=-1;
        int n=s.size();
        for(int i=0;i<n-1;i++){
            if(s[i]>s[i+1]){
                s[i]--;
                index=i+1;
                while(index>1&&s[index-1]<s[index-2]){
                    index--;
                    s[index-1]--;
                }
                break;
            }
        }
        for(int i=index;i>=0&&i<n;i++){
            s[i]='9';
        }
        int res=stoi(s);
        return res;
    }
};

监控二叉树

class Solution {
public:
    
    int minCameraCover(TreeNode* root) {
    unordered_map<TreeNode* ,int> mymap;
    stack<TreeNode*> sta;
    sta.push(root);
    TreeNode* cur=root;
    int count=0;
    while(!sta.empty()){
        cur=sta.top();
        sta.pop();
        if(cur){
            sta.push(cur);
            sta.push(nullptr);
            if(cur->left){
                sta.push(cur->left);
            }
            if(cur->right){
                sta.push(cur->right);
            }
        }else{
            cur=sta.top();
            sta.pop();
            bool put=0;
            if(cur->right){
                if(mymap[cur->right]==0){
                    put=1;
                }else if(mymap[cur->right]==2){
                    mymap[cur]=1;
                }
            }
            if(cur->left){
                if(mymap[cur->left]==0){
                    put=1;
                }else if(mymap[cur->left]==2){
                    mymap[cur]=1;
                }
            }
            if(put){
                mymap[cur]=2;
                count++;
                mymap[cur->left]=mymap[cur->right]=1;
            }else if(cur==root&&!mymap[cur]){
                mymap[cur]=2;
                count++;
            }
        }
    }    
        return count;
    }
};

相关文章:

  • DeepSeek开源周,第四弹再次来袭,优化并行策略
  • cesium 解决加载带动画的glb不播放动画问题
  • 记录MFC联合halcon界面显示开发
  • 查询NFT图片地址
  • 42 session反序列化漏洞
  • JavaWeb25.02.27
  • 【前端】XML,XPATH,与HTML的关系
  • 推荐算法工程师的技术图谱和学习路径
  • 实验环境搭建集锦(docker linux ros2+强化学习环境+linux上单片机串口调试)
  • Kylin麒麟操作系统服务部署 | Nginx服务部署
  • BRD4缺失通过GRP78灭活内质网应激,延缓脱氢表雄酮诱导的卵巢颗粒细胞凋亡
  • 波导阵列天线 学习笔记11双极化全金属垂直公共馈电平板波导槽阵列天线
  • 【技术笔记】Cadence 实现原理图元器件的自动标号
  • [教程]在CentStream 9简单部署一个Nginx web服务器
  • 基于多层感知机(MLP)实现MNIST手写体识别
  • SpringBoot Maven 项目 pom 中的 plugin 插件用法整理
  • [高等数学] 定积分的概念与性质
  • 《从入门到精通:蓝桥杯编程大赛知识点全攻略》(十八)-农夫约翰的奶酪块、蛋糕游戏、奶牛体检
  • linux 后台执行并输出日志
  • Filp动画
  • 新疆自治区住房建设厅网站/网络营销计划书怎么写
  • 青岛日文网站制作/网站制作公司排名
  • 网站数据库空间/制作网站的基本步骤
  • 安徽天筑建设集团网站/刷死粉网站推广
  • wordpress搭建官网视频/陕西seo主管
  • 设计网站公司哪里好/项目外包平台