由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 求String中递增子字符串的个数怎么解?要求O(nlogn)
相关主题
两个矩阵的算法题c++ define 一问
请教palindrome算法[转载] 简单的题都不敢做了.
Algorithm Question: Given an array and a sum value returnInterview question
git算法问题
Is it safe to use unique_ptr with STL container?能有人详细讲一下这两道google的面试题吗?
Java job schedulercomplexity of set operation?
问一道排序题目几道面试题:memory, sort, 等
一FG家常见题 (转载)Please help me prove SUM(logi) is Omega(nlogn) (转载)
相关话题的讨论汇总
话题: given话题: int话题: element
进入Programming版参与讨论
1 (共1页)
S*******C
发帖数: 822
1
Google面试题
Given a sequence S of integers, a subsequence is any selection of numbers of
S in the same order as S. For example, if S = 1,1,2,3,5,8,13 then 2,3,8 is
a subsequence of S and so is 1,1,5,13 and so is 1,2,3. However, 5,6,7 is not
a subsequence, 8,5,2 is also not a subsequence.
A subsequence T is increasing is T[i] < T[i+1] for all i.
Given a sequence S = 4, 9, 3, 8, 6, 7, 10 .... : we can have 3, 8, 10 or 4,
9, 10 or 4, 6,7,10 as its increasing subsequences.
Our task is : given a sequence S of n numbers, count the number of
increasing subsequences of S. Given an O( n log n) algorithm to accomplish
this task.
S*******C
发帖数: 822
2
我这里有一个求最长递增子序列长度的O(nlogn)动态规划算法作为参考
public class LongestIncreasingSubseq {
static int min_last_element[];// 记录长度为i的递增子序列中最大元素的最小
值, min_last_element is an sorted array
static int lengthOfLIS;
public static void main(String args[]) {
int a1[] = new int[] { 12, 11, 13, 10, 14, 11, 15, 12, 17, 20, 11,
22 };
min_last_element= new int[a1.length];
System.out.println(findLongestIncreasingSubseq(a1, a1.length));
}
// 返回min_last_element[i]中刚刚大于x的那个元素的下标
static int binarySearch(int[] min_last_element, int lengthOfLIS, int x) {
int left = 0, right = lengthOfLIS, mid;
while (left <= right) {
mid = (left + right) / 2;
if (x > min_last_element[mid])
left = mid + 1;
else if (x < min_last_element[mid])
right = mid - 1;
else
return mid;
}
return left;
}
static int findLongestIncreasingSubseq(int[] A, int size) {
min_last_element[0] = A[0];
lengthOfLIS = 1;
for (int i = 1; i < size; i++) {
if (A[i] > min_last_element[lengthOfLIS - 1]) {//case 1
min_last_element[lengthOfLIS] = A[i];
lengthOfLIS++;
} else {//case 2
int pos = binarySearch(min_last_element, lengthOfLIS, A[i]);
min_last_element[pos] = A[i];
}
}
return lengthOfLIS;
}
}
1 (共1页)
进入Programming版参与讨论
相关主题
Please help me prove SUM(logi) is Omega(nlogn) (转载)Is it safe to use unique_ptr with STL container?
Re: amazon onsite interview question (转载)Java job scheduler
Efficient algorithms for finding number, help please问一道排序题目
问一道算法题一FG家常见题 (转载)
两个矩阵的算法题c++ define 一问
请教palindrome算法[转载] 简单的题都不敢做了.
Algorithm Question: Given an array and a sum value returnInterview question
git算法问题
相关话题的讨论汇总
话题: given话题: int话题: element