forked from andrewdcrypt/LeetCodeStuff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArranging Coins.php
40 lines (31 loc) · 988 Bytes
/
Arranging Coins.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#Arranging Coins
Description:
Given the integer n, return the number of complete rows of the staircase you will build.
Method One:
basically using the loop to set how many coins is needed each row
and calculate the leftover $n amount after each row
as long as $n is greater than or equal to the coins needed
it will be considered completed
set up loop that will loop through given $n
set condition that checks if $n >= $coin
using $n subtract it with the $coin and
increment $stair_completed by 1 otherwise break
return $stair_completed
class Solution {
/**
* @param Integer $n
* @return Integer
*/
function arrangeCoins($n) {
$stair_completed = 0;
for($coin = 1; $coin <= $n; $coin++){
if ($n >= $coin){
$n = $n - $coin;
$stair_completed++;
}else{
break;
}
}
return $stair_completed;
}
}