当前位置:网站首页>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;
}
};边栏推荐
- Sslhandshakeexception: no subject alternative names present - sslhandshakeexception: no subject alternative names present
- 【Camera基础(二)】摄像头驱动原理和开发&&V4L2子系统驱动架构
- memcached全面剖析–3. memcached的删除机制和发展方向
- B站带货当学新东方
- socket(2)
- Distributed basic concepts
- Golang reflection operation collation
- 一文理解OpenStack网络
- C语言实现DNS请求器
- EditText controls the soft keyboard to search
猜你喜欢
![[product design and R & D collaboration tool] Shanghai daoning provides you with blue lake introduction, download, trial and tutorial](/img/0f/e0b261496d04ca3da8a7d7d19e5bf1.png)
[product design and R & D collaboration tool] Shanghai daoning provides you with blue lake introduction, download, trial and tutorial

去掉录屏提醒(七牛云demo)

虚拟货币7个月蒸发2万亿美元,“马斯克们”终结15万人暴富梦

(待补充)GAMES101作业7提高-实现微表面模型你需要了解的知识

leetcode_191_2021-10-15

Football information query system based on C language course report + project source code + demo ppt+ project screenshot

EditText controls the soft keyboard to search

2022国际女性工程师日:戴森设计大奖彰显女性设计实力

力扣每日一题-第26天-496.下一个更大元素Ⅰ

Auto. JS to automatically authorize screen capture permission
随机推荐
[cloud native learning notes] deploy applications through yaml files
CondaValueError: The target prefix is the base prefix. Aborting.
Address mapping of virtual memory paging mechanism
memcached全面剖析–3. memcached的删除机制和发展方向
升哲科技 AI 智能防溺水服务上线
C语言-关键字1
Multi task model of recommended model: esmm, MMOE
Foundations of Cryptography
memcached完全剖析–1. memcached的基础
自己总结的wireshark抓包技巧
Notes_ Vlan
Golang reflection operation collation
188. 买卖股票的最佳时机 IV
01---两列波在相遇处发生干涉的条件
关于Unity中的transform.InverseTransformPoint, transform.InverseTransofrmDirection
Arkit与Character Creator动画曲线的对接
[product design and R & D collaboration tool] Shanghai daoning provides you with blue lake introduction, download, trial and tutorial
02---纵波不可能产生的现象
Blender's simple skills - array, rotation, array and curve
Station B takes goods to learn from New Oriental