From a583694c522359cf2fab3e3ad7da5dc3d9bd3b38 Mon Sep 17 00:00:00 2001 From: "Joon Yeol, Lee" Date: Wed, 30 Apr 2025 10:21:08 +0900 Subject: [PATCH] =?UTF-8?q?[=EC=9D=B4=EC=A4=80=EC=97=B4]=20=20Day3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2511.cpp" | 67 +++++++++++++++++++ .../3310.cpp" | 44 ++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 "leetcode2/1easy/\354\235\264\354\244\200\354\227\264/2511.cpp" create mode 100644 "leetcode2/2medium/\354\235\264\354\244\200\354\227\264/3310.cpp" diff --git "a/leetcode2/1easy/\354\235\264\354\244\200\354\227\264/2511.cpp" "b/leetcode2/1easy/\354\235\264\354\244\200\354\227\264/2511.cpp" new file mode 100644 index 00000000..94a2ad2b --- /dev/null +++ "b/leetcode2/1easy/\354\235\264\354\244\200\354\227\264/2511.cpp" @@ -0,0 +1,67 @@ +class Solution { + public: + int captureForts(vector& forts) { + int output = 0; + + for (int i = 0; i < forts.size(); i++) + { + if (forts[i] == 1) + { + int left = i - 1; + int right = i + 1; + + int leftCnt = 0; + int rightCnt = 0; + + int maxLeft = 0; + int maxRight = 0; + + bool metEmptyLeft = false; // meeting -1, 1 or end is wrong + bool metEmptyRight = false; + + while (left >= 0) + { + if (forts[left] == 0) + { + leftCnt++; + } + else if (forts[left] == 1) + { + break; + } + else + { + maxLeft = max(maxLeft, leftCnt); + metEmptyLeft = true; + break; + } + left--; + } + if (metEmptyLeft) + output = max(output, maxLeft); + while (right <= forts.size()-1) + { + if (forts[right] == 0) + { + rightCnt++; + } + else if (forts[right] == 1) + { + break; + } + else + { + maxRight = max(maxRight, rightCnt); + metEmptyRight = true; + break; + } + right++; + } + if (metEmptyRight) + output = max(output, maxRight); + } + } + + return output; + } + }; \ No newline at end of file diff --git "a/leetcode2/2medium/\354\235\264\354\244\200\354\227\264/3310.cpp" "b/leetcode2/2medium/\354\235\264\354\244\200\354\227\264/3310.cpp" new file mode 100644 index 00000000..3975f327 --- /dev/null +++ "b/leetcode2/2medium/\354\235\264\354\244\200\354\227\264/3310.cpp" @@ -0,0 +1,44 @@ +class Solution { + public: + unordered_set sus; + map > graph; + map > reverseGraph; + + void dfs(int target) + { + if (sus.count(target)) return; + sus.insert(target); + for (int n: graph[target]) dfs(n); + } + + vector remainingMethods(int n, int k, vector>& invocations) { + + for (vector v: invocations) + { + graph[v[0]].push_back(v[1]); + reverseGraph[v[1]].push_back(v[0]); + } + + dfs(k); + + for (int i : sus) + { + for (int rev: reverseGraph[i]) + { + if (!sus.count(rev)) + { + vector all(n); + iota(all.begin(), all.end(), 0); + return all; + } + } + } + + vector output; + for (int j = 0; j < n; j++) + { + if (!sus.count(j)) output.push_back(j); + } + return output; + } + }; \ No newline at end of file