Leetcode3168. 候诊室中的最少椅子数

Every day a Leetcode

题目来源:3168. 候诊室中的最少椅子数

解法1:模拟

代码:

c 复制代码
/*
 * @lc app=leetcode.cn id=3168 lang=cpp
 *
 * [3168] 候诊室中的最少椅子数
 */

// @lc code=start
class Solution
{
public:
    int minimumChairs(string s)
    {
        int chair = 0;
        int max_chair = INT_MIN;
        for (char &c : s)
        {
            if (c == 'E')
            {
                chair++;
                if (chair > max_chair)
                    max_chair = chair;
            }
            else
                chair--;
        }
        return max_chair;
    }
};
// @lc code=end

结果:

复杂度分析:

时间复杂度:O(n),其中 n 是字符串 s 的长度。

空间复杂度:O(1)。

相关推荐
感哥2 小时前
C++ 多态
c++
沐怡旸9 小时前
【底层机制】std::string 解决的痛点?是什么?怎么实现的?怎么正确用?
c++·面试
River41612 小时前
Javer 学 c++(十三):引用篇
c++·后端
感哥14 小时前
C++ std::set
c++
Fanxt_Ja15 小时前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
侃侃_天下15 小时前
最终的信号类
开发语言·c++·算法
博笙困了15 小时前
AcWing学习——差分
c++·算法
青草地溪水旁16 小时前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(2)
c++·设计模式·抽象工厂模式
青草地溪水旁16 小时前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(1)
c++·设计模式·抽象工厂模式
感哥16 小时前
C++ std::vector
c++