You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This code is simple to understand; however, not very efficient. The runtime is `O(n^3)`.
Notice we're adding up the numbers from `i` to `j` on each cycle. But, we can optimize this. We can keep a local variable and add the new number to it. That way, we don't have to revisit previous numbers.
If you noticed we adding up the numbers from `i` to `j` on each cycle. But, we can optimize this. We can keep a local variable and add the new number to it. That way, we don't have to revisit previous numbers.
We need to visit each node in both lists and merge them in ascending order. Note: We don't need to copy the values nor create new nodes.
Another case to take into consideration is that lists might have different lengths. So, if one list runs out, we have to keep taking elements from the remaining list.
*Algorithm*:
- Have a pointer for each list
- While there's a pointer that is not null, visite them
- Compare each list node's value and take the smaller one.
- Advance the pointer of the taken node to the next one.
Notice that the problem mentions that lists could be huge (millions of nodes). If the first character on each list is different, we are unnecessarily computing millions of nodes, when a straightforward check will do the job.
A better way to solve this problem is iterating over each character on both lists, and when we found mistmatch, we return `false` immediately. If they are the same, we still have to visit all of them.
*Algorithm*:
- Set a pointer to iterate over each node in the lists.
- For each node, have an index (starting at zero) and compare if both lists have the same data.
- When the index reaches the last character on the current node, we move to the next node.
- If we found that a character from one list doesn't match the other, we return `false`.
This solution is an `O(n^2)`. Can we do better? We can!
Here's an idea: start backward, so we know when there's a warmer temperature beforehand. The last element is always 0 (because there are no more temperatures ahead of it). We can place each element's index that we visit on a stack. If the current weather is bigger than the stack top, we remove it until a bigger one remains or the stack is empty. If the stack has a value, we calculate the number of days ahead. Otherwise, it is 0.
*Algorithm*:
- Traverse the daily temperatures backward
- Push each temperature to a stack.
- While the current temperature is larger than the one at the top of the stack, pop it.
- If the stack is empty, then there's no warmer weather ahead, so it's 0.
- If the stack has an element, calculate the index delta.
The stack contains the indexes rather than the temperatures themselves.
*Complexity Analysis*:
- Time: `O(n)`. We visit each element on the array once.
- Space: `O(1)`. The worst-case scenario is ascending order without duplicates. The stack will hold at most 70 items (100 - 30). If we didn't have the range restriction, then space complexity would be `O(n)`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Arrays are one of the most used data structures. You probably have used it a lot, but are you aware of the runtimes of `splice`, `shift`, `indexOf`, and other operations? In this chapter, we are going deeper into the most common operations and their runtimes.
Arrays are one of the most used data structures. You probably have used it a lot but are you aware of the runtimes of `splice`, `shift`, `indexOf` and other operations? In this chapter, we are going deeper into the most common operations and their runtimes.
==== Array Basics
An array is a collection of things (strings, characters, numbers, objects, etc.). They can be many or zero.
TIP: Strings are a collection of Unicode characters, and most of the array concepts apply to them.
TIP: Strings are a collection of Unicode characters and most of the array concepts apply to them.
.Fixed vs. Dynamic Size Arrays
****
Some programming languages have fixed-size arrays like Java and {cpp}.
Fixed-size arrays might be a hassle when your collection gets full, and you have to create a new one with a bigger size. Those programming languages also have built-in dynamic arrays: we have `vector` in {cpp} and `ArrayList` in Java. Dynamic programming languages like JavaScript, Ruby, and Python use dynamic arrays by default.
Some programming languages have fixedsize arrays like Java and {cpp}.
Fixedsize arrays might be a hassle when your collection gets full, and you have to create a new one with a bigger size. For that, those programming languages also have built-in dynamic arrays: we have `vector` in {cpp} and `ArrayList` in Java. Dynamic programming languages like JavaScript, Ruby, and Python use dynamic arrays by default.
****
Arrays look like this:
.Array representation: each value is accessed through an index.
image::image16.png[image,width=388,height=110]
Arrays are a sequential collection of elements that can be accessed randomly using an index. Let’s take a look at the different operations that we can do with arrays.
Arrays are a sequential collection of elements that can be accessed randomly using an index. Let’s take a look into the different operations that we can do with arrays.
==== Insertion
Arrays are built-in in most languages. Inserting an element is simple; you can either add them at creation time or after initialization. Below you can find an example for both cases:
Arrays are built-in into most languages. Inserting an element is simple; you can either add them at creation time or after initialization. Below you can find an example for both cases:
.Inserting elements into an array
[source, javascript]
Expand All
@@ -45,7 +45,7 @@ array2[100] = 2;
array2 // [empty ×ばつ 3, 1, empty ×ばつ 96, 2]
----
Using the index, you can replace whatever value you want. Also, you don't have to add items next to each other. The size of the array will dynamically expand to accommodate the data. You can reference values at whatever index you like: index 3 or even 100! In `array2`, we inserted 2 numbers, but the length is 101, and there are 99 empty spaces.
Using the index, you can replace whatever value you want. Also, you don't have to add items next to each other. The size of the array will dynamically expand to accommodate the data. You can reference values at whatever index you like: index 3 or even 100! In `array2`, we inserted 2 numbers but the length is 101 and there are 99 empty spaces.
<1> at position `1`, delete `0` elements and insert `111`.
The Big O for this operation would be *O(n)* since, in the worst case, it would move most of the elements to the right.
The Big O for this operation would be *O(n)* since in worst case it would move most of the elements to the right.
.JavaScript built-in `array.splice`
****
The `splice()` method changes an array's contents by removing existing elements or adding new elements. Splice returns an array containing the deleted items.
The `splice()` method changes the contents of an arrayby removing existing elements or adding new elements. Splice returns an array containing the deleted elements.
Runtime: O(n).
****
Expand All
@@ -116,15 +116,15 @@ Adding to the tail of the array doesn’t change other indexes. E.g., element 2
.JavaScript built-in `array.push`
****
The `push()` method adds one or more elements to the end of an array and returns the array's new length.
The `push()` method adds one or more elements to the end of an array and returns the new length of the array.
Runtime: O(1).
****
[[array-search-by-value]]
==== Searching by value and index
Searching by the index is very easy using the `[]` operator:
Searching by index is very easy using the `[]` operator:
.Search by index
[source, javascript]
Expand DownExpand Up
@@ -185,7 +185,7 @@ We would have to loop through the whole array (worst case) or until we find it:
==== Deletion
There are three possible deletion scenarios (similar to insertion): removing at the beginning, middle, or end.
There are three possible scenarios for deletion (similar to insertion): removing at the beginning, middle or end.
Deleting from the middle might cause most of the array elements to move up one position to fill in for the eliminated item. Thus, runtime: O(n).
Deleting from the middle might cause most of the elements of the array to move up one position to fill in for the eliminated item. Thus, runtime: O(n).
===== Deleting element from the end
Expand DownExpand Up
@@ -282,7 +282,7 @@ To sum up, the time complexity of an array is:
// tag::array-q-max-subarray[]
===== Max Subarray
*AR-1*) _Given an array of integers, find the maximum sum of consecutive elements (subarray)._
Given an array of integers, find the maximum sum of consecutive elements (subarray).
*AR-2*) _You are given an array of integers. Each value represents the closing value of the stock on that day. You are only given one chance to buy and then sell. What's the maximum profit you can obtain? (Note: you have to buy first and then sell)_
You are given an array of integers. Each value represents the closing value of the stock on that day. You are only given one chance to buy and then sell. What's the maximun profit you can obtain? (Note: you have to buy first and then sell)
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.