string|st|擂台贪心

lc2007

枚举右 维护左+sort贪心

class Solution {

public:

vector<int> findOriginalArray(vector<int>& changed)

{

int n=changed.size();

if(n%2) return {};

unordered_map<int,int> hash;

vector<int> ret;

//枚举右 维护左+sort贪心

sort(changed.begin(),changed.end());

for(int i=0;i<n;i++)

{

int t=changed[i];

if(t%2==0 && hash[t/2])

{

hash[t/2]--;

ret.push_back(t/2);

}

else

hash[t]++;

}

int m=ret.size();

if(m==(n/2))

return ret;

return {};

}

};

lc3175

微调1535.

显然索引队列模拟会超时,要擂台贪心

class Solution {

public:

int findWinningPlayer(vector<int>& skills, int k)

{

int n=skills.size();

int mx=0,win=0;

for(int i=1;i<n;i++)

{

if(skills[i]>skills[mx])

{

win=1;//重置

mx=i;

}

else

win++;

if(win==k) return mx;

}

return mx;

}

};

没事干可以模拟写一下暴力队列

class Solution {

public:

int findWinningPlayer(vector<int>& skills, int k) {

int n = skills.size();

queue<int> q;

for (int i = 0; i < n; i++) q.push(i); // 入队索引

int cnt = 0;

int prev = q.front();

q.pop();

while (1)

{

int curr = q.front();

q.pop();

if (skills[prev] > skills[curr])

{ // 前胜者赢

cnt++;

q.push(curr); // 败者去尾

}

else

{

++cnt = 1;++

++q.push(prev);
prev = curr; //update
++

}

if (cnt == k) return prev;

}

}

};

lc1599

模拟摩天轮每轮接新游客、最多4人登舱,算每轮利润,记下利润最大时的最少轮转次数,没正利润就返回-1

懒的喷题目描述..

翻译:经营4舱摩天轮,每次轮转前会来新游客,每舱最多坐4人,游客登舱付费、轮转要花成本;可随时停运(停运后免费转完让游客落地)。求"利润最大时的最少轮转次数",没正利润则返回-1。

变量设计:

  1. 等待游客数 :记录当前没登上摩天轮的游客量;

  2. 各舱游客数 :用数组(长度4)存每个座舱的游客数(轮转时更新)

  3. 总利润 :累计(登舱人数×登舱成本) - (轮转次数×运行成本);

  4. 最大利润 + 对应最少轮转次数 :实时记录利润峰值及首次达到该值的轮转次数

class Solution {

public:

int minOperationsMaxProfit(vector<int>& customers, int boardingCost, int runningCost)

{

int wait = 0;

int profit = 0;

int max_profit = 0;

int res = -1;

int rotate = 0; // 当前轮转次数

int idx = 0;

// 模拟轮转过程:直到没有等待游客且所有新游客都已处理

while (wait > 0 || idx < customers.size()) {

// 1. 处理新到达的游客(当前轮转前到达)

if (idx < customers.size())

wait += customers[idx++];

// 2. 本轮登舱的游客数(最多4人)

int board = min(wait, 4);

wait -= board;

profit += board * boardingCost - runningCost; // 计算本轮利润(登舱收入 - 轮转成本)

rotate++; // 轮转次数+1

// 3. 更新

if (profit > max_profit) {

max_profit = profit;

res = rotate;

}

}

return max_profit > 0 ? res : -1;

}

};

lc1003

创建栈

class Solution {

/*

输入:s = "aabcbc"

输出:true

解释:

"" -> "abc" -> "aabcbc"

因此,"aabcbc" 有效

*/

public:

bool isValid(string s)

{

stack<char> st;

bool ok=false;

for(auto& c:s)

{

if(c=='c')

{

if(!st.empty() && st.top()=='b')

{

st.pop();

if(!st.empty() && st.top()=='a')

{

st.pop();

ok=true;

}

else

st.push('b');

}

}

if(!ok)

st.push(c);

ok=false;

}

return st.empty()?true:false;

}

};

数组模拟

用原字符串当栈,遍历字符,检查是否能按"a→b→c"顺序匹配,++匹配到c就消去前两个(a、b)++

class Solution {

public:

bool isValid(string s) { // s 同时作为栈

int i = 0; // i-1 表示栈顶下标,i=0 表示栈为空

for (char c: s) {

if (c > 'a' && (i == 0 || c - s[--i] != 1))

return false;

if (c < 'c')

s[i++] = c; // 入栈

}

return i == 0;

}

};

相关推荐
颜酱7 小时前
单调栈:从模板到实战
javascript·后端·算法
CoovallyAIHub10 小时前
仿生学突破:SILD模型如何让无人机在电力线迷宫中发现“隐形威胁”
深度学习·算法·计算机视觉
CoovallyAIHub10 小时前
从春晚机器人到零样本革命:YOLO26-Pose姿态估计实战指南
深度学习·算法·计算机视觉
CoovallyAIHub10 小时前
Le-DETR:省80%预训练数据,这个实时检测Transformer刷新SOTA|Georgia Tech & 北交大
深度学习·算法·计算机视觉
CoovallyAIHub11 小时前
强化学习凭什么比监督学习更聪明?RL的“聪明”并非来自算法,而是因为它学会了“挑食”
深度学习·算法·计算机视觉
CoovallyAIHub11 小时前
YOLO-IOD深度解析:打破实时增量目标检测的三重知识冲突
深度学习·算法·计算机视觉
NAGNIP1 天前
轻松搞懂全连接神经网络结构!
人工智能·算法·面试
NAGNIP1 天前
一文搞懂激活函数!
算法·面试
董董灿是个攻城狮1 天前
AI 视觉连载7:传统 CV 之高斯滤波实战
算法