tl/dr: I need help with the "generate()" method described below... a nudge in the correct direction or perhaps, a good reference/tutorial/something that helps me understand dynamic programming better.
So I'm taking this Coursera course (Computer Science: Programming with a Purpose) not for certificate/credit -- just for the info -- and the programming assignments have been a lot harder than a 100-level, "intro to CS for non-CS folks" (that appears to be how its "marketed") should be (IMO). I'm an experienced programmer (or at least I thought I was) and went thru a CS program where I graduated with a bachelor's back in 1995.
Anyhow, I've been struggling with stuff that uses dynamic programming and the like (I don't remember ever "getting" dynamic programming back in the day) and I'm down to one program in the programming assignments in order to just close out the whole class (I HATE leaving things undone.)
Anyhow, this is the assignment specification: (spoilered 'cuz its kinda big and I don't know how Coursera or Princeton feel about my "publishing" this question)
I've got everything but
I did write some code to futz around with swapping various things so I could "see" how they affected the outcome. In doing so, I found that the numbers coming out of "perfect swapping" (i.e., reversing things) seemed to follow numbers in Pascal's triangle. But that gave me no further insight other than to realize that perhaps the solution might need some recursion.
If anyone could give me a nudge in the right direction, I'd appreciate it.
So I'm taking this Coursera course (Computer Science: Programming with a Purpose) not for certificate/credit -- just for the info -- and the programming assignments have been a lot harder than a 100-level, "intro to CS for non-CS folks" (that appears to be how its "marketed") should be (IMO). I'm an experienced programmer (or at least I thought I was) and went thru a CS program where I graduated with a bachelor's back in 1995.
Anyhow, I've been struggling with stuff that uses dynamic programming and the like (I don't remember ever "getting" dynamic programming back in the day) and I'm down to one program in the programming assignments in order to just close out the whole class (I HATE leaving things undone.)
Anyhow, this is the assignment specification: (spoilered 'cuz its kinda big and I don't know how Coursera or Princeton feel about my "publishing" this question)
Inversions. Suppose that a music site wants to compare your song preferences to those of a friend. One approach is to have you and your friend each rank a set ofnsongs and count the number of pairs of songs(i, j)for which you preferitojbut your friend prefersjtoi. When the count is low, the preferences are similar.
More generally, given an array of integers, a pair of elementsa(i)anda(j)are inverted ifi < janda(i) > a(j). For example, the arraya()has 1 inversion and the arrayb()has 4 inversions.
(wow, that's big)![]()
Write a programInversions.javathat implements the following API:
Code:
public class Inversions {
// return the number of inversion in the permutation a()
public static long count(int[] a) {
}
// return a permutation of length n with exactly k inversions
public static int() generate(int n, int k) {
}
// takes an integer n and a long k as command-line arguments,
// and prints a permutation of length n with exactly k inversions
public static void main(String[] args) {
}
}
Here is some more information about the required behavior:
- Permutations. A permutation of length
nis an integer array of lengthnthat contains each of thenintegers0, 1, ..., n–1exactly once.- Output format. The
main()method should print the permutation of lengthnto standard output as a sequence ofnintegers, separated by whitespace, all on one line.- Performance. The
count()method should take time proportional ton2in the worst case. Thegenerate()method should take time proportional tonin the worst case.- Corner cases. You may assume that the arguments to
generate()satisfyn ≥ 0and0 ≤ k ≤ 1/2 * n * (n−1); this guarantees the existence of a permutation of lengthnwith exactlykinversions.
Here are a few sample executions:
Code:~/Desktop/performance> java-introcs Inversions 10 0 0 1 2 3 4 5 6 7 8 9 ~/Desktop/performance> java-introcs Inversions 10 1 0 1 2 3 4 5 6 7 9 8 ~/Desktop/performance> java-introcs Inversions 10 45 9 8 7 6 5 4 3 2 1 0 ~/Desktop/performance> java-introcs Inversions 10 20 9 8 0 1 2 3 7 4 5 6
I've got everything but
generate() working. (I've figured out a really bad way (quad-nested loops) to do the counting and I'm working on making it better.) My first attempt was simply just randomly swapping things k times, but that didn't solve the problem (obviously), so when I went to figure out why, I saw that my implementation was completely wrong and then I realized I didn't even know how to approach solving that part of the problem since it would grow so fast. I did realize that I could generate ALL the permutations of n things then use count() to find one that met the spec, but it would be super, duper slow.I did write some code to futz around with swapping various things so I could "see" how they affected the outcome. In doing so, I found that the numbers coming out of "perfect swapping" (i.e., reversing things) seemed to follow numbers in Pascal's triangle. But that gave me no further insight other than to realize that perhaps the solution might need some recursion.
If anyone could give me a nudge in the right direction, I'd appreciate it.