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;

}

};

相关推荐
无极低码2 小时前
ecGlypher新手安装分步指南(标准化流程)
人工智能·算法·自然语言处理·大模型·rag
软件算法开发2 小时前
基于海象优化算法的LSTM网络模型(WOA-LSTM)的一维时间序列预测matlab仿真
算法·matlab·lstm·一维时间序列预测·woa-lstm·海象优化
superior tigre3 小时前
22 括号生成
算法·深度优先
努力也学不会java4 小时前
【缓存算法】一篇文章带你彻底搞懂面试高频题LRU/LFU
java·数据结构·人工智能·算法·缓存·面试
旖-旎4 小时前
二分查找(x的平方根)(4)
c++·算法·二分查找·力扣·双指针
ECT-OS-JiuHuaShan5 小时前
朱梁万有递归元定理,重构《易经》
算法·重构
智者知已应修善业5 小时前
【51单片机独立按键控制数码管移动反向,2片74CH573/74CH273段和位,按键按下保持原状态】2023-3-25
经验分享·笔记·单片机·嵌入式硬件·算法·51单片机
khddvbe6 小时前
C++并发编程中的死锁避免
开发语言·c++·算法
C羊驼6 小时前
C语言:两天打鱼,三天晒网
c语言·经验分享·笔记·算法·青少年编程
菜菜小狗的学习笔记6 小时前
剑指Offer算法题(四)链表
数据结构·算法·链表