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;

}

};

相关推荐
shangjian0072 分钟前
AI大模型-机器学习-算法-线性回归
人工智能·算法·机器学习
mjhcsp16 分钟前
C++ KMP 算法:原理、实现与应用全解析
java·c++·算法·kmp
lizhongxuan17 分钟前
Manus: 上下文工程的最佳实践
算法·架构
CS创新实验室29 分钟前
《计算机网络》深入学:海明距离与海明码
计算机网络·算法·海明距离·海明编码
WW_千谷山4_sch32 分钟前
MYOJ_10599:CSP初赛题单10:计算机网络
c++·计算机网络·算法
YuTaoShao1 小时前
【LeetCode 每日一题】1458. 两个子序列的最大点积——(解法三)状态压缩
算法·leetcode·职场和发展
位东风1 小时前
希尔排序(Shell Sort)详解
算法·排序算法
AI科技星1 小时前
光速飞行器动力学方程的第一性原理推导、验证与范式革命
数据结构·人工智能·线性代数·算法·机器学习·概率论
橘颂TA1 小时前
【剑斩OFFER】算法的暴力美学——leetCode 946 题:验证栈序列
c++·算法·leetcode·职场和发展·结构与算法