add: 230827

This commit is contained in:
Eatswap 2023-08-28 03:05:31 +08:00
parent cc60ceaceb
commit 9860bb7a54
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
6 changed files with 98 additions and 1 deletions

9
cpp/2308/CMakeLists.txt Normal file
View File

@ -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})

40
cpp/2308/LC230827.cpp Normal file
View File

@ -0,0 +1,40 @@
#include <functional>
#include <unordered_map>
#include <vector>
class LC230827 {
public:
static bool canCross(const std::vector<int>&);
};
bool LC230827::canCross(const std::vector<int>& stones) {
if (stones[1] != 1)
return false;
const int n = stones.size();
std::unordered_map<unsigned long long, int> can_jump;
std::unordered_map<int, int> 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<int(int, int)> 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);
}

28
cpp/2308/data_structure.h Normal file
View File

@ -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<typename... Ts>
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

11
cpp/2308/defs.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef LEETCODE_CPP_DEFS_H
#define LEETCODE_CPP_DEFS_H
#include <vector>
class LC230827 {
public:
static bool canCross(const std::vector<int>&);
};
#endif //LEETCODE_CPP_DEFS_H

8
cpp/2308/main.cpp Normal file
View File

@ -0,0 +1,8 @@
#include "defs.h"
#include <iostream>
int main() {
LC230827 s;
std::cout << s.canCross({0,1,3,5,6,8,12,17});
return 0;
}

View File

@ -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)