Skip to content

Commit

Permalink
Merge pull request #2619 from aadil42/patch-62
Browse files Browse the repository at this point in the history
  • Loading branch information
aakhtar3 authored Sep 8, 2023
2 parents eb7a6ac + c633e25 commit 7a54eed
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions javascript/0441-arranging-coins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* 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(log(n)) | Space O(1)
* @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 discriminant = 1 + 8 * n;
let sqrtDiscriminant = Math.sqrt(discriminant);

let result1 = Math.floor((-1 + sqrtDiscriminant) / 2);
let result2 = Math.floor((-1 - sqrtDiscriminant) / 2);

return Math.max(result1, result2);
};

0 comments on commit 7a54eed

Please sign in to comment.