当前位置:网站首页>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;
}
};边栏推荐
- Volcano成Spark默认batch调度器
- leetcode-201_2021_10_17
- Tdengine can read and write through dataX
- 升哲科技 AI 智能防溺水服务上线
- Logical backup: mysqldump vs physical backup: xtrabackup
- [Web Security Basics] some details
- 传输层 udp && tcp
- leetcode_1365
- Concepts of kubernetes components
- About transform InverseTransformPoint, transform. InverseTransofrmDirection
猜你喜欢

Advanced secret of xtransfer technology newcomers: the treasure you can't miss mentor
Visit Amazon memorydb and build your own redis memory database

123. 买卖股票的最佳时机 III

OSI and tcp/ip model

123. the best time to buy and sell shares III

福建省发改委福州市营商办莅临育润大健康事业部指导视察工作

BPF_ PROG_ TYPE_ SOCKET_ Filter function implementation

C语言实现DNS请求器

Transport layer UDP & TCP

Intelligent fish tank control system based on STM32 under Internet of things
随机推荐
力扣每日一题-第26天-496.下一个更大元素Ⅰ
leetcode_191_2021-10-15
C语言-关键字1
Slider controls the playback progress of animator animation
Static routing experiment
188. the best time to buy and sell stocks IV
02---纵波不可能产生的现象
Network layer & IP
leetcode1863_2021-10-14
Blender's landscape
Interpretation of ebpf sockops code
Auto. JS to realize automatic unlocking screen
Wireshark packet capturing skills summarized by myself
Football information query system based on C language course report + project source code + demo ppt+ project screenshot
Data link layer & some other protocols or technologies
2022国际女性工程师日:戴森设计大奖彰显女性设计实力
Blender's simple skills - array, rotation, array and curve
介绍BootLoader、PM、kernel和系统开机的总体流程
Oauth2.0 introduction
Memcached full profiling – 1 Fundamentals of memcached