Skip to content

Commit 216fba0

Browse files
committed
Update
1 parent e6aa2ea commit 216fba0

File tree

3 files changed

+80
-26
lines changed

3 files changed

+80
-26
lines changed

clock-angle.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Clock Angle
3+
*
4+
* Given two integers, an hour and a minute, write a function to calculate the angle between the two hands on a clock representing that time.
5+
*
6+
* Example 1:
7+
* Input: 3, 0
8+
* Output: 90
9+
*
10+
* Example 2:
11+
* Input: 3, 20
12+
* Output: 20
13+
*
14+
* Example 3:
15+
* Input: 12, 0
16+
* Output: 0
17+
*
18+
* Example 4:
19+
* Input: 2, 45
20+
* Output: 172.5
21+
*
22+
*/
23+
24+
/**
25+
* Calculate the clock angle
26+
* @param {Number} hour
27+
* @param {Number} minute
28+
* @return {Number}
29+
* @time comlexity: O(1)
30+
* @space complexity: O(1)
31+
*/
32+
const clockAngle = (hour, minute) => {
33+
const ANGLE_PER_HOUR = 360 / 12,
34+
ANGLE_PER_MINUTE = 360 / 60,
35+
minuteHand = minute * ANGLE_PER_MINUTE,
36+
hourHand = hour * ANGLE_PER_HOUR + minute / 60 * ANGLE_PER_HOUR;
37+
38+
const angle = Math.abs(hourHand - minuteHand);
39+
40+
return Math.min(360 - angle, angle);
41+
};
42+
43+
44+
import { assert } from 'chai';
45+
46+
describe('Clock Angle', () => {
47+
48+
it('3:00 should be 90 degree', () => {
49+
assert.strictEqual(clockAngle(3, 0), 90);
50+
});
51+
52+
it('3:20 should be 20 degree', () => {
53+
assert.strictEqual(clockAngle(3, 20), 20);
54+
});
55+
56+
it('12:00 should be 0 degree', () => {
57+
assert.strictEqual(clockAngle(12, 0), 0);
58+
});
59+
60+
it('2:45 should be 172.5 degree', () => {
61+
assert.strictEqual(clockAngle(2, 45), 172.5);
62+
});
63+
64+
it('9:00 should be 90 degree', () => {
65+
assert.strictEqual(clockAngle(9, 0), 90);
66+
});
67+
68+
});

test.js

+10-24
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
1-
function lcs(wordX, wordY) {
2-
const m = wordX.length;
3-
const n = wordY.length;
4-
const l = [];
1+
function test(str) {
2+
let ans = '';
3+
const map = {};
54

6-
for (let i = 0; i <= m; i++) {
7-
l[i] = []; // {1}
8-
for (let j = 0; j <= n; j++) {
9-
l[i][j] = 0; // {2}
10-
}
11-
}
5+
for (let i = str.length - 1; i >= 0; i--) {
6+
map[str[i]] = (map[str[i]] || 0) + 1;
127

13-
for (let i = 0; i <= m; i++) {
14-
for (let j = 0; j <= n; j++) {
15-
if (i === 0 || j === 0) {
16-
continue;
17-
} else if (wordX[i - 1] === wordY[j - 1]) {
18-
l[i][j] = l[i - 1][j - 1] + 1; // {3}
19-
} else {
20-
const a = l[i - 1][j];
21-
const b = l[i][j - 1];
22-
l[i][j] = a > b ? a : b; // {4} max(a,b)
23-
}
8+
if (map[str[i]] === 1) {
9+
ans = str[i];
2410
}
2511
}
26-
console.log(l);
27-
return l[m][n]; // {5}
12+
13+
return ans;
2814
}
2915

30-
console.log(lcs('acbaed', 'abcadf'));
16+
console.log(test(" tutorial horizon"));

unique-paths-iii.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class UniquePath {
6666
/**
6767
* Set the grid
6868
* @param {Array[][]} grid
69-
* @time complexity: O(mn), where m is the number of rows and n is the number of cols
69+
* @time complexity: O(mn), where m is the number of rows and n is the number of columns
7070
* @space complexity: O(mn)
7171
*/
7272
setGrid(grid) {
@@ -101,7 +101,7 @@ class UniquePath {
101101
* @param {Number} col
102102
* @param {Number} validSquare
103103
* @return {Number} Number of unique paths
104-
* @time complexity: O(4^m*n), where m is the number of rows and n is the number of cols,
104+
* @time complexity: O(4^m*n), where m is the number of rows and n is the number of columns,
105105
* it walks in 4 directions (up, down, left, right). So its 4 power m * n
106106
* @space complexity: O(mn), because of its recursion stacks
107107
*/

0 commit comments

Comments
 (0)