CanChen
ggchen@mail.ustc.edu.cn
Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If there isn‘t one, return 0 instead.
When I first met this problem, I tried the brute force method.There are n^2 combinations so just get the sum of these subarrays.The time complexity is O(n^3).
Then I find that the process of computing the sum can be simplified since there is lots of redundancy. Once we get the accumulating sum, we can get the sum of the subarrays easily. As a result, the time complexity becomes O(n^2).
After that, with the help of hashmap, we can reduce the time complexity to O(n) with the cost of O(n) space.
key:prefixsum and hashmap
Given a string, find the length of the longest substring T that contains at most k distinct characters.
This is the traditional sliding window problem. To solve this problem, we have to maintain an array recording the last occuring position and then move the window.
key:sliding window
Given a string, find the length of the longest substring T that contains at most k distinct characters.
This is the traditional sliding window problem. To solve this problem, we have to maintain an array recording the last occuring position and then move the window.
key:sliding window
Given a time represented in the format "HH:MM", form the next closest time by reusing the current digits. There is no limit on how many times a digit can be reused.
This comes from Google and I just list all possibilities without any tricks.
You have N bulbs in a row numbered from 1 to N. Initially, all the bulbs are turned off. We turn on exactly one bulb everyday until all bulbs are on after N days.
You are given an array bulbs of length N where bulbs[i] = x means that on the (i+1)th day, we will turn on the bulb at position x where i is 0-indexed and x is 1-indexed.
Given an integer K, find out the minimum day number such that there exists two turned on bulbs that have exactly K bulbs between them that are all turned off.
If there isn‘t such day, return -1.
This is a special sliding window problem and I find for sliding window problems, the most important thing is to have a physical picture in my mind. Then solve it with proper changes.
key:sliding window
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of W.
If there is no such window in S that covers all characters in T, return the empty string "". If there are multiple such minimum-length windows, return the one with the left-most starting index.
This problems reminds me of the traditional problem in DP. In fact, we can decompose T and add one char everytime to solve this DP problem.
key:DP
原文:https://www.cnblogs.com/JuliaAI123/p/12656180.html