Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions leetcode2/2medium/최원준/Q16.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package Leetcode.최원준;

/*
1. 아이디어 :
정렬 후, 왼쪽 숫자는 순회를 하고, 중간 + 오른쪽 숫자는 투포인터로 합을 구한다.

2. 시간복잡도 :
O( n^2 )

3. 자료구조/알고리즘 :
- / 투포인터
*/

import java.util.Arrays;

public class Q16 {
class Solution {
public int threeSumClosest(int[] nums, int target) {
int maxDiff = Integer.MAX_VALUE, ans = -1;
Arrays.sort(nums);
int n = nums.length;

for (int left = 0; left < n; left ++) {
int mid = left+1, right = n-1;

while (mid < right) {
int total = nums[left] + nums[mid] + nums[right];
int cDiff = Math.abs(total - target);
if (cDiff < maxDiff) {
maxDiff = cDiff;
ans = total;
}
if (total < target) {
mid++;
} else {
right--;
}
}
}
return ans;
}
}
}
48 changes: 48 additions & 0 deletions leetcode2/2medium/최원준/Q210_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package Leetcode.최원준;

/*
1. 아이디어 :
degrees 배열을 만들어서 각 과목의 선행과목 개수를 저장한다.

2. 시간복잡도 :
O( n + p ) // n: 과목 개수, p: 선행과목 개수

3. 자료구조/알고리즘 :
큐 / bfs
*/

import java.util.*;

public class Q210_2 {
class Solution {
public int[] findOrder(int n, int[][] prerequisites) {
Map<Integer, List<Integer>> aftCourses = new HashMap<>();
int[] degrees = new int[n];

for (int[] p : prerequisites) {
int aft = p[0], pre = p[1];
degrees[aft]++;
aftCourses.putIfAbsent(pre, new ArrayList<>());
aftCourses.get(pre).add(aft);
}

int[] ans = new int[n];
int idx = 0;

Deque<Integer> deque = new ArrayDeque<>();
for (int i=0; i<n; i++) if (degrees[i] == 0) deque.add(i);

while (!deque.isEmpty()) {
int c = deque.pollFirst();
ans[idx++] = c;

for (int aftCourse : aftCourses.getOrDefault(c, new ArrayList<>())) {
degrees[aftCourse]--;
if (degrees[aftCourse] == 0) deque.add(aftCourse);
}
}

return idx==n ? ans : new int[]{};
}
}
}
77 changes: 77 additions & 0 deletions leetcode2/2medium/최원준/Q380.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package Leetcode.최원준;

/*
1. 아이디어 :
삭제시:
- 삭제할 숫자의 인덱스를 찾는다.
- 삭제할 숫자의 인덱스에 마지막 숫자를 넣는다.
- 마지막 숫자의 인덱스를 삭제할 숫자의 인덱스로 업데이트한다.
- 마지막 숫자를 삭제한다.

2. 시간복잡도 :
O( 1 ) // insert, remove

3. 자료구조/알고리즘 :
해시맵 / -

*/

import java.util.*;

public class Q380 {
class Dummy {
int val;
public Dummy(int val) {
this.val = val;
}
}

class RandomizedSet {
Random rand = new Random();
Map<Integer, Integer> numToIndex;
List<Dummy> dummyList = new ArrayList<>();

public RandomizedSet() {
numToIndex = new HashMap<>();
}

public boolean insert(int val) {
if (!numToIndex.containsKey(val)) {
Dummy dum = new Dummy(val);
numToIndex.put(val, dummyList.size());
dummyList.add(dum);
return true;
}
return false;
}

public boolean remove(int val) {
if (numToIndex.containsKey(val)) {
int idx = numToIndex.get(val);

dummyList.get(idx).val = dummyList.get(dummyList.size()-1).val;

int lastVal = dummyList.get(dummyList.size()-1).val;
numToIndex.put(lastVal, idx);
numToIndex.remove(val);

dummyList.remove(dummyList.size()-1);
return true;
}
return false;
}

public int getRandom() {
int randIdx = rand.nextInt(dummyList.size());
return dummyList.get(randIdx).val;
}
}

/**
* Your RandomizedSet object will be instantiated and called as such:
* RandomizedSet obj = new RandomizedSet();
* boolean param_1 = obj.insert(val);
* boolean param_2 = obj.remove(val);
* int param_3 = obj.getRandom();
*/
}