From 2b7010b25f98c97d5abe69c7b2fc0a64328d61a3 Mon Sep 17 00:00:00 2001 From: Lam Haoyin Date: Sat, 15 Jan 2022 11:26:56 +0800 Subject: [PATCH] add: 220115 --- 2201/220115.cpp | 95 +++++++++++++++++++++++++++++++++++++++++++++ 2201/CMakeLists.txt | 2 +- 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 2201/220115.cpp diff --git a/2201/220115.cpp b/2201/220115.cpp new file mode 100644 index 0000000..ae39503 --- /dev/null +++ b/2201/220115.cpp @@ -0,0 +1,95 @@ +#include +#include +#include +#include + +/** + * 1345. Jump Game IV + * Given an array of integers arr, you are initially positioned at the first index of the array. + * In one step you can jump from index i to index: + * i + 1 where: i + 1 < arr.length. + * i - 1 where: i - 1 >= 0. + * j where: arr[i] == arr[j] and i != j. + * Return the minimum number of steps to reach the last index of the array. + * Notice that you can not jump outside of the array at any time. + */ + +class Solution { +private: + inline static int dp[50005]; +public: + static auto minJumps(const std::vector& arr) { + int n = arr.size(); + std::unordered_map> m; + for (int i = 0; i < n; ++i) { + if (m.count(arr[i])) + m[arr[i]].push_back(i); + else + m[arr[i]] = {i}; + } + std::memset(dp, 0x3F, sizeof(int) * n); + dp[0] = 0; + + if (m.size() > n / 3) { + while (true) { + int cnt = 0; + for (int i = 1; i < n; ++i) { + if (dp[i] > dp[i - 1] + 1) { + ++cnt; + dp[i] = dp[i - 1] + 1; + } + if (i + 1 < n && dp[i] > dp[i + 1] + 1) { + ++cnt; + dp[i] = dp[i + 1] + 1; + } + for (int j : m[arr[i]]) { + if (dp[i] > dp[j] + 1) { + ++cnt; + dp[i] = dp[j] + 1; + } + } + } + if (!cnt) + break; + } + } else { + while (true) { + int cnt = 0; + for (const auto& i : m) { + int min = 0x3FFFFFFF; + for (const auto& j : i.second) { + if (j - 1 >= 0 && dp[j] > 1 + dp[j - 1]) { + dp[j] = 1 + dp[j - 1]; + ++cnt; + } + if (j + 1 < n && dp[j] > 1 + dp[j + 1]) { + dp[j] = 1 + dp[j + 1]; + ++cnt; + } + min = std::min(dp[j], min); + } + for (const auto& j : i.second) { + if (min + 1 < dp[j]) { + dp[j] = min + 1; + ++cnt; + } + } + } + if (!cnt) + break; + } + } + return dp[n - 1]; + } +}; + +int main() { + // {100, -23, -23, 404, 100, 23, 23, 23, 3, 404} + // {7, 6, 9, 6, 9, 6, 9, 7} + std::vector num; + num.reserve(50000); + for (int i = 0; i < 50000; ++i) + num.push_back(7); + std::cout << Solution::minJumps(num); + return 0; +} \ No newline at end of file diff --git a/2201/CMakeLists.txt b/2201/CMakeLists.txt index 7afa65b..081edd4 100644 --- a/2201/CMakeLists.txt +++ b/2201/CMakeLists.txt @@ -3,4 +3,4 @@ PROJECT(2201) SET(CMAKE_CXX_STANDARD 23) -ADD_EXECUTABLE(2201 220115-CN.cpp) \ No newline at end of file +ADD_EXECUTABLE(2201 220115.cpp) \ No newline at end of file