| cire (1851) | ||||
I read the OP's post and, from the sound of it, he was just barely over the time limit. Perhaps I was mistaken. While I (think) I understand the basics of big O notation, I'm not certain how to classify the (other) solution I came up with, which was simply to keep track of the ranges of "occupied" shelf. In the best case (all elements removed from an end) it would be O(1). In the worst case, for a particular i, i/2 ranges have to be traversed (and in that case, every search results in a range being split, which is costly.) I can't quite conceptualize how these factors figure in. No college here and programming is a hobby -- maybe I just don't have the background to grasp it on an intuitive level. Annoyingly verbose code:
[ Edit: On a side note, I wanted to try it out on the site linked by the OP just to make sure it worked, but in the process somehow I selected a different problem for the submission, so of course it failed. Spent quite a bit of time re-tracing through the logic before I figured out I'd been submitting for the wrong problem! ] | ||||
|
Last edited on
|
||||
| helios (10126) | |||||||||
|
How about a tree? Every node can hold how many books its subtrees hold, and leaves can hold sequences of books. For example, we start out with ([1,2,3,4,5,6,7,8,9,10], 0, null, 0, null)input: 2
input: 5 5>1, so modify the right branch:
input: 5 5>1, so modify the right branch. 5-1>3, so modify the right-right branch:
input: 3 3>1, so modify the right branch. 3-1<=3, so modify the right-left branch:
And so on. On average, removing a book should take O(log n). If an array of books is kept, the sequences of books can be represented as a pair of integers denoting ranges on the vector. | |||||||||
|
|
|||||||||
| A 06 (10) | ||||
|
@JLBorges and @Cire I will have a look at your codes, thank you. Meanwhile here's what happened, using @ne555 idea,
i came up with this:
It is working for the sample example and for my own test cases. But when it is submitted, i get a wrong answer, however it is well within the time limit. So i think O(N^2) will be fine for this problem. | ||||
|
|
||||
| A 06 (10) | |
| @helios I didn't understand your post, i always get confused when it comes to trees. So i always try to avoid using them. | |
|
|
|
| helios (10126) | |
|
Alright. My solution passed. Worst time: case #0, 1.8 seconds. Best time: case #5, 0.01 seconds. EDIT: Oh, by the way, cire: I didn't read all of your code, but I think I originally I had the same idea you have. If I'm right, then your solution takes O(N^2) time, because in the worst case, you have to iterate over the entire list every time a book is removed. The first time the list will contain one element; the second, two; the third, three; and so on. Adding up everything, we come up with (1/2)*N^2 + (1/2)*N iterations. As N tends to infinity, the linear term becomes insignificant, so the function looks a lot like (1/2)*N^2. One quadratic function is basically the same as another, so big O notation drops the coefficient: O(N^2). | |
|
Last edited on
|
|
| cire (1851) | |||||
Correct.
Well, this is true up to a point. Once we have removed N/2 books the (maximum) number of iterations required to traverse the list declines by one for each subsequent removal. (At that point there are only one element ranges left .) So the maximum number of iterations would look something like: let J = N/2, then 2(1+2+ ... +J). Which is the same as 2( (J(J+1))/2) = J(J+1) = J2 + J And if we substitute N back in: (N/2)2+N/2 = (N2+N)/4 Which, I suppose, as N approaches infinity is overwhelmed by N2! I guess I could've stopped at J2+ J. Drat. I thought I was on to something. And, I suppose worst case should only be considering M < n/2, anyway. Thanks for makin' me think. results from the submission: Worst Case was 0 at 1.81 Best Case was 3 at .01 | |||||
|
Last edited on
|
|||||
| ne555 (4041) | ||
|
Passed too (worst time 1.67) @JLBorges, A 06 (maybe rollie too)
| ||
|
|
||
| rollie (304) | |||
As long as the gauntlet has been thrown down...impl of my original solution, worst time 0.66s for problem #9
| |||
|
Last edited on
|
|||
| helios (10126) | ||
What would happen if, while M>=2*N, someone entered a string of N twos? | ||
|
|
||
| cire (1851) | |||
I should hope the number of books to remove would never be greater than the number of books. | |||
|
|
|||
| helios (10126) | |
| M is the number of books, so M < N/2 is a contradiction. | |
|
|
|
| cire (1851) | |
| So it is. Must've mixed them up at some point. | |
|
|
|
| A 06 (10) | |
|
@ne555 Yes I understood why!! Its because the array test[] isnt sorted. Rollie's solution is similar to what i had thought, only his set is sorted so it works! Thank you everyone for helping me solve this problem, now i have many different methods for solving this thanks a lot. | |
|
Last edited on
|
|