METHOD: Maintain two stacks A and B. Push into A. To pop look at B. If B is empty then pop A completely and push it into B and then pop from B. Otherwise simply pop from B.
QUESTION : 1)What is the difference between running time and amortized running time?
2)Why is the amortized running time constant here? Will it not increase with the increasing number of input and when we decide to pop from it? Because if we keep pushing then A fills up quite a lot while B is empty. Now if we decided to pop from B then I need to copy all of A and then pop.
When looking at amortized cost, you don't look at a single operation but on multiple operations which occur during program execution. The idea is that, if you have a lot of operations which are very cheap (like a single push or pop), and few operations which are expensive (like popping all items from A and pushing it to B) you can "distribute" the cost of the expensive operations to the inexpensive ones. This gives you a "overall" cost compared to the worst case O(n) for a single dequeue.
In your example, you can show that each element is pushed to a stack two times at max (once for adding and once for pushing it to the other stack) and popped max. two times (once for popping it from the stack and once for popping it to remove it from the queue). So for an enqueue operation, the amortized max. cost is 3 (because when an element is pushed and never popped it might still be popped and pushed to the other stack) and 1 for a dequeue which is both constant.
The key idea here is that an item moves from stack1 to stack2 just once in its whole lifetme i.e. its pushed in stack1, gets moved to stack2 and then popped out. There's no going back and forth whatsoever. So it can undergo 4 operations at the most in its life cycle
(First push) Initial push in stack1 on enqueue
(First pop) When popped for movement from stack 1 to stack2
(Second push) When pushed in stack 2 for movement from stack 1 to stack2
(Second pop) When popped out on dequeue
Let's say each push/pop operation costs $1. So an item would consume $4 right from getting enqueued to getting dequeued. So if you were running this enqueue/dequeue business, your business would break even ($0 profit or loss) if you started charging $4 for each enqueue and dequeue operation. Hence a $4 amortised cost for each enqueue/dequeue combined operation.
Rather if you were running an enqueue only business, you could just charge $1 since you have only 1 push to do and your job is done. Hence a $1 amortised cost for each enqueue operation.
And if you were running a dequeue only business, you would charge $3 for each dequeue operation as you would have to pop twice and push once as described in the steps above. Hence a $3 amortised cost for each dequeue operation.
Related
In short, here is my problem:
I go from view controller A to VC B
Then from VC B to VC A.
Loop 1 and 2 for a number of times. All by pushing.
Now I edit VC A (e.g. Liking one of my post) and save. Then I go back by tapping on Back button. I want to see the changes in VC A (e.g. The number of likes increase 1 in my post)
That is the simple case when I only have 2 VCs. You can have many VCs in the loop, and you can may want to update other VCs not in the loop as well.
One example is in social network app like Instagram, where you can go to your profile, followers list, go to sb's profile, go to their follower list, go to your profile from there..so on and so on. Then at the end you make a like of one post, and you want to update view in Home tab, as well as all views in middle of the loop, while you tapping on Back button.
I know we can implement in 2 ways:
In each VC, in viewwillappear we check for update and then update. The disadvantage is that it will not work in offline mode. And it is bulky to check everywhere.
Use notification. One view having changes will notify other views to update. But you have to define by yourself which views will accept which types of notification, and having multiple notification triggered in your app is quite messy and hard to manage.
Are there any other ways? How are you doing in your app?
My question is more of asking about architect, not for coding sample, so please answer by giving a solution, an architecture and some short analysis of the result of it.
This question is for both ios and android.
My suggestion is
Save the number of likes and the post id in NSUserDefaults and check in each view controller while your post id is same as the value in NSUserDefaults the increment it by your count of like for the post in NSUserDefaults . But at some point of time you have to sync it with webservice .
I am using allocations to test my decompression code for my app. I use a Master UITableViewController to display 11 filenames. the test app screenshot is as followings:
when you select one, it will push a UIViewController to display the file, then come back to Master UITableViewController.
Now if you repeat to select one cell, there is no memory spike in the chart. But if you select from 1 to 2, it will increase at least 224KB memory between the generations. I test from 1 to 11. all these changes will will increase almost the same memory. the screenshot is as followings:
I check the detail find there is a VM:UITableViewCellSelectedBackground(CALayer) (some named VM:CoreAnimation, but all are the same 224KB) in the growth. And if i select the selected cells, there is no such big memory spike. it seems they will only be created at the first time one cell is selected.
Now my question is why are they come out? is this a kind of memory leak? and if yes, how can i fix it? Any help would be much appreciated.
UPDATE:
i have confirmed there is nothing related to my decompression code, if i comment out the decompression code, the VM:UITableViewCellSelectedBackground(CALayer) is still there when selected a new cell.
I have tested in simulator, result is similar to you. But memory will fall down after a while.
I don't think this is memory leak. When we select one cell, system may add a translucent layer above the it.
The layer is created when needed so it increase the memory.
I am sure once one cell is selected, the system will create some objects (something is related to the layer or the layer itself) that won't be released though the cell is deselected later, those objects maybe some cache to improve the performance when the cell is selected next time.
There's two views on a NavigationController stack:
EDIT ITEM-DETAILS VIEW (= basically a form)
SHOW ALL ITEMS IN A TABLE VIEW
I wonder if there are best practices for the task I have:
When the user taps "BACK" in the UINavigationController-bar (while being in view 1) the app should update the item on the server.
That's not so difficult, but the BACK-action leads to view 2, and 2 is not up-to date, because the update happened in the background and wasn't through before the GET-request for the table view data finished.
So in order to have view 2 always show accurate data, I have several options. All a bit annoying.. (for example having ViewController of view 2 talk to server on 1's behalf and update itself when completed, or having a "update happened" notification that triggers a reload, ...)
But.. what's a good best-practice for this case?
I think I would make a central place for the Items. Lets call it a ItemsStore. ItemsStore is a singleton that has the responsibility to have a set of the latest items and give access to the items. It also fires notifications if new data arrives, or old data is saved.
In this case:
View 2 adds the data to the store. The store notifies that there are
changes.
View 1 updates on the notification.
View 2 also asks the ItemsStore to
save the data to the server.
I would not give the responsibility of loading and saving to the controllers, it will get ugly and complex.
The Scenario:
I have a UITableView that displays data loaded from a network resource. A synchronization with that resource can take a while to complete, and the results are loaded in piecemeal. The network communication happens in the background via NSOperationQueue. Results are delivered via a notification, with control being transfered back to the main thread to insert the new results. The user is capable of performing operations on the data in the table while this is happening.
The Problem:
If the user performs a delete operation on one of the rows in the table while the synchronization is happening, it is very likely that the row count will change while the table view is animating the deletion (because the animation results in several iterations through the event loop, and the notification handler for new results gets called). This leads to an internal inconsistency exception.
The synchronization results in a call to reloadData on the table view as new results are loaded in batches. More importantly, it causes the row count to change.
I have been unable to find a way to know that the table is animating (or be notified when it has finished), so that I can delay the processing of new results. Is this possible?
Wrap all your table changes in beginUpdates/endUpdates. According to the docs, these can be nested, so you shouldn't have problems with simultaneous inserts and deletes.
When I use addSubview method and then removeFromSubview to load next ViewController then after load view some times, my app crash. I have many images on views.
I think my memory isn't released, in spite of I use ARC.
What I should use to make it work? I tried addChildViewController, but then my view aren't loading.
As per memory management guidelines, whenever you say addSubview, the reference count increases by 1 and whenever you say removeFromSuperview the reference count decreases by one. So, if you are removing any view, that you have added to any view, the reference count should be adjusted and should not cause memory leak.
What problem I can foresee is that you are having memory leak in the added view (the view, that you are adding multiple times) and this in order overflowing your memory. Try maintaining the reference counts and it will work perfectly. As an alternate solution, you can also track memory leaks by using instrument tools.