算法有毒植物对于高测试案例来说太慢了

时间:2017-01-22 19:03:59

标签: java algorithm doubly-linked-list

您好我试图在hackerrank上解决有毒植物算法问题这里是Link我的解决方案很好,它工作正常但是当我们有高测试用例时它太慢了。我制作了自己的双重链表。并添加了一个额外的meth7od删除(节点n),删除O(1)中的节点。所以我不知道问题出在哪里以及为什么我的代码很慢。谁能告诉我如何改进我的算法?

import java.util.Scanner;

public class Main
{
public static class DoublyLinkedList
{
    private class Node
    {
        int value;
        Node next;
        Node prev;

        public Node(int value)
        {
            this.value = value;
            next = null;
            prev = null;
        }
    }

    private Node head = null;

    // We will hold the pointer to the last Node so we can inser O(1) at the end.
    private Node last = null;

    // Current node we will lose to travel through the list.
    private Node current = null;

    public void addFirst(int value)
    {
        if(head == null)
        {
            head = new Node(value);
            last = head;
            return;
        }

        // Make the new node and connect it with the list.
        Node newNode = new Node(value);
        head.prev = newNode;
        newNode.next = head;

        // Set new Node to be the head.
        head = newNode;
    }

    public void addLast(int value)
    {
        // If last is null, it means that we have an empty list.
        if(last == null)
        {
            last = new Node(value);
            head = last;
            return;
        }
        // If last is not nulkl, make it to be null.
        Node newNode = new Node(value);

        last.next = newNode;
        newNode.prev = last;
        last = newNode;
    }

    public void deleteNode(Node node)
    {
        // Delete node in list.

        // If node is empty, return.
        if(node == null)
            return;

        // If node is head node.
        if(node == head)
        {
            delete(node.value);
            return;
        }

        // if node is the last Node.
        if(node == last)
        {
            last = last.prev;
            last.next.prev = null;
            last.next = null;
            return;
        }

        // Now we know that the node is somewhere in the middle.
        node.prev.next = node.next;
        node.next.prev = node.prev;
    }

    public void delete(int value)
    {
        // If list is empty, return;
        if(head == null)
            return;

        current = head;

        // Loop until we find the value or we get to the end of the list
        while(current != null && current.value != value)
            current = current.next;

        // If current is null, it means passed value doesnt exist in the list.
        if(current == null)
            return;

        // If value is last Node. We will need to update the last Node.
        if(current == last)
        {
            last = last.prev;
            last.next.prev = null;
            last.next = null;
            return;
        }

        // If value is head.
        if(current == head)
        {
            head = head.next;
            head.prev.next = null;
            head.prev = null;
            return;
        }
        // If value is in the middle of the list delete

        current.prev.next = current.next;
        current.next.prev = current.prev;
    }

    public int solve()
    {
        boolean finish = true;
        int counter = 0;

        // We will set finish to true, when we have no more elements to delete
        while(finish)
        {
            finish = false;


            // Go through the end of the list and delete values.
            current = last;
            while(current != null)
            {
                if(current.prev != null && current.value > current.prev.value)
                {
                    // Delete the element.
                    current = current.prev;
                    deleteNode(current.next);
                    finish = true;
                    continue;
                }
                current = current.prev;
            }

            if(finish)
                counter++;
        }
        return counter;
    }
}

public static void main(String[] args)
{
    // Make the list
    DoublyLinkedList list = new DoublyLinkedList();

    Scanner sc = new Scanner(System.in);
    int test = sc.nextInt();

    // Fill the list.
    for(int i=0; i<test; i++)
        list.addLast(sc.nextInt());

    System.out.println(list.solve());

}
}

解决此任务的主要方法是solve()。

0 个答案:

没有答案