单调队列和单调栈

public class Main{
 
    public static int[] MaxBuilding(int[] arr){
        if(arr == null || arr.length < 0) return null;
        int[] res = new int[arr.length];
        Stack<Integer> stack = new Stack<>();
        for(int i =0;i<arr.length;i++){
            res[i] = stack.size();
            while(!stack.isEmpty()&&arr[i]>=arr[stack.peek()]){
                stack.pop();
            }
            stack.push(i);
        }
        stack.clear();
        for(int i =arr.length-1;i>=0;i--){
            res[i]+=1+stack.size();
            while(!stack.isEmpty()&&arr[i]>=arr[stack.peek()]){
                stack.pop();
            }
            stack.push(i);
        }
        
        return res;
    }
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int len = sc.nextInt();
        int[] arr = new int[len];
        for(int i = 0 ; i < len ; i++){
            arr[i] = sc.nextInt();
        }
        int[] res = MaxBuilding(arr);
        for (int i = 0; i < res.length; i++) {
            System.out.print(res[i] + " ");
        }
    }
 
}

 

class MaxQueue {
    private Deque<Integer> deque = null;
    //单调队列 按输入顺序维护单调递增队列
    private Deque<Integer> help = null;

    public MaxQueue() {
        deque = new ArrayDeque<>();
        help = new ArrayDeque<>();
    }
    
    public int max_value() {
        if(help.isEmpty()){
            return -1;
        }
        return help.peek();

    }
    
    public void push_back(int value) {
        deque.offer(value);
        while(!help.isEmpty()&&help.peekLast()<value){
            help.pollLast();
        }
        help.offer(value);
    }
    
    public int pop_front() {
        if(deque.isEmpty()){
            return -1;
        }
        int val = deque.pop();
        if(val==help.peek()){
            help.pop();
        }
        return val;
    }
}

/**
 * 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();
 */

 

import java.util.*;
public class Main{
      public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str = sc.next().trim();
        int k = sc.nextInt();
        StringBuffer sb = new StringBuffer(""+str.charAt(0));
  
        for(int i = 1;i<str.length();i++){            
            char x = sb.charAt(sb.length()-1);
            while(k>0&&sb.length()>0&&sb.charAt(sb.length()-1)<str.charAt(i)){
                sb.deleteCharAt(sb.length()-1);
                k--;
            }
            sb.append(str.charAt(i));    
        }
          if(k==0){
              System.out.println(sb.toString());
          }else{
              System.out.println(sb.substring(0,sb.length()-k));
          }
         
    } 
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值