-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0050-powx-n.js
142 lines (117 loc) · 2.94 KB
/
0050-powx-n.js
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* Brute Force - Multiply
* Time O(N) | Space O(1)
* https://leetcode.com/problems/powx-n/
* @param {number} x
* @param {number} n
* @return {number}
*/
var myPow = (x, n) => {
if (n < 0) {
x = (1 / x);
n = (-n);
}
return getPow(x, n);/* Time O(N) */
}
var getPow = (x, n, pow = 1) => {
for (let i = 0; i < n; i++) {/* Time O(N) */
pow = pow * x;
}
return pow;
}
/**
* DFS
* Time (log(N)) | Space O(log(N))
* https://leetcode.com/problems/powx-n/
* @param {number} x
* @param {number} n
* @return {number}
*/
var myPow = (x, n) => {
const isBaseCase1 = ((x === 1.0) || (n === 0));
if (isBaseCase1) return 1;
const isBaseCase2 = (n === 1);
if (isBaseCase2) return x;
const isEven = ((n % 2) === 0);
if (isEven) return myPow((x * x), (n / 2));/* Time O(log(N)) | Space O(log(N)) */
const isOdd = ((n % 2) === 1);
if (isOdd) return (x * myPow(x, (n - 1)));/* Time O(log(N)) | Space O(log(N)) */
return (1 / myPow(x, -n));
};
/**
* DFS
* Time (log(N)) | Space O(log(N))
* https://leetcode.com/problems/powx-n/
* @param {number} x
* @param {number} n
* @return {number}
*/
var myPow = (x, n) => {
const isBaseCase = (n === 0);
if (isBaseCase) return 1;
const abs = Math.abs(n);
const isEven = ((abs % 2) === 0);
const power = isEven
? myPow((x * x), (abs / 2)) /* Time O(log(N)) | Space O(log(N)) */
: (myPow((x * x), ((abs - 1) / 2)) * x);/* Time O(log(N)) | Space O(log(N)) */
const isNegative = (n < 0);
return isNegative
? (1 / power)
: power;
};
/**
* Fast Power - Recursive
* Time O(log(N)) | Space O(log(N))
* https://leetcode.com/problems/powx-n/
* @param {number} x
* @param {number} n
* @return {number}
*/
var myPow = (x, n) => {
if (n < 0) {
x = 1 / x;
n = -n;
}
return fastPow(x, n);/* Time O(log(N)) | Space O(log(N)) */
}
var fastPow = (x, n) => {
const isBaseCase = n === 0;
if (isBaseCase) return 1.0;
const half = fastPow(x, n / 2);/* Time O(log(N)) | Space O(log(N)) */
const isEven = ((n % 2) === 0);
if (isEven) return (half * half);
const isOdd = ((n % 2) === 1);
if (isOdd) return ((half * half) * x);
}
/**
* Fast Power - Iterative
* Time O(log(N)) | Space O(1)
* https://leetcode.com/problems/powx-n/
* @param {number} x
* @param {number} n
* @return {number}
*/
var myPow = (x, n) => {
if (n < 0) {
x = (1 / x);
n = (-n);
}
let [ pow, product ] = [ 1, x ];
for (let i = n; (0 < i); i = (i >> 1)) {/* Time O(log(N)) */
const isOdd = ((i % 2) === 1);
if (isOdd) pow = (pow * product);
product = (product * product);
}
return pow;
}
/**
* Number - Math
* Time O(1) | Space O(1)
* https://leetcode.com/problems/powx-n/
* @param {number} x
* @param {number} n
* @return {number}
*/
var myPow = (x, n) => {
return Math.pow(x,n).toFixed(5);
}