add: 220118 (+CN)

This commit is contained in:
Lam Haoyin 2022-01-18 10:26:26 +08:00
parent 8dd2e6b8da
commit 47949b5ec3
No known key found for this signature in database
GPG Key ID: 8C089CB1A2B7544F
4 changed files with 73 additions and 1 deletions

View File

@ -1,6 +1,12 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
/**
* 290. Word Pattern
* Given a pattern and a string s, find if s follows the same pattern.
* Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s.
*/
class Solution { class Solution {
public: public:
static bool wordPattern(const std::string& pattern, const std::string& s) { static bool wordPattern(const std::string& pattern, const std::string& s) {

33
2201/220118-CN.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
/**
* 539. Minimum Time Difference
* Given a list of 24-hour clock time points in "HH:MM" format, return the minimum minutes difference between any two time-points in the list.
*/
class Solution {
public:
static int findMinDifference(std::vector<std::string>& timePoints) {
std::sort(timePoints.begin(), timePoints.end());
int h1, m1, h2, m2, min = 1441, n = timePoints.size();
std::sscanf(timePoints[0].c_str(), "%d:%d", &h1, &m1);
std::sscanf(timePoints.back().c_str(), "%d:%d", &h2, &m2);
min = (h1 - h2 + 24) * 60 + (m1 - m2);
for (int i = 1; i < n; ++i) {
std::sscanf(timePoints[i].c_str(), "%d:%d", &h2, &m2);
min = std::min(min, (h2 - h1) * 60 + (m2 - m1));
h1 = h2;
m1 = m2;
}
return min;
}
};
int main() {
std::vector<std::string> args {"00:00", "23:59", "00:00"};
std::cout << Solution::findMinDifference(args);
return 0;
}

33
2201/220118.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <vector>
#include <iostream>
/**
* 605. Can Place Flowers
* You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots.
* Given an integer array flowerbed containing 0's and 1's, where 0 means empty and 1 means not empty, and an integer n, return if n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule.
*/
class Solution {
public:
static bool canPlaceFlowers(std::vector<int>& flowerbed, int n) {
int size = flowerbed.size();
flowerbed.push_back(0);
if (!flowerbed[0] && !flowerbed[1]) {
flowerbed[0] = 1;
--n;
}
for (int i = 1; n && i < size; ++i) {
if (!flowerbed[i] && !flowerbed[i - 1] && !flowerbed[i + 1]) {
flowerbed[i] = 1;
--n;
}
}
return n == 0;
}
};
int main() {
std::vector<int> arg {0, 0, 1, 0, 1, 0, 0};
std::cout << Solution::canPlaceFlowers(arg, 2);
return 0;
}

View File

@ -3,4 +3,4 @@ PROJECT(2201)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2201 220117.cpp) ADD_EXECUTABLE(2201 220118.cpp)