1588. Sum of all odd length subarrays
describe
You are given a positive integer array arr. Please calculate the sum of all possible odd length subarrays.
A subarray is defined as a continuous subsequence in the original array.
Please return the sum of all odd length subarrays in arr.
thinking
- Try to find the number of times each number in the array is added together, and the coefficients
- For the first element, the coefficient a[1] = n / 2 + n% 2, n is the length of the array;
- For the second element, the coefficient a[2] = a[1] - 1 + (n-1) / 2 + (n-1)%2;
- It is not difficult to get the coefficient a[i] = a[i] = a[i] = a[i - 1] - (i / 2 + I% 2) + (n-i) / 2 + (n-i)%2 for the first element;
- The result is obtained by multiplying and adding the corresponding element values by their coefficients
Problem
int sumOddLengthSubarrays(int* arr, int arrSize) { int a[100] = {0}; int sum = 0; a[0] = arrSize / 2 + arrSize % 2; for(int i = 1; i < arrSize; i++) { a[i] = a[i - 1] - (i / 2 + i % 2) + (arrSize - i) / 2 + (arrSize - i) % 2; } for(int i = 0; i < arrSize; i++) { sum += arr[i] * a[i]; } return sum; }
Interview Question 17.13. Restore Spaces
describe
Oh, no! You accidentally deleted all the spaces and punctuation in a long article and capitalized it in lowercase. Like the sentence "I reset the computer. I t still did't boot!" iresetthecomputeritstilldidntboot. Before dealing with punctuation and capitalization, you have to break it into words. Of course, you have a thick dictionary, but some words are not in it. Assume that the article is represented by sentence, and design an algorithm to break the article, requiring the fewest unrecognized characters and returning the number of unrecognized characters.
Example:
Input:
dictionary = ["looked","just","like","her","brother"]
sentence = "jesslookedjustliketimherbrother"
Output: 7
Interpretation: The sentence is followed by "jess look just like Tim her brother", a total of seven unrecognized characters.
Tips:
0 <= len(sentence) <= 1000
The total number of characters in a dictionary does not exceed 150000.
You can think that dictionary and sentence contain only lowercase letters.
thinking
_The first thought is dynamic programming, because the minimum number of unmatched characters to return, so for string sentence, you can first define the minimum number of unmatched characters in the first istring, and then calculate the minimum number of unmatched characters in the first i+1 string, simply find out the relationship between them.
- State definition, dp[i] used to represent the minimum number of unmatched characters in the first I strings
- State transition, followed by dp[i + 1]
- The i+1 string, regardless of mismatch, is first treated as a mismatch, that is, dp[i + 1] = dp[i] + 1 (subsequently, if it is matched, but more unmatched words are matched, then it is better to mismatch)
- If there happens to be a string of idx~i that matches the dictionary, take the median and minimum values of dp[idx] and dp[i] and update dp[i + 1], that is, dp[i + 1] = math.min(dp[idx], dp[i + 1])
Problem
Or old and practical java writing convenience, 09afd9sjoqjihbn
class Solution { public int respace(String[] dictionary, String sentence) { //Entire the set, then process the string Set<String> dic = new HashSet<>(Arrays.asList(dictionary)); int n = sentence.length(); int[] dp = new int[n + 1]; for(int i = 1; i < n + 1; i++){ //First add 1 for unmatched processing; dp[i] = dp[i - 1] + 1; //If a small value is matched for(int idx = 0; idx < i; idx++){ if(dic.contains(sentence.substring(idx, i))){ dp[i] = Math.min(dp[i], dp[idx]); } } } return dp[n]; } }
Expand
_You can construct a dictionary tree to query which words end with the i + 1 character (inserting words in reverse order when building a dictionary tree) to reduce time complexity when matching lookups. For a dictionary tree, you can see Dictionary Tree The dictionary tree optimization solution is as follows
class Solution { public int respace(String[] dictionary, String sentence) { // Build Dictionary Tree Trie trie = new Trie(); for (String word: dictionary) { trie.insert(word); } // State transition, dp[i] represents the minimum number of mismatches for the first I characters of a string int n = sentence.length(); int[] dp = new int[n + 1]; for (int i = 1; i <= n; i++) { dp[i] = dp[i - 1] + 1; for (int idx: trie.search(sentence, i - 1)) { dp[i] = Math.min(dp[i], dp[idx]); } } return dp[n]; } } class Trie { TrieNode root; public Trie() { root = new TrieNode(); } // Insert words in reverse order into the dictionary tree public void insert(String word) { TrieNode cur = root; for (int i = word.length() - 1; i >= 0; i--) { int c = word.charAt(i) - 'a'; if (cur.children[c] == null) { cur.children[c] = new TrieNode(); } cur = cur.children[c]; } cur.isWord = true; } // Find the endPos-terminated words in sentence and return their opening subscripts. public List<Integer> search(String sentence, int endPos) { List<Integer> indices = new ArrayList<>(); TrieNode cur = root; for (int i = endPos; i >= 0; i--) { int c = sentence.charAt(i) - 'a'; if (cur.children[c] == null) { break; } cur = cur.children[c]; if (cur.isWord) { indices.add(i); } } return indices; } } class TrieNode { boolean isWord; TrieNode[] children = new TrieNode[26]; public TrieNode() {} }