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

Create 0441-arranging-coins.js #2619

merged 4 commits into from
Sep 8, 2023

Conversation

aadil42
Copy link
Contributor

@aadil42 aadil42 commented Jun 22, 2023

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)

Solved 0441-arranging-coins.js in JS.
*/
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.

Copy link
Collaborator

@aakhtar3 aakhtar3 left a 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.
@aadil42
Copy link
Contributor Author

aadil42 commented Jul 3, 2023

What? I don't get it. What do you mean by the period?

@aadil42
Copy link
Contributor Author

aadil42 commented Sep 5, 2023

@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
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.

Adding Space complexity to binary-search solution.

/**
* Binary Search
* Time O(n*log(n)) | Space O(1)
Copy link
Collaborator

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

Copy link
Contributor Author

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.
@aakhtar3 aakhtar3 merged commit 7a54eed into neetcode-gh:main Sep 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants