|
| 1 | +/** |
| 2 | + * Using the JavaScript language, have the function optimalAssignments(strArr) |
| 3 | + * read strArr which will represent an NxN matrix and it will be in the |
| 4 | + * following format: ["(n,n,n...)","(...)",...] where the n's represent |
| 5 | + * integers. This matrix represents a machine at row i performing task at column |
| 6 | + * j. The cost for this is matrix[i][j]. Your program should determine what |
| 7 | + * machine should perform what task so as to minimize the whole cost and it |
| 8 | + * should return the pairings of machines to tasks in the following format: |
| 9 | + * (i-j)(...)... Only one machine can perform one task. For example: if strArr |
| 10 | + * is ["(5,4,2)","(12,4,3)","(3,4,13)"] then your program should return |
| 11 | + * (1-3)(2-2)(3-1) because assigning the machines to these tasks gives the least |
| 12 | + * cost. The matrix will range from 2x2 to 6x6, there will be no negative costs |
| 13 | + * in the matrix, and there will always be a unique answer. |
| 14 | + * |
| 15 | + * https://www.coderbyte.com/results/bhanson:Optimal%20Assignments:JavaScript |
| 16 | + * |
| 17 | + * @param {array} strArr |
| 18 | + * @return {string} |
| 19 | + */ |
| 20 | +function optimalAssignments(strArr) { |
| 21 | + // Parse input to 2d array |
| 22 | + const matrix = strArr.map(row => row.match(/\d+/g).map(Number)); |
| 23 | + |
| 24 | + // Array index = machine (row) |
| 25 | + // Array value = task (col) |
| 26 | + const tasks = []; |
| 27 | + for (let i = 0; i < strArr.length; i++) { |
| 28 | + tasks.push(i); |
| 29 | + } |
| 30 | + |
| 31 | + // Brute force check all possibilities |
| 32 | + const permutations = permute(tasks); |
| 33 | + |
| 34 | + let minCost = Number.MAX_SAFE_INTEGER; |
| 35 | + let minAssignment = []; |
| 36 | + permutations.forEach(permutation => { |
| 37 | + const cost = getTotalCost(matrix, permutation); |
| 38 | + |
| 39 | + if (cost < minCost) { |
| 40 | + minCost = cost; |
| 41 | + minAssignment = permutation; |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + // Format output to spec |
| 46 | + const result = minAssignment |
| 47 | + .map((value, index) => `(${index + 1}-${value + 1})`) |
| 48 | + .join(''); |
| 49 | + |
| 50 | + return result; |
| 51 | +} |
| 52 | + |
| 53 | +function getTotalCost(matrix, assignment) { |
| 54 | + let cost = 0; |
| 55 | + for (let i = 0; i < assignment.length; i++) { |
| 56 | + cost += matrix[i][assignment[i]]; |
| 57 | + } |
| 58 | + return cost; |
| 59 | +} |
| 60 | + |
| 61 | +// https://en.wikipedia.org/wiki/Heap's_algorithm |
| 62 | +// Iterative |
| 63 | +function permute(arr) { |
| 64 | + let count = Array(arr.length).fill(0); |
| 65 | + |
| 66 | + const results = [arr.slice()]; |
| 67 | + |
| 68 | + let i = 0; |
| 69 | + while (i < arr.length) { |
| 70 | + if (count[i] < i) { |
| 71 | + if (i % 2 === 0) { |
| 72 | + const temp = arr[0]; |
| 73 | + arr[0] = arr[i]; |
| 74 | + arr[i] = temp; |
| 75 | + } else { |
| 76 | + const temp = arr[count[i]]; |
| 77 | + arr[count[i]] = arr[i]; |
| 78 | + arr[i] = temp; |
| 79 | + } |
| 80 | + results.push(arr.slice()); |
| 81 | + count[i]++; |
| 82 | + i = 0; |
| 83 | + } else { |
| 84 | + count[i] = 0; |
| 85 | + i++; |
| 86 | + } |
| 87 | + } |
| 88 | + return results; |
| 89 | +} |
| 90 | + |
| 91 | +module.exports = optimalAssignments; |
0 commit comments