The first three questions are warm, and the last one is a little routine. Generally speaking, it is not difficult
Knowledge points involved: simulation, string, dynamic programming, longest ascending subsequence, binary optimization
5956. Find the first palindrome string in the array
Given a dictionary w, find the first palindrome string, if no empty string is output
Problem solution
simulation
// cpp class Solution { public: bool check(const string& s) { int i = 0, j = s.length() - 1; while (i <= j) { if (s[i++] != s[j--]) return 0; } return 1; } string firstPalindrome(vector<string>& w) { for (auto& i: w) if (check(i)) return i; return ""; } };
5957. Add spaces to the string
Given a string s and an array of positive integers a, add a space to the corresponding position in s according to the value in a
Problem solution
Two pointer scanning
// cpp class Solution { public: string addSpaces(string s, vector<int>& spaces) { int j = 0; string ans; for (int i = 0; i < s.length(); ++i) { if (j < spaces.size() && i == spaces[j]) ans += ' ', ++j; ans += s[i]; } return ans; } };
5958. Number of smooth decline stages of stocks
Given a positive integer array, a represents the stock price every day. If there is an interval decreasing by 1, we call it a smooth decline. Calculate the number of smooth decline intervals in a
Problem solution
In simple dp, dp[i] is defined to mean the number of days of smooth decline at the end of day I, then
- If a[i] + 1 = a[i - 1], dp[i] = dp[i - 1] + 1
- Otherwise, dp[i] = 1
Calculate according to the state transition equation, and finally accumulate the dp array
// cpp class Solution { public: long long getDescentPeriods(vector<int>& p) { int n = p.size(); typedef long long LL; vector<LL> dp(n); dp[0] = 1; for (int i = 1; i < n; ++i) { if (p[i - 1] - 1 == p[i]) dp[i] = dp[i - 1] + 1; else dp[i] = 1; } LL ans = 0; for (int i = 0; i < n; ++i) ans += dp[i]; return ans; } };
5959. Minimum number of operations to increment array K
Given a positive integer array A and a positive integer k, if there is a[i - k] \leq a[i] for each position I, then we call it K increment. Now you can spend an operand to change the element a]i] at position I into any positive integer. Please calculate the minimum operand data that makes a K increment. The length n of array a meets 1\leq n\leq 10^5
Problem solution
A simple discovery is that the transformed array satisfies a[i] \leq a[i + k]\leq a[i + 2k]\leq Therefore, we can split the original array into k sub arrays with a length of \ lfloor\frac{n}{k}\rfloor
Considering the subproblem, given a positive integer array b with length m, how many operands does it cost to make b monotonous and unabated?
Just calculate the length l of the longest non decreasing sub sequence, and the answer is m - l. for details, refer to dilworth theorem. We do this calculation for each sub array, and finally sum it. The time complexity is \ mathcal{O}(n\log\lfloor\frac{n}{k}\rfloor)
// cpp class Solution { public: int kIncreasing(vector<int>& arr, int k) { int n = arr.size(), ans = 0; for (int i = 0; i < k; ++i) { vector<int> dp; int cnt = 0; for (int j = i; j < n; j += k) { auto it = upper_bound(dp.begin(), dp.end(), arr[j]); if (it == dp.end()) dp.push_back(arr[j]); else *it = arr[j]; ++cnt; } ans += cnt - dp.size(); } return ans; } };