[LeetCode#46] Permutation
·
Algorithm/문제풀이
❒ Description제목Permutation링크https://leetcode.com/problems/permutations/description/자료구조비선형 (그래프)시간복잡도 O(n!)Permutation(순열)이란, 모든 가능한 경우를 그래프 형태로 나열한 결과라고 할 수 있다.이 문제는 BackTraking을 통해서 해결하는 문제인데, 모든 가능한 순열을 생성하기 때문에nums의 크기가 커질수록 시간이 급격히 증가한다. ❒ Solution1. Solution1public class Solution { public List> permute(int[] nums) { List> results = new ArrayList(); List elements = Arrays.s..
[프로그래머스#42576] 완주하지 못한 선수
·
Algorithm/문제풀이
❒ Description제목완주하지 못한 선수링크https://school.programmers.co.kr/learn/courses/30/lessons/42576자료구조선형 (해시)시간복잡도O(n + k)이번 문제는 해시맵을 사용하여 간단하게 해결 할 수 있는 문제인데 다른 풀이에서 HashMap을 생성할 때 Stream을 이용한 풀이가 있어서 메모.   ❒ Map 생성Code 1다음과 같은 배열 `"A", "B", "C", "A", "C", "A"`에서 각 요소가 몇 번 나왔는지를 Map 자료구조로 표현할 때나는 주로 아래와 같이 코드를 작성했다.Map counter = new HashMap();for (String name : students) { counter.put(name, counter.get..
[LeetCode#973] K Closest Points to Origin
·
Algorithm/문제풀이
❒ Description제목K Closest Points to Origin링크https://leetcode.com/problems/k-closest-points-to-origin/description/자료구조선형 (우선순위 큐)시간복잡도O(nlogn) 이번 문제는 우선순위 큐를 사용하여 해결하는 문제이다. Sort, Heap, Quick Select를 사용하는구현도 있는데 이것에 대해서는 해당 섹션 공부할 때 같이 볼 예정이다. 이 문제는 Comparator와 Comparable 중 하나를 사용해서 우선순위를 결정해줘야 한다.Comparator를 통해서 우선순위를 정해줄 경우 main method에서 `Comparator#compare`메서드를제정의 해줘야해서 DDD에 어긋난다. 반면에 Comparabl..
[LeetCode#23] Merge K sorted Lists
·
Algorithm/문제풀이
❒ Description제목Merge K sorted Lists링크https://leetcode.com/problems/merge-k-sorted-lists/description/자료구조선형 (우선순위 큐)시간복잡도O(nlogk) 이번 문제에서도 가상의 노드 Root를 사용되었다. 이쯤 느껴지는 것은 뭔가 정렬이 들어가는경우에는 가상의 노드 접근을 생각해보는 것도 좋은 거 같다. 아무튼 문제의 핵심은 자료구조 Priority Queue를 사용해서 해결하는 문제였다.Priory Queue에 대한 정리는 이곳에 할 것이다.    ❒ Solution전체 코드더보기public ListNode mergeKLists(ListNode[] lists) { PriorityQueue pq = new PriorityQ..
[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..