-
-
Notifications
You must be signed in to change notification settings - Fork 195
[soobing] WEEK 01 Solutions #1186
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
65c57e6
feat(soobing): two-sum
soobing 16009a3
feat(soobing): best-time-to-buy-and-sell-stock
soobing 2d511d5
feat(soobing): contains-duplicate
soobing 607512f
feat(soobing): product-of-array-except-self
soobing fdf3120
feat(soobing): maximum-subarray
soobing 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
function maxProfit(prices: number[]): number { | ||
let minPrice = Infinity; | ||
let maxProfit = 0; | ||
for (let i = 0; i < prices.length; i++) { | ||
if (prices[i] < minPrice) { | ||
minPrice = prices[i]; | ||
} | ||
|
||
if (prices[i] - minPrice > maxProfit) { | ||
maxProfit = prices[i] - minPrice; | ||
} | ||
} | ||
return maxProfit; | ||
} |
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,10 @@ | ||
function containsDuplicate(nums: number[]): boolean { | ||
const map = new Map(); | ||
for (let i = 0; i < nums.length; i++) { | ||
if (map.get(nums[i])) { | ||
return true; | ||
} else map.set(nums[i], 1); | ||
} | ||
|
||
return false; | ||
} |
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,9 @@ | ||
function maxSubArray(nums: number[]): number { | ||
let currentSum = nums[0]; | ||
let maxSum = nums[0]; | ||
for (let i = 1; i < nums.length; i++) { | ||
currentSum = Math.max(nums[i], currentSum + nums[i]); | ||
maxSum = Math.max(maxSum, currentSum); | ||
} | ||
return maxSum; | ||
} |
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,19 @@ | ||
function productExceptSelf(nums: number[]): number[] { | ||
const left = Array(nums.length).fill(1); | ||
const right = Array(nums.length).fill(1); | ||
const result = Array(nums.length); | ||
|
||
for (let i = 1; i < nums.length; i++) { | ||
left[i] = left[i - 1] * nums[i - 1]; | ||
} | ||
|
||
for (let i = nums.length - 2; i >= 0; i--) { | ||
right[i] = right[i + 1] * nums[i + 1]; | ||
} | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
result[i] = left[i] * right[i]; | ||
} | ||
|
||
return result; | ||
} |
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,27 @@ | ||
// 1. Brute force | ||
function twoSum(nums: number[], target: number): number[] { | ||
for (let i = 0; i < nums.length - 1; i++) { | ||
for (let j = i + 1; j < nums.length; j++) { | ||
if (nums[i] + nums[j] === target) { | ||
return [i, j]; | ||
} | ||
} | ||
} | ||
return []; | ||
} | ||
|
||
// 2. Hashmap | ||
function twoSum(nums: number[], target: number): number[] { | ||
const map = new Map(); | ||
for (let i = 0; i < nums.length; i++) { | ||
map.set(nums[i], i); | ||
} | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
const targetIndex = map.get(target - nums[i]); | ||
if (targetIndex && targetIndex !== i) { | ||
return [i, targetIndex]; | ||
} | ||
} | ||
return []; | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
다양하게 풀어보는 점이 좋았습니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
헉 스케줄이 있는지 몰랐습니다...!! 알려주셔서 감사해요!!