dfs|bfs|定长栈|栈+双指针

lc1574

用栈可以维护单增序列,但是怎么样去找这个删除的最短子序列呢

  • 找最长前缀非递减序列、最长后缀非递减序列
  • 再用双指针找二者最优拼接点(因为想最小可能的删除,所以存在双指针的单调性)

class Solution {

public:

int findLengthOfShortestSubarray(vector<int>& arr) {

int n = arr.size();

if (n <= 1) return 0;

// 步骤1:找最长前缀非递减序列的结束位置left

int left = 0;

while (left + 1 < n && arrleft <= arrleft + 1) {

left++;

}

if (left == n - 1) return 0; // 整个数组已非递减

// 步骤2:找最长后缀非递减序列的起始位置right

int right = n - 1;

while (right - 1 >= 0 && arrright - 1 <= arrright) {

right--;

}

// 步骤3:初始候选答案(只保留前缀或只保留后缀的情况)

int ret = min(n - left - 1, right);

// 步骤4:双指针找前缀和后缀的最优拼接点

int i = 0, j = right;

while (i <= left && j < n) {

++if (arri <= arrj) {
// 可以拼接,更新答案
ret = min(ret, j - i - 1);
++

i++;

} ++else
j++;
++

}

return ret;

}

};

lc1673

定长栈

+剩余数的条件三判断 保障了k长

while (!st.empty() && x < st.back() && st.size() + nums.size() - i > k) st.pop_back();

class Solution {

public:

vector<int> mostCompetitive(vector<int>& nums, int k) {

vector<int> st;

for (int i = 0; i < nums.size(); i++) {

int x = numsi;

while (!st.empty() && x < st.back() && ++st.size() + nums.size() - i > k) {
st.pop_back();
++

}

++if (st.size() < k)
st.push_back(x);
++

}

return st;

}

};

lc865

(DFS)计算节点深度

若左右子树深度相同,当前节点即为最深叶子的最近公共祖先(LCA);

否则取深度更深的子树的LCA

pair<TreeNode*,int> dfs(TreeNode* root,int depth)

亮点:pair dfs

出口!

auto left记录下一层拿到的结果,再去进行return if-else比较,确定return what

class Solution {

public:

TreeNode* subtreeWithAllDeepest(TreeNode* root){

return dfs(root,0).first;

}

//还和深度有关 维护一个pair

pair<TreeNode*,int> dfs(TreeNode* root,int depth)

{

//出口 空节点

if(!root)

return {nullptr,depth-1};

auto left=dfs(root->left,depth+1);

//1 catch return left

auto right=dfs(root->right,depth+1);

//返回控制

if(!root->left && !root->right)

return {root,depth};

// 比较左右子树深度

++if(left.second == right.second)++

++return {root, left.second};++

++//++2 battle = return root,dep

else if(left.second > right.second)

return left;

//3 !=已完成 继承return longer

else

return right;

}

};

lcr44

class Solution {

public:

vector<int> largestValues(TreeNode* root)

{

if(!root) return {};

vector<int> ret;

queue<TreeNode*> q;

q.push(root);

while(q.size())

{

int sz=q.size();

int mx=INT_MIN;

while(sz--)

{

auto t=q.front();

q.pop();

mx=max(mx,t->val);

if(t->left)

q.push(t->left);

if(t->right)

q.push(t->right);

}

ret.push_back(mx);

}

return ret;

}

};

相关推荐
ZhengEnCi1 小时前
Agent 架构怎么选:从单 Agent 到多 Agent 编排的选型决策指南
算法·架构
秋天的怀念_David1 小时前
第二十二夜 · 数字迷雾
java·程序人生·算法
Pniubi2 小时前
力扣283移动零(双指针法)
算法·leetcode·职场和发展
hhb_6182 小时前
大模型落地三重优化实战
人工智能·算法·机器学习
云和数据.ChenGuang2 小时前
光哥说Python RAG 系统需要哪些评估指标:从检索到生成的全链路评测指南
网络·人工智能·python·算法·langchain
2601_970703834 小时前
工业除湿机租赁:把一份租金合同拆开看
大数据·数据结构·算法·数据库架构·推荐算法
心中有你02144 小时前
Java实现Dijkstra最短路径算法(邻接表 + 优先队列)
java·数据库·算法
ZhengEnCi5 小时前
同样的模型,为什么本地和云 API 差别那么大:量化精度、聊天模板与采样参数的三大元凶
人工智能·算法·llm
Flynn Liang5 小时前
RAG 答非所问/幻觉怎么破?Spring AI QuestionAnswerAdvisor 召回优化实战(TopK/重排/混合检索)
人工智能·算法·spring·rag·幻觉·springai
liminla!5 小时前
SQL 从入门到进阶:语法速查与实战练习(完整优化版)
数据库·sql·算法