当前位置:网站首页>Li Kou daily question - day 25 -496 Next larger element I
Li Kou daily question - day 25 -496 Next larger element I
2022-06-24 21:45:00 【Chongyou research Sen】
2022.6.23 Did you brush the questions today ?
subject :
nums1 Middle number x Of Next bigger element Refer to x stay nums2 Corresponding position in On the right side Of first Than x Big elements .
Here are two for you There are no repeating elements Array of nums1 and nums2 , Subscript from 0 Start counting , among nums1 yes nums2 Subset .
For each 0 <= i < nums1.length , Find satisfaction nums1[i] == nums2[j] The subscript j , And in nums2 determine nums2[j] Of Next bigger element . If there is no next larger element , Then the answer to this query is -1 .
Returns a length of nums1.length Array of ans As the answer , Satisfy ans[i] As mentioned above Next bigger element
analysis :
Given two arrays , for example
num1【1,2,3】,num2【1,2,4,3】, be num1 Of 【1】 Corresponding num2 Of 【2】,num1 Of 2 Corresponding num2 Of 【4】,num1 Of 【3】 No corresponding , Then return 【2,4,-1】
That is, for the elements in array one , We're going to be in the array 2 Find out whether there is an element that satisfies the following conditions
1. stay num2 The next element must be the first element to the right of the element
2. And the element is larger than the element in array one
The idea is to solve by violence , Traversing two arrays , Look for... In the first layer of circulation num1 Value , See if there is an element in the second layer of the loop that meets this requirement .
analysis :
1. Violent solution
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
vector<int>vec;
int flag = 0;
for (auto i = 0; i < nums1.size(); ++i)
{
for (auto j = 0; j < nums2.size(); ++j)
{
if (nums1[i] == nums2[j])
{
flag = 1;
}
if (flag == 1&&nums1[i]<nums2[j])
{
vec.push_back(nums2[j]);
flag = 0;
break;
}
if (flag == 1 && nums1[i] >= nums2[j]&&j==nums2.size()-1)
{
vec.push_back(-1);
flag = 0;
break;
}
}
}
return vec;
}
};2. Monotonic stack
This class of methods is used to solve the larger type of the next element in the array .
The idea is to use a stack to maintain a stack arranged from small to large , Then insert the data into map in , And then map Insert vector in ( Personally, it is still very difficult ). If a larger value is encountered during stack insertion , It's about to go out of the stack , This method needs more practice !
class Solution {
public:
vector<int> nextGreaterElement(vector<int>& nums1, vector<int>& nums2) {
unordered_map<int, int> hashmap;
stack<int> st;
for (int i = nums2.size() - 1; i >= 0; --i)
{
int num = nums2[i];
while (!st.empty() && num >= st.top())
{
st.pop();
}
hashmap[num] = st.empty() ? -1 : st.top();
st.push(num);
}
vector<int>res(nums1.size());
for (auto i = 0; i < nums1.size(); ++i)
{
res[i] = hashmap[nums1[i]];
}
return res;
}
};边栏推荐
- memcached全面剖析–2. 理解memcached的內存存儲
- Blender's simple skills - array, rotation, array and curve
- 【Camera基础(二)】摄像头驱动原理和开发&&V4L2子系统驱动架构
- 基于ASP.NET开发的固定资产管理系统源码 企业固定资产管理系统源码
- ping: www.baidu. Com: unknown name or service
- 188. the best time to buy and sell stocks IV
- VirtualBox虚拟机安装Win10企业版
- 188. 买卖股票的最佳时机 IV
- Ebpf XDP mount point analysis
- Blender's landscape
猜你喜欢

EditText controls the soft keyboard to search

基于STM32的物联网下智能化养鱼鱼缸控制控制系统

AntDB数据库在线培训开课啦!更灵活、更专业、更丰富

XTransfer技术新人进阶秘诀:不可错过的宝藏Mentor

Multi task model of recommended model: esmm, MMOE

为什么生命科学企业都在陆续上云?

一文理解OpenStack网络

Oauth2.0 introduction

Failed to open after installing Charles without any prompt

【产品设计研发协作工具】上海道宁为您提供蓝湖介绍、下载、试用、教程
随机推荐
CondaValueError: The target prefix is the base prefix. Aborting.
Tutorial on obtaining JD cookies by mobile browser
RFC 793 why to send reset and when to send reset
TCP specifies the source port
Tdengine can read and write through dataX
Debugging Analysis of Kernel panics and Kernel oopses using System Map
Pattern recognition - 0 introduction
力扣每日一题-第25天-496.下一个更大元素Ⅰ
多线程收尾
Memcached comprehensive analysis – 2 Understand memcached memory storage
Page replacement of virtual memory paging mechanism
2022 international women engineers' Day: Dyson design award shows women's design strength
Address mapping of virtual memory paging mechanism
Data link layer & some other protocols or technologies
Concepts of kubernetes components
Functional analysis of ebpf tracepoint
基于ASP.NET开发的固定资产管理系统源码 企业固定资产管理系统源码
Pattern recognition - 1 Bayesian decision theory_ P1
Blender FAQs
TCP_ Nodelay and TCP_ CORK