Press "Enter" to skip to content

Posts tagged as “desgin”

花花酱 LeetCode 1381. Design a Stack With Increment Operation

Design a stack which supports the following operations.

Implement the CustomStack class:

  • CustomStack(int maxSize) Initializes the object with maxSize which is the maximum number of elements in the stack or do nothing if the stack reached the maxSize.
  • void push(int x) Adds x to the top of the stack if the stack hasn’t reached the maxSize.
  • int pop() Pops and returns the top of stack or -1 if the stack is empty.
  • void inc(int k, int val) Increments the bottom k elements of the stack by val. If there are less than k elements in the stack, just increment all the elements in the stack.

Example 1:

Input
["CustomStack","push","push","pop","push","push","push","increment","increment","pop","pop","pop","pop"]
[[3],[1],[2],[],[2],[3],[4],[5,100],[2,100],[],[],[],[]]
Output
[null,null,null,2,null,null,null,null,null,103,202,201,-1]
Explanation
CustomStack customStack = new CustomStack(3); // Stack is Empty []
customStack.push(1);                          // stack becomes [1]
customStack.push(2);                          // stack becomes [1, 2]
customStack.pop();                            // return 2 --> Return top of the stack 2, stack becomes [1]
customStack.push(2);                          // stack becomes [1, 2]
customStack.push(3);                          // stack becomes [1, 2, 3]
customStack.push(4);                          // stack still [1, 2, 3], Don't add another elements as size is 4
customStack.increment(5, 100);                // stack becomes [101, 102, 103]
customStack.increment(2, 100);                // stack becomes [201, 202, 103]
customStack.pop();                            // return 103 --> Return top of the stack 103, stack becomes [201, 202]
customStack.pop();                            // return 202 --> Return top of the stack 102, stack becomes [201]
customStack.pop();                            // return 201 --> Return top of the stack 101, stack becomes []
customStack.pop();                            // return -1 --> Stack is empty return -1.

Solution: Simulation

Time complexity:
init: O(1)
pop: O(1)
push: O(1)
inc: O(k)

C++

花花酱 LeetCode 707. Design Linked List

Problem

Design yourĀ implementation of the linked list. You can choose to use the singly linked list or the doubly linked list. A node in a singlyĀ linked list should have two attributes:Ā valĀ andĀ next.Ā valĀ is the value of the current node, andĀ nextĀ isĀ aĀ pointer/reference to the next node. If you want to use the doubly linked list,Ā you will needĀ one more attributeĀ prevĀ to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.

Implement these functions in your linked list class:

  • get(index) : Get the value ofĀ theĀ index-thĀ node in the linked list. If the index is invalid, returnĀ -1.
  • addAtHead(val) : Add a node of valueĀ valĀ before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
  • addAtTail(val) : Append a node of valueĀ valĀ to the last element of the linked list.
  • addAtIndex(index, val) : Add a node of valueĀ valĀ before theĀ index-thĀ node in the linked list.Ā IfĀ indexĀ equalsĀ to the length ofĀ linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
  • deleteAtIndex(index) : DeleteĀ theĀ index-thĀ node in the linked list, if the index is valid.

Example:

Note:

  • All values will be in the range ofĀ [1, 1000].
  • The number of operations will be in the range ofĀ [1, 1000].
  • Please do not use the built-in LinkedList library.




Solution: Single linked list

Keep tracking head and tail of the list.

Time Complexity:

addAtHead, addAtTail O(1)

addAtIndex O(index)

deleteAtIndex O(index)

Space complexity: O(1)

C++

Tracking head/tail and size of the list.

v2

Python3

Java

 

花花酱 LeetCode 872. Implement Rand10() Using Rand7()

Problem

Given a functionĀ rand7Ā which generates a uniform random integer in the range 1 to 7, write a functionĀ rand10Ā which generates a uniform random integer in the range 1 to 10.

Do NOT use system’sĀ Math.random().

Example 1:

Input: 1
Output: [7]

Example 2:

Input: 2
Output: [8,4]

Example 3:

Input: 3
Output: [8,1,10]

Note:

  1. rand7Ā is predefined.
  2. Each testcase has one argument:Ā n, the number of times thatĀ rand10Ā is called.

Solution: Math

Time complexity: O(49/40) = O(1)

Time complexity: O(7/6 + 7 / 5) = O(1)

 

花花酱 LeetCode 641. Design Circular Deque

Problem

Design your implementation of the circular double-ended queue (deque).
Your implementation should support following operations:

  • MyCircularDeque(k): Constructor, set the size of the deque to be k.
  • insertFront(): Adds an item at the front of Deque. Return true if the operation is successful.
  • insertLast(): Adds an item at the rear of Deque. Return true if the operation is successful.
  • deleteFront(): Deletes an item from the front of Deque. Return true if the operation is successful.
  • deleteLast(): Deletes an item from the rear of Deque. Return true if the operation is successful.
  • getFront(): Gets the front item from the Deque. If the deque is empty, return -1.
  • getRear(): Gets the last item from Deque. If the deque is empty, return -1.
  • isEmpty(): Checks whether Deque is empty or not.
  • isFull(): Checks whether Deque is full or not.

Example:

MyCircularDeque circularDeque = new MycircularDeque(3); // set the size to be 3
circularDeque.insertLast(1);			// return true
circularDeque.insertLast(2);			// return true
circularDeque.insertFront(3);			// return true
circularDeque.insertFront(4);			// return false, the queue is full
circularDeque.getRear();  				// return 32
circularDeque.isFull();				// return true
circularDeque.deleteLast();			// return true
circularDeque.insertFront(4);			// return true
circularDeque.getFront();				// return 4

Note:

  • All values will be in the range of [1, 1000].
  • The number of operations will be in the range ofĀ [1, 1000].
  • Please do not use the built-in Deque library.

Solution

Using head and tail to pointer to the head and the tail in the circular buffer.

Related Problems