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
31 changes: 31 additions & 0 deletions leetcode2/1easy/최원준/Q3487.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package Leetcode.최원준;

/*
1. 아이디어 :
방문처리와, 최대값을 유지

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

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

import java.util.HashSet;
import java.util.Set;

public class Q3487 {
class Solution {
public int maxSum(int[] nums) {
Set<Integer> visited = new HashSet<>();
int ans = 0;
int cmax = Integer.MIN_VALUE;
for (int num : nums) {
if (!visited.contains(num) && num > 0) ans+=num;
visited.add(num);
cmax = Math.max(cmax, num);
}
return ans == 0? cmax : ans;
}
}
}
66 changes: 66 additions & 0 deletions leetcode2/2medium/최원준/Q911.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package Leetcode.최원준;

/*
1. 아이디어 :
time에 따른 최고 투표자를 미리 계산합니다.
q 메서드가 호출될떄마다 이분탐색으로 시간에 해당하는 인덱스를 찾고, 그 인덱스의 투표자를 리턴

2. 시간복잡도 :
O( n ) / O( log n ) * 호출횟수(m)

3. 자료구조/알고리즘 :
해시맵 / 이분탐색
*/

import java.util.HashMap;
import java.util.Map;

public class Q911 {
class TopVotedCandidate {
int n;
int[] times;
int[] rank;

public TopVotedCandidate(int[] persons, int[] times) {
this.n = persons.length;
this.times = times;
this.rank = new int[n];

Map<Integer, Integer> counter = new HashMap<>();
int cmax = 0;
int candid = -1;

for (int i=0; i<n; i++) {
int person = persons[i], time = times[i];
counter.put(person, counter.getOrDefault(person, 0)+1);

if (counter.get(person) >= cmax) { // >= 최근 반영
cmax = counter.get(person);
candid = person;
}
rank[i] = candid;
}
}

public int q(int t) {
int left = 0, right = n-1;

while (left <= right) {
int mid = left + (right-left)/2;
if (times[mid] <= t) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return rank[right];

}
}

/**
* Your TopVotedCandidate object will be instantiated and called as such:
* TopVotedCandidate obj = new TopVotedCandidate(persons, times);
* int param_1 = obj.q(t);
*/
}
66 changes: 66 additions & 0 deletions leetcode2/3hard/최원준/Q1928.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package Leetcode;

/*
1. 아이디어 :
다익스트라 사용

2. 시간복잡도 :
O( n * log n + m ) // n: 노드, m: 엣지

3. 자료구조/알고리즘 :
해시맵 / 다익스트라
*/

import java.util.*;

public class Q1928 {
class Pair {
int node;
int time;
int fees;
public Pair(int node, int time, int fees) {
this.node=node;
this.time=time;
this.fees=fees;
}
}

class Solution {
public int minCost(int maxTime, int[][] edges, int[] passingFees) {
// n: nodes, m: edges
int n = passingFees.length;
Map<Integer, List<int[]>> graph = new HashMap<>();
for (int[] edge : edges) { // m
int u = edge[0], v = edge[1], c = edge[2];
graph.putIfAbsent(u, new ArrayList<>());
graph.putIfAbsent(v, new ArrayList<>());
graph.get(u).add(new int[]{v, c});
graph.get(v).add(new int[]{u, c});
}

PriorityQueue<Pair> pq = new PriorityQueue<>((a, b) -> {
return a.fees - b.fees;
});
pq.add(new Pair(0,0,passingFees[0]));

int[][] dp = new int[n][maxTime+1]; // [x][y] = x노드에 y시간에 도착했을때, 최소 fees
for (int[] row : dp) Arrays.fill(row, Integer.MAX_VALUE);
dp[0][0] = passingFees[0];

while (!pq.isEmpty()) { // n
Pair curr = pq.poll(); // logn
int cnode = curr.node, ctime = curr.time, cfees = curr.fees;
if (dp[cnode][ctime] != cfees) continue;
if (cnode == n-1) return cfees;

for (int[] neighbor : graph.getOrDefault(cnode, new ArrayList<>())) { // m
int nnode = neighbor[0], ntime = neighbor[1], nfees = passingFees[neighbor[0]];
if (ctime + ntime > maxTime || dp[nnode][ctime + ntime] <= cfees + nfees) continue;
dp[nnode][ctime+ntime] = cfees + nfees;
pq.add(new Pair(nnode, ctime+ntime, cfees + nfees));
}
}
return -1;
}
}
}