int len = INT_MAX; string res = ""; for(int i = 0; i < sl; i++){ if(dp[i][tl-1] != -1){ if(len > i-dp[i][tl-1]+1){ len = i-dp[i][tl-1]+1; res = S.substr(dp[i][tl-1], len); } } } return res; }
300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence.
Example:
Input: [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.
class Solution { public: /* The count array isthe dp structure that store thenumberof increasing subsequence starts fromthe current number traverse all numbers behindthe current number, assume the index of a numberbehindthe current numberis j, the index ofthe current numberis i if nums[i] is smaller than nums[j] andthecount[j] is largest inthe numbers which are behindthe current number, thencount[i] is added bythecount[j] The resultisthe largest one inthecount array. */
for(int i = (int)nums.size() - 2; i >= 0; i int iLargest = 0; for(int j = i + 1; j < nums.size(); j++){ if(nums[i] < nums[j] && iLargest < counts[j]){ iLargest = counts[j]; } } if(iLargest > 0) counts[i] += iLargest; } int largest = counts[0]; for(int i = 1; i < counts.size(); i++){ if(largest < counts[i]) largest = counts[i]; } return largest; }
};
354. Russian Doll Envelopes
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envelope can fit into another if and only if both the width and height of one envelope is greater than the width and height of the other envelope.
What is the maximum number of envelopes can you Russian doll? (put one inside other)
Note: Rotation is not allowed.
Example:
Input: [[5,4],[6,4],[6,7],[2,3]] Output: 3 Explanation: The maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).
近期评论