From 47949b5ec350e3e638155e2f442f3da8b992c07a Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Tue, 18 Jan 2022 10:26:26 +0800 Subject: [PATCH] add: 220118 (+CN) --- 2201/220117.cpp | 6 ++++++ 2201/220118-CN.cpp | 33 +++++++++++++++++++++++++++++++++ 2201/220118.cpp | 33 +++++++++++++++++++++++++++++++++ 2201/CMakeLists.txt | 2 +- 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 2201/220118-CN.cpp create mode 100644 2201/220118.cpp diff --git a/2201/220117.cpp b/2201/220117.cpp index efe220c..ac5797c 100644 --- a/2201/220117.cpp +++ b/2201/220117.cpp @@ -1,6 +1,12 @@ #include #include +/** + * 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 { public: static bool wordPattern(const std::string& pattern, const std::string& s) { diff --git a/2201/220118-CN.cpp b/2201/220118-CN.cpp new file mode 100644 index 0000000..56238c7 --- /dev/null +++ b/2201/220118-CN.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +#include + +/** + * 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& 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 args {"00:00", "23:59", "00:00"}; + std::cout << Solution::findMinDifference(args); + return 0; +} \ No newline at end of file diff --git a/2201/220118.cpp b/2201/220118.cpp new file mode 100644 index 0000000..a8c3552 --- /dev/null +++ b/2201/220118.cpp @@ -0,0 +1,33 @@ +#include +#include + +/** + * 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& 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 arg {0, 0, 1, 0, 1, 0, 0}; + std::cout << Solution::canPlaceFlowers(arg, 2); + return 0; +} \ No newline at end of file diff --git a/2201/CMakeLists.txt b/2201/CMakeLists.txt index 8f9a4a7..70d671a 100644 --- a/2201/CMakeLists.txt +++ b/2201/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2201) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2201 220117.cpp) \ No newline at end of file +ADD_EXECUTABLE(2201 220118.cpp) \ No newline at end of file