How to do scheduling using dynamic programming? [closed] - algorithm

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 5 years ago.
Improve this question
The Problem
Given N input vector = [x1, x2, ..., xl] and each of them have same length L. Define F = summation of N input vector.
And the aim is to find a set of integer T = [n1, n2, ..., nN] represents delay of each input vector so that the maximum value of F is minimized.
Example
Assume 2 Input vector [0, 3, -1, 1, 0], [0, 2, -1, 0, 0]:
if T = [0, 0], then F = [0, 5, -2, 1, 0] and max(F) = 5 which is obviously not a good result.
if T = [1, 0], so that F = [0, 2, 2, -1, 1, 0], max(F) = 2. This T is we aim to find. The computing process below may help understand this problem:
T = [1, 0]
Vector1 with delay 1 [0, 3, -1, 1, 0]
Vector2 with delay 0 [0, 2, -1, 0, 0]
F = [0, 2, 2, -1, 1, 0]
The Question
Any idea how to delay vector T using dynamic programming?
Any advise would be greatly appreciated.

Related

Rearrange the negative elements of the array so that they are in non-descending order [closed]

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 1 year ago.
Improve this question
Rearrange the negative elements of the array so that they are in non-descending order. Leave the remaining elements in their places.
It will show like that: [-1, 1, -3, 6, -5, -9] -> [-9, 1, -5, 6, -3, -1]
How can I do this without using additional arrays?
There are many ways to do that. Here is one.
arr = [-1, 1, -3, 6, -5, -9]
negs = arr.select { |n| n < 0 }.sort
#=> [-9, -5, -3, -1]
arr.map { |n| n >= 0 ? n : negs.shift }
#=> [-9, 1, -5, 6, -3, -1]

Sort algorithms with time complexity in the order of number of elements [duplicate]

This question already has answers here:
How to sort a list with given range in O(n)
(4 answers)
Is there an O(n) integer sorting algorithm?
(6 answers)
Closed 5 years ago.
I am looking for an O(n) sort algorithm where n is the number of elements to sort. I know that highly optimized sorting algorithms are O(n log n) but I was told that under the following condition we can do better. The condition is:
We are sorting numbers in a small enough range, say 0 to 100.
Say we have the following
unsortedArray = [4, 3, 4, 2]
Here is the algorithm:
Step 1) Iterate over the unsortedArray and use each element as the index into a new array we call countingArray. The value we will hold in each position is the count of times that that number appears. Each time we access a position we increment it by 1.
countingArray = [0, 0, 0, 0, 0, ..., 0, 0, 0, 0] // before iteration
countingArray = [0, 0, 0, 0, 1, ..., 0, 0, 0, 0] // after handling 4
countingArray = [0, 0, 0, 1, 1, ..., 0, 0, 0, 0] // after handling 3
countingArray = [0, 0, 0, 1, 2, ..., 0, 0, 0, 0] // after the second 4
countingArray = [0, 0, 1, 1, 2, ..., 0, 0, 0, 0] // after handling 2
We can allocate countingArray in advance because the range of the numbers we wish to sort is limited and known a-priori. In your example countingArray will have 101 elements.
Time complexity of this step is O(n) because you are iterating over n elements from unsortedArray. Inserting them into countingArray has constant time complexity.
Step 2) As shown in the example above countingArray is going to have positions with value 0 where there were no numbers to count in unsortedArray. We are going to skip these positions in the following iteration we will describe.
In countingArray non-zero positions define a number that we want to sort, and the content in that position define the count of how many times that number should appear in the final sortedArray.
We iterate over countingArray and starting at the first position of sortedArray put that number into count number of adjacent positions. This builds sortedArray and takes O(n).
countingArray = [0, 0, 1, 1, 2, ..., 0, 0, 0, 0]
// After skipping the first 2 0s and seeing a count of 1 in position 2
sortedArray = [2, 0, 0, 0]
// After seeing a count of 1 in position 3
sortedArray = [2, 3, 0, 0]
// In position 4 we have a count of 2 so we fill 4 in 2 positions
sortedArray = [2, 3, 4, 4]
=======
Total time complexity is O(n) * 2 = O(n)

How do I sort the digits of a number in order from smallest to largest? [closed]

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 7 years ago.
Improve this question
How do I sort the digits of a number in order? I want a four digit number to be changed around and made in order from smallest to largest/largest to smallest.
For example, given 8493, I want that number to become 3489.
Hack the number into digits, sort them and put them back together.
In your case: 8493 -> {8, 4, 9, 3} -> {3, 4, 8, 9} -> 3489
With 2 digits: 51 -> {5, 1} -> {1, 5} -> 15
With 12 digits: 511111011111 -> {5, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1} -> {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5} -> 11111111115 (here it depends if you want to keep the 0 or not.
Convert int to string, then use Best way to reverse a string. If needed convert string back to int.

"Interpolate" an array (add new elements between existing) [closed]

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 8 years ago.
Improve this question
I can't seem to find a ruby-way to transform this
[0, 1, 2, 3]
into
[0, 0.5, 1, 1.5, 2, 2.5, 3]
e.g, insert new elements based on existing ones applying some rule.
EDIT: I'm asking about a general case, not necessarily a 0.5 increment. Say, when elements are not successive:
[1, 3, 12] => [1, 2, 3, 7.5, 12]
So the accepted answer is just perfect here, thanks.
arr = [0, 1, 2, 3]
def rule(x, y)
(x + y) / 2.0
end
arr.each_cons(2).flat_map{|x, y| [x, rule(x, y), y]} # => [0, 0.5, 1, 1, 1.5, 2, 2, 2.5, 3]

checking if 2 numbers of array add up to Input number in ruby [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to ruby on rails .
I was programming ruby and want to try checking if 2 numbers of array add up to Input number in ruby.
eg,
array A[]= {3, 1, 8, 11, 5, 7}
given integer say N = 6
answer will be 1,5.
I know how to program it in java,C++ but i am stuck in ruby coding,
Can anyone please help me.Thanks in advance
You can use Array#combination:
ary = [3, 1, 8, 11, 5, 7]
n = 6
ary.combination(2).detect { |a, b| a + b == n }
#=> [1, 5]
combination(2) creates an array of all combinations of length 2, i.e. [3,1], [3,8], [3,11] etc.
detect { |a, b| a + b == n } returns the first pair with sum n
You can use find_all instead of detect to return all pairs with sum n.
a = [3, 1, 8, 11, 4, 5, 7, 2]
> a.combination(2).select {|i| i.inject(:+) == 6 }
#=> [[1, 5], [4, 2]]
a = [3, 1, 8, 11, 5, 7]
p a.combination(2).find{|i| i.inject(:+) == 6}
# >> [1, 5]

Resources