首页 > 其他 > 详细

LeetCode20200407

时间:2020-04-07 21:39:03      阅读:78      评论:0      收藏:0      [点我收藏+]

CanChen ggchen@mail.ustc.edu.cn


 

Maximum Size Subarray Sum Equals k

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

 

Longest Substring with At Most K Distinct Characters

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

 

Longest Substring with At Most K Distinct Characters

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

 

Next Closest Time

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.

 

K Empty Slots

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

 

Minimum Window Subsequence

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

 

LeetCode20200407

原文:https://www.cnblogs.com/JuliaAI123/p/12656180.html

(1)
(1)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!