Dynamic array implementation of stack - stack

A programmer wants to create a dynamic array implementation of stack where instead of using repeated doubling, a new array of size n+10 is created every time the array cannot accommodate more elements.
For example, for inserting the first element, array of size 0+10=10 will be created. After inserting 10 elements, for inserting the 11th element a new array of size 10+10=20 will be created and the previous array elements will be copied to this new array.
What is the time complexity of this stack implementation?

After n insertions, the array will be O(n) sized, somewhere between n-10 to n+10.
Let's look at the last time the array was increased. Assuming the size of the array is n+10, the calculation goes as follows:
size change -> 10 insertions -> size change -> 10 insertions ....
n+10 -> 10 insertions -> n -> 10 insertions -> n-10 -> 10 insertions -> ... -> n-n = 0 sized
so far, for n insertions, we have total:
Σ(n - 10*i) operations, Σ goes from i=-1 to n
we get n*n total run-time, now we divide by n to get the run-time of each insertion, so it becomes O(n), amortized run-time for each operation.

To accommodate n*10 elements the array has to grow n times, so this might seem to be O(n). However, growing an array involves copying its elements to the new array, which required 10 copy operations the first time, 20 the second, and so on, until n*10 the nth. So I would say this is O(n^2).

Related

How do I create a list of non-repeating cells/numbers in Google Sheets?

I’m trying to emulate Minesweeper in Google Sheets, and for this I want to create a second map adjacent to the first with all of the correct values already in it. To randomize bomb position, I need a list of random numbers or cells(cells would be preferable). However, I cannot figure out how to do this without ending up repeating numbers. The result would ideally be a vertical array of cell coordinates. Thank you!
Answer
The following formula should produce the result you desire:
=SORTN(FLATTEN(MAKEARRAY(10,10,LAMBDA(row,col,ADDRESS(row,col)))),20,,RANDARRAY(100),)
In =MAKEARRAY, change the first 10 to adjust how many rows to randomly choose from, or the second 10 to adjust how many columns to choose from. The value in =RANDARRAY must be equal to the product of the number of rows and the number of columns. (e.g. in the above example, 10*10=100).
Change the 20 to adjust how many randomly chosen values to return.
Explanation
=MAKEARRAY is used to generate an array of every possible row and column combination. It accepts a =LAMBDA, which in this case is just the =ADDRESS function. The first two arguments of =MAKEARRAY determine how large the array should be, which is why changing them adjusts how many rows/columns to randomly pick from.
Then, the result of =MAKEARRAY is squashed into a single column using the =FLATTEN formula.
Finally, the entire thing is sorted randomly using =SORTN combined with =RANDARRAY. =SORTN also limits the number of results that are returned dependent on its second argument, which is why changing it adjusts how many results are returned.
If you want information on how to "freeze" the value of =RANDARRAY so it doesn't recalculate each time you change something, check out this question by player0.
Functions used:
=MAKEARRAY
=LAMBDA
=ADDRESS
=FLATTEN
=SORTN
=RANDARRAY

Linked lists in MIPS - point of example?

I am working with linked lists in MIPS, and have one example that I am not sure about. It says:
write function rotate(head, a, b) that rotates elements of linked lists from position a to position b, such that if element was on position i(that belongs segment a,b) its new position is b+a-i. Numbers a,b are choosen such that a<b. Now, I don't get point of this example, because after we change elements for every position i, list will stay the same because of the formula(also it is considered that position of elements in list starts with 1 and goes to length of list).
Also, for every node in list, I think it is enough to replace only value field for corresponding indexes right(next field is not neccessary). Last sentence I am considering only if I had example where elements of list would really change(if my assumption above is correct).

How to generate a certain amount of numbers and spread them randomly across a grid?

I want to generate the number 2 5 times and the number 1 10 times. I'm trying to spread these across a String Grid in Delphi randomly. I also want to fill the rest of the grid that isn't 1 or 2, with 0's. I have no idea how to even start here.
It would look something like this (P stands for player and there would only be 5 2's and 10 1's): https://gyazo.com/aeef05c3a92ce7847c0f42ad40faa733
Given a grid with dimensions m×n, create an array of length m * n. Put five 2's and 10 1's in the array, and fill the remainder with 0's. (We'll assume the product of m and n is at least 15.) Shuffle the array. Copy each element of the shuffled array into successive cells in the grid.
While the approach represented in Robs answer will do the job I personally think it is way to complicated for it's purpose.
So what would be more simpler approach?
Well your goal is to place these numbers at random positions in grid.
How do you determine position of some object in a grid? You do it by its X (Column) and Y (Row) coordinates.
So how do you get random position in a grid? Simple chose two random values for X and Y coordinates.
As for placing certain numbers of number 1 and number 2 use two simple loops.

2D array - checking for column with most "on" values

So I have a 2D array of "1" and "0" values, a value is either on or off, this can generate shapes and I want to check for vertical lines, for example:
[0,0,1,0,1,0,0,0]
[0,0,0,0,1,0,0,0]
[0,1,0,0,1,0,0,0]
[0,0,0,0,1,0,0,0]
[1,0,0,0,1,0,0,0]
[0,0,0,0,1,0,0,0]
that has a vertical line in column 5 so we will return those ones and strip all other results (change the 1's that aren't part of that line to 0's...)
[0,0,0,0,1,0,0,0]
[0,0,0,0,1,0,0,0]
[0,0,0,0,1,0,0,0]
[0,0,0,0,1,0,0,0]
[0,0,0,0,1,0,0,0]
[0,0,0,0,1,0,0,0]
My 2 dimensional array is much more complex, it is about a 300x600 2D array. In-order to visually see the values I generated UIViews with red background and stuck them on the screen in a view the size of my array. This is what the returned image was (the blue arrow was later photoshopped in to indicate the longest vertical line (the values we want to keep)
So what's a good method to find the longest vertical line (of "1" values) in a 2 dimensional array and change all other values to zero. (So that if I render the array in a visual graph format again only this is displayed (other red dots are fading out because they have been changed from "1" values to "0" values.)
I was thinking maybe something along the lines of generating a for-loop that would keep track of all of the "1" values in each column and a set of maybe any given 6 columns that are consecutively next to each other that have the largest amount of "1" values is the area (6 columns wide) where the longest vertical line is most likely located, but I can see a few issues with this, also I don't know how to get the rows that are part of that line after I have the columns.... hmmmm
*Note: I am making my "2 dimensional array" by just have a variable for the fixed number of columns and then I have an array that just has all of the values for all column/row combinations. For example a 3x3 board would be [0,0,0,0,1,0,0,0,0] which I can then understand means this:
[0,0,0]
[0,1,0]
[0,0,0]
Because I know there is always 3 columns per row.
[row1column1,row1column2,row1column3,row2column1,row2column2,row2column3,row3column1,row3column2,row3column3]
Here's some sample code (you'll need to take care of the allocations and some semantics - e.g: assigning one array to another)
int array2d[]; // which is 1d from what I understood - this holds your initial values
int columnCount; // you already have the number of columns, this is the var that's holding it
int maxColumn[], column[]; // these will hold the values of the maximum column and the current looped column
int maxIndex = 0, max=0; // used to determine which of the columns is the longest
int rowCount = array2d.count/columnCount; // find out how many rows there are
// first loop through the entire array, set everything to '0', while storing the values of the longest column (so far) in a different array
for(int i=0; i<columnCount; i++){
int columnSum = 0;
int index = i;
// go through every row of the column i
for(int j=0; j<rowCount; j++, index+=rowCount){
columnSum=columnSum+array2d[index];
column[j]=array2d[index];
array2d[index]=0;
}
if(columnSum>max){
max = columnSum;
maxIndex = i; // the column index
maxColumn = column;
}
}
// we found the longest column (maxIndex), now we need to set its values back to what they were previously
int index = maxIndex;
for(int j=0; j<rowCount; j++, index+=rowCount){
array2d[index]=maxColumn[j];
}
Since you want to find the longest of 6 consecutive columns, I believe that setting columnCount/=6; should work (it'll also make the rowCount=rowCount*6)
Your requirements aren't very precise, which makes me think you need to think harder about what you want, or state it more clearly. Take a look at the attached figure:
It's six pixels wide, so I image you could get this sort of a chain in your image. There's no connected sequence of 1's longer than 4 in any of the columns, yet the chain itself is 18 pixels long. Would you need to deal with a situation like this? If yes, then just summing over individual columns isn't enough; you need a more sophisticated method, like finding "connected components".

What order do batched row insertions/deletions take place in UITableView?

UITableView allows you to batch editing operations using beginUpdates and endUpdates.
My question is: do I need to know whether it does deletions or insertions first? Or can I refer to everything by the index path prior to beginUpdates and it'll magically work?
Suppose I have a table:
A (currently index path 0,0)
B (0,1)
C (0,2)
D (0,3)
E (0,4)
F (0,5)
I want to turn it into:
A (0,0)
C (0,1)
D (0,2)
H (0,3)
E (0,4)
F (0,5)
Thus, I've deleted B (which was at 0,1) and inserted H (which was inserted after D — at 0,4 before the deletions, or 0,3 after).
So, between my begin/end updates calls, which of these will work?
deleteRowsAtIndexPaths: 0,1, followed by insertRowsAtIndexPaths:
0,4
deleteRowsAtIndexPaths: 0,1, followed by
insertRowsAtIndexPaths: 0,3
insertRowsAtIndexPaths: 0,4, followed by deleteRowsAtIndexPaths: 0,1
insertRowsAtIndexPaths: 0,3, followed by deleteRowsAtIndexPaths: 0,1
The relevant Apple documentation for this is under Ordering of Operations and Index Paths.
Deletion and reloading operations within an animation block specify which rows and sections in the original table should be removed or reloaded; insertions specify which rows and sections should be added to the resulting table. The index paths used to identify sections and rows follow this model.
So the table view will first perform any delete or update operations, whose index paths refer to index paths in the original table contents. Then insertions are performed, and those index paths refer to index paths after deletions have occured.
So in theory your number '2' option should be the one you want.

Resources