是什么使得三端队列的 Java 链表实现速度缓慢?

问题描述 投票:0回答:3

我正在解决 Kattis 问题 Teque,并且必须实现一个 teque(三端队列),只需四个操作:推到前面、推到后面、推到中间(中位数)和读取索引处的项目。这次作业的重点是时间复杂度,所以我使用了尾链表来轻松地在前面和后面添加元素。

对于push-to-middle 函数,我没有在列表上调用size(),而是在Teque 中存储了一个类变量来跟踪列表中的元素数量。这使得查找非常快。

class Teque {
    public TailedLinkedList list;
    public Kattio io = new Kattio(System.in, System.out);
    public int numItems;

    public Teque() {
        this.list = new TailedLinkedList();
        this.numItems = 0;
    }

    public void push_back(int x) {
        numItems++;
        list.addBack(x);
    }

    public void push_front(int x) {
        numItems++;
        list.addFront(x);
    }

    public void push_middle(int x) {

        int median = (numItems + 1) / 2;
        list.addAtIndex(median, x);
        numItems++;
    }

    public int get(int i) {
        int result = list.getItemAtIndex(i);
        io.println(result);
        io.flush();
        return result;
    }
}

TaailedLinkedList 的实现:

import java.util.*;

class TailedLinkedList implements ListInterface {
    // attributes
    public ListNode head;
    public ListNode tail;
    public int num_nodes;

    // interface methods

    // Return true if list is empty; otherwise return false.
    public boolean isEmpty() { return (num_nodes == 0); }

    // Return number of items in list
    public int size() { return num_nodes; }

    // return index of item if item is found in the list, otherwise return -1
    public int indexOf(int item) {
        int index = 0;

        for (ListNode cur = head; cur != null; cur = cur.getNext()) {
            if (cur.getItem() == item) 
                return index;
            else 
                index++;
        }
        return -1;
    }

    // return true if item is in the list false otherwise
    public boolean contains(int item) {
        if (indexOf(item) != -1)
            return true;
        return false;
    }

    // get item at index
    public int getItemAtIndex(int index) {
        int counter = 0;
        int item = 0;

        if (index < 0 || index > size()-1) {
            System.out.println("invalid index");
            System.exit(1);
        }
        if (index == size()-1)
            item = tail.getItem();
        else {
            for (ListNode cur = head; cur != null; cur = cur.getNext()) {
                if (counter == index) {
                    item = cur.getItem();
                    break;
                }
                counter++;
            }
        }
        return item;
    }

    // Return first item
    public int getFirst() { return getItemAtIndex(0); }

    // Return last item
    public int getLast() { return getItemAtIndex(size()-1); }

    // add item at position index, shifting all current items from index onwards to the right by 1 
    // pre: 0 <= index <= size()
    public void  addAtIndex(int index, int item) {
        ListNode cur;
        ListNode newNode = new ListNode(item);

        if (index >= 0 && index <= size()) {
            if (index == 0) // insert in front
                insert(null,newNode);
            else if (index == size()) // insert at the back, don't have to move all the way to the back
                insert(tail,newNode);
            else {
                cur = getNodeAtIndex(index-1); // access node at index-1
                insert(cur,newNode);
            }
        }
        else { // index out of bounds
            System.out.println("invalid index");
            System.exit(1);
        }
    } 

    // Add item to front of list
    public void addFront(int item) { addAtIndex(0,item); }

    // Add item to back of list
    public void addBack(int item) { addAtIndex(size(),item); }

    // remove item at index and return it
    // pre: 0 <= index < size()
    public int removeAtIndex(int index) {
        ListNode cur;
        int item = 0;

        // index within bounds and list is not empty
        if (index >= 0 && index < size() && head != null) {
            if (index == 0) // remove 1st item
                item = remove(null);
            else {
                cur = getNodeAtIndex(index-1); // access node at index-1
                item = remove(cur);
            }
        }
        else { // index out of bounds
            System.out.println("invalid index or list is empty");
            System.exit(1);
        }
        return item;
    }

    // Remove first node of list
    public int removeFront() { return removeAtIndex(0); }

    // Remove last node of list
    public int removeBack() { return removeAtIndex(size()-1); }

    // Print values of nodes in list.
    public void print() {
        if (head == null)
            System.out.println("Nothing to print...");
        else {
            ListNode cur = head;
            System.out.print("List is: " + cur.getItem());
            for (int i=1; i < num_nodes; i++) {
             cur = cur.getNext();
             System.out.print(", " + cur.getItem());
            }
            System.out.println(".");
        }
    }

    // non-interface helper methods
    public ListNode getHead() { return head; }
    public ListNode getTail() { return tail; }

    /* return the ListNode at index */
    public ListNode getNodeAtIndex(int index) {
        int counter = 0;
        ListNode node = null;

        if (index < 0 || index > size()-1) {
            System.out.println("invalid index");
            System.exit(1);
        }
        if (index == size()-1) // return tail if index == size()-1
            return tail;
        for (ListNode cur = head; cur != null; cur = cur.getNext()) {
            if (counter == index) {
                node = cur;
                break;
            }
            counter++;
        }
        return node;
    }

    // insert newNode after the node referenced by cur 
    public void insert(ListNode cur, ListNode n) {
        // insert in front
        if (cur == null) {
            n.setNext(head);
            head = n; // update head
            if (tail == null) // update tail if list originally empty
                tail = head;
        }
        else { // insert anywhere else
            n.setNext(cur.getNext());
            cur.setNext(n);
            if (cur == tail) // update tail if inserted new last item
                tail = tail.getNext();
        }
        num_nodes++;
    }

    // remove the node referenced by cur.next, and return the item in the node 
    // if cur == null, remove the first node 
    public int remove(ListNode cur) {
        int value;

        if (cur == null) { // remove 1st node
            value = head.getItem();
            head = head.getNext(); // update head
            if (num_nodes == 1) // update tail to null if only item in list is removed
                tail = null;
        }
        else { // remove any other node
            value = cur.getNext().getItem();
            cur.setNext(cur.getNext().getNext());
            if (cur.getNext() == null) // update tail if last item is removed
                tail = cur;
        }
        num_nodes--;

        return value;
    }
}

但是,我的代码仍然不够时间(输入10万行,限制2秒)。有没有明显的地方可以改进我的算法?

addAtIndex
getItemAtIndex
运行时间为 O(n)。

java linked-list deque
3个回答
2
投票

push_midle
导致遍历列表的一半。解决方案是使用“两个列表”,前半部分和后半部分。如果列表不平衡,请在两个列表之间移动元素。 (ListIterator 也可能这样做;尽管对于一种幼稚的方法,人们必须担心 ConcurrentModificationException。)


1
投票

frontIndex = front.length / 2 和 front.length - 1 之间的任何值
  • frontMidIndex = frontIndex - 1
  • backIndex = 0 到 back.length / 2 之间的任意值
  • backMidIndex = backIndex + 1
  • 有效内容: front[frontIndex .. frontMidIndex[ + back[backMidIndex .. backIndex[

推前:

frontIndex > 0?
  • 设置 frontIndex,减少 frontIndex
    否则前面MidIndex
  • 将 front 数组向后移动 x,将 frontIndex 和 frontMidIndex 增加 x,如上设置< front.length
    否则
  • 将某些部分复制到后面或增长数组,然后像上面一样继续
  • 回推:

同样适用于后退、backIndex (
    0)、切换入/递减和移位方向
  • < back.length), backMidIndex (>
  • pushMid(这需要更多思考):

最佳的是正面和背面的有效长度相等或相差一。 然后这就变成了 frontMidIndex 或 backMidIndex 处的集合(与上面的逻辑类似)
  • 如果 mid 落在两个数组之一的有效范围的中间,你可以
  • 移动此数组中的内容以创建元素的位置。
    • 或者在两个数组之间移动内容来调整 midIndexes 以进一步 pushMid。

0
投票

根据我对数组(列表,队列,堆栈...)之间的操作的理解,如果我们只使用数组,总是很难节省添加/删除元素的时间,同时仍然具有 O(1) 时间复杂度来访问随机索引元素- 就像 obejcts 一样。因此,我使用 hashmap 来达到这个 kattis 问题中添加和访问的 O(1) 复杂度。再加上Joop Eggen的想法,问题就可以解决了。

© www.soinside.com 2019 - 2024. All rights reserved.