Skip to content

Commit a77bd21

Browse files
authored
Create 1688-count-of-matches-in-tournament.js
1 parent 9ae14e3 commit a77bd21

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var numberOfMatches = function(n) {
6+
const obj = { res: 0 }
7+
helper(n, obj)
8+
return obj.res
9+
};
10+
11+
function helper(num, obj) {
12+
if(num <= 1) return
13+
const odd = num % 2 === 1
14+
if(odd) {
15+
const tmp = Math.floor((num - 1) / 2)
16+
obj.res += tmp
17+
helper(tmp + 1, obj)
18+
} else {
19+
const tmp = Math.floor(num / 2)
20+
obj.res += tmp
21+
helper(tmp, obj)
22+
}
23+
}

0 commit comments

Comments
 (0)