Leetcode剑指offerQ59-||

本题要求均摊复杂度为O(1),看了答案使用的双队列,一个来记录值,一个用来记录最大值(就是排序),但是看了代码,发现在push_back时仍然需要进行类似排序操作的while循环,不能理解均摊o(1).

package algorithm.jianzhiOffer;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

class ListNode {
    public int val;
    public ListNode next;

    ListNode(int x) {
        this.val = x;
        this.next = null;

    }
}

public class Q59 {
    ListNode max_list;
    int[] queue;

    int start, end;

    public Q59() {
        start = end = 0;
        queue = new int[10001];
        max_list = new ListNode(-1);
    }

    public int max_value() {
        if (end == start) return -1;
        return max_list.next.val;
    }

    public void push_back(int value) {

        if (start == end) max_list.next = new ListNode(value);
        else {
            ListNode tmp = max_list;
            while (tmp.next != null && tmp.next.val >= value) {
                tmp = tmp.next;
            }
            ListNode newNode = new ListNode(value);
            newNode.next = tmp.next;
            tmp.next = newNode;
        }
        queue[++end] = value;

    }

    public int pop_front() {
        if (end == start) return -1;
        ListNode tmp = max_list;
        while (tmp.next.val != queue[start + 1]) tmp = tmp.next;
        tmp.next = tmp.next.next;
        return queue[++start];

    }

    public void test() throws Exception {
        Q59 q = new Q59();
        String names[] = new String[]{"max_value", "pop_front", "max_value", "push_back", "max_value", "pop_front", "max_value", "pop_front", "push_back", "pop_front", "pop_front", "pop_front", "push_back", "pop_front", "max_value", "pop_front", "max_value", "push_back", "push_back", "max_value", "push_back", "max_value", "max_value", "max_value", "push_back", "pop_front", "max_value", "push_back", "max_value", "max_value", "max_value", "pop_front", "push_back", "push_back", "push_back", "push_back", "pop_front", "pop_front", "max_value", "pop_front", "pop_front", "max_value", "push_back", "push_back", "pop_front", "push_back", "push_back", "push_back", "push_back", "pop_front", "max_value", "push_back", "max_value", "max_value", "pop_front", "max_value", "max_value", "max_value", "push_back", "pop_front", "push_back", "pop_front", "max_value", "max_value", "max_value", "push_back", "pop_front", "push_back", "push_back", "push_back", "pop_front", "max_value", "pop_front", "max_value", "max_value", "max_value", "pop_front", "push_back", "pop_front", "push_back", "push_back", "pop_front", "push_back", "pop_front", "push_back", "pop_front", "pop_front", "push_back", "pop_front", "pop_front", "pop_front", "push_back", "push_back", "max_value", "push_back", "pop_front", "push_back", "push_back", "pop_front"};
        Method max_value = Q59.class.getDeclaredMethod("max_value", null);
        Method pop_front = Q59.class.getDeclaredMethod("pop_front", null);
        Method push_back = Q59.class.getDeclaredMethod("push_back", int.class);
        int[] aa = new int[]{46, 868, 525, 123, 646, 229, 871, 285, 45, 140, 837, 545, 561, 237, 633, 98, 806, 717, 186, 268, 29, 866, 239, 3, 850, 310, 674, 770, 525, 425, 720, 373, 411, 831, 765, 701};
        int i = 0;
        for (String name : names) {

            if (name.equals("max_value")) {
                System.out.print((int) max_value.invoke(q) + ",");
            }
            if (name.equals("pop_front")) {
                System.out.print((int) pop_front.invoke(q) + ",");
            }
            if (name.equals("push_back")) {
                push_back.invoke(q, aa[i++]);
                System.out.print("null" + ",");
            }

        }
        int a = 0;
    }

    public static void main(String[] argc) throws Exception {
        new Q59().test();

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值