It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm trying to find an algorithm which uses linear space of memory for:
Given two strings x and y over an arbitrary alphabet, determine their longest common sub sequence.
Note that when you're calculating the next row of the table in the dynamic programming solution to solve the LCS problem, you only need the previous row and your current row. Then you can modify the dynamic programming solution to keep track of only the previous row and the current row instead of the m x n table. Every time you reach the end of the current row, you set the previous row to the current row, and start from the beginning of the row again. You do this m times where m is the number of rows in your table. This will use space linear in the number of columns.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have come across a problem about the determination of triangle, it says:
Given a sorted integer array(length n), determinate whether you could
build a triangle by choosing three integers from the array, the
answer is "yes" or "no".
A naive solution is by scanning all the possibilities but it turn out to be O(n^3), seems
it will be C(n, 3) possibilities.
Assuming that the integers represent side lengths and array(0) > 0,
bool IsTriangle(int[] aray, int start) {
if(array.length - start <= 2) return false;
return (array(start+2) < array(start+1) + array(start+0))
|| IsTriangle(array,start+1);
}
This works because the list of integers is sorted; thus the RHS will always be larger using any subsequent elements of the array, and the LHS will be smaller using any previous elements of the array, and thus can satisfy the triangle inequality only if the selected three consecutive elements satisfy it. This is of course O(n) and can easily be converted to a (less elegant but more performant) iterative solution.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Design a data struture for integers of range 1 to 1000(non repeating) for following operations.
1)Insertion
2)Deletions
3)Search
4)int Anyvalid() -> This should return any valid/present number present at the time.
Example if 1,5,7
are present,then return any of the 3 number.
All the operations should be 0(1)/Constant time.
I thought of bit vector but it give 0(n) in case of AnyvalidElement()..
for rest all it works in 0(1).
Use a doubly linked list and an array of pointers to the list nodes.
Insert n: Add n to the front of the list, array[n] = pointer to newly
added node.
Delete n: Use array to jump to the correct node in the
list and remove it. Set array[n] = NULL;
Search n: check if array[n] != NULL
AnyValid: return front of the list
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have a list of projects and each project takes you exactly two days to be completed and has a due date. let P[i].id, P[i].duedate, and p[i].value be the id of the project, the due date of the project, and the value you get if you complete the project on time(on or before due date)
write an algorithm that takes as input array A and returns a schedule of which projects you will do and when, to maximize the value you get.
the output of the algorithm is an array B such that B[i] is the id of the project that you will work on during day i, i>= 1.
no more than one project in a particular date, and you don't get the value of the project unless you complete it by the due date, today is day 0 and you will start working on the projects from day 1 (the due date is an integer), e.g., if the due date of a project is 5, you can choose to work on it on days 3 and 5)
1- write the algorithm.
2- prove that the algorithm is optimal?
3- what is the time complexity for the algorithm?
If all the values be same it's simple, just greedy approach by selecting least possible due date works well.
When the values are different, you can use similar approach but this time by dynamic programming (I'll assume your due dates are discrete).
Create an array of size Max{due date} name it as V, this array holds maximum possible value which can be earned in specific time, and another array for each value in V to save the selected tasks in related V[i], now you have this DP choice:
V[0] = 0, V[1] = max{value_x1, V[i] = Max {V[i-2] + value_xi, V[i-1]}
Here value_xi means maximum value task which has due date equal or smaller than i, Also this task shouldn't be in V[i-2] selection, after that update V[i] selection.
Finally I'll left to you to finish your homework by finding the order of this algorithm and it's correctness, also you can improve memory usage.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Please help solve this:
Telephone numbers are often given out as a word representation, so that they are easy to remember. For example if my number is 4357, the text given is HELP. There could be many other possibilities with the same digits, most of which do not make sense.
Write a space-and-time-optimal function that can, given a phone number, print the possible words that can be formed from it.
Based on the detailed explanation in the comment this should be a simple permutation combination problem:
Each digit will have a number of characters associated to it (example 4 could mean either of G,H or I) and then for a combination of digits the permutation can be computed.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Say you wanted to find which input causes function x to output value y, and you know the (finite) range of possible inputs.
The input and output are both numbers, and positively correlated.
What would be the best way to optimize that?
I'm currently just looping through all of the possible inputs.
Thanks.
One solution would be a binary search over the possible inputs.
Flow:
find the median input x
get the output from function(x)
if the output is less than the desired y
start over using the smaller half of the possible inputs
else
start over using the larger half of the possible inputs
A binary search algorithm, perhaps?
http://en.wikipedia.org/wiki/Binary_search_algorithm
If the range is finite and small, a precomputed lookup table might be the fastest way
if you have some sets of know "x" data that yield "y" you can divied between training and test sets and use neural networks.