Posts

Showing posts from February, 2021

Assignments Array

  Squares of a Sorted Array Given an integer array  nums  sorted in  non-decreasing  order, return  an array of  the squares of each number  sorted in non-decreasing order .   Example 1: Input: nums = [-4,-1,0,3,10] Output: [0,1,9,16,100] Explanation: After squaring, the array becomes [16,1,0,9,100]. After sorting, it becomes [0,1,9,16,100]. Example 2: Input: nums = [-7,-3,2,3,11] Output: [4,9,9,49,121]   Constraints: 1 <= nums.length <=  10 4 -10 4  <= nums[i] <= 10 4 nums  is sorted in  non-decreasing  order.   Follow up:  Squaring each element and sorting the new array is very trivial, could you find an  O(n)  solution using a different approach? Max Consecutive Ones Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s. The...

Array Series III

     Array Capacity VS Length If somebody asks you how long the DVD Array is, what what would your answer be? There are two different answers you might have given. The number of DVDs the box could hold, if it was full, or The number of DVDs currently in the box. Both answers are correct, and both have very different meanings! It's important to understand the difference between them, and use them correctly. We call the first one the  capacity  of the Array, and the second one the  length  of the Array. Array Capacity Let's say we've created a new Array like this. DVD [] array = new DVD [ 6 ] Is it a valid operation to insert an element at  array[6] ? What about at  array[10] ? Nope, neither of these are valid. When we created the Array, we specified that it can hold up to  6  DVD's. This is the Array's  capacity . Remembering that indexing starts at  0 , we can only insert items at  array[0] ,  array[1] ,...

Array Series II

     Accessing Elements in Arrays The two most primitive Array operations are writing elements into them, and reading elements from them. All other Array operations are built on top of these two primitive operations. Writing Items into an Array To put a DVD into the Array, we need to decide which of the 15 places we'd like it to go in. Each of the places is identified using a number in the range of  0  to  N - 1 . The 1st place is  0 , the 2nd place is  1 , the 3rd place is  2 ... all the way up to the 15th place, which is  14 . We call these numbers that identify each place  indexes . Let's put the DVD for  The Avengers  into the eighth place of the Array we created above. And that's it. We've put the DVD for  The Avengers  into our Array! Let's put a few more DVD's in. Notice that we put  The Incredibles  into the Array at index  3 . What happens if we now run this next piece of code? Because we ...

Array

Image
    Array - A DVD box❓ Suppose you had a bunch of DVDs at home that you wanted to arrange neatly. What would be the ideal choice for storing such a thing? You could find a cardboard box (or some other box) big enough to arrange all of the DVDs neatly, right? It's as simple as that. However, you might want to add a new DVD to the box, or you might want to get rid of the old ones that you've watched a million times over in the past. An important consideration for this box would be that you would only place DVDs in it and nothing else; you wouldn't place your clothes in it, for example. The box would contain multiple items, but all of them would be of the same type. In this case, that type is DVD. Items of the same type share properties. For DVDs, those properties include: All the DVDs would be inside a plastic cover. The cover would have the name of the movie, the cast, and all sorts of other details. All the covers would be of exactly the same size and would contain just one...