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 code is simple to understand; however, not very efficient. The runtime is `O(n^3)`.
31
+
32
+
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.
The runtime is much better: `O(n)`. Can we still do better?
40
+
41
+
We can use a greedy approach, where do one pass through the array. We only add the numbers if their sum is larger than just taking the current element.
There are multiple examples that we can simulate: bear markets (when prices are going down), bullish markets (when prices are going up), and zig-zag markets (when prices are going up and down).
58
+
59
+
[source, javascript]
60
+
----
61
+
// zig-zag market
62
+
maxProfit([5, 10, 5, 10]); // => 5
63
+
// bullish market
64
+
maxProfit([1, 2, 3]); // => 2
65
+
// bearish market
66
+
maxProfit([3, 2, 1]); // => 0
67
+
----
68
+
69
+
During the bearish markets, the profit will always be 0. Since if you buy, we are only going to lose.
70
+
71
+
We can do a brute force solution doing all combinations:
Copy file name to clipboardExpand all lines: book/content/part02/array.asc
+24-32
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@ ifndef::imagesdir[]
4
4
endif::[]
5
5
6
6
[[array]]
7
-
=== Array
7
+
=== Array [[array-chap]]
8
8
(((Array)))
9
9
(((Data Structures, Linear, Array)))
10
10
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.
@@ -17,7 +17,8 @@ TIP: Strings are a collection of Unicode characters and most of the array concep
17
17
18
18
.Fixed vs. Dynamic Size Arrays
19
19
****
20
-
Some programming languages have fixed size arrays like Java and C++. Fixed size 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 C++ and `ArrayList` in Java. Dynamic programming languages like JavaScript, Ruby, and Python use dynamic arrays by default.
20
+
Some programming languages have fixed size arrays like Java and {cpp}.
21
+
Fixed size 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.
21
22
****
22
23
23
24
Arrays look like this:
@@ -275,46 +276,37 @@ To sum up, the time complexity of an array is:
275
276
|===
276
277
//end::table
277
278
278
-
==== Array Exercises
279
+
==== Interview Questions
280
+
(((Interview Questions, Arrays)))
279
281
280
-
1) Implement an efficient algorithm that rotate an array `a` an `k` number of times.
282
+
// tag::array-q-max-subarray[]
283
+
===== Max Subarray
284
+
285
+
Given an array of integers, find the maximum sum of consecutive elements (subarray).
2) Implement an algorithm that takes two arrays of numbers and return a new array with the sum.
300
+
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)
Copy file name to clipboardExpand all lines: book/content/part03/map.asc
+1-1
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,7 @@ In short, you set `key`/`value` pair and then you can get the `value` using the
24
24
The attractive part of Maps is that they are very performant usually *O(1)* or *O(log n)* depending on the implementation. We can implement the maps using two different underlying data structures:
25
25
26
26
* *HashMap*: it’s a map implementation using an *array* and a *hash function*. The job of the hash function is to convert the `key` into an index that maps to the `value`. Optimized HashMap can have an average runtime of *O(1)*.
27
-
* *TreeMap*: it’s a map implementation that uses a self-balanced Binary Search Tree (like <<c-avl-tree#>>). The BST nodes store the key, and the value and nodes are sorted by key guaranteeing an *O(log n)* look up.
27
+
* *TreeMap*: it’s a map implementation that uses a self-balanced Binary Search Tree (like <<c-avl-tree>>). The BST nodes store the key, and the value and nodes are sorted by key guaranteeing an *O(log n)* look up.
Copy file name to clipboardExpand all lines: book/content/preface.asc
+2-2
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@
3
3
4
4
=== What is in this book?
5
5
6
-
_{doctitle}_ is a book that can be read from cover to cover, where each section builds on top of the previous one. Also, it can be used as a reference manual where developers can refresh specific topics before an interview or look for ideas to solve a problem optimally. (Check out the <<a-time-complexity-cheatsheet#,Time Complexity Cheatsheet>> and <<index#, topical index>>)
6
+
_{doctitle}_ is a book that can be read from cover to cover, where each section builds on top of the previous one. Also, it can be used as a reference manual where developers can refresh specific topics before an interview or look for ideas to solve a problem optimally. (Check out the <<a-time-complexity-cheatsheet,Time Complexity Cheatsheet>> and <<index, topical index>>)
7
7
8
8
This publication is designed to be concise, intending to serve software developers looking to get a firm conceptual understanding of data structures in a quick yet in-depth fashion. After reading this book, the reader should have a fundamental knowledge of algorithms, including when and where to apply it, what are the trade-offs of using one data structure over the other. The reader will then be able to make intelligent decisions about algorithms and data structures in their projects.
9
9
@@ -82,4 +82,4 @@ Measurement is the first step that leads to control and eventually to improvemen
82
82
83
83
Your feedback is very welcome and valuable. Let us know what your thoughts about this book — what you like or ideas to make it better.
84
84
85
-
To send us feedback, e-mail us at [email protected], send a tweet to https://twitter.com/iAmAdrianMejia[@iAmAdrianMejia], or using the hash tags `#dsaJS`, `#javascript` and `#algorithms`.
85
+
To send us feedback, e-mail us at [email protected], send a tweet to https://twitter.com/iAmAdrianMejia[@iAmAdrianMejia], or using the hash tag `#dsaJS`.
0 commit comments