当前位置:网站首页>An unordered array of N integers. Find the first number after each element that is larger than it. The time complexity is O (n)
An unordered array of N integers. Find the first number after each element that is larger than it. The time complexity is O (n)
2022-06-22 05:48:00 【evanYang_】
Get the title first , It's easy to think about each element , Traversing the elements behind it, you can find the first one larger than it , But in this case, the time consumption may exceed O(n), Therefore, a lot of information may be missed , So that we have to compare every time , What should I use ?
Let's imagine , If my current processing object is the first element , If the second element is smaller than me , So what I need to do now is not to compare the relationship between the third element and the first element , Instead, the object being processed becomes the second element . If the third element is larger than the second , I'm going back to the first element , Does this save a lot of time ?
So we need a container to store unprocessed elements , You can know , Elements are last in, first out , Like the second element , The latter cut gets the result first , So we can use the stack (stack) To store unprocessed elements .
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;
import java.util.Stack;
public class Test {
public static void main(String[] args) {
//int[] a = {2,1,3,2,5};
int[] a = {2,1,3,5,2};
int[] ints = demoStack(a);
for (int i = 0; i < ints.length; i++) {
System.out.print(ints[i]+" ");
}
}
public static int[] demoStack(int[] num){
int len=num.length;
if(len==0) return null; // An empty array , Returns an empty
int[] result = new int[len]; // Return results : initialization -1, Means not found
Stack<Integer> notFind = new Stack<>(); // Stack :num No eligible element index found in
int i=0;
while(i<len) // Traversal array
{
// If the stack is empty or currently num The element is not larger than the top of the stack , Stack the current element , Index back
if(notFind.empty() || num[notFind.peek()]>=num[i])
{
notFind.push(i++);
}
// Elements to be processed , And num The current element is larger than the stack top index element , eligible , Update the value of the index in the result array , The top of the stack .
else
{
result[notFind.peek()]=num[i];
notFind.pop();
}
}
while (!notFind.empty()){
result[notFind.peek()]=-1;
notFind.pop();
}
return result;
}
}

边栏推荐
- Reptile initial and project
- 机器学习笔记 七:强大的神经网络表述
- c files always get rebuild when make -------- . PHONY in Makefile
- SCM future employment development direction, learn SCM must know some entry-level knowledge and industry prospects, read the benefit
- Want to put Facebook ads but don't know where to start? This article takes you to know more about
- Key points of Facebook account "unsealing, anti sealing and maintaining ID" have been collected!
- Overview analysis and investment forecast report of semiconductor refrigeration devices in the world and China 2022-2027
- Analysis of 43 cases of MATLAB neural network: Chapter 29 research on the application of limit learning machine in regression fitting and classification -- Comparative Experiment
- QEMU ARM interrupt system architecture 2
- Independent station optimization list - how to effectively improve the conversion rate in the station?
猜你喜欢

Machine learning note 8: octave for handwritten digit recognition based on Neural Network

Sourcetree reported an error SSH failure

Gerrit Code Review Setup

Clion installation Download

nacos server 源码运行实现

Independent station optimization list - how to effectively improve the conversion rate in the station?

Cookie setting and reading in C #

Machine learning Note 6: number recognition of multiple classification problems in logistic regression

Delete the packaging use of pop-up components

sourcetree报错ssh失败
随机推荐
为什么说“ CPS联盟营销 ” 是性价比最高的推广方式?
Research Report on global and Chinese active Ethernet access device industry demand trend and investment prospect 2022-2027
A reminder to cross-border sellers who are still "shopping"!
Development prospect and investment potential prediction report of China's rare earth permanent magnet industry during the "14th five year plan" period 2022-2027
我不建议你工作太拼命
P1061 [noip2006 popularization group] counting method of jam
Want to put Facebook ads but don't know where to start? This article takes you to know more about
Data storage (Advanced)
How wechat applet assigns values to sub components
c files always get rebuild when make -------- . PHONY in Makefile
线性回归:最小二乘、泰尔森估计、RANSAC
牛的学术圈 II(春季每日一题 49)
关于图片懒加载的实现(总结梳理)
AUTOSAR从入门到精通100讲(150)-SOA架构及应用
基于CRU中的tmp数据进行年平均气温分析
Gerrit Code Review Setup
Delete the packaging use of pop-up components
Vscode minimalist installation tutorial
C语言指针(进阶)
Amazon and independent station are not simply two choices