LeetCode 第 423 场周赛个人题解
Q1. 检测相邻递增子数组 I
原题链接
Q1. 检测相邻递增子数组 I
思路分析
线性DP
定义 f[i] 为 以下标 i 为结尾的最长递增子数组长度
f[i] = f[i - 1] + 1,如果 nums[i] > nums[i - 1]
然后看一下 是否存在 f[i] >= k and f[i + k] >= k
时间复杂度:O(N)
AC代码
class Solution:def hasIncreasingSubarrays(self, nums: List[int], k: int) -> bool:n = len(nums)f = [1] * nfor i in range(1, n):if nums[i] > nums[i - 1]:f[i] = f[i - 1] + 1for i in range(n - k):if f[i] >= k and f[i + k] >= k:return Truereturn False
Q2. 检测相邻递增子数组 II
原题链接
Q2. 检测相邻递增子数组 II
思路分析
二分 + T1
直接二分,把T1拿过来check
时间复杂度:O(nlogn)
AC代码
class Solution:def check(self, nums: List[int], k: int) -> bool:n = len(nums)f = [1] * nfor i in range(1, n):if nums[i] > nums[i - 1]:f[i] = f[i - 1] + 1for i in range(n - k):if f[i] >= k and f[i + k] >= k:return Truereturn Falsedef maxIncreasingSubarrays(self, nums: List[int]) -> int:n = len(nums)lo = 0hi = nwhile lo < hi:x = (lo + hi + 1) // 2if self.check(nums, x):lo = xelse:hi = x - 1return lo
Q3. 好子序列的元素之和
原题链接
Q3. 好子序列的元素之和
思路分析
dp + 计数
注意到值域很小,直接dp计数
f[x] 为 x 结尾的好序列的和,g[x] 为 x 结尾的好序列的个数
递推关系显然
时间复杂度:O(M)
AC代码
using i64 = long long;template<class T>
constexpr T power(T a, i64 b) {T res = 1;for (; b; b /= 2, a *= a) {if (b % 2) {res *= a;}}return res;
}constexpr i64 mul(i64 a, i64 b, i64 p) {i64 res = a * b - i64(1.L * a * b / p) * p;res %= p;if (res < 0) {res += p;}return res;
}
template<i64 P>
struct MLong {i64 x;constexpr MLong() : x{} {}constexpr MLong(i64 x) : x{norm(x % getMod())} {}static i64 Mod;constexpr static i64 getMod() {if (P > 0) {return P;} else {return Mod;}}constexpr static void setMod(i64 Mod_) {Mod = Mod_;}constexpr i64 norm(i64 x) const {if (x < 0) {x += getMod();}if (x >= getMod()) {x -= getMod();}return x;}constexpr i64 val() const {return x;}explicit constexpr operator i64() const {return x;}constexpr MLong operator-() const {MLong res;res.x = norm(getMod() - x);return res;}constexpr MLong inv() const {assert(x != 0);return power(*this, getMod() - 2);}constexpr MLong &operator*=(MLong rhs) & {x = mul(x, rhs.x, getMod());return *this;}constexpr MLong &operator+=(MLong rhs) & {x = norm(x + rhs.x);return *this;}constexpr MLong &operator-=(MLong rhs) & {x = norm(x - rhs.x);return *this;}constexpr MLong &operator/=(MLong rhs) & {return *this *= rhs.inv();}friend constexpr MLong operator*(MLong lhs, MLong rhs) {MLong res = lhs;res *= rhs;return res;}friend constexpr MLong operator+(MLong lhs, MLong rhs) {MLong res = lhs;res += rhs;return res;}friend constexpr MLong operator-(MLong lhs, MLong rhs) {MLong res = lhs;res -= rhs;return res;}friend constexpr MLong operator/(MLong lhs, MLong rhs) {MLong res = lhs;res /= rhs;return res;}friend constexpr std::istream &operator>>(std::istream &is, MLong &a) {i64 v;is >> v;a = MLong(v);return is;}friend constexpr std::ostream &operator<<(std::ostream &os, const MLong &a) {return os << a.val();}friend constexpr bool operator==(MLong lhs, MLong rhs) {return lhs.val() == rhs.val();}friend constexpr bool operator!=(MLong lhs, MLong rhs) {return lhs.val() != rhs.val();}
};template<>
i64 MLong<0LL>::Mod = i64(1E18) + 9;template<int P>
struct MInt {int x;constexpr MInt() : x{} {}constexpr MInt(i64 x) : x{norm(x % getMod())} {}static int Mod;constexpr static int getMod() {if (P > 0) {return P;} else {return Mod;}}constexpr static void setMod(int Mod_) {Mod = Mod_;}constexpr int norm(int x) const {if (x < 0) {x += getMod();}if (x >= getMod()) {x -= getMod();}return x;}constexpr int val() const {return x;}explicit constexpr operator int() const {return x;}constexpr MInt operator-() const {MInt res;res.x = norm(getMod() - x);return res;}constexpr MInt inv() const {assert(x != 0);return power(*this, getMod() - 2);}constexpr MInt &operator*=(MInt rhs) & {x = 1LL * x * rhs.x % getMod();return *this;}constexpr MInt &operator+=(MInt rhs) & {x = norm(x + rhs.x);return *this;}constexpr MInt &operator-=(MInt rhs) & {x = norm(x - rhs.x);return *this;}constexpr MInt &operator/=(MInt rhs) & {return *this *= rhs.inv();}friend constexpr MInt operator*(MInt lhs, MInt rhs) {MInt res = lhs;res *= rhs;return res;}friend constexpr MInt operator+(MInt lhs, MInt rhs) {MInt res = lhs;res += rhs;return res;}friend constexpr MInt operator-(MInt lhs, MInt rhs) {MInt res = lhs;res -= rhs;return res;}friend constexpr MInt operator/(MInt lhs, MInt rhs) {MInt res = lhs;res /= rhs;return res;}friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {i64 v;is >> v;a = MInt(v);return is;}friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {return os << a.val();}friend constexpr bool operator==(MInt lhs, MInt rhs) {return lhs.val() == rhs.val();}friend constexpr bool operator!=(MInt lhs, MInt rhs) {return lhs.val() != rhs.val();}
};template<>
int MInt<0>::Mod = 998244353;template<int V, int P>
constexpr MInt<P> CInv = MInt<P>(V).inv();constexpr int P = 1000000007;
using Z = MInt<P>;class Solution {
public:int sumOfGoodSubsequences(vector<int>& nums) {const int M = std::ranges::max(nums) + 1;std::vector<Z> f(M + 1), g(M + 1);Z res = 0;for (int x : nums) {if (x > 0) {g[x] += g[x - 1];f[x] += f[x - 1] + g[x - 1] * x;}if (x + 1 <= M) {g[x] += g[x + 1];f[x] += f[x + 1] + g[x + 1] * x;}g[x] += 1;f[x] += x;}for (Z x : f) {res += x;}return res.x;}
};
Q4. 统计小于 N 的 K 可约简整数
原题链接
Q4. 统计小于 N 的 K 可约简整数
思路分析
数位dp
小于等于 n 的含 k 个1的数字个数如何求?——数位dp
合法的 k 怎么预处理 —— 暴力
关于数位dp:数位dp详解,记忆化搜索,递推,OJ精讲_数位dp记忆化搜索-CSDN博客
时间复杂度:O(N^2)
AC代码
const int P = 1'000'000'007;constexpr int N = 800;
int memo[N][N + 1];class Solution {
public:int countKReducibleNumbers( std::string s, int k) {int n = s.size();std::vector<bool> f(n + 1, false);f[1] = true;for (int x = 2; x <= n; ++x) {int cnt = 0;int t = x;int temp_x = x;while (temp_x != 1 && cnt < k) {temp_x = __builtin_popcount(temp_x);cnt++;}f[t] = (cnt < k);}memset(memo, -1, sizeof(memo));auto dfs = [&](auto &&self, int n, int pre, bool lim) -> int {if (pre < 0) return 0;if (n == -1) return pre == 0 ? 1 : 0;if (lim && ~memo[n][pre]) return memo[n][pre];int top = lim ? 1 : (s[n] - '0');int res = 0;for (int x = 0; x <= top; ++x) {res += self(self, n - 1, pre - x, lim || x < (s[n] - '0'));if (res >= P) res -= P;}return memo[n][pre] = res;};std::reverse(s.begin(), s.end());int res = 0;int c1 = 0;for (char x : s) {if (x == '1') c1++;}for (int x = 0; x <= n; ++x) {if (!f[x]) continue;res += dfs(dfs, n - 1, x, false);if (res >= P) res -= P;}if (f[c1]) {res = (res - 1 + P) % P;}return res;}
};