Skip to content

Commit cf3c703

Browse files
committed
Add solution #858
1 parent 040c6e7 commit cf3c703

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,7 @@
665665
855|[Exam Room](./0855-exam-room.js)|Medium|
666666
856|[Score of Parentheses](./0856-score-of-parentheses.js)|Medium|
667667
857|[Minimum Cost to Hire K Workers](./0857-minimum-cost-to-hire-k-workers.js)|Hard|
668+
858|[Mirror Reflection](./0858-mirror-reflection.js)|Medium|
668669
867|[Transpose Matrix](./0867-transpose-matrix.js)|Easy|
669670
868|[Binary Gap](./0868-binary-gap.js)|Easy|
670671
872|[Leaf-Similar Trees](./0872-leaf-similar-trees.js)|Easy|

solutions/0858-mirror-reflection.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* 858. Mirror Reflection
3+
* https://leetcode.com/problems/mirror-reflection/
4+
* Difficulty: Medium
5+
*
6+
* There is a special square room with mirrors on each of the four walls. Except for the southwest
7+
* corner, there are receptors on each of the remaining corners, numbered 0, 1, and 2.
8+
*
9+
* The square room has walls of length p and a laser ray from the southwest corner first meets the
10+
* east wall at a distance q from the 0th receptor.
11+
*
12+
* Given the two integers p and q, return the number of the receptor that the ray meets first.
13+
*
14+
* The test cases are guaranteed so that the ray will meet a receptor eventually.
15+
*/
16+
17+
/**
18+
* @param {number} p
19+
* @param {number} q
20+
* @return {number}
21+
*/
22+
var mirrorReflection = function(p, q) {
23+
const gcd = findGCD(p, q);
24+
const m = q / gcd;
25+
const n = p / gcd;
26+
27+
if (m % 2 === 0) return 0;
28+
return n % 2 === 1 ? 1 : 2;
29+
};
30+
31+
function findGCD(a, b) {
32+
return b === 0 ? a : findGCD(b, a % b);
33+
}

0 commit comments

Comments
 (0)