본문 바로가기

분류 전체보기

(86)
[Medium] 142. Linked List Cycle II Leetcode 142번 문제에서는 Linked List가 주어지고, 주어진 Linked List에서 cycle이 발생하는지 알아내고, 발생하면 발생 지점의 노드를 반환하고, cycle이 발생 하지 않으면 null를 return해주면 된다. #풀이 public class Solution { public ListNode detectCycle(ListNode head) { HashSet hs = new HashSet(); boolean dup = false; while(head != null){ if(!hs.add(head)){ //HashSet에 add가 중복이 발생해 false return시 현재노드 반환 return head; } head = head.next; } return null; } } 이 풀..
[Easy] 35. Search Insert Position Leetcode 35번 문제는 주어진 정렬된 array에서 target을 찾는 문제이다. 만약 target이 array안에 없다면, insertion point를 찾는 문제이다. Searching algorithm의 time complexity는 O(log n) 이어야 한다. Time complextiy와 array가 sorted 돼있다는 점을 고려하면 Binary Search Algoritm를 써서 풀수 있다. #풀이 public int searchInsert(int[] A, int target) { int low = 0, high = A.length-1; while(low target) high = mid-1; else low = mid+1; } return low; } Binary Search후에 ..
[Easy] 100. Same Tree Leetcode 100번 문제는 이진 트리 문제이다. 2개의 이진 트리의 root가 주어진다. 두 개의 이진 트리가 똑같은지 알아내는 문제이다. 여기서 똑같다는 정의는 트리의 형태가 같고 모든 노드의 값들도 같아야 한다. #풀이 class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if (q == null && p == null){ return true; } if(q != null && p == null || q == null && p != null){ return false; } if(p.val != q.val){ return false; } return isSameTree(p.left, q.left) && isSameTree(p.r..
[Lecture 1-2] Processes Part II (Process State) #Process State 프로세스의 상태는 크게 3가지, 세분화 되었을 때는 많으면 5가지에서 15가지 까지 나뉠 수 있다. 프로세스 상태의 개수는 OS 아키텍쳐 마다 다르게 디자인 되어있기 때문에 OS 마다 다를 수 있다. 우리는 크게 3가지 process state를 알아보자. Running 프로세스가 하나의 명령어를 할당 받아 CPU에서 실행 시키고 있는 상태 Ready 프로세스가 운영체제의 승인을 받고 CPU에서 실행이 준비된 상태 (CPU 지정을 기다리는 상태/No CPU is ready at this state, CPU 제외 다른 자원들은 프로세스의 메모리 로드 상태) Blocked (waiting) 프로세스가 어떠한 event (보통 I/O 요청에 의한 syscall , 또는 동기신호[sy..
[Lecture 1-1] Processes Part I #프로세스란? 컴퓨터 운영체제에서 프로세스란 컴퓨터에서 실행되고 있는 컴퓨터 프로그램을 말한다. 프로그램과 프로세스의 차이는 컴퓨터에 다운로드만 돼있는 명령어와 정적 데이터는 프로그램이고, 그 프로그램을 실행시키는 순간 프로세스가 된다. 운영체제는 각 프로세스 마다 locial flow를 부여하고 single-thread 프로그램은 하나, multi-thread 프로그램에는 여러 개의 flow를 부여한다. 또, 운영체제는 private, protected address space와 같은 memory address를 부여하고 file descriptor와 같은 abstracted resources도 부여한다. # Context Switching Context switching은 CPU가 현재 실행하고 있는..
[Easy] 141. Linked List Cycle Leetcode 141번 문제는 주어진 Linkedlist에 cycle ,즉 순환인 발생하는지 알아내는 문제이다. 위의 예제처럼 어느 한 노드에서 뒤에 노드로 뒤돌아가 반복된다면 cycle이 발생한 것이므로 true를 return 하면 된다. #풀이 Hashset을 사용한 풀이 public class Solution { public boolean hasCycle(ListNode head) { HashSet set = new HashSet(); boolean isCycle = false; while(head != null){ isCycle = set.add(head); head = head.next; if(!isCycle){ return !isCycle; } } return !isCycle; } } Set..
[Easy] 206. Reverse Linked List Leetcode 206번 문제는 주어진 연결 리스트를 반대로 뒤집는 문제이다. 연결 리스트의 reference to the head가 주어진다. #풀이 # Iteratively (반복) 풀이 class Solution { public ListNode reverseList(ListNode head) { ListNode prev = null; while(head != null){ ListNode nextNode = head.next; head.next = prev; prev = head; head = nextNode; } return prev; } } # recursively (재귀적) 풀이 class Solution { public ListNode reverseList(ListNode head) { //..
[Easy]1672. Richest Customer Wealth Leetcode 1672번 문제는 integer array를 담고 있는 array가 주어진다. 각 array를 더했을 때 가장 값이 큰 값을 return 하면 된다. 간단한 nested for-loop의로 풀 수 있다. class Solution { public int maximumWealth(int[][] accounts) { int res = 0; for(int i =0;i