File tree Expand file tree Collapse file tree 3 files changed +114
-0
lines changed
remove-nth-node-from-end-of-list Expand file tree Collapse file tree 3 files changed +114
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .ArrayList ;
2+ import java .util .Arrays ;
3+ import java .util .Comparator ;
4+ import java .util .List ;
5+
6+ class Solution {
7+
8+ public int [][] merge (int [][] intervals ) {
9+ // TC: O(N log N)
10+ // SC: O(N)
11+
12+ // length가 2보다 적으면 그대로 반환
13+ if (intervals .length < 2 ) {
14+ return intervals ;
15+ }
16+
17+ List <int []> output = new ArrayList <>();
18+
19+ // intervals 배열을 시작 시간 기준으로 정렬
20+ Arrays .sort (intervals , Comparator .comparingInt (a -> a [0 ]));
21+
22+ for (int [] interval : intervals ) {
23+ // output이 비어있거나, 현재 interval이 마지막에 추가된 구간과 겹치지 않으면 추가
24+ if (output .isEmpty () || output .get (output .size () - 1 )[1 ] < interval [0 ]) {
25+ output .add (interval );
26+ } else {
27+ // 겹치는 경우, 마지막 구간의 끝 시간을 업데이트
28+ output .get (output .size () - 1 )[1 ] = Math .max (output .get (output .size () - 1 )[1 ], interval [1 ]);
29+ }
30+ }
31+
32+ // List<int[]>를 int[][] 배열로 변환하여 반환
33+ return output .toArray (new int [output .size ()][]);
34+ }
35+ }
Original file line number Diff line number Diff line change 1+ import java .util .LinkedList ;
2+
3+ // Definition for singly-linked list.
4+ class ListNode {
5+ int val ;
6+ ListNode next ;
7+ ListNode () {}
8+ ListNode (int val ) { this .val = val ; }
9+ ListNode (int val , ListNode next ) { this .val = val ; this .next = next ; }
10+ }
11+
12+ class Solution {
13+
14+ public static void main (String [] args ) {
15+ Solution s = new Solution ();
16+ var node = new ListNode (1 , new ListNode (2 ));
17+ s .removeNthFromEnd (node , 1 );
18+ }
19+
20+ public ListNode removeNthFromEnd (ListNode head , int n ) {
21+ // 문제: 링크드리스트의 head가 주어지면 끝에서 N번째 노드를 삭제한 결과를 반환하라
22+ // 풀이: n번째만큼 first 이동, 그 후 second를 1칸씩 함께 이동 시킨다 first가 끝에 도달할 때 까지
23+ // 전체 길이 L, 주어진 N이 있을때 이렇게 하면 L - N - 1 위치를 구할 수 있다.
24+ // TC: O(N)
25+ // SC: O(1)
26+ var dummy = new ListNode (-1 , head );
27+ var first = head ;
28+ for (int i =0 ; i <n ; i ++) {
29+ first = first .next ;
30+ }
31+
32+ var second = dummy ;
33+
34+ while (first != null ) {
35+ first = first .next ;
36+ second = second .next ;
37+ }
38+
39+ second .next = second .next .next ;
40+ return dummy .next ;
41+ }
42+
43+ }
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+ 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+
18+ public boolean isSameTree (TreeNode p , TreeNode q ) {
19+ // 풀이: 재귀로 left와 right를 비교하면서 같은지 확인한다.
20+ // TC: O(N)
21+ // SC: O(N)
22+ return dfs (p , q );
23+ }
24+
25+ private boolean dfs (TreeNode p , TreeNode q ) {
26+ if (p == null || q == null ) {
27+ return p == q ; // 둘 다 null이면 true, 하나만 null이면 false
28+ }
29+
30+ if (p .val != q .val ) { // 값이 다르면 false
31+ return false ;
32+ }
33+
34+ return dfs (p .left , q .left ) && dfs (p .right , q .right );
35+ }
36+ }
You can’t perform that action at this time.
0 commit comments