- Notifications
You must be signed in to change notification settings - Fork2.4k
Refactor JS - Array & Hash#445
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
80 changes: 72 additions & 8 deletionsjavascript/1-Two-Sum.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 |
---|---|---|
@@ -1,15 +1,79 @@ | ||
/** | ||
* Brute Force - Linear Search | ||
* Time O(N^2) | Space O(1) | ||
* https://leetcode.com/problems/two-sum/ | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number[]} | ||
*/ | ||
var twoSum = (nums, target) => { | ||
for (let curr = 0; curr < nums.length; curr++) {/* Time O(N) */ | ||
const complement = target - nums[curr]; | ||
for (let next = (curr + 1); next < nums.length; next++) {/* Time O(N) */ | ||
const num = nums[next]; | ||
const isTarget = num === complement | ||
if (isTarget) return [ curr, next ]; | ||
} | ||
} | ||
return [ -1, -1 ]; | ||
} | ||
/** | ||
* Hash Map - 2 Pass | ||
* Time O(N) | Space O(N) | ||
* https://leetcode.com/problems/two-sum/ | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number[]} | ||
*/ | ||
var twoSum = (nums, target) => { | ||
const map = getMap(nums); /* Time O(N) | Space O(N) */ | ||
return getSum(nums, target, map)/* Time O(N) */ | ||
} | ||
const getMap = (nums, map = new Map()) => { | ||
for (let index = 0; index < nums.length; index++) {/* Time O(N) */ | ||
map.set(nums[index], index); /* Space O(N) */ | ||
} | ||
return map | ||
} | ||
const getSum = (nums, target, map) => { | ||
for (let index = 0; index < nums.length; index++) {/* Time O(N) */ | ||
const complement = target - nums[index]; | ||
const sumIndex = map.get(complement); | ||
const isTarget = map.has(complement) && (map.get(complement) !== index) | ||
if (isTarget) return [ index, sumIndex ] | ||
} | ||
return [ -1, -1 ]; | ||
} | ||
/** | ||
* Hash Map - 1 Pass | ||
* Time O(N) | Space O(N) | ||
* https://leetcode.com/problems/two-sum/ | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number[]} | ||
*/ | ||
var twoSum = (nums, target, map = new Map()) => { | ||
for (let index = 0; index < nums.length; index++) {/* Time O(N) */ | ||
const num = nums[index]; | ||
const complement = (target - num); | ||
const sumIndex = map.get(complement); | ||
const isTarget = map.has(complement) | ||
if (isTarget) return [ index, sumIndex ]; | ||
map.set(num, index); /* Space O(N) */ | ||
} | ||
return [ -1, -1 ]; | ||
} |
140 changes: 70 additions & 70 deletionsjavascript/128-Longest-consecutive-sequence.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 |
---|---|---|
@@ -1,94 +1,94 @@ | ||
/** | ||
* Brute Force | ||
* Greedy - Max Score | ||
* Time O (N^3) | Space O(1) | ||
* https://leetcode.com/problems/longest-consecutive-sequence/ | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var longestConsecutive = (nums, maxScore = 0) => { | ||
for (const num of nums) {/* Time O(N) */ | ||
let [ currNum, score ] = [ num, 1 ]; | ||
while (isStreak(nums, (currNum + 1))) {/* Time O(N * N) */ | ||
currNum++; | ||
score++; | ||
} | ||
maxScore = Math.max(maxScore, score); | ||
} | ||
returnmaxScore; | ||
} | ||
const isStreak = (nums, num) => { | ||
for (let i = 0; i < nums.length; i++) {/* Time O(N) */ | ||
const isEqual = nums[i] === num | ||
if (isEqual) return true; | ||
} | ||
return false; | ||
} | ||
/** | ||
* Sort - HeapSort Space O(1) | QuickSort Space O(log(K)) | ||
* Greedy - Max Score | ||
* Time O (N * log(N)) | Space O(1) | ||
* https://leetcode.com/problems/longest-consecutive-sequence/ | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var longestConsecutive = (nums) => { | ||
if (!nums.length) return 0; | ||
nums.sort((a, b) => a - b);/* Time O(N * log(N)) | Space O(1 || log(N)) */ | ||
return search(nums); /* Time O(N) */ | ||
} | ||
const search = (nums) => { | ||
let [ maxScore, score ] = [ 1, 1 ]; | ||
for (let i = 1; i < nums.length; i++) {/* Time O(N) */ | ||
const isPrevDuplicate = nums[i - 1] === nums[i] | ||
if (isPrevDuplicate) continue | ||
const isStreak = nums[i] === ((nums[i - 1]) + 1) | ||
if (isStreak) { score++; continue; } | ||
maxScore = Math.max(maxScore, score); | ||
score = 1; | ||
} | ||
return Math.max(maxScore, score); | ||
} | ||
/** | ||
* Hash Set - Intelligent Sequence | ||
* Greedy - Max Score | ||
* Time O (N) | Space O(N) | ||
* https://leetcode.com/problems/longest-consecutive-sequence/ | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var longestConsecutive = (nums, maxScore = 0) => { | ||
const numSet = new Set(nums); /* Time O(N) | Space O(N) */ | ||
for (const num of [ ...numSet ]) { /* Time O(N) */ | ||
const prevNum = num - 1; | ||
if (numSet.has(prevNum)) continue;/* Time O(N) */ | ||
let [ currNum, score ] = [ num, 1 ]; | ||
const isStreak = () => numSet.has(currNum + 1) | ||
while (isStreak()) { /* Time O(N) */ | ||
currNum++; | ||
score++; | ||
} | ||
maxScore = Math.max(maxScore, score); | ||
} | ||
returnmaxScore; | ||
} |
81 changes: 61 additions & 20 deletionsjavascript/217-Contains-Duplicate.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 |
---|---|---|
@@ -1,31 +1,72 @@ | ||
/** | ||
* Brute Force - Linear Search | ||
* Time O(N^2) | Space O(1) | ||
* https://leetcode.com/problems/contains-duplicate/ | ||
* @param {number[]} nums | ||
* @return {boolean} | ||
*/ | ||
var containsDuplicate = (nums) => { | ||
for (let right = 0; right < nums.length; right++) {/* Time O(N) */ | ||
for (let left = 0; left < right; left++) { /* Time O(N) */ | ||
const isDuplicate = nums[left] === nums[right]; | ||
if (isDuplicate) return true; | ||
} | ||
} | ||
return false; | ||
} | ||
/** | ||
* Sort - HeapSort Space O(1) | QuickSort Space O(log(N)) | ||
* Time O(N * log(N)) | Space O(1) | ||
* https://leetcode.com/problems/contains-duplicate/ | ||
* @param {number[]} nums | ||
* @return {boolean} | ||
*/ | ||
var containsDuplicate = (nums) => { | ||
nums.sort((a, b) => a - b);/* Time O(N * log(N)) | Space O(1 || log(N)) */ | ||
return hasDuplicate(nums); | ||
} | ||
const hasDuplicate = (nums) => { | ||
for (let curr = 0; curr < (nums.length - 1); curr++) {/* Time O(N) */ | ||
const next = (curr + 1); | ||
const isNextDuplicate = nums[curr] === nums[next]; | ||
if (isNextDuplicate) return true; | ||
} | ||
return false; | ||
} | ||
/** | ||
* Hash Set | ||
* Time O(N) | Space O(N) | ||
* https://leetcode.com/problems/contains-duplicate/ | ||
* @param {number[]} nums | ||
* @return {boolean} | ||
*/ | ||
var containsDuplicate = (nums) => { | ||
const numsSet = new Set(nums);/* Time O(N) | Space O(N) */ | ||
const isEqual = numsSet.size === nums.length; | ||
return !isEqual; | ||
}; | ||
/** | ||
* Hash Set - Early Exit | ||
* Time O(N) | Space O(N) | ||
* https://leetcode.com/problems/contains-duplicate/ | ||
* @param {number[]} nums | ||
* @return {boolean} | ||
*/ | ||
var containsDuplicate = (nums, numsSet = new Set()) => { | ||
for (const num of nums) {/* Time O(N) */ | ||
if (numsSet.has(num)) return true; | ||
numsSet.add(num); /* Space O(N) */ | ||
} | ||
return false; | ||
}; |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.