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;

}

};

相关推荐
C++ 老炮儿的技术栈12 小时前
Qt Creator中不写代如何设置 QLabel的颜色
c语言·开发语言·c++·qt·算法
子春一13 小时前
Flutter for OpenHarmony:构建一个 Flutter 数字消消乐游戏,深入解析网格状态管理、合并算法与重力系统
算法·flutter·游戏
草履虫建模19 小时前
力扣算法 1768. 交替合并字符串
java·开发语言·算法·leetcode·职场和发展·idea·基础
naruto_lnq21 小时前
分布式系统安全通信
开发语言·c++·算法
Jasmine_llq21 小时前
《P3157 [CQOI2011] 动态逆序对》
算法·cdq 分治·动态问题静态化+双向偏序统计·树状数组(高效统计元素大小关系·排序算法(预处理偏序和时间戳)·前缀和(合并单个贡献为总逆序对·动态问题静态化
爱吃rabbit的mq1 天前
第09章:随机森林:集成学习的威力
算法·随机森林·集成学习
(❁´◡`❁)Jimmy(❁´◡`❁)1 天前
Exgcd 学习笔记
笔记·学习·算法
YYuCChi1 天前
代码随想录算法训练营第三十七天 | 52.携带研究材料(卡码网)、518.零钱兑换||、377.组合总和IV、57.爬楼梯(卡码网)
算法·动态规划
不能隔夜的咖喱1 天前
牛客网刷题(2)
java·开发语言·算法
VT.馒头1 天前
【力扣】2721. 并行执行异步函数
前端·javascript·算法·leetcode·typescript