-
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
Conversation
Solved 0441-arranging-coins.js in JS.
javascript/0441-arranging-coins.js
Outdated
*/ | ||
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Please check.
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.
.
Making math more readable.
What? I don't get it. What do you mean by the period? |
@aakhtar3, any update on this? I don't get it, what do you mean by the period? |
javascript/0441-arranging-coins.js
Outdated
/** | ||
* Binary Search | ||
* | ||
* Time O(n*log(n)) | Space O |
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
if(n === 2**31 - 1) {
right = right - 1;
}
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.
Adding Space complexity to binary-search solution.
javascript/0441-arranging-coins.js
Outdated
|
||
/** | ||
* Binary Search | ||
* Time O(n*log(n)) | Space O(1) |
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.
Should be log(n) for binary search
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.
Sorry 😅. I updated it.
Fixed Time-complexity.
Solved 0441-arranging-coins.js in JS.
File(s) Added: 0441-arranging-coins.js
Language(s) Used: JavaScript
Submission URLs: https://leetcode.com/problems/arranging-coins/submissions/977117480/ (linear)
https://leetcode.com/problems/arranging-coins/submissions/977117662/ (binary search)
https://leetcode.com/problems/arranging-coins/submissions/984649285/ (math)