Algorithm
-
Number of Students Unable to Eat LunchAlgorithm/Queue and Stack 2021. 11. 22. 23:59
The school cafeteria offers circular and square sandwiches at lunch break, referred to by numbers 0 and 1 respectively. All students stand in a queue. Each student either prefers square or circular sandwiches. The number of sandwiches in the cafeteria is equal to the number of students. The sandwiches are placed in a stack. At each step: If the student at the front of the queue prefers the sandw..
-
Number of Recent CallsAlgorithm/Queue and Stack 2021. 11. 22. 23:01
You have a RecentCounter class which counts the number of recent requests within a certain time frame. Implement the RecentCounter class: RecentCounter() Initializes the counter with zero recent requests. int ping(int t) Adds a new request at time t, where t represents some time in milliseconds, and returns the number of requests that has happened in the past 3000 milliseconds (including the new..
-
First Unique Character in a StringAlgorithm/String 2021. 11. 22. 22:40
Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1. Example 1: Input: s = "leetcode" Output: 0 Example 2: Input: s = "loveleetcode" Output: 2 Example 3: Input: s = "aabb" Output: -1 Constraints: 1
-
Implement Queue using StacksAlgorithm/Queue and Stack 2021. 11. 22. 22:08
Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty). Implement the MyQueue class: void push(int x) Pushes element x to the back of the queue. int pop() Removes the element from the front of the queue and returns it. int peek() Returns the element at the front of the queue. boolean..
-
Implement Stack using QueuesAlgorithm/Queue and Stack 2021. 11. 22. 20:55
https://leetcode.com/problems/implement-stack-using-queues/ Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty). Implement the MyStack class: void push(int x) Pushes element x to the top of the stack. int pop() Removes the element on the top of the stack and returns it. int top() Re..
-
Linked List | Partition ListAlgorithm/Linked List 2021. 7. 19. 23:07
Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. 주어진 Linked List를 파티션 하라는 문제이다. Integer 값이 주어지는데, 이보다 작으면 순서대로 왼쪽에, 크거나 같으면 순서대로 오른쪽에 나열해서 새로운 Linked List를 리턴하는 문제이다. Dummy Node를 이용해서 손쉽게 풀 수 있다. /** * Definition for ..
-
Linked List | Swap Nodes in PairsAlgorithm/Linked List 2021. 7. 12. 15:45
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.) 2 노드씩 짝을 지어서, 순서를 바꾸라는 문제이다. 또한, 값을 변형하는것이 아닌, 노드의 위치를 바꾸라는 문제이다. 만약 노드의 갯수가 0개, 1개일 경우는 그대로 리턴해야하며, 노드 갯수가 홀수일 때, 마지막 노드는 그대로 둔다. 한번의 Trip만에 해결할 수 있는 방식이 떠올라 바로 코딩을 했다. 해법은 아래와 같다. class Solution { publi..