一维动态规划
| int *dp; |
| |
| |
| int recursive(int n) { |
| if (n == 0)return 0; |
| if (n == 1) return 1; |
| |
| if (dp[n] != -1) return dp[n]; |
| dp[n] = recursive(n - 2) + recursive(n - 1); |
| return dp[n]; |
| } |
| |
| int fib(int n) { |
| dp = (int *) malloc(sizeof(int) * (n + 1)); |
| memset(dp, -1, sizeof(int) * (n + 1)); |
| return recursive(n); |
| } |
| |
| int fib(int n) { |
| int dp[31]; |
| dp[0] = 0; |
| dp[1] = 1; |
| for (int i = 2; i <= n; ++i) |
| |
| dp[i] = dp[i - 2] + dp[i - 1]; |
| return dp[n]; |
| } |
| |
| int fib(int n) { |
| if (n < 2) return n; |
| int left = 0; |
| int mid = 1; |
| int right; |
| |
| for (int i = 2; i <= n; ++i) { |
| right = left + mid; |
| left = mid; |
| mid = right; |
| } |
| |
| return right; |
| } |
| |
| int fib(int n) { |
| double sqrt5 = sqrt(5); |
| double fibN = pow((1 + sqrt5) / 2, n) - pow((1 - sqrt5) / 2, n); |
| |
| return round(fibN / sqrt5); |
| } |
| |
| int durations[3] = {1, 7, 30}; |
| |
| int min(int a, int b) { |
| return a > b ? b : a; |
| } |
| |
| |
| int recursive(int *days, int daysSize, int *costs, int curIndex) { |
| if (curIndex == daysSize) return 0; |
| int res = 0x7fffffff; |
| |
| for (int i = 0; i < 3; ++i) { |
| |
| int index = curIndex; |
| |
| int nextDay = days[index] + durations[i]; |
| while (index < daysSize && days[index] < nextDay) |
| index++; |
| |
| res = min(res, costs[i] + recursive(days, daysSize, costs, index)); |
| } |
| return res; |
| } |
| |
| |
| int mincostTickets(int *days, int daysSize, int *costs, int costsSize) { |
| return recursive(days, daysSize, costs, 0); |
| } |
| |
| int durations[3] = {1, 7, 30}; |
| int *dp; |
| |
| int min(int a, int b) { |
| return a > b ? b : a; |
| } |
| |
| |
| int recursive(int *days, int daysSize, int *costs, int curIndex) { |
| if (curIndex == daysSize) return 0; |
| if (dp[curIndex] != 0x7fffffff) return dp[curIndex]; |
| int res = 0x7fffffff; |
| |
| for (int i = 0; i < 3; ++i) { |
| |
| int index = curIndex; |
| |
| int nextDay = days[index] + durations[i]; |
| while (index < daysSize && days[index] < nextDay) |
| index++; |
| |
| res = min(res, costs[i] + recursive(days, daysSize, costs, index)); |
| } |
| dp[curIndex] = res; |
| return res; |
| } |
| |
| |
| int mincostTickets(int *days, int daysSize, int *costs, int costsSize) { |
| dp = (int *) malloc(sizeof(int) * daysSize); |
| for (int i = 0; i < daysSize; ++i) dp[i] = 0x7fffffff; |
| return recursive(days, daysSize, costs, 0); |
| } |
| int min(int a, int b) { |
| return a > b ? b : a; |
| } |
| |
| |
| int mincostTickets(int *days, int daysSize, int *costs, int costsSize) { |
| |
| int durations[3] = {1, 7, 30}; |
| int dp[daysSize + 1]; |
| for (int i = 0; i < daysSize; ++i) dp[i] = 0x7fffffff; |
| dp[daysSize] = 0; |
| for (int curIndex = daysSize - 1; curIndex >= 0; curIndex--) { |
| |
| for (int i = 0; i < 3; ++i) { |
| |
| int index = curIndex; |
| |
| int nextDay = days[index] + durations[i]; |
| while (index < daysSize && days[index] < nextDay) |
| index++; |
| |
| dp[curIndex] = min(dp[curIndex], dp[index] + costs[i]); |
| } |
| } |
| return dp[0]; |
| } |
| int len; |
| |
| |
| int recursive(char *s, int curIndex) { |
| |
| if (curIndex == len) return 1; |
| int res; |
| |
| if (s[curIndex] == '0') { |
| res = 0; |
| } else { |
| |
| res = recursive(s, curIndex + 1); |
| int val = (s[curIndex] - '0') * 10 + (s[curIndex + 1] - '0'); |
| if (curIndex + 1 < len && val <= 26) |
| |
| res += recursive(s, curIndex + 2); |
| } |
| return res; |
| } |
| |
| |
| int numDecodings(char *s) { |
| len = strlen(s); |
| return recursive(s, 0); |
| } |
| int len; |
| int *dp; |
| |
| |
| int recursive(char *s, int curIndex) { |
| |
| if (curIndex == len) return 1; |
| if (dp[curIndex] != -1) return dp[curIndex]; |
| int res; |
| |
| if (s[curIndex] == '0') { |
| res = 0; |
| } else { |
| |
| res = recursive(s, curIndex + 1); |
| if (curIndex + 1 < len && (s[curIndex] - '0') * 10 + (s[curIndex + 1] - '0') <= 26) |
| |
| res += recursive(s, curIndex + 2); |
| } |
| dp[curIndex] = res; |
| return res; |
| } |
| |
| |
| int numDecodings(char *s) { |
| len = strlen(s); |
| dp = (int *) malloc(sizeof(int) * len); |
| memset(dp, -1, sizeof(int) * len); |
| return recursive(s, 0); |
| } |
| int numDecodings(char *s) { |
| int len = strlen(s);; |
| int *dp = (int *) malloc(sizeof(int) * (len + 1)); |
| dp[len] = 1; |
| for (int curIndex = len - 1; curIndex >= 0; curIndex--) { |
| if (s[curIndex] == '0') { |
| |
| dp[curIndex] = 0; |
| } else { |
| |
| dp[curIndex] = dp[curIndex + 1]; |
| if (curIndex + 1 < len && (s[curIndex] - '0') * 10 + (s[curIndex + 1] - '0') <= 26) |
| |
| dp[curIndex] += dp[curIndex + 2]; |
| } |
| } |
| |
| return dp[0]; |
| } |
| |
| |
| int numDecodings(char *s) { |
| int len = strlen(s);; |
| int left, mid = 1, right; |
| for (int curIndex = len - 1; curIndex >= 0; curIndex--) { |
| if (s[curIndex] == '0') { |
| |
| left = 0; |
| } else { |
| |
| left = mid; |
| if (curIndex + 1 < len && (s[curIndex] - '0') * 10 + (s[curIndex + 1] - '0') <= 26) |
| |
| left += right; |
| } |
| right = mid; |
| mid = left; |
| } |
| |
| return left; |
| } |
| int len; |
| const int mod = 1e9 + 7; |
| |
| |
| int recursive(char *s, int curIndex) { |
| if (curIndex == len) return 1; |
| |
| if (s[curIndex] == '0') return 0; |
| |
| int res = (s[curIndex] == '*' ? 9 : 1) * recursive(s, curIndex + 1); |
| |
| if (curIndex + 1 < len) { |
| |
| |
| if (s[curIndex] != '*') { |
| if (s[curIndex + 1] != '*') { |
| |
| |
| if ((s[curIndex] - '0') * 10 + s[curIndex + 1] - '0' <= 26) |
| res += recursive(s, curIndex + 2); |
| } else { |
| |
| if (s[curIndex] == '1') |
| |
| res += 9 * recursive(s, curIndex + 2); |
| if (s[curIndex] == '2') |
| |
| res += 6 * recursive(s, curIndex + 2); |
| } |
| } else { |
| if (s[curIndex + 1] != '*') { |
| |
| if (s[curIndex + 1] <= '6') |
| |
| res += 2 * recursive(s, curIndex + 2); |
| else |
| |
| res += recursive(s, curIndex + 2); |
| } else { |
| |
| |
| res += 15 * recursive(s, curIndex + 2); |
| } |
| } |
| } |
| return res % mod; |
| } |
| |
| |
| int numDecodings(char *s) { |
| len = strlen(s); |
| return recursive(s, 0); |
| } |
| int len; |
| const int mod = 1e9 + 7; |
| int *dp; |
| |
| |
| long long recursive(char *s, int curIndex) { |
| if (curIndex == len) return 1; |
| |
| if (s[curIndex] == '0') return 0; |
| |
| if (dp[curIndex] != -1) return dp[curIndex]; |
| |
| long long res = (s[curIndex] == '*' ? 9 : 1) * recursive(s, curIndex + 1); |
| |
| if (curIndex + 1 < len) { |
| |
| |
| if (s[curIndex] != '*') { |
| if (s[curIndex + 1] != '*') { |
| |
| |
| if ((s[curIndex] - '0') * 10 + s[curIndex + 1] - '0' <= 26) |
| res += recursive(s, curIndex + 2); |
| } else { |
| |
| if (s[curIndex] == '1') |
| |
| res += 9 * recursive(s, curIndex + 2); |
| if (s[curIndex] == '2') |
| |
| res += 6 * recursive(s, curIndex + 2); |
| } |
| } else { |
| if (s[curIndex + 1] != '*') { |
| |
| if (s[curIndex + 1] <= '6') |
| |
| res += 2 * recursive(s, curIndex + 2); |
| else |
| |
| res += recursive(s, curIndex + 2); |
| } else { |
| |
| |
| res += 15 * recursive(s, curIndex + 2); |
| } |
| } |
| } |
| dp[curIndex] = res % mod; |
| return dp[curIndex]; |
| } |
| |
| |
| int numDecodings(char *s) { |
| len = strlen(s); |
| dp = (int *) malloc(sizeof(int) * len); |
| memset(dp, -1, sizeof(int) * len); |
| return recursive(s, 0); |
| } |
| |
| int numDecodings(char *s) { |
| const int mod = 1e9 + 7; |
| int len = strlen(s); |
| |
| long long *dp = (long long *) malloc(sizeof(long long) * (len + 1)); |
| dp[len] = 1; |
| |
| for (int curIndex = len - 1; curIndex >= 0; curIndex--) { |
| |
| if (s[curIndex] == '0') { |
| dp[curIndex] = 0; |
| continue; |
| } |
| |
| dp[curIndex] = (s[curIndex] == '*' ? 9 : 1) * dp[curIndex + 1]; |
| |
| if (curIndex + 1 < len) { |
| |
| |
| if (s[curIndex] != '*') { |
| if (s[curIndex + 1] != '*') { |
| |
| |
| if ((s[curIndex] - '0') * 10 + s[curIndex + 1] - '0' <= 26) |
| dp[curIndex] += dp[curIndex + 2]; |
| } else { |
| |
| if (s[curIndex] == '1') |
| |
| dp[curIndex] += 9 * dp[curIndex + 2]; |
| if (s[curIndex] == '2') |
| |
| dp[curIndex] += 6 * dp[curIndex + 2]; |
| } |
| } else { |
| if (s[curIndex + 1] != '*') { |
| |
| if (s[curIndex + 1] <= '6') |
| |
| dp[curIndex] += 2 * dp[curIndex + 2]; |
| else |
| |
| dp[curIndex] += dp[curIndex + 2]; |
| } else { |
| |
| |
| dp[curIndex] += 15 * dp[curIndex + 2]; |
| } |
| } |
| } |
| dp[curIndex] %= mod; |
| } |
| |
| return (int) dp[0]; |
| } |
| |
| int numDecodings(char *s) { |
| const int mod = 1e9 + 7; |
| int len = strlen(s); |
| |
| long long left, mid = 1, right; |
| |
| for (int curIndex = len - 1; curIndex >= 0; curIndex--) { |
| |
| if (s[curIndex] == '0') { |
| left = 0; |
| right = mid; |
| mid = left; |
| continue; |
| } |
| |
| left = (s[curIndex] == '*' ? 9 : 1) * mid; |
| |
| if (curIndex + 1 < len) { |
| |
| |
| if (s[curIndex] != '*') { |
| if (s[curIndex + 1] != '*') { |
| |
| |
| if ((s[curIndex] - '0') * 10 + s[curIndex + 1] - '0' <= 26) |
| left += right; |
| } else { |
| |
| if (s[curIndex] == '1') |
| |
| left += 9 * right; |
| if (s[curIndex] == '2') |
| |
| left += 6 * right; |
| } |
| } else { |
| if (s[curIndex + 1] != '*') { |
| |
| if (s[curIndex + 1] <= '6') |
| |
| left += 2 * right; |
| else |
| |
| left += right; |
| } else { |
| |
| |
| left += 15 * right; |
| } |
| } |
| } |
| left %= mod; |
| right = mid; |
| mid = left; |
| } |
| |
| return (int) left; |
| } |
| bool isUgly(int n) { |
| if (n <= 0) return false; |
| |
| while (n % 2 == 0) n /= 2; |
| while (n % 3 == 0) n /= 3; |
| while (n % 5 == 0) n /= 5; |
| return n == 1; |
| } |
| int min(int a, int b) { |
| return a > b ? b : a; |
| } |
| |
| int min3(int a, int b, int c) { |
| return min(min(a, b), c); |
| } |
| |
| int nthUglyNumber(int n) { |
| |
| int dp[n + 1]; |
| dp[1] = 1; |
| int i2 = 1, i3 = 1, i5 = 1; |
| int curIndex = 2; |
| |
| while (curIndex <= n) { |
| int val2 = dp[i2] * 2; |
| int val3 = dp[i3] * 3; |
| int val5 = dp[i5] * 5; |
| int m = min3(val2, val3, val5); |
| |
| if (m == val2) i2++; |
| if (m == val3) i3++; |
| if (m == val5) i5++; |
| |
| dp[curIndex++] = m; |
| } |
| |
| return dp[n]; |
| } |
| int longestValidParentheses(char *s) { |
| int len = strlen(s); |
| if (len <= 1) return 0; |
| |
| |
| int dp[len]; |
| for (int i = 0; i < len; ++i) dp[i] = 0; |
| int max = 0; |
| for (int i = 1; i < len; ++i) { |
| |
| if (s[i] == ')') { |
| |
| int index = i - 1 - dp[i - 1]; |
| if (index >= 0 && s[index] == '(') |
| dp[i] = dp[i - 1] + 2 + (index > 0 ? dp[index - 1] : 0); |
| } |
| |
| if (dp[i] > max) max = dp[i]; |
| } |
| return max; |
| } |
| int findSubstringInWraproundString(char *s) { |
| int len = strlen(s); |
| int str[len]; |
| |
| for (int i = 0; i < len; ++i) |
| str[i] = s[i] - 'a'; |
| |
| |
| |
| int dp[26] = {0}; |
| dp[str[0]] = 1; |
| int pre, cur, count = 1; |
| for (int i = 1; i < len; ++i) { |
| cur = str[i]; |
| pre = str[i - 1]; |
| if ((pre + 1) % 26 == cur) |
| count++; |
| else |
| count = 1; |
| if (count > dp[cur]) |
| dp[cur] = count; |
| } |
| int res = 0; |
| for (int i = 0; i < 26; ++i) |
| res += dp[i]; |
| return res; |
| } |
| |
| int distinctSubseqII(char *s) { |
| int len = strlen(s); |
| int mod = 1e9 + 7; |
| |
| int all = 1; |
| |
| int newAdd; |
| |
| int count[26] = {0}; |
| for (int i = 0; i < len; ++i) { |
| newAdd = (all - count[s[i] - 'a'] + mod) % mod; |
| all = (all + newAdd) % mod; |
| count[s[i] - 'a'] = (count[s[i] - 'a'] + newAdd) % mod; |
| } |
| return (all - 1 + mod) % mod; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步