add: 220518 + CN [cpp]
This commit is contained in:
parent
e9d90c396d
commit
4943988171
|
|
@ -0,0 +1,42 @@
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,13 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 {
|
class Solution {
|
||||||
public:
|
public:
|
||||||
static std::vector<std::vector<int>> criticalConnections(int n, const std::vector<std::vector<int>>& e) {
|
static std::vector<std::vector<int>> criticalConnections(int n, const std::vector<std::vector<int>>& e) {
|
||||||
|
|
|
||||||
|
|
@ -3,4 +3,4 @@ PROJECT(2205)
|
||||||
|
|
||||||
SET(CMAKE_CXX_STANDARD 23)
|
SET(CMAKE_CXX_STANDARD 23)
|
||||||
|
|
||||||
ADD_EXECUTABLE(2205 220518.cpp)
|
ADD_EXECUTABLE(2205 220518-CN.cpp)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue