Does Linkedlist keep track of mines in constant time?

EDIT:

This is not as trivial as you think. Consider the fact that each addition of a new number forces the old number out of the linked list.

The solution doesn't seem as simple as keeping track of a minute number with a variable. What if the minimum is pushed out of LinkedList? So what? How do you know what a new mine is?

I heard this interview question:

You have a fixed-length list.

  • At time t = 0, the linked list is filled with random numbers.

  • Each time you add one new number is sent to the head of the linked list, and one number is pressed from the tail.

  • Only one bypass is allowed before the first interval.

    • This means that there has ever been one workaround. Not once at any time t.
      ,
  • O (1).

- min .

, ?


:

, . : .

+5
6

, - .

-,

O (1) . .

-,

LL (CSQ).

-, () node, .

1 op CSQ

, --hence O (1). , , , O (n). O(lg n).

min

, O (1). .

+9

, LL . , , , , .

-, , , , , , , . . 1D- O (n) .

LL ( ) , O (1) ( ). O (n).

, , . // .:)

, , , !

0

, (, , ), ():

struct abc                                    // sorry for the name :)
{
    int pos
    int num
}
initialize
{
    struct abc min_array[linked_list_length];   // O(1) space

    fill(min_array, linked_list_elements);      // place every element in the array
    sort(min_array);                            // sort in ascending order, the sorting
                                                // compares num
}

insert_element
{
    linked_list.push(value);
    linked_list.pop();

    for each element in min_array {
        element.pos += 1

        if element.pos > linked_list.elementCount then   // this was popped out!
            element.pos = 1;
            element.num = value;
        end if
    }

    sort(min_array);        // as before, sorting compares only num
}

get_min_value
{
    return min_array[0];
}
0

, , - :

, , N . , (, ). O (1) , N .

0

, , , ( , ). , node. node . , : nextPointer, value minSoFar. , , .

, O (1). , , node. , ,

5 13

5 | 7 | 6 | 9 | 5 | 13 |

{5,5} | {7,6} | {6,6} | {} 9,9 | {13,13} | ({nodeVal, minSoFar})

, 3 13,

{3,3} | {5,5} | {7,6} | {6,6} | {9,9} |

3 , ,

, 15

{} 15,5 | {5,5} | {7,6} | {6,6} | {9,9} |

15 , , 5.

, /, , , head, O (1)

, minValueSoFar .

0

If node can be defined as follows, then the solution is simple:

class Node {
    int data;
    Node next;
    Node prevMin;
}
0
source

All Articles