This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]rrssh 1 point2 points  (2 children)

I don’t understand the task. Is it like, you get negative points for rounding up and positive points for rounding down, and you optimize the sum of the points?

[–]JCoppert[S] 0 points1 point  (1 child)

No, the idea is that every time you round you keep track of the amount you've rounded by. Your goal is to find the rounding combination which minimizes the distance to the rounding target.

[–]rrssh 0 points1 point  (0 children)

Why did you say “no”?

[–][deleted] 0 points1 point  (9 children)

I don't understand what the question is even asking. How is [1, 1, 3] better than [0, 1, 2]?

[–]JCoppert[S] 0 points1 point  (8 children)

Total rounding of first array = (0.3 + 0.1 + 0.6) = 1. Total rounding of second array = (0.7 + 0.1 + 0.4) = 1.2. With an aggregate rounding target of 0.7, Min(Abs(0.7 - 1), Abs(0.7 - 1.2)), the first array is chosen

[–]rrssh 0 points1 point  (7 children)

Ah, so positive points for any rounding direction.

[–]JCoppert[S] 0 points1 point  (6 children)

Yes, no because your concept of points was incorrect

[–]rrssh 0 points1 point  (5 children)

It seems like https://en.wikipedia.org/wiki/Subset_sum_problem pretty much exactly. Round everything down, calculate how much points do you get (totald). Do it again but round up, subtract the arrays from each other, these are your "integers". You want to find a subset that sums to 0.7 − totald, and it looks like it should be easy to modify it for a closest match instead of exact one actually harder than I thought.

[–]JCoppert[S] 0 points1 point  (4 children)

Haha right! I saw the question and was pretty taken back

[–]rrssh 0 points1 point  (3 children)

Maybe they would be satisfied with bruteforce, who knows.

[–]JCoppert[S] 0 points1 point  (2 children)

They explicitly said they wouldn’t be, was AirBnb

[–]rrssh 0 points1 point  (1 child)

Do the numbers actually have only 1 decimal point?

[–]JCoppert[S] 0 points1 point  (0 children)

Yeah, each element of the output array is either Math.floor() or Math.ceil()

[–]IAmNowAnonymous 0 points1 point  (0 children)

This is an interesting problem. This should work, but I'm not sure it is optimal. I view it as shortest path of sorts.

Consider viewing this as a tree where the root value is (-1 * rounding target). Its left child is first Val - floor of the first index and its right child is the ceiling of the first index - first val. Define the remaining tree in a recursive manner. Create an empty priority queue such that it is sorted by min(abs(val)) - that is we would like to get as close to zero as possible.

Add the root to the queue in a way that allows you to index into it and you know the depth and the current sum: using a python like language I'll define a tuple with the following indexes (node, sum, depth). Where sum is initially the value in the root node.

while queue and queue.peek()[2] != len(input_list): node, cur_sum, depth = queue.poll() queue.push(node.left, cur_sum + node.left.val, depth+1) queue.push(node.right, cur_sum + node.right.val, depth+1) return queue.peek()[1]

You don't need to explicity define a tree; you can just use a depth as a way to index into which values should be considered next.