Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Given a rod of length n inches and an array of prices that contains prices of all pieces of the size smaller than n. Using dynamic programming we can get the maximum value and corresponding pieces of the rod. Is there any algorithm which will produce kth maximum value with the corresponding cut for this problem?
Problem : Find k th max price for rod cutting problem.
I think the algorithm can be tweaked in following way :
Change the recursion in rod cutting problem from :
cutRod(n) = max(price[i] + cutRod(n-i-1)) for all i in {0, 1 .. n-1}
To :
Top_K_Price_CutRod(n)[] = top_k(price[i] + cutRod(n-i-1)) for all i in {0, 1 .. n-1}
Basically, at every recursion step, return max k prices for that subpart, because only those can be eventually in overall maximum k .
Bruteforce way is to return all the possible prices of that subpart, but we know for sure that prices which have rank greater than k for this subpart, can't be in top k prices of complete rod eventually.
So here Top_K_Price_CutRod(n)[] is an array of k max prices for that subpart.
At root of the recursion, you will be left with max top k prices.
I don't think we can optimize more than this, as at every recursion node its top k value can be among final top k. So u have to top k values for all subparts.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
There are N nodes connected by N-1 edges. The weight of each edge is 1 and it's possible to reach any node from any other node.
We are given a subset of nodes. We need to pair(1 to 1 mapping) the subset of nodes and find the maximum distance possible after pairing.
For example:
N = 8 ( Number of nodes)
subset of nodes = [2,4,5,6]
Graph:
7
|
6--1--2--8
|
3--4
|
5
Solution
Maximum distance:
Pairs
(2,4) : 2-1-3-4 => distance 3
(6,5) : 6-1-3-5 => distance 3
max distance = 3+3 = 6
It's possible to form other pairs but max distance will always comes out to 6.
How to calculate max distance?
For the first find the centroid of the tree.
Next perform the following operations for each pair of vertices:
Find the distance between vertices and the centroid of the tree. The sum of these values is the maximum distance between the vertices.
First of all, as there are n nodes, and n-1 edges always connecting them, this must be a tree.
Let's say, length of subset, k.
The obvious approach is to run bfs for all the k nodes, it will give you all the distances from the nodes from the subset.
Then, we can do a nested loop (k^2) to find which pair has the largest distance.
Or, we can keep a max_distance variable, and update the max_distance while running the bfs, it will get rid of the extra k^2 term.
We can further optimize this using an LCA. Once we select a root and have the LCAs, we can find any distance pair easily by, d(node1, node2) = d(node1) + d(node2) - 2 x lca(node1, node2)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Suppose I want to find partition number of n, aka p(n). Here Euler's Pentagonal number theorem, a dynamic programming based solution is present, which has time and complexities O(n^2), O(n^2\log(n)) respectively.
Is there any improvement over this algorithm to reduce complexity or is there any proof showing that this algorithm is the best possible for this problem/ reducing complexity bellow this is NP-hard. Also what about the space-time trade off. Can we reduce time/space complexity by increasing space/time complexity respectively (keeping in mind that each complexity should not be more that O(n^3).
The following recurrence can be directly translated to code:
where
,
import numpy as np
def num_partitions(n):
# recursive function with an auxiliary cache to avoid recomputing
# the same value more than once
def get(n, k, aux):
# terminate the recursion
if n < k:
return 0
if k == 1 or k == n:
return 1
# check if the value is already in the cache - if not, compute
# it recursively
if aux[n][k] == -1:
aux[n][k] = get(n-k, k, aux) + get(n-1, k-1, aux)
return aux[n][k]
return np.sum([get(n, k, np.ones((n+1,n+1)) * -1) for k in range(1, n+1)], dtype=np.int)
import sys
print(num_partitions(int(sys.argv[1])))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
suppose that we have a m*n matrix that each rows are in order. so, i only know that order of best algorithm for this problem is O(m(log m + log n)).
(It was a test question and result is this order)
but i don't know how this algorithm works
One idea can be like this.
If I ask you what is the rank of a given number x in the original matrix? How do you answer this question?
One answer can be:
Just binary search the first occurrence of x or greater element on each row. and then add the individual ranks.
int rank = 1;
for (int i = 0; i < m; ++i) {
rank += std::lower_bound(matrix[i].begin(), matrix[i].end(), x);
}
This can be done in O(m * log n) time(m binary searches on n sized arrays).
Now we just need to do a binary search on x(between 0 and INT_MAX or matrix[0][k]) to find the kth rank. Since INT_MAX is const, that will make the overall time complexity O(m * log n) theoretically. One optimization, which can be done use intelligent ranges in place of matrix[i].begin(), matrix[i].end().
PS: Still wondering the O(m*(log m + log n)) or O( m * (log mn)) solution.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I am working on a histogram problem, and then I ran into the following math problem:
Given N numbers, where the value of each number is different, denoted as v1, v2, ..., vn, and the probability of selecting each number is p1, p2, ..., pn, respectively.
Now if I select K numbers based on the given probabilities, where K <= N, what is the expectation of the sum of those K numbers? Note that the selection is without replacement, so that the K numbers cannot involve duplicate numbers. I understand that if the selection is with replacement, the expectation of the sum of the K numbers equals K * E(V), where E(V) = v1*p1 + v2*p2 + ... + vn*p2.
Furthermore, what about the expectation of the variance of those K numbers?
This question is better formulated at here.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Kind of a math question, but very programming related. Doing some Big-O problems and I have an algorithm where a for loop will run n times, where k = input size, n = max power of 4 where (k)/(4^n) >= 1. How can I represent max power of 4 where (k)/(4^n) >= 1 in one mathematic statement?
floor ( (log k)/(log 4) ).
Or something along those lines.
Mathematic statement: [log_4(k)]
Code: floor( log(k) / log(4) )
log base 4 of k? Can take the floor if you only care about integer n.
Taking (k)/(4^n) >= 1, multiply both sides by 4^n to get k >= 4^n, and then take the log base 4 (log_4) of both sides to get log_4 k >= n, or n <= log_4 k. (Equivalently, take log of both sides and get log k >= log(4^n), then note log(4^n) = n log(4), and divide to get (log k)/(log 4) >= n). Choose the largest integer n satisfying this inequality, which is floor(log_4 k).