-
Notifications
You must be signed in to change notification settings - Fork 0
[pull] master from amejiarosario:master #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2fab838
add config test
amejiarosario 4d75825
chore(jest): fix commands
amejiarosario b5a00dd
feat(book:array): add solutions for interview questions
amejiarosario 996f7aa
chore(vscode): set colors
amejiarosario ca119f2
feat(book): add appendix D with interview question solutions
amejiarosario 57960e2
fix(book): multiple broken links and bump epub version
amejiarosario 5935b95
feat(book/array): add max subarray questions and solution
amejiarosario 6ab8bc4
feat(book/array): add stock questions and solution
amejiarosario dbdef21
fix(book): workaround for c++ issue in asciidoc
amejiarosario 9b356d9
Merge pull request #67 from amejiarosario/feat/add-exercises-solutions
amejiarosario e573929
:bookmark: chore(release): 1.11.0
semantic-release-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat(book/array): add max subarray questions and solution
- Loading branch information
commit 5935b95ea761402b296e5a4fbd6e3643714bcaf9
There are no files selected for viewing
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 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
1 change: 1 addition & 0 deletions
book/interview-questions/max-subarray.data.js
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
book/interview-questions/max-subarray.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// tag::description[] | ||
/** | ||
* Find the maximun sum of contiguous elements in an array. | ||
* @examples | ||
* maxSubArray([1, -3, 10, -5]); // => 10 | ||
* maxSubArray([-3,4,-1,2,1,-5]); // => 6 | ||
* @param {number[]} a - Array | ||
*/ | ||
function maxSubArray(a) { | ||
// end::description[] | ||
// tag::solution[] | ||
let max = -Infinity; | ||
let local = 0; | ||
|
||
a.forEach((n) => { | ||
local = Math.max(n, local + n); | ||
max = Math.max(max, local); | ||
}); | ||
|
||
return max; | ||
} | ||
// end::solution[] | ||
|
||
// tag::maxSubArrayBrute1[] | ||
function maxSubArrayBrute1(nums) { | ||
let max = -Infinity; | ||
|
||
for (let i = 0; i < nums.length; i++) { // O(n^3) | ||
for (let j = i + 1; j <= nums.length; j++) { // O(n^2) | ||
const sum = nums.slice(i, j).reduce((a, n) => n + a, 0); // O(n) | ||
max = Math.max(max, sum); // O(1) | ||
} | ||
} | ||
|
||
return max; | ||
} | ||
// end::maxSubArrayBrute1[] | ||
|
||
// tag::maxSubArrayBrute2[] | ||
function maxSubArrayBrute2(nums) { | ||
let max = -Infinity; | ||
|
||
for (let i = 0; i < nums.length; i++) { // O(n) * O(n) | ||
let local = 0; | ||
for (let j = i; j < nums.length; j++) { // O(n) | ||
local += nums[j]; | ||
max = Math.max(max, local); | ||
} | ||
} | ||
return max; | ||
} | ||
// end::maxSubArrayBrute2[] | ||
|
||
module.exports = { maxSubArrayBrute1, maxSubArrayBrute2, maxSubArray }; |
16 changes: 16 additions & 0 deletions
book/interview-questions/max-subarray.spec.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const { maxSubArray, maxSubArrayBrute1, maxSubArrayBrute2 } = require('./max-subarray'); | ||
const largeArray = require('./max-subarray.data'); | ||
|
||
describe('Max Subarray Sum', () => { | ||
[maxSubArray, maxSubArrayBrute1, maxSubArrayBrute2].forEach((fn) => { | ||
describe(`with ${fn.name}`, () => { | ||
it('should work with small arrays', () => { | ||
expect(fn([-2, 1, -3, 4, -1, 2, 1, -5, 4])).toEqual(6); | ||
}); | ||
|
||
it('should work with large arrays', () => { | ||
expect(fn(largeArray)).toEqual(4853); | ||
}); | ||
}); | ||
}); | ||
}); |
19 changes: 0 additions & 19 deletions
book/interview-questions/rotate-array-left.spec.js
Oops, something went wrong.
File renamed without changes.
23 changes: 23 additions & 0 deletions
lab/exercises/01-arrays/rotate-array-left.spec.js
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const { rotateLeft, rotateLeftBruteForce } = require('./rotate-array-left'); | ||
|
||
const largeArray = Array(1e6).fill(1).map((t) => t * Math.random()); | ||
|
||
[rotateLeft, rotateLeftBruteForce].forEach((fn) => { | ||
xdescribe(`Rotate Left ${fn.name}`, () => { | ||
describe('when data is small', () => { | ||
it('should work with 1', () => { | ||
expect(fn([1, 2, 3], 1)).toEqual([2, 3, 1]); | ||
}); | ||
|
||
it('should work with 4', () => { | ||
expect(fn([1, 2, 3, 4, 5], 4)).toEqual([5, 1, 2, 3, 4]); | ||
}); | ||
}); | ||
|
||
xdescribe('when data is large', () => { | ||
it('should work at scale', () => { | ||
expect(fn(largeArray, 75863)).toEqual(largeArray); | ||
}); | ||
}); | ||
}); | ||
}); |
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
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.