Leetcode-Question-646: Maximum Length of Pair Chain

原文链接:

https://leetcode.com/problems/maximum-length-of-pair-chain/

解析:

 

Leetcode-Question-115: Distinct Subsequences

原文链接
https://leetcode.com/problems/distinct-subsequences/

题目描述
115. Distinct Subsequences
Difficulty: Hard

Given a string S and a string T, count the number of distinct subsequences of T in S.

A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, “ACE” is a subsequence of “ABCDE” while “AEC” is not).

Here is an example:
S = “rabbbit”, T = “rabbit”

Return 3.

解析:

动态规划法(动手画图吧)。

参考:https://discuss.leetcode.com/topic/9488/easy-to-understand-dp-in-java

Updated 2017.10.20

 

Leetcode-Question-377: Combination Sum IV

原文链接
https://leetcode.com/problems/combination-sum-iv/

题目描述
377. Combination Sum IV
Difficulty: Medium

Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.

Example:

Follow up:
What if negative numbers are allowed in the given array?
How does it change the problem?
What limitation we need to add to the question to allow negative numbers?

解析

Updated 2017.10.18

Follow-up:

 

Leetcode-Question-213: House Robbery II

原文链接:

https://leetcode.com/problems/house-robber-ii/

题目描述:

House Robber II
Difficulty: Medium

Note: This is an extension of House Robber.

After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, the security system for these houses remain the same as for those in the previous street.

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.

 

解答:

照搬https://leetcode.com/discuss/36770/9-lines-0ms-o-1-space-c-solution, 利用动态规划,O(1)的空间复杂度。

 

This problem is a little tricky at first glance. However, if you have finished the House Robber problem, this problem can simply be decomposed into two House Robber problems. Suppose there are n houses, since house 0 and n – 1 are now neighbors, we cannot rob them together and thus the solution is now the maximum of

Rob houses 0 to n – 2;
Rob houses 1 to n – 1.