3 ms·
I think it's a little trickier than that, since the problem asks for the indices not the numbers, which would be lost in the sorted version. You'd have to keep
by nkurz 3y ago
I think it's a little trickier than that, since the problem asks for the indices not the numbers, which would be lost in the sorted version. You'd have to keep an additional mapping from the sorted to original positions. And are we counting the sorting and space for the maps as free? Or do we have to account for them somehow?
Mostly, I just don't like the problem specification. What does it actually mean to "solve it efficiently"? How many times are we going to be searching each input array? How much memory do we have available? What tradeoff between memory and computation is allowable? How much does a comparison cost us versus accessing memory? Are all targets equally likely?
Assuming as theoretical computer scientists tend to do that we can ignore all constant time operations, that access to memory is free, that an unknown but fixed amount memory is available, and that we'll be searching the same array forever ruins the problem for me. Fill in the numbers and we can talk about what's optimal, but the "spherical cow" approach does nothing for me.
- tromp 3y ago> I think it's a little trickier than that, since the problem asks for the indices not the numbers, which would be lost in the sorted version. Good point. > You'd have to keep an additional mapping from the sorted to original positions. It's simpler to just find the two numbers in the original unsorted array.
- raverbashing 3y ago> You'd have to keep an additional mapping from the sorted to original positions. 2*O(n) is still O(n)
- taeric 3y agoThough, at the low level, it is perfectly valid to compare the full costs, not just the growth rates. Right?
- deleted 3y ago[deleted]
- marginalia_nu 3y agoFor an input array of values I[], create an array A = [ 0 ... n ] so that n = len(I). Sort the indices in A based on their corresponding value in I. Now A is the mapping, A[n] is the index in I of the n+1:th largest value, and you can binary search in the same way, by comparing I[A[i]]. This is strictly more space efficient than a hash table. You're not going to win any awards for being cache efficient, but then again, when the competition is a damn hash table it's not too bad ;P
- tialaramex 3y agoFor the specifics, as someone else observed, walking nums to find the values we've discovered and identify their indices is very affordable, it's N operations, and if we can't afford N we're in trouble since we definitely need to do at least N comparisons. To your general point, sure, one thing that's nice (but takes a colossal effort) about Advent of Code is that it has specific inputs for players, and they have specific answers, so although it's usual for participants to write code which could (you hope) solve the problem in the abstract, the actual test is whether you solved the input you actually had. Sometimes not all conceivable inputs are even tractable (and some people find this situation frustrating if it happens) only the inputs given to real players may be suitable. For this exercise the provided Go solution gets to handwave a bunch of problems. For example, Go's "map" hash table allocates whenever it wants to and has amortized table growth on top. That can really add up for a problem like this. If I attacked it in Rust I would make HashMap::with_capacity(nums.len()) which does a single allocation up front, I might not need all this space if I exit early, but it won't hurt. I don't know if there's even a way to write that in Go.
- ape4 3y agoIt seems like it could be a "waste" to make a hash or sorted list only to throw throw it away after one use. Depending on what's required.