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;

}

};

相关推荐
im_AMBER3 小时前
Leetcode 74 K 和数对的最大数目
数据结构·笔记·学习·算法·leetcode
t198751284 小时前
电力系统经典节点系统潮流计算MATLAB实现
人工智能·算法·matlab
断剑zou天涯4 小时前
【算法笔记】蓄水池算法
笔记·算法
长安er4 小时前
LeetCode 206/92/25 链表翻转问题-“盒子-标签-纸条模型”
java·数据结构·算法·leetcode·链表·链表翻转
唯道行4 小时前
计算机图形学·23 Weiler-Athenton多边形裁剪算法
算法·计算机视觉·几何学·计算机图形学·opengl
CoderYanger4 小时前
动态规划算法-01背包问题:50.分割等和子集
java·算法·leetcode·动态规划·1024程序员节
花月C4 小时前
个性化推荐:基于用户的协同过滤算法
开发语言·后端·算法·近邻算法
lxh01135 小时前
最长递增子序列
前端·数据结构·算法
Youyzq5 小时前
前端项目发布到cdn上css被编译失效问题rgba失效和rgb失效
前端·css·算法·cdn
风筝在晴天搁浅6 小时前
代码随想录 516.最长回文子序列
算法