From 6e1ef2ed507dee79717f37c9e1542702865433a7 Mon Sep 17 00:00:00 2001 From: 724thomas <724thomas@gmail.com> Date: Wed, 9 Jul 2025 10:28:31 +0900 Subject: [PATCH] =?UTF-8?q?[=EC=B5=9C=EC=9B=90=EC=A4=80]=20Day08?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Q1689.java" | 24 ++++ .../Q2467.java" | 105 ++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 "leetcode2/2medium/\354\265\234\354\233\220\354\244\200/Q1689.java" create mode 100644 "leetcode2/2medium/\354\265\234\354\233\220\354\244\200/Q2467.java" diff --git "a/leetcode2/2medium/\354\265\234\354\233\220\354\244\200/Q1689.java" "b/leetcode2/2medium/\354\265\234\354\233\220\354\244\200/Q1689.java" new file mode 100644 index 00000000..3c0feea5 --- /dev/null +++ "b/leetcode2/2medium/\354\265\234\354\233\220\354\244\200/Q1689.java" @@ -0,0 +1,24 @@ +package Leetcode.최원준; + +/* +1. 아이디어 : + + +2. 시간복잡도 : +O( ) + +3. 자료구조/알고리즘 : + + */ + +public class Q1689 { + class Solution { + public int minPartitions(String n) { + int cmax = 0; + for (int i=0; i> neighborMap = new HashMap<>(); + List bobRoute; + Set visited; + Set bobVisited = new HashSet<>(); + + int[] amount; + int bob; + int[] bobTime; + public void findBobRoute(int idx, List curr) { + if (idx == bob) { + bobRoute = new ArrayList<>(curr); + return; + } + + for (int neighbor: neighborMap.get(idx)) { + if (visited.contains(neighbor)) continue; + curr.add(neighbor); + visited.add(neighbor); + + findBobRoute(neighbor, curr); + + curr.remove(curr.size()-1); + visited.remove(neighbor); + } + } + + public int getIncome(int node, int aliceTime) { + int bobArrive = bobTime[node]; + if (bobArrive == -1 || aliceTime < bobArrive) return amount[node]; + if (aliceTime == bobArrive) return amount[node] / 2; + return 0; + } + + public int bfs() { + int ans = Integer.MIN_VALUE; + Deque deque = new ArrayDeque<>(); //pos, income, time; + deque.add(new int[]{0,0,0}); + + while (!deque.isEmpty()) { + int[] c = deque.pollFirst(); + int pos = c[0], income = c[1], time = c[2]; + + List neighbors = neighborMap.get(pos); + boolean isLeaf = true; + int totalIncome = income + getIncome(pos, time); + for (int neighbor : neighbors) { + if (visited.contains(neighbor)) continue; + visited.add(neighbor); + deque.add(new int[]{neighbor, totalIncome, time+1}); + isLeaf = false; + } + + if (isLeaf) ans = Math.max(ans, totalIncome); + } + return ans; + } + + public int mostProfitablePath(int[][] edges, int bob, int[] amount) { + this.bob = bob; + this.amount = amount; + for (int[] e : edges) { + int u = e[0], v = e[1]; + neighborMap.putIfAbsent(u, new ArrayList<>()); + neighborMap.putIfAbsent(v, new ArrayList<>()); + neighborMap.get(u).add(v); + neighborMap.get(v).add(u); + } + + // Bob 루트 계산 + visited = new HashSet<>(); + visited.add(0); + findBobRoute(0, new ArrayList<>(List.of(0))); + Collections.reverse(bobRoute); + // System.out.println(bobRoute); + + // Bob 시간 생성 + int n = amount.length; + bobTime = new int[n]; + Arrays.fill(bobTime, -1); + for (int t=0; t < bobRoute.size(); t++) { + int node = bobRoute.get(t); + bobTime[node] = t; + } + + return bfs(); + } + } +}