-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Create 0441-arranging-coins.js #2619
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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,62 @@ | ||
/** | ||
* https://leetcode.com/problems/arranging-coins/ | ||
* Linear time | ||
* Time O(n) | Space O(1) | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var arrangeCoins = function(n) { | ||
|
||
let steps = 1; | ||
let canBuild = 0; | ||
|
||
while(n >= steps) { | ||
n = n - steps; | ||
canBuild++; | ||
steps++; | ||
} | ||
|
||
return canBuild || 1; | ||
}; | ||
|
||
/** | ||
* Binary Search | ||
* | ||
* Time O(n*log(n)) | Space O | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var arrangeCoins = function(n) { | ||
|
||
let left = 1; | ||
let right = n; | ||
let result = 0; | ||
|
||
while(left <= right) { | ||
|
||
const mid = Math.floor((right+left)/2); | ||
const total = (1 + mid) * (mid/2); | ||
if(n < total) { | ||
right = mid -1; | ||
} else { | ||
left = mid+1; | ||
result = Math.max(result, mid); | ||
} | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
/** | ||
* Math | ||
* Time O(1) | Space O(1) | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var arrangeCoins = function(n) { | ||
|
||
let result1 = Math.floor((-1 + Math.sqrt(1+(8*n)))/2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Make the math operations more readable const value = Math.sqrt((8 * n) + 1)
const add = ((value + -1) >> 1)
const sub = ((value - -1) >> 1) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Please check. |
||
let result2 = Math.floor((-1 - Math.sqrt(1+(8*n)))/2); | ||
|
||
return Math.max(result1, result2); | ||
}; |
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.
Missing Space
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.
I added the Space complexity. Sorry for that. I have a question, in the binary-search solution, I tried to get the middle value with bit-operation. It is giving me the wrong answer for this input (n=2147483647). Any idea? I checked the number, it's (2^31 - 1). And we're adding 1 to that number. We're overflowing out of the bits. To fix this I can add the below condition
It does work here
I know in other binary-search questions we did bit manipulation to get mid value. All the solutions were submitted successfully, but can they cause any trouble in the future? If Leetcode decides to change the constraint to those questions?
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.
Let me know if I didn't explain myself clearly. Sorry for the long ass question.