Create 0441-arranging-coins.js#2619
Conversation
Solved 0441-arranging-coins.js in JS.
| */ | ||
| var arrangeCoins = function(n) { | ||
|
|
||
| let result1 = Math.floor((-1 + Math.sqrt(1+(8*n)))/2); |
There was a problem hiding this comment.
Suggestion: Make the math operations more readable
const value = Math.sqrt((8 * n) + 1)
const add = ((value + -1) >> 1)
const sub = ((value - -1) >> 1)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? |
| /** | ||
| * Binary Search | ||
| * | ||
| * Time O(n*log(n)) | Space O |
There was a problem hiding this comment.
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.
Let me know if I didn't explain myself clearly. Sorry for the long ass question.
Adding Space complexity to binary-search solution.
|
|
||
| /** | ||
| * Binary Search | ||
| * Time O(n*log(n)) | Space O(1) |
There was a problem hiding this comment.
Should be log(n) for binary search
There was a problem hiding this comment.
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)