diff --git a/cpp/2308/CMakeLists.txt b/cpp/2308/CMakeLists.txt new file mode 100644 index 0000000..6557bb5 --- /dev/null +++ b/cpp/2308/CMakeLists.txt @@ -0,0 +1,9 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 3.24) +PROJECT(2308) + +SET(CMAKE_CXX_STANDARD 23) +SET(CMAKE_EXPORT_COMPILE_COMMANDS true) + +FILE(GLOB src *.cpp) + +ADD_EXECUTABLE(2308 ${src}) diff --git a/cpp/2308/LC230827.cpp b/cpp/2308/LC230827.cpp new file mode 100644 index 0000000..bb2d517 --- /dev/null +++ b/cpp/2308/LC230827.cpp @@ -0,0 +1,40 @@ +#include +#include +#include + +class LC230827 { +public: + static bool canCross(const std::vector&); +}; + +bool LC230827::canCross(const std::vector& stones) { + if (stones[1] != 1) + return false; + + const int n = stones.size(); + std::unordered_map can_jump; + std::unordered_map rev; + for (int i = 0; i < n; ++i) + rev[stones[i]] = i; + + auto key_of = [&](int position, int k) { + return ((unsigned long long)(k) << 32) | position; + }; + + std::function dp = [&](int position, int k) { + if (position == n - 1) + return 1; + const auto key = key_of(position, k); + if (can_jump.count(key)) + return can_jump[key]; + int ret = 0; + for (int i = -1; i <= 1; ++i) { + if (k + i <= 0 || !rev.count(stones[position] + k + i)) + continue; + ret |= dp(rev[stones[position] + k + i], k + i); + } + return can_jump[key] = ret; + }; + + return dp(1, 1); +} diff --git a/cpp/2308/data_structure.h b/cpp/2308/data_structure.h new file mode 100644 index 0000000..27ce1ba --- /dev/null +++ b/cpp/2308/data_structure.h @@ -0,0 +1,28 @@ +#ifndef LEETCODE_CPP_DATA_STRUCTURE_H +#define LEETCODE_CPP_DATA_STRUCTURE_H + +struct ListNode { + int val; + ListNode* next; + + explicit ListNode(int x = 0, ListNode* next = nullptr) : val(x), next(next) {} +}; + +ListNode* construct(int x) { + return new ListNode(x); +} + +template +ListNode* construct(int x, Ts... xs) { + return new ListNode(x, construct(xs...)); +} + +struct TreeNode { + int val; + TreeNode* left; + TreeNode* right; + + explicit TreeNode(int x, TreeNode* left = nullptr, TreeNode* right = nullptr) : val(x), left(left), right(right) {} +}; + +#endif //LEETCODE_CPP_DATA_STRUCTURE_H diff --git a/cpp/2308/defs.h b/cpp/2308/defs.h new file mode 100644 index 0000000..177a252 --- /dev/null +++ b/cpp/2308/defs.h @@ -0,0 +1,11 @@ +#ifndef LEETCODE_CPP_DEFS_H +#define LEETCODE_CPP_DEFS_H + +#include + +class LC230827 { +public: + static bool canCross(const std::vector&); +}; + +#endif //LEETCODE_CPP_DEFS_H diff --git a/cpp/2308/main.cpp b/cpp/2308/main.cpp new file mode 100644 index 0000000..277c8cd --- /dev/null +++ b/cpp/2308/main.cpp @@ -0,0 +1,8 @@ +#include "defs.h" +#include + +int main() { + LC230827 s; + std::cout << s.canCross({0,1,3,5,6,8,12,17}); + return 0; +} \ No newline at end of file diff --git a/cpp/CMakeLists.txt b/cpp/CMakeLists.txt index ca2c4d9..48bad0f 100644 --- a/cpp/CMakeLists.txt +++ b/cpp/CMakeLists.txt @@ -31,5 +31,6 @@ ADD_EXECUTABLE(leetcode-cpp main.cpp 2302/230218.cpp 2302/230218-CN.cpp 2302/230 # ADD_SUBDIRECTORY(2302) # ADD_SUBDIRECTORY(2303) # ADD_SUBDIRECTORY(2304) -ADD_SUBDIRECTORY(2305) +# ADD_SUBDIRECTORY(2305) +ADD_SUBDIRECTORY(2308) ADD_SUBDIRECTORY(more)