Topic description
Define a queue and implement the function max_value to get the maximum value in the queue, requiring the function MAX_VALUE, PUSH_BACK, and POP_FRONT's average time complexity to be O (1).
If the team is empty, POP_FRONT and MAX_VALUE need to return -1
Method 1: Violence
class MaxQueue {
int[] q = new int[20000];
int begin = 0;
int end = 0;
public MaxQueue() {
}
public int max_value() {
int ans = -1;
for(int i = begin;i!=end;i++){
if(q[i]>ans){
ans = q[i];
}
}
return ans;
}
public void push_back(int value) {
q[end++] = value;
}
public int pop_front() {
if(begin == end){
return -1;
}
return q[begin++];
}
}
Method Two:
Use oneDouble-end queue DequeWhen you enter the team, if DequeTailelementBe less thanThe element value of the team will be entered.Elements smaller than value all out of the teamAfterwards, enter the team; otherwise it will enter the team directly.
class MaxQueue {
Queue<Integer> q;
Deque<Integer> d;
public MaxQueue() {
q = new LinkedList<Integer>();
d = new LinkedList<Integer>();
}
public int max_value() {
if(d.isEmpty()){
return -1;
}
return d.peekFirst();// Get the first element of the double-end queue
}
public void push_back(int value) {
while(!d.isEmpty()&& d.peekLast()<value){
d.pollLast();// Get and remove the last element of the double-end queue
}
d.offerLast(value);/ / Insert the specified element into the end of the double-end queue
q.offer(value);/ / Add the specified element to the end of this queue
}
public int pop_front() {
if(q.isEmpty()){
return -1;
}
int ans = q.poll();/ / Get the element of the team head and delete the element
if(ans == d.peekFirst()){
d.pollFirst();// Get and remove the first element of the double-end queue
}
return ans;
}
}
/**
* Your MaxQueue object will be instantiated and called as such:
* MaxQueue obj = new MaxQueue();
* int param_1 = obj.max_value();
* obj.push_back(value);
* int param_3 = obj.pop_front();
*/
Remember three things: 1. Code standardization, indentation, variable naming, 2. Code integrity, considering all boundary inputs 3. Code robustness, consider error handling, defensive programming The ...
Article Directory 03. Repeated numbers in the array simple analysis code 04. Search in a two-dimensional array analysis code 07. Rebuild Binary Tree analysis code 39. Numbers that occur more than half...
The tree is traversed first, and then the array traversed in the middle is reversed. Title: Interview Question 32-III. Print the binary tree from top to bottom III Please implement a function to print...
Hi everybody, I'mFangyuan Everyone wants to cheer up today Question number Sword Point Offer 17. Print from 1 to the largest n digits Sword Point Offer 18. Delete the node of the linked list Sword Poi...
Topic description Implement a function to determine a binary tree is not symmetrical. If a binary tree is like a mirror, then it is symmetrical. Recursive method: Termination condition: When L and R a...
And two numbers for s Do question blog link Title link describe Example Initial Code Template Code Do question blog link Title link https://leetcode-cn.com/problems/he-wei-sde-liang-ge-shu-zi-lcof/ de...
Motion range of robots Do question blog link Title link describe Example Initial Code Template Code Do question blog link Title link https://leetcode-cn.com/problems/ji-qi-ren-de-yun-dong-fan-wei-lcof...
topic Source: LeetCode Link: https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof Problem-solving ideas Binary Search Tree, (Again: binary search tree, binary sort tree) It is e...
Given a binary search tree, please find the k-th largest node in it. Example 1: Example 2: limit: Question source link: https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof Prob...
Interview Question 17. Print from 1 to the largest n digits Enter the number n, and print out in order from 1 to the largest n-digit decimal number. For example, if you enter 3, 1, 2, and 3 will be pr...