File tree 5 files changed +231
-0
lines changed
search-in-rotated-sorted-array
5 files changed +231
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ *
3
+ * solution : topological sort
4
+ * tc : O(E + V)
5
+ * sc : O(E + V)
6
+ *
7
+ * */
8
+ class Solution {
9
+ public boolean canFinish (int numCourses , int [][] prerequisites ) {
10
+ List <Integer >[] adj = new ArrayList [numCourses ];
11
+ for (int i = 0 ; i < numCourses ; i ++) {
12
+ adj [i ] = new ArrayList <>();
13
+ }
14
+ boolean [] v = new boolean [numCourses ];
15
+ int [] indeg = new int [numCourses ];
16
+ for (int [] pre : prerequisites ) {
17
+ int src = pre [0 ];
18
+ int dst = pre [1 ];
19
+ adj [src ].add (dst );
20
+ indeg [dst ]++;
21
+ }
22
+ Queue <Integer > q = new LinkedList <>();
23
+ for (int i = 0 ; i < numCourses ; i ++) {
24
+ if (indeg [i ] == 0 ) {
25
+ q .add (i );
26
+ v [i ] = true ;
27
+ }
28
+ }
29
+ int cnt = 0 ;
30
+ while (!q .isEmpty ()) {
31
+ int curNode = q .poll ();
32
+ cnt ++;
33
+ for (int dst : adj [curNode ]) {
34
+ indeg [dst ]--;
35
+ if (indeg [dst ] ==0 && !v [dst ]) {
36
+ q .add (dst );
37
+ }
38
+ }
39
+ }
40
+
41
+ return cnt == numCourses ;
42
+
43
+ }
44
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * public class TreeNode {
4
+ * int val;
5
+ * TreeNode left;
6
+ * TreeNode right;
7
+ * TreeNode() {}
8
+ * TreeNode(int val) { this.val = val; }
9
+ * TreeNode(int val, TreeNode left, TreeNode right) {
10
+ * this.val = val;
11
+ * this.left = left;
12
+ * this.right = right;
13
+ * }
14
+ * }
15
+ */
16
+ class Solution {
17
+ public TreeNode invertTree (TreeNode root ) {
18
+ if (root == null ) return null ;
19
+ TreeNode left = invertTree (root .left );
20
+ TreeNode right = invertTree (root .right );
21
+ root .right = left ;
22
+ root .left =right ;
23
+ return root ;
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ * solution : dp
3
+ * tc : O(n)
4
+ * sc : O(n)
5
+ *
6
+ * let dp[i] farthest point available to reach
7
+ *
8
+ * */
9
+ class Solution {
10
+ public boolean canJump (int [] nums ) {
11
+ int [] dp = new int [nums .length ];
12
+ dp [0 ] = nums [0 ];
13
+ for (int i = 1 ; i < nums .length ; i ++) {
14
+ if (dp [i -1 ] >= i ) {
15
+ dp [i ] = Math .max (nums [i ] + i , dp [i -1 ]);
16
+ }
17
+ }
18
+ return dp [dp .length -1 ] >= dp .length -1 ;
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * public class ListNode {
4
+ * int val;
5
+ * ListNode next;
6
+ * ListNode() {}
7
+ * ListNode(int val) { this.val = val; }
8
+ * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9
+ * }
10
+ */
11
+ class Solution {
12
+ public ListNode mergeKLists (ListNode [] lists ) {
13
+ List <ListNode > list = new LinkedList <>();
14
+ for (ListNode l : lists ) {
15
+ list .add (l );
16
+ }
17
+ return divide (list );
18
+
19
+ }
20
+ public ListNode divide (List <ListNode > list ) {
21
+ //두개씩 나누기. 두개 안되면 그냥 리스트에 넣고 리턴
22
+ int n = list .size ();
23
+ ListNode sum1 ;
24
+ ListNode sum2 ;
25
+ if (list .size () <= 2 ) {
26
+ return merge (list );
27
+ }
28
+
29
+ int mid = n / 2 ;
30
+ List <ListNode > upper = new LinkedList <>();
31
+ List <ListNode > last = new LinkedList <>();
32
+
33
+
34
+ for (int i = 0 ; i < mid ; i ++) {
35
+ upper .add (list .get (i ));
36
+
37
+ }
38
+ sum1 = divide (upper );
39
+
40
+
41
+ for (int i = mid ; i < n ; i ++) {
42
+ last .add (list .get (i ));
43
+ }
44
+ sum2 = divide (last );
45
+
46
+ List <ListNode > m = new LinkedList <>();
47
+ m .add (sum1 );
48
+ m .add (sum2 );
49
+
50
+
51
+ return merge (m );
52
+
53
+
54
+ }
55
+ public ListNode merge (List <ListNode > list ) {
56
+ //edge
57
+ if (list .size ()==0 ) return null ;
58
+ if (list .size () == 1 ) return list .get (0 );
59
+
60
+ ListNode n1 = list .get (0 );
61
+ ListNode n2 = list .get (1 );
62
+ // System.out.println(n1.val + " : " + n2.val);
63
+
64
+ ListNode res = new ListNode (0 );
65
+ ListNode head = res ;
66
+
67
+ while (n1 != null && n2 != null ) {
68
+ if (n1 .val < n2 .val ){
69
+ res .next = n1 ;
70
+ res = res .next ;
71
+ n1 = n1 .next ;
72
+ } else {
73
+ res .next = n2 ;
74
+ res = res .next ;
75
+ n2 = n2 .next ;
76
+ }
77
+
78
+ }
79
+ if (n1 == null ) res .next = n2 ;
80
+ if (n2 == null ) res .next = n1 ;
81
+ return head .next ;
82
+
83
+
84
+
85
+ }
86
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+
3
+ input : rotated array
4
+ output : index of target else -1
5
+
6
+ 4 5 6 7 0 1 2
7
+ target = 6
8
+ return 2
9
+
10
+ solution1) brute force
11
+ return index after loop
12
+ tc : O(n)
13
+ sc : O(1)
14
+
15
+ solution2)
16
+ separte array into 2 parts to make sure each part is sorted.
17
+ find the rotated point, binary search for target
18
+ O(logn) + O(logn)
19
+
20
+ tc : O(logn)
21
+ sc : O(1)
22
+ */
23
+ class Solution {
24
+ public int search (int [] nums , int target ) {
25
+ int l = 0 ;
26
+ int r = nums .length - 1 ;
27
+ while (l < r ) {
28
+ int mid = (r - l ) / 2 +l ;
29
+ if (nums [mid ] <= nums [r ]) {
30
+ r = mid ;
31
+ } else {
32
+ l = mid + 1 ;
33
+ }
34
+ }
35
+ // determine which part
36
+ int start = 0 ;
37
+ int end = nums .length - 1 ;
38
+ if (nums [start ] <= target && nums [Math .max (0 , l -1 )] >= target ) {
39
+ end = Math .max (0 , l - 1 );
40
+ } else if (nums [l ] <= target && nums [end ] >= target ){
41
+ start = l ;
42
+ }
43
+
44
+ while (start <= end ) {
45
+ int mid = (end - start ) / 2 + start ;
46
+ if (nums [mid ] == target ) {
47
+ return mid ;
48
+ } else if (nums [mid ] < target ) {
49
+ start = mid + 1 ;
50
+ } else {
51
+ end = mid - 1 ;
52
+ }
53
+ }
54
+ return -1 ;
55
+ }
56
+ }
You can’t perform that action at this time.
0 commit comments