add: 220114

This commit is contained in:
Lam Haoyin 2022-01-14 23:28:19 +08:00
parent dde8caae46
commit 74f36ba498
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
3 changed files with 83 additions and 1 deletions

50
2201/220114-CN.cpp Normal file
View File

@ -0,0 +1,50 @@
#include <vector>
#include <iostream>
#include <queue>
class Solution {
private:
static auto getCartesianProduct(const std::vector<int>& p, const std::vector<int>& q, int limP, int limQ) {
std::vector<std::vector<int>> ret;
for (int i = 0; i < limP; ++i) {
for (int j = 0; j < limQ; ++j) {
ret.push_back({p[i], q[j]});
}
}
return ret;
}
public:
static auto kSmallestPairs(const std::vector<int>& nums1, const std::vector<int>& nums2, int k) {
auto n1 = nums1.size(), n2 = nums2.size();
if (k >= n1 * n2)
return getCartesianProduct(nums1, nums2, n1, n2);
std::vector<std::vector<int>> ret;
auto cmp = [&] (const std::vector<int>& i, const std::vector<int>& j) {
return nums1[i[0]] + nums2[i[1]] > nums1[j[0]] + nums2[j[1]];
};
std::priority_queue<std::vector<int>, std::vector<std::vector<int>>, decltype(cmp)> q(cmp);
for (int i = 0; i < n1; ++i) {
q.push({i, 0});
}
for (int i = 0; i < k; ++i) {
auto t = q.top();
q.pop();
ret.push_back({nums1[t[0]], nums2[t[1]]});
if (1 + t[1] < n2)
q.push({t[0], 1 + t[1]});
}
return ret;
}
};
int main() {
auto ret = Solution::kSmallestPairs({-10, -4, 0, 0, 6}, {3, 5, 6, 7, 8, 100}, 10);
for (const auto& i: ret) {
std::printf("[%d, %d]\n", i[0], i[1]);
}
return 0;
}

32
2201/220114.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <string>
#include <iostream>
#include <cctype>
class Solution {
public:
static int myAtoi(const std::string& s) {
long long ret = 0;
int sgn = 1, pos = 0;
for (; std::isspace(s[pos]); ++pos);
if (s[pos] == '-')
sgn = -sgn;
if (s[pos] == '-' || s[pos] == '+')
++pos;
for (; std::isdigit(s[pos]); ++pos) {
ret = (ret << 3) + (ret << 1) + (s[pos] ^ 48);
if (ret > INT32_MAX)
break;
}
ret *= sgn;
if (ret < INT32_MIN)
return INT32_MIN;
else if (ret > INT32_MAX)
return INT32_MAX;
return int(ret & 0xFFFFFFFFLL);
}
};
int main() {
std::cout << Solution::myAtoi("42");
return 0;
}

View File

@ -3,4 +3,4 @@ PROJECT(2201)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2201 220113.cpp) ADD_EXECUTABLE(2201 220114-CN.cpp)