Circular Deque의 두 가지 구현
·
Algorithm/내용 정리
❒ Description[LeetCode#641] Design Circular Deque에서 Array와 Doubly-Linked list를 통해서 Circular-DQ를 구현해봤다. 하지만 Doubly-Linked list를 통한 구현은 원형 큐의 이점을 잘 살리지 못한다고 했다.여기서는 왜 이점을 살리지 못하는지 정리해본다.   ❒ 동작원리1. insertFrontFront 앞에 새로운 데이터를 삽입한다.Front가 현재 0번 째 index를 가리키고 있다면, Front는 마지막 index를 가리키도록 변경된다. 2. insertLastRear 뒤에 새로운 데이터를 삽입한다.Rear가 현재 마지막 index를 가리키고 있다면, Rear는 첫 번째 index를 가리키도록 변경된다.   ❒ Why?1. ..
[LeetCode#641] Design Circular Deque
·
Algorithm/문제풀이
❒ Description제목Design Circular Deque링크https://leetcode.com/problems/design-circular-deque/description/자료구조선형 (배열 / 이중 연결 리스트)시간복잡도O(1)이번 문제는 이중 연결 리스트로 Circular-Deque를 구현하는 문제였다.근데 사실 이중 연결 리스트로  Circular-Deque를 구현하면 원형의 이점을 살릴 수 없다.실제로 원형의 이점을 살리기 위해선 이 문제를 연결리스트가 아닌 배열로 풀이해야 한다.따라서 이번 문제는 이중 연결 리스트와 배열 모두 구현 해볼 것이다.   ❒ Solution1. 이중 연결 리스트 (Doubly-Linked List)더보기public class MyCircularDeque {..
[LeetCode#622] Design Circular Queue
·
Algorithm/문제풀이
❒ Description제목Design Circular Queue링크https://leetcode.com/problems/design-circular-queue/description/자료구조선형 (배열)시간복잡도O(1)이 번 문제는 배열을 사용해서 Circular-Queue(원형 큐)를 구현하는 문제이다.   ❒ Solution public class MyCircularQueue { int q[]; int front = 0; int rear = -1; int len = 0; public boolean enQueue(int value) { if (!isFull()) { rear = (rear + 1) % q.length; q[r..
[LeetCode#739] Daily Temperatures
·
Algorithm/문제풀이
❒ Description제목Daily Temperature링크https://leetcode.com/problems/daily-temperatures/description/자료구조스택시간 복잡도O(n) 이번 문제의 핵심은 인덱스이다. 그리고 두 가지 풀이가 있는데 첫 번째는 Stack + Index 이고, 두 번째는 Index만을 사용하는 문제풀이다. 둘 해결책 모두 O(n)의 시간 복잡도를 가진다. 하지만 두번 째 솔루션이 6ms로 4배정도 빨랐다.      ❒ Solution1. Stack아래 코드는 최초에 작성한 코드로 runtime 26ms를 기록한 코드이다. public int[] dailyTemperatures(int[] temperatures) { int[] result = new int[te..
[LeetCode#92] Reverse Linked List II
·
Algorithm/문제풀이
❒ Description제목Reverse Linked List II링크https://leetcode.com/problems/reverse-linked-list-ii/자료구조선형 (연결 리스트)시간복잡도O(n) 이번 문제에서는 기억하고 있으면 나중에 사용하기 좋은 공식? 같은 것들을 알았다.가상의 노드 Root 생성하기특정 범위의 노드의 순서만 변경하기. ❒ Solutionpublic ListNode reverseBetween(ListNode head, int left, int right) { if (head == null) { return null; } // 가상의 노드 생성하기 ListNode root = new ListNode(0); root.next = head; // 노드 변경 시작점으로 이동하기..
[LeetCode#24] Swap Nodes In Pairs
·
Algorithm/문제풀이
❒ Description제목Swap Nodes In Pairs링크https://leetcode.com/problems/swap-nodes-in-pairs/description/자료구조선형 (연결 리스트)  ❒ Solution1. 값만 교환 : 임시 변수를 사용 public ListNode swapPairs(ListNode head) { ListNode node = head; while (node != null && node.next != null) { int temp; temp = node.val; node.val = node.next.val; node.next.val = temp; node = node.next.next; } ..
[LeetCode#206] Reverse Linked List
·
Algorithm/문제풀이
❒ Description제목Reverse Linked List링크https://leetcode.com/problems/reverse-linked-list/자료구조선형 (연결리스트) 이번 문제는 연결리스트를 뒤집는 방법에 대해 알 수 있는 문제다.기억해두면 쓸모가 있을 듯.   ❒ Solve1.  재귀 구조public ListNode reverse(ListNode node, ListNode prev) { if (node == null) { return prev; } ListNode next = node.next; node.next = prev; return reverse(next, node);} `1 → 2 → 3`를 reverse 하는 케이스1st 2nd3rd4thnod..
[LeetCode#21] Merge Two Sorted Lists
·
Algorithm/문제풀이
❒ Description제목Merge Two Sorted Lists링크https://leetcode.com/problems/merge-two-sorted-lists/description/자료구조선형 자료구조푼 날짜7/24     ❒ Solution1. Brute Force public static void main(String[] args) { // create Test Case ListNode node1_3 = new ListNode(4, null); ListNode node1_2 = new ListNode(2, node1_3); ListNode node1_1 = new ListNode(1, node1_2); ListNode node2_3 = new ListNode(4, nu..
[LeetCode#234] Palindrome Linked List
·
Algorithm/문제풀이
❒ Description제목Palindrome Linked List링크https://leetcode.com/problems/palindrome-linked-list/description/자료구조선형자료 구조 (연결 리스트)푼 날짜7/24    ❒ Solution1. Deque 자료구조를 사용한 풀이public static boolean solve(ListNode head) { Deque deque = new LinkedList(); ListNode node = head; while (node != null) { deque.add(node.val); node = node.next; } while (!deque.isEmpty() && deque.size()..
[LeetCode#121] Best Time to Buy and Sell Stock
·
Algorithm/문제풀이
❒ Description제목Best Time to Buy and Sell Stock링크https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/자료구조선형 자료구조풀 날짜07/24    ❒ Solutionpublic static int maxProfit(int[] prices) { int maxProfit = 0; int minPrice = prices[0]; for (int price : prices) { minPrice = Math.min(minPrice, price); maxProfit = Math.max(maxProfit, price - minPrice); } return..