当前位置:网站首页>leetcode:单调栈结构(进阶)
leetcode:单调栈结构(进阶)
2022-06-28 03:06:00 【OceanStar的学习笔记】
题目来源
题目描述


题目解析
#include <vector>
#include <iostream>
std::vector<int> arr(1000000);
std::vector<std::vector<int>> ans(1000000, std::vector<int>(2));
// stack1 : 相等值的位置也放
// stack2 : 只放不相等值的最后一个位置
// 比如 : arr = { 3, 3, 3, 4, 4, 6, 6, 6}
// 位置 0 1 2 3 4 5 6 7
// 如果位置依次压栈,
// stack1中的记录是(位置) : 0 1 2 3 4 5 6 7
// stack1中的记录是(位置) : 2 4 7
std::vector<int> stack1(1000000);
std::vector<int> stack2(1000000);
void getNearLess(int n){
int stackSize1 = 0;
int stackSize2 = 0;
for (int i = 0; i < n; ++i) {
while (stackSize1 > 0 && arr[stack1[stackSize1 - 1]] > arr[i]) {
int curIndex = stack1[--stackSize1];
int left = stackSize2 < 2 ? -1 : stack2[stackSize2 - 2];
ans[curIndex][0] = left;
ans[curIndex][1] = i;
if (stackSize1 == 0 || arr[stack1[stackSize1 - 1]] != arr[curIndex]) {
stackSize2--;
}
}
if (stackSize1 != 0 && arr[stack1[stackSize1 - 1]] == arr[i]) {
stack2[stackSize2 - 1] = i;
} else {
stack2[stackSize2++] = i;
}
stack1[stackSize1++] = i;
}
while (stackSize1 != 0) {
int curIndex = stack1[--stackSize1];
int left = stackSize2 < 2 ? -1 : stack2[stackSize2 - 2];
ans[curIndex][0] = left;
ans[curIndex][1] = -1;
if (stackSize1 == 0 || arr[stack1[stackSize1 - 1]] != arr[curIndex]) {
stackSize2--;
}
}
}
int main()
{
int n ;
std::cin >> n;
for (int i = 0; i < n; ++i) {
std::cin >> arr[i];
}
getNearLess(n);
std::string builder;
for (int i = 0; i < n; ++i) {
builder = builder + std::to_string(ans[i][0]) + " " + std::to_string(ans[i][1]) + "\n";
}
std::cout << builder << "\n";
return 0;
}
边栏推荐
- 小程序image组件不显示图片?
- 启牛商学院赠送证券账户是真的吗?开户到底安不安全呢
- 图片的懒加载和预加载
- 第九章 APP项目测试(3) 测试工具
- composition api在项目中的使用总结
- 可扩展存储系统(上)
- How does the open-ended Hall current sensor help the transformation of DC power distribution?
- 可扩展数据库(下)
- Win10 如何删除系统盘大文件hiberfil.sys
- django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3
猜你喜欢

数据库系列之MySQL和TiDB中慢日志分析

Analysis of slow logs in MySQL and tidb of database Series

【PaddleDetection】ModuleNotFoundError: No module named ‘paddle‘

Win 10出现bitlocke恢复,蓝屏错误代码0x1600007e

Extensible database (I)

Self use tool unity video player that monkeys can use

Etcd database source code analysis -- network layer server rafthandler between clusters

STM32 peripheral SDIO and SD card configuration

A pit filling trip based on LNMP to build a personal website

第九章 APP项目测试(3) 测试工具
随机推荐
The same is MB. Why is the gap so large?
局域网内共享打印机的几种方式
文档问题
数据库
WebSocket(简单体验版)
事件委托的原理
SSH框架的搭建(下)
Learning - useful resources
nn. Parameter and torch nn. Init series of functions to initialize model parameters
Lost connection repair: make "hide and seek" nowhere to hide
数组的方法
数据库的迁移
Custom controls under WPF and adaption of controls in Grid
View the SQL execution plan according to explain and optimize the SQL
Self use tool unity video player that monkeys can use
【PaddleDetection】ModuleNotFoundError: No module named ‘paddle‘
Introduction to kubernetes resource object and common commands
电子地图坐标系统研究整理
JVM一:JVM入门以及Class文件认识
Database migration