Skip to content

Commit fc7683e

Browse files
authored
Merge pull request #1181 from aakhtar3/math
refactor: js - math
2 parents 09ffb55 + e586fa0 commit fc7683e

8 files changed

+839
-138
lines changed

javascript/2013-Detect-Squares.js

+38-29
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,52 @@
1-
var DetectSquares = function () {
2-
this.ptsCount = {};
3-
this.pts = [];
4-
};
5-
6-
/**
7-
* @param {number[]} point
8-
* @return {void}
1+
/*
2+
* Time O(N) | Space O(N)
3+
* https://leetcode.com/problems/detect-squares
94
*/
10-
DetectSquares.prototype.add = function (point) {
11-
var str = point[0] + ',' + point[1];
12-
if (!this.ptsCount[str]) {
13-
this.ptsCount[str] = 0;
5+
class DetectSquares {
6+
constructor () {
7+
this.map = {}; /* Space O(N) */
8+
this.points = [];/* Space O(N) */
149
}
10+
11+
add (point, { map, points } = this) {
12+
const [ x, y ] = point;
13+
const key = this.getKey(x, y);
14+
const value = ((map[key] || 0) + 1);
1515

16-
this.ptsCount[str]++;
17-
this.pts.push(point);
18-
};
16+
map[key] = value; /* Space O(N) */
17+
points.push(point);/* Space O(N) */
18+
}
1919

20-
/**
21-
* @param {number[]} point
22-
* @return {number}
23-
*/
24-
DetectSquares.prototype.count = function (point) {
25-
var res = 0;
26-
var [x, y] = point;
27-
for (var [px, py] of this.pts) {
28-
if (Math.abs(px - x) !== Math.abs(py - y) || x === px || y === py) {
29-
continue;
20+
count (point, { points } = this, score = 0) {
21+
const [ x1, y1 ] = point;
22+
23+
for (const [ x2, y2 ] of points) {/* Time O(N) */
24+
const isSame = (Math.abs(x2 - x1) === Math.abs(y2 - y1));
25+
const isEqual = ((x1 === x2) || (y1 === y2));
26+
const canSkip = (!isSame || isEqual);
27+
if (canSkip) continue;
28+
29+
score += this.getScore(x1, y1, x2, y2);
3030
}
3131

32-
res += (this.ptsCount[[x, py]] || 0) * (this.ptsCount[[px, y]] || 0);
32+
return score;
33+
};
34+
35+
getKey (x, y) {
36+
return `${x},${y}`;
3337
}
3438

35-
return res;
39+
getScore (x1, y1, x2, y2, { map } = this) {
40+
const [ aKey, bKey ] = [ this.getKey(x1, y2), this.getKey(x2, y1) ];
41+
const [ aScore, bScore ] = [ (map[aKey] || 0), (map[bKey] || 0) ];
42+
43+
return (aScore * bScore);
44+
}
3645
};
3746

38-
/**
47+
/**
3948
* Your DetectSquares object will be instantiated and called as such:
4049
* var obj = new DetectSquares()
4150
* obj.add(point)
4251
* var param_2 = obj.count(point)
43-
*/
52+
*/

javascript/202-Happy-Number.js

+104-15
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,114 @@
1-
function isHappy(n) {
2-
const visit = new Set();
1+
/**
2+
* Hash Set - seen dynamic
3+
* Time O(log(N)) | Space O(log(N))
4+
* https://leetcode.com/problems/happy-number/
5+
* @param {number} n
6+
* @return {boolean}
7+
*/
8+
var isHappy = (n, seen = new Set()) => {
9+
const hasCycle = () => ((n === 1) || (seen.has(n)));
10+
while (!hasCycle()) {/* Time O(log(N)) */
11+
seen.add(n); /* Space O(log(N)) */
12+
n = getNext(n); /* Time O(log(N)) */
13+
}
14+
15+
return (n === 1);
16+
};
17+
18+
var getNext = (n, sum = 0) => {
19+
while (0 < n) {/* Time O(log(N)) */
20+
const remainder = (n % 10);
21+
22+
n = Math.floor((n / 10));
23+
sum += (remainder * remainder);
24+
}
25+
26+
return sum;
27+
}
328

4-
while (!visit.has(n)) {
5-
visit.add(n);
6-
n = sumOfSquares(n);
29+
/**
30+
* Hash Set - seen static
31+
* Time O(log(N)) | Space O(1)
32+
* https://leetcode.com/problems/happy-number/
33+
* @param {number} n
34+
* @return {boolean}
35+
*/
36+
var isHappy = (n) => {
37+
const cycles = [ 4, 16, 37, 58, 89, 145, 42, 20 ];
38+
const seen = new Set(cycles);/* Time O(1) | Space O(1) */
739

8-
if (n == 1) return true;
40+
const hasCycle = () => ((n === 1) || (seen.has(n)));
41+
while (!hasCycle()) { /* Time O(log(N)) | Space O(1) */
42+
n = getNext(n);
943
}
1044

11-
return false;
45+
return n === 1;
1246
}
1347

14-
function sumOfSquares(n) {
15-
let output = 0;
48+
var getNext = (n, sum = 0) => {
49+
while (0 < n) {/* Time O(log(N)) */
50+
const remainder = (n % 10);
51+
52+
n = Math.floor((n / 10));
53+
sum += (remainder * remainder);
54+
}
55+
56+
return sum;
57+
}
1658

17-
while (n) {
18-
let digit = n % 10;
19-
digit = digit ** 2;
20-
output += digit;
21-
n = Math.floor(n / 10);
59+
/**
60+
* Pointer - n === 1 || n === 4
61+
* Time O(log(N)) | Space O(1)
62+
* https://leetcode.com/problems/happy-number/
63+
* @param {number} n
64+
* @return {boolean}
65+
*/
66+
var isHappy = (n) => {
67+
const hasCycle = () => ((n === 1) || (n === 4));
68+
while (!hasCycle()) {/* Time O(log(N)) */
69+
n = getNext(n); /* Time O(log(N)) */
2270
}
2371

24-
return output;
72+
return n === 1;
2573
}
74+
75+
var getNext = (n, sum = 0) => {
76+
while (0 < n) {/* Time O(log(N)) */
77+
const remainder = (n % 10);
78+
79+
n = Math.floor((n / 10));
80+
sum += (remainder * remainder);
81+
}
82+
83+
return sum;
84+
}
85+
86+
/**
87+
* Slow Fast
88+
* Time O(log(N)) | Space O(1)
89+
* https://leetcode.com/problems/happy-number/
90+
* @param {number} n
91+
* @return {boolean}
92+
*/
93+
var isHappy = (n) => {
94+
let [ slow, fast ] = [ n, getNext(n) ];
95+
96+
const hasCyle = () => ((fast === 1) || (slow === fast));
97+
while (!hasCyle()) { /* Time O(log(N)) */
98+
slow = getNext(slow); /* Time O(log(N)) */
99+
fast = getNext(getNext(fast));/* Time O(log(N)) */
100+
}
101+
102+
return (fast === 1);
103+
}
104+
105+
var getNext = (n, sum = 0) => {
106+
while (0 < n) {/* Time O(log(N)) */
107+
const remainder = (n % 10);
108+
109+
n = Math.floor((n / 10));
110+
sum += (remainder * remainder);
111+
}
112+
113+
return sum;
114+
}

javascript/43-Multiply-Strings.js

+105-19
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,116 @@
11
/**
2+
* Matrix
3+
* Time O(N * M) | Space O(N + M)
4+
* https://leetcode.com/problems/multiply-strings/
25
* @param {string} num1
36
* @param {string} num2
47
* @return {string}
58
*/
6-
var multiply = function (num1, num2) {
7-
if (num1 === '0' || num2 === '0') {
8-
return '0';
9-
}
9+
var multiply = (num1, num2) => {
10+
const isZero = ((num1 === '0') || (num2 === '0'));
11+
if (isZero) return '0';
12+
13+
const buffer = initBuffer(num1, num2);/* | Space (N + M) */
14+
15+
multiplication(num1, num2, buffer) /* Time O(N * M) */
16+
removeLeadingZero(buffer); /* Time O(N + M) | Time O(N + M)*/
17+
18+
return buffer.join(''); /* Time O(N + M) | Space O(N + M) */
19+
};
1020

11-
var res = new Array(num1.length + num2.length).fill(0);
12-
var revNum1 = [...num1].reverse();
13-
var revNum2 = [...num2].reverse();
14-
15-
for (var i1 in revNum1) {
16-
for (var i2 in revNum2) {
17-
var digit = revNum1[i1] * revNum2[i2];
18-
var index = Number(i1) + Number(i2);
19-
res[index] += digit;
20-
res[index + 1] += Math.floor(res[index] / 10);
21-
res[index] = res[index] % 10;
21+
var initBuffer = (num1, num2) => {
22+
const size = (num1.length + num2.length);
23+
24+
return new Array(size).fill(0);/* Space (N + M) */
25+
}
26+
27+
var multiplication = (num1, num2, buffer) => {
28+
for (let i = (num1.length - 1); (0 <= i); i--) {/* Time O(N) */
29+
for (let j = (num2.length - 1); (0 <= j); j--) {/* Time O(M) */
30+
update(num1, i, num2, j, buffer); /* Space O(N + M) */
2231
}
2332
}
33+
}
2434

25-
if (res[res.length - 1] === 0) {
26-
res.pop();
27-
}
35+
var removeLeadingZero = (buffer) => {
36+
const isLeadZero = (buffer[0] === 0);
37+
if (!isLeadZero) return;
38+
39+
buffer.shift();/* Time O(N + M) | Time O(N + M) */
40+
}
41+
42+
var update = (num1, i, num2, j, buffer) => {
43+
const curPos = (i + j);
44+
const prevPos = curPos + 1;
2845

29-
return res.reverse().join('');
46+
const carry = buffer[prevPos];
47+
const product = getProduct(num1, i, num2, j);
48+
const sum = (carry + product);
49+
50+
const remainder = (sum % 10);
51+
const value = ((sum - remainder) / 10);
52+
53+
buffer[prevPos] = remainder;/* Space O(N + M) */
54+
buffer[curPos] += value; /* Space O(N + M) */
55+
}
56+
57+
var getProduct = (num1, i, num2, j) => {
58+
const [ iNum, jNum ] = [ Number(num1[i]), Number(num2[j]) ];
59+
60+
return (iNum * jNum);
61+
}
62+
63+
/**
64+
* Matrix
65+
* Time O(N * M) | Space O(N + M)
66+
* https://leetcode.com/problems/multiply-strings/
67+
* @param {string} num1
68+
* @param {string} num2
69+
* @return {string}
70+
*/
71+
var multiply = (num1, num2) => {
72+
const isZero = ((num1 === '0') || (num2 === '0'));
73+
if (isZero) return '0';
74+
75+
const buffer = initBuffer(num1, num2);/* | Space O(N + M) */
76+
77+
multiplication(num1, num2, buffer); /* Time O(N * M) | Space O(N + M) */
78+
removeLeadingZero(buffer); /* Time O(N + M) | Space O(N + M) */
79+
80+
return buffer.join(''); /* Time O(N + M) | Space O(N + M) */
3081
};
82+
83+
var initBuffer = (num1, num2) => new Array(num1.length + num2.length).fill(0);/* Space O(N + M) */
84+
85+
var multiplication = (num1, num2, buffer) => {
86+
[ num1, num2 ] = /* Time O(N + M) */
87+
[ reverse(num1), reverse(num2) ];
88+
89+
for (var i1 in num1) {/* Time O(N) */
90+
for (var i2 in num2) {/* Time O(M) */
91+
update(num1, i1, num2, i2, buffer);/* Space O(N + M) */
92+
}
93+
}
94+
95+
buffer.reverse();/* Time O(N + M) */
96+
}
97+
98+
const reverse = (s) => s
99+
.split('') /* Time O(K) | Space O (K) */
100+
.reverse();/* Time O(K) */
101+
102+
var update = (num1, i1, num2, i2, buffer) => {
103+
const product = num1[i1] * num2[i2];
104+
const index = Number(i1) + Number(i2);
105+
106+
buffer[index] += product; /* Space O(N + M) */
107+
buffer[(index + 1)] += Math.floor(buffer[index] / 10);/* Space O(N + M) */
108+
buffer[index] = (buffer[index] % 10); /* Space O(N + M) */
109+
}
110+
111+
var removeLeadingZero = (buffer) => {
112+
const isZero = (buffer[0] === 0);
113+
if (!isZero) return;
114+
115+
buffer.shift();/* Time O(N + M) | Space O(N + M) */
116+
}

0 commit comments

Comments
 (0)