2023/09/12 3

[Leetcode Top Interview 150] 80. Remove Duplicates from Sorted Array II

난이도 : medium 문제링크 int array인 nums가 주어진다 (non-decreasing order) 중복 요소를 지워라 (in-place) 두번까지 나타난 요소까지만 중복 표기 가능 순서를 유지하라 1. 접근법 in-place 문제 배열을 인덱스로 접근하여 풀기 non-decreasing이며, 두개까지 중복허용이 가능하므로 투포인터를 사용하여 위치를 설정하기 2. 의사코드 int i=0; while (left

Algorithm/(Java) PS 2023.09.12

[Leetcode Top Interview 150] 373. Find K Pairs with Smallest Sums

난이도 : medium 문제링크 2개의 integer 리스트 nums1, nums2와 숫자k가 주어진다 리스트는 non-decreasing order이다 합이 작은 순서대로 k 개의 두 배열의 원소로 구성된 pair들을 리턴하세요 1. 접근법 합이 작으려면, 작은 숫자부터 뽑아서 pair를 구성해야 함 최소힙 자료구조를 사용하여 pair를 만든다 2. 의사코드 // 좌표의 합을 가지고 오름차순한 우선순위 큐를 생성한다 PriorityQueue pq = new PriorityQueue( (a, b) -> (a[0] + a[1]) - (b[0] + b[1]) ); // pq에 원소들을 모두 넣고 for (int n1 : nums1) { for (int n2 : nums2) { pq.offer(new int[..

Algorithm/(Java) PS 2023.09.12

[Leetcode Top Interview 150] 215. Kth Largest Element in an Array

난이도 : medium 문제링크 int array인 nums와 int값인 k가 주어진다 nums 배열에서 k번째로 큰 값을 리턴하라 1. 접근법 유일한 값이 아닌, 중복된 값을 포함하여 리턴해야 함 최대 힙을 구현하여, k번째 pop된 값을 리턴하기 2. 의사코드 1. Heap 클래스 구현 public void push(int e) { arr[n++] = e; bubbleUp(n-1); } public void pop() { int top = arr[n-1]; arr[0] = top; n--; bubbleDown(0); return top; } private void bubbleUp(int i) { if (i가 0보다 작거나 같으면) return; int parentIdx = (i-1)/2; if (ar..

Algorithm/(Java) PS 2023.09.12