Skip to content
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 4 commits into from
Sep 8, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions javascript/0441-arranging-coins.js
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing Space

Copy link
Contributor Author

@aadil42 aadil42 Sep 7, 2023

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?

Copy link
Contributor Author

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.

* @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);
Copy link
Collaborator

@aakhtar3 aakhtar3 Jun 30, 2023

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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
};