File tree Expand file tree Collapse file tree 6 files changed +192
-0
lines changed
search-in-rotated-sorted-array Expand file tree Collapse file tree 6 files changed +192
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .ArrayList ;
2+ import java .util .LinkedList ;
3+ import java .util .List ;
4+ import java .util .Queue ;
5+
6+ class Solution {
7+
8+ // TC: O(numCourses + E) E는 prerequisites 배열의 길이 (즉, 간선의 개수).
9+ // SC: O(numCourses + E)
10+ public boolean canFinish (int numCourses , int [][] prerequisites ) {
11+ int [] inDegree = new int [numCourses ];
12+ List <List <Integer >> adj = new ArrayList <>();
13+
14+ for (int i = 0 ; i < numCourses ; i ++) {
15+ adj .add (new ArrayList <>());
16+ }
17+
18+ for (int [] pre : prerequisites ) {
19+ adj .get (pre [1 ]).add (pre [0 ]);
20+ inDegree [pre [0 ]]++;
21+ }
22+
23+ Queue <Integer > queue = new LinkedList <>();
24+ for (int i = 0 ; i < numCourses ; i ++) {
25+ if (inDegree [i ] == 0 ) {
26+ queue .add (i );
27+ }
28+ }
29+ for (int i = 0 ; i < numCourses ; i ++) {
30+ if (queue .isEmpty ()) {
31+ return false ;
32+ }
33+ int course = queue .poll ();
34+
35+ for (int nextCourse : adj .get (course )) {
36+ inDegree [nextCourse ]--;
37+ if (inDegree [nextCourse ] == 0 ) {
38+ queue .add (nextCourse );
39+ }
40+ }
41+ }
42+ return queue .isEmpty ();
43+ }
44+ }
45+
Original file line number Diff line number Diff line change 1+ class Solution {
2+
3+ // TC : O(N)
4+ // SC : O(1)
5+ public TreeNode invertTree (TreeNode root ) {
6+ if (root == null ) {
7+ return null ;
8+ }
9+
10+ TreeNode tmp = root .left ;
11+ root .left = root .right ;
12+ root .right = tmp ;
13+
14+ invertTree (root .right );
15+ invertTree (root .left );
16+
17+ return root ;
18+ }
19+
20+ }
21+
Original file line number Diff line number Diff line change 1+ class Solution {
2+
3+ public boolean canJump (int [] nums ) {
4+ int furthestIndex = 0 ;
5+ for (int i = 0 ; i < nums .length ; i ++) {
6+ if (furthestIndex < i ) {
7+ return false ;
8+ }
9+
10+ furthestIndex = Math .max (furthestIndex , i + nums [i ]);
11+ }
12+ return true ;
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ import java .util .PriorityQueue ;
2+
3+ /**
4+ * Definition for singly-linked list. public class ListNode { int val; ListNode next; ListNode() {}
5+ * ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val;
6+ * this.next = next; } }
7+ */
8+
9+ // TC : PQ -> O(NlogN)
10+ // SC: Linked list -> O(N)
11+ class Solution {
12+
13+ public ListNode mergeKLists (ListNode [] lists ) {
14+ if (lists == null || lists .length == 0 ) {
15+ return null ;
16+ }
17+ PriorityQueue <Integer > pq = new PriorityQueue <>();
18+
19+ for (int i = 0 ; i < lists .length ; i ++) {
20+ while (lists [i ] != null ) {
21+ pq .add (lists [i ].val );
22+ lists [i ] = lists [i ].next ;
23+ }
24+ }
25+ ListNode dummy = new ListNode (0 );
26+ ListNode current = dummy ;
27+ while (!pq .isEmpty ()) {
28+ current .next = new ListNode (pq .poll ());
29+ current = current .next ;
30+ }
31+
32+ return dummy .next ;
33+ }
34+ }
35+
Original file line number Diff line number Diff line change 1+ class Solution {
2+
3+ // TP: O(N+M)
4+ // SP: O(1)
5+ public String minWindow (String s , String t ) {
6+ int [] tFreq = new int [128 ];
7+ for (char c : t .toCharArray ()) {
8+ tFreq [c ]++;
9+ }
10+
11+ int left = 0 ;
12+ int right = 0 ;
13+ int minLen = Integer .MAX_VALUE ;
14+ int minLeft = 0 ;
15+ int count = 0 ;
16+ int tLen = t .length ();
17+
18+ while (right < s .length ()) {
19+ if (tFreq [s .charAt (right ++)]-- > 0 ) { // tFreq[s.charAt(right++)]-- > 0 -> s.charAt(right) is in t
20+ count ++; // s의 문자가 t에 있기 때문에 count를 증가시킨다.
21+ }
22+
23+ if (count == tLen ) {
24+ // 아래 while문은 left를 옮기면서 s의 문자가 t에 있는지 확인한다. (최소 길이를 찾기 위해)
25+ while (left < right && tFreq [s .charAt (left )] < 0 ) { // tFreq[s.charAt(left)] < 0 -> s.charAt(left) is not in t
26+ tFreq [s .charAt (left ++)]++;
27+ }
28+
29+ if (right - left < minLen ) {
30+ minLen = right - left ;
31+ minLeft = left ;
32+ }
33+ tFreq [s .charAt (left ++)]++; // left 한 칸 올리기
34+ count --;
35+ }
36+
37+ }
38+
39+ return minLen == Integer .MAX_VALUE ? "" : s .substring (minLeft , minLeft + minLen );
40+
41+ }
42+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+
3+ // TC : O(logN)
4+ // SC : O(1)
5+ public int search (int [] nums , int target ) {
6+ int left = 0 ;
7+ int right = nums .length - 1 ;
8+
9+ while (left <= right ) {
10+ int mid = left + (right - left ) / 2 ;
11+ if (nums [mid ] == target ) {
12+ return mid ;
13+ }
14+
15+ if (nums [left ] <= nums [mid ]) {
16+ if (nums [left ] <= target && target < nums [mid ]) {
17+ right = mid - 1 ;
18+ } else {
19+ left = mid + 1 ;
20+ }
21+ } else {
22+ if (nums [mid ] < target && target <= nums [right ]) {
23+ left = mid - 1 ;
24+ } else {
25+ right = mid + 1 ;
26+ }
27+ }
28+
29+ }
30+
31+ return -1 ;
32+ }
33+ }
34+
35+
You can’t perform that action at this time.
0 commit comments