diff --git "a/leetcode2/1easy/\354\265\234\354\233\220\354\244\200/Q3487.java" "b/leetcode2/1easy/\354\265\234\354\233\220\354\244\200/Q3487.java" new file mode 100644 index 00000000..fbd1dab9 --- /dev/null +++ "b/leetcode2/1easy/\354\265\234\354\233\220\354\244\200/Q3487.java" @@ -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 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; + } + } +} diff --git "a/leetcode2/2medium/\354\265\234\354\233\220\354\244\200/Q911.java" "b/leetcode2/2medium/\354\265\234\354\233\220\354\244\200/Q911.java" new file mode 100644 index 00000000..746fb0e8 --- /dev/null +++ "b/leetcode2/2medium/\354\265\234\354\233\220\354\244\200/Q911.java" @@ -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 counter = new HashMap<>(); + int cmax = 0; + int candid = -1; + + for (int i=0; i= 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); + */ +} diff --git "a/leetcode2/3hard/\354\265\234\354\233\220\354\244\200/Q1928.java" "b/leetcode2/3hard/\354\265\234\354\233\220\354\244\200/Q1928.java" new file mode 100644 index 00000000..fed22048 --- /dev/null +++ "b/leetcode2/3hard/\354\265\234\354\233\220\354\244\200/Q1928.java" @@ -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> 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 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; + } + } +}