File tree Expand file tree Collapse file tree 4 files changed +112
-0
lines changed
lowest-common-ancestor-of-a-binary-search-tree
non-overlapping-intervals Expand file tree Collapse file tree 4 files changed +112
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int rob (int [] nums ) {
3+ // ์ธ์ ํ ๊ฒฝ์ฐ๋ ์ ์ธํ ์ต๋ ํฉ
4+ // ํ์ด: dp๋ก ํ์ดํ๋ค. dp[i] = max(dp[i-1], dp[i-2] + nums[i])
5+ // TC: O(N), SC: O(N)
6+ if (nums .length == 1 ) {
7+ return nums [0 ];
8+ }
9+
10+ var dp = new int [nums .length ];
11+ dp [0 ] = nums [0 ];
12+ dp [1 ] = Math .max (nums [0 ], nums [1 ]);
13+
14+ for (int i =2 ; i <nums .length ; i ++) {
15+ dp [i ] = Math .max (dp [i -1 ], dp [i -2 ] + nums [i ]);
16+ }
17+
18+ return dp [nums .length -1 ];
19+ }
20+ }
Original file line number Diff line number Diff line change 1+
2+ // Definition for a binary tree node.
3+ class TreeNode {
4+ int val ;
5+ TreeNode left ;
6+ TreeNode right ;
7+
8+ TreeNode (int x ) {
9+ val = x ;
10+ }
11+ }
12+
13+
14+ class Solution {
15+ public TreeNode lowestCommonAncestor (TreeNode root , TreeNode p , TreeNode q ) {
16+ // ํ์ด: p,q ๋๋ค root ๋ณด๋ค ๊ฐ์ด ์ ์ผ๋ฉด ์ผ์ชฝ ์๋ธํธ๋ฆฌ๋ฅผ, ๋๋ค ํฌ๋ฉด ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ๋ฅผ ํ์ํด์ผํ๋ค.
17+ // ๊ทธ๋ ์ง ์์ผ๋ฉด ํด๋น root๊ฐ ์ต์ ๊ณตํต ์กฐ์์ด ๋๋ค.
18+ // TC: O(H), H: ํธ๋ฆฌ์ ๋์ด
19+ // SC: O(1)
20+ var node = root ;
21+ while (node != null ) {
22+ if (p .val < node .val && q .val < node .val ) {
23+ node = node .left ;
24+ } else if (p .val > node .val && q .val > node .val ) {
25+ node = node .right ;
26+ } else {
27+ break ;
28+ }
29+ }
30+
31+ return node ;
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ import java .util .List ;
2+
3+ // Definition of Interval:
4+ class Interval {
5+ int start , end ;
6+ Interval (int start , int end ) {
7+ this .start = start ;
8+ this .end = end ;
9+ }
10+ }
11+
12+
13+ public class Solution {
14+ /**
15+ * @param intervals: an array of meeting time intervals
16+ * @return: if a person could attend all meetings
17+ */
18+ public boolean canAttendMeetings (List <Interval > intervals ) {
19+ // ํ์ด: ์ ๋ ฌ ํ ์์์ ๋น๊ตํด๊ฐ๋ฉฐ ์กฐ๊ฑด์ ๋ง์ง ์์ผ๋ฉด false ๋ฅผ ๋ฐํํ๋ค.
20+ // TC: O(N)
21+ // SC: O(1)
22+
23+ var sortedIntervals = intervals .stream ().sorted ().toList ();
24+
25+ for (int i =0 ; i <sortedIntervals .size ()-1 ; i ++) {
26+ if (sortedIntervals .get (i ).end > sortedIntervals .get (i +1 ).start ) {
27+ return false ;
28+ }
29+ }
30+ return true ;
31+ }
32+ }
Original file line number Diff line number Diff line change 1+ import java .util .Arrays ;
2+ import java .util .Comparator ;
3+
4+ class Solution {
5+ public int eraseOverlapIntervals (int [][] intervals ) {
6+ // ํ์ด: ์ ๋ ฌ ํ ๊ฐ ๊ตฌ๊ฐ์ ๋ ๊ฐ์ ์ ์ฅํด๊ฐ๋ฉฐ ์ ์์ ๋น๊ต, ์ ๊ฑฐ ๋์์ผ๋ ์นด์ดํธ๋ฅผ ์ฆ๊ฐ์์ผ ๋ฐํํ๋ค.
7+ // TC: O(N)
8+ // SC: O(1)
9+
10+ // intervals๋ฅผ ๊ฐ ๊ตฌ๊ฐ์ ๋ ๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌ
11+ Arrays .sort (intervals , Comparator .comparingInt (a -> a [1 ]));
12+
13+ int answer = 0 ;
14+ int end = intervals [0 ][1 ]; // ์ฒซ ๋ฒ์งธ ๊ตฌ๊ฐ์ ๋ ๊ฐ
15+
16+ // ๋ ๋ฒ์งธ ๊ตฌ๊ฐ๋ถํฐ ์ํํ๋ฉฐ ๊ฒน์น๋์ง ํ์ธ
17+ for (int i = 1 ; i < intervals .length ; i ++) {
18+ if (intervals [i ][0 ] < end ) { // ํ์ฌ ๊ตฌ๊ฐ์ด ์ด์ ๊ตฌ๊ฐ๊ณผ ๊ฒน์น๋ฉด
19+ answer ++; // ์ ๊ฑฐ ํ์๋ฅผ ์ฆ๊ฐ
20+ } else {
21+ end = intervals [i ][1 ]; // ๊ฒน์น์ง ์์ผ๋ฉด ํ์ฌ ๊ตฌ๊ฐ์ ๋ ๊ฐ์ ๊ฐฑ์
22+ }
23+ }
24+
25+ return answer ;
26+ }
27+ }
You canโt perform that action at this time.
0 commit comments