diff --git a/cpp/2205/220518-CN.cpp b/cpp/2205/220518-CN.cpp new file mode 100644 index 0000000..3816199 --- /dev/null +++ b/cpp/2205/220518-CN.cpp @@ -0,0 +1,42 @@ +#include + +/** + * 668. Kth Smallest Number in Multiplication Table + * Nearly everyone has used the Multiplication Table. The multiplication table of size m x n is an integer matrix mat where mat[i][j] == i * j (1-indexed). + * Given three integers m, n, and k, return the kth smallest element in the m x n multiplication table. + */ + +class Solution { +public: + static constexpr int findKthNumber(int m, int n, int k) { + auto compute = [&](int upper_bound) { + int ans = 0; + for (int i = 1; i <= m; ++i) + ans += (upper_bound / i <= n) ? (upper_bound / i) : n; + return ans; + }; + int L = 1, R = m * n, M; + + // [L, R] + while (L < R) { + M = (L + R) >> 1; + int x = compute(M); + if (x > k) { + R = M; + } else if (x < k) { + L = M + 1; + } else { + break; + } + } + + M = (L + R + 1) >> 1; + for (int c = compute(M), p; (p = compute(M - 1)) >= k || p == c; --M); + return M; + } +}; + +int main() { + std::printf("%d\n", Solution::findKthNumber(2, 3 ,6)); + return 0; +} diff --git a/cpp/2205/220518.cpp b/cpp/2205/220518.cpp index 348f12e..c618f49 100644 --- a/cpp/2205/220518.cpp +++ b/cpp/2205/220518.cpp @@ -1,6 +1,13 @@ #include #include +/** + * 1192. Critical Connections in a Network + * There are n servers numbered from 0 to n - 1 connected by undirected server-to-server connections forming a network where connections[i] = [ai, bi] represents a connection between servers ai and bi. Any server can reach other servers directly or indirectly through the network. + * A critical connection is a connection that, if removed, will make some servers unable to reach some other server. + * Return all critical connections in the network in any order. + */ + class Solution { public: static std::vector> criticalConnections(int n, const std::vector>& e) { diff --git a/cpp/2205/CMakeLists.txt b/cpp/2205/CMakeLists.txt index e92713e..9e5d13e 100644 --- a/cpp/2205/CMakeLists.txt +++ b/cpp/2205/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2205) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2205 220518.cpp) +ADD_EXECUTABLE(2205 220518-CN.cpp)