File tree 5 files changed +135
-0
lines changed
search-in-rotated-sorted-array
5 files changed +135
-0
lines changed Original file line number Diff line number Diff line change
1
+ // 사이클을 찾는 문제. DFS를 이용해서 방문했는지 여부를 관리하고 체크하면된다.
2
+ // DFS를 진행 중인 것을 들고 있어야 사이클 여부를 판단할 수 있다.
3
+ // 간선 수와 노드의 수에 따라 시간 복잡도가 결정됨 O(N+L)
4
+ class Solution {
5
+ public boolean canFinish (int numCourses , int [][] prerequisites ) {
6
+ List <List <Integer >> graph = new ArrayList <>();
7
+ for (int i = 0 ; i < numCourses ; i ++) {
8
+ graph .add (new ArrayList <>());
9
+ }
10
+
11
+ for (int [] pair : prerequisites ) {
12
+ graph .get (pair [1 ]).add (pair [0 ]);
13
+ }
14
+
15
+ // 0: 방문 X
16
+ // -1: DFS 진행 중
17
+ // 1: 방문 완료
18
+ int [] visited = new int [numCourses ];
19
+
20
+ // 모든 노드에서 DFS 수행
21
+ for (int i = 0 ; i < numCourses ; i ++) {
22
+ if (dfs (graph , visited , i )) return false ;
23
+ }
24
+
25
+ return true ;
26
+ }
27
+
28
+ private boolean dfs (List <List <Integer >> graph , int [] visited , int node ) {
29
+ if (visited [node ] == -1 ) return true ; // 방문 중이면 사이클이 발견
30
+ if (visited [node ] == 1 ) return false ;
31
+
32
+ visited [node ] = -1 ; // 진행 중 표기
33
+ for (int next : graph .get (node )) {
34
+ if (dfs (graph , visited , next )) return true ;
35
+ }
36
+ visited [node ] = 1 ;
37
+
38
+ return false ;
39
+ }
40
+ }
Original file line number Diff line number Diff line change
1
+ // 트리의 형태를 바꾸는건 대부분 DFS로 푸는게 공간복잡도 측면에서 유리하다.
2
+ // BFS로도 풀이 가능하지만, BFS는 대부분 큐를 이용해서 공간복잡도가 높아진다.
3
+ class Solution {
4
+ public TreeNode invertTree (TreeNode root ) {
5
+ if (root == null ) return null ;
6
+
7
+ TreeNode temp = root .left ;
8
+ root .left = invertTree (root .right );
9
+ root .right = invertTree (temp );
10
+
11
+ return root ;
12
+ }
13
+ }
Original file line number Diff line number Diff line change
1
+ // 현재 위치에서 최대로 갈 수 있는 인덱스의 이력을 갱신하면서 최대 범위를 넘지 않았나를 계속 판별하면 됨
2
+ // 처음엔 DP인줄 알았는데 DP 배열 없이 그냥 풀림
3
+ class Solution {
4
+ public boolean canJump (int [] nums ) {
5
+ int maxReach = 0 ;
6
+
7
+ for (int i = 0 ; i < nums .length ; i ++) {
8
+ if (i > maxReach ) {
9
+ return false ;
10
+ }
11
+
12
+ maxReach = Math .max (maxReach , i + nums [i ]);
13
+ if (maxReach >= nums .length - 1 ) {
14
+ return true ;
15
+ }
16
+ }
17
+
18
+ return true ;
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ // 우선순위 큐로 다 합쳐버린 다음에
2
+ // 큐를 순회돌면서 연결리스트를 재생성한다. 성능은 비교적 낮게 잡힘
3
+ // 분할 정복법으로 풀면 더 좋은 성능이 나온다고 한다.
4
+ class Solution {
5
+ public ListNode mergeKLists (ListNode [] lists ) {
6
+ PriorityQueue <ListNode > pq = new PriorityQueue <>((a , b ) -> a .val - b .val );
7
+
8
+ for (ListNode node : lists ) {
9
+ if (node != null ) pq .offer (node );
10
+ }
11
+
12
+ ListNode newLists = new ListNode (-1 );
13
+ ListNode curr = newLists ;
14
+
15
+ while (!pq .isEmpty ()) {
16
+ ListNode minNode = pq .poll ();
17
+ curr .next = minNode ;
18
+ curr = curr .next ;
19
+
20
+ if (minNode .next != null ) {
21
+ pq .offer (minNode .next );
22
+ }
23
+ }
24
+
25
+ return newLists .next ;
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ // O(log n)의 시간복잡도를 문제에서 요구하고 있다.
2
+ // O(log n)의 시간복잡도를 갖는 탐색 문제는 대부분 바이너리 서치로 해결된다.
3
+ // 주어진 배열이 정렬되어있다는 점에서 이진탐색을 이용하면 된다는 것을 알 수 있다.
4
+ class Solution {
5
+ public int search (int [] nums , int target ) {
6
+ int left = 0 , right = nums .length - 1 ;
7
+
8
+ while (left <= right ) {
9
+ int mid = left + (right - left ) / 2 ;
10
+
11
+ if (nums [mid ] == target ) {
12
+ return mid ;
13
+ }
14
+
15
+ // 왼쪽이 정렬된 경우
16
+ if (nums [left ] <= nums [mid ]) {
17
+ if (nums [left ] <= target && target < nums [mid ]) {
18
+ right = mid - 1 ;
19
+ } else {
20
+ left = mid + 1 ;
21
+ }
22
+ }
23
+ // 오른쪽이 정렬된 경우
24
+ else {
25
+ if (nums [mid ] < target && target <= nums [right ]) {
26
+ left = mid + 1 ;
27
+ } else {
28
+ right = mid - 1 ;
29
+ }
30
+ }
31
+ }
32
+
33
+ return -1 ;
34
+ }
35
+ }
You can’t perform that action at this time.
0 commit comments