LeetCode202004011

CanChen ggchen@mail.ustc.edu.cn


Recently I am reviewing leetcode top100 liked problems that I have solved before and I find the most important thing is intuition, which means you need to come up with the solution by yourself step by step.

1.Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product.

This problem is similar to the problem "max subarray sum" and you need to calculate the max sum ended at every index. The only different thing is that you should determine the current num is negative or positive. Correspondingly, you need to record the max and min product at the same time.
Keyword:DP

2.Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.

When I first met this problem, I tried to expand the square from (0,0) and gradually found the pattern. This is a traditional DP problem. The max area of the square ended at (i,j) is determined by that at (i-1,j),(i,j-1) and (i-1, j-1).
Keyword:DP

3.Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.

The naive solution is to take every bar as the height and find where it could extend to two directions. The time complexity is O(n*n). In this process, many comparings are unnecessary.
In the best solution, a stack is used, where an element's index is pushed into the stack if the element is bigger than the top while the index at the stack's top is popped out if the current element is smaller than the top.
This is quite tricky and the time complexity is O(n).
keyword: Stack

4.Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

I observe that one node belongs to the left child of the LCA while another node belongs to the right child of the LCA. As a result, we can write a recursive function to determine whether one of the two nodes belongs to its right child or left child.

5.Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

For any palindromic string the start char must equal to the end char and when this requirement is satisfied we only need to see whether the string stripped of the start and end is palindromic or not. We can use DP to solve this.
Keyword: DP

6.Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.

get(key)- Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

put(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

Could you do both operations in O(1) time complexity?

This problem is kind of new to me. Here the key question is how to know the least recently used item. We can use double linked list and hash table. When calling for get(key), we search in the hash table and get the corresponding node. When returning its value, we move this node to the head. When calling for put(key,value), we do the same thing. But if the cache reached its capacity, we need to remove its tail.

Keyword: double linked list and hash table

7.Invert a binary tree.

Remember to use a tmp variable to store the changed node.
Keyword: Tree

8.Implement a trie with insert, search, and startsWith methods.

Trie is a widely used data structure. Binary tree is a tree with two children and trie is a tree with 26 children, representing 26 letters. Any word is a path from the root to the leaf.

9.You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night. Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.

When listing some examples, I found the all sequences have only one or two gap. Here comes the DP. The best result ended at index i is determined by the the result at index (i-1) and index(i-2).

Keyword: DP

10.The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given two integers x and y, calculate the Hamming distance.

Keyword: Bits computation

11.Given an array of strings, group anagrams together.

Keyword: Sort and Dict

12.Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

First we can analyse this problem and find that at any place the left subtring must contain left parenthess no less than the right.
Assume we have n left and right at the initial stage, then consume one parenthese everytime. But keep left no less than right.
Keyword: Recursive

 

posted @ 2020-04-11 20:43  Klaus-Chen  阅读(106)  评论(0编辑  收藏  举报