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
40 changes: 40 additions & 0 deletions leetcode3/이진희/2136. Earliest Possible Day of Full Bloom.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
1. 아이디어:
- 모든 꽃을 피우는 가장 짧은 일 수 계산
- 꽃을 심는 기간동안 다른 꽃을 같이 심을수 없다.
- 그러므로 plantTime은 고정되고 growTime에 따라 기간이 결정
- growTime이 길수록 먼저 심는게 유리하므로 growTime이 높은순으로 정렬 후 값을 계산한다

2. 시간복잡도: O(N)

3. 자료구조/알고리즘: 그리디
*/

class Solution {
public int earliestFullBloom(int[] plantTime, int[] growTime) {
// 1. 꽃을 심는 작업은 겹칠 수 없다
// 2. 꽃은 growTime+1에 핀다
// 3. 꽃을 나눠 심어도 된다

List<int[]> flowers = new ArrayList<>();

for(int i=0; i<plantTime.length; i++) {
flowers.add(new int[]{plantTime[i], growTime[i]});
}

Collections.sort(flowers, (a,b) -> Integer.compare(b[1],a[1]));

int allBloomingPeriod = flowers.get(0)[0];
int nextBlooming = flowers.get(0)[1]+1;

for(int i=1; i<plantTime.length; i++) {
int p = flowers.get(i)[0];
int g = flowers.get(i)[1]+1;

allBloomingPeriod += p;
nextBlooming = Math.max(nextBlooming-p, g);
}

return allBloomingPeriod + nextBlooming -1;
}
}
61 changes: 61 additions & 0 deletions leetcode3/이진희/3613. Minimize Maximum Component Cost.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*

1. 아이디어 :
- 그래프에서 k개의 컴포넌트를 남기기 위한 최소 비용 구하기
- 간선 비용 오름차순 정렬 후, 비용이 작은 간선부터 순서대로 연결
- Union-Find를 통해 두 노드를 연결할 때마다 연결(간선) 수를 증가시키며, n-k개 연결 시점의 가중치 반환

2. 시간복잡도 : O(E log E)

3. 자료구조/알고리즘 : 크루스칼 알고리즘 (Union-Find)

*/

class Solution {
private int[] parent;
public int minCost(int n, int[][] edges, int k) {

if(edges.length == 0) return 0;

// 가중치 기준 오름차순 정렬
Arrays.sort(edges, (a,b) -> Integer.compare(a[2],b[2]));

parent = new int[n];
Arrays.fill(parent, -1);

int link = 0;

for(int i=0; i<edges.length; i++) {
int a = edges[i][0];
int b = edges[i][1];
int w = edges[i][2];

if(union(a,b)) link++;

if(link == n-k) return w;
}

return 0;
}

private int find(int x) {
if(parent[x] < 0) return x;
return parent[x] = find(parent[x]);
}

private boolean union(int x, int y) {
int rootX = find(x);
int rootY = find(y);

if(rootX == rootY) return false;

if(parent[rootX] > parent[rootY]) parent[rootX] = rootY;
else if(parent[rootX] < parent[rootY]) parent[rootY] = rootX;
else {
parent[rootX] = rootY;
parent[rootY]--;
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*

1. 아이디어 : 조건을 만족하는 원소쌍중 가장 큰 값 구하기
i,j의 순서는 더 큰 원소 idx가 먼저와야함을 명심

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

3. 자료구조/알고리즘 : 완전탐색

*/


class Solution {
public int[] maxProductPair(int[] nums, int target) {
// 조건을 만족하는 모든 pair중 nums[i] * nums[j]이 가장 큰 값
// 없으면 [-1, -1]

int[] ans = {-1, -1};
int maxVal = Integer.MIN_VALUE;

for(int i=0; i<nums.length-1; i++) {
for(int j=i+1; j<nums.length; j++) {
int iVal = nums[i];
int jVal = nums[j];

if((iVal + jVal) != target || iVal == jVal) continue;
if(jVal*iVal > maxVal) {
maxVal = jVal*iVal;

if(jVal>iVal) {
ans[0] = j;
ans[1] = i;
}
else {
ans[0] = i;
ans[1] = j;
}
}
}
}

return ans;
}
}