Skip to content

Commit 01f63bd

Browse files
committed
add MirrorReflection
1 parent 6c6f30a commit 01f63bd

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

hi-leetcode/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
| 0841 | [Keys And Rooms](https://leetcode.com/problems/keys-and-rooms/) | [Java](./src/main/java/me/meet/leetcode/medium/KeysAndRooms.java) | Medium |
4747
| 0845 | [Longest Mountain In Array](https://leetcode.com/problems/longest-mountain-in-array/) | [Java](./src/main/java/me/meet/leetcode/medium/LongestMountainInArray.java) | Medium |
4848
| 0846 | [Hand Of Straights](https://leetcode.com/problems/hand-of-straights/) | [Java](./src/main/java/me/meet/leetcode/medium/HandOfStraights.java) | Medium |
49+
| 0858 | [Mirror Reflection](https://leetcode.com/problems/mirror-reflection/) | [Java](./src/main/java/me/meet/leetcode/medium/MirrorReflection.java) | Medium |
4950
| 0875 | [Koko Eating Bananas](https://leetcode.com/problems/koko-eating-bananas/) | [Java](./src/main/java/me/meet/leetcode/medium/KokoEatingBananas.java) | Medium |
5051
| 0881 | [Boats To Save People](https://leetcode.com/problems/boats-to-save-people/) | [Java](./src/main/java/me/meet/leetcode/medium/BoatsToSavePeople.java) | Medium |
5152
| 0894 | [All Possible Full Binary Trees](https://leetcode.com/problems/all-possible-full-binary-trees/) | [Java](./src/main/java/me/meet/leetcode/medium/AllPossibleFullBinaryTrees.java) | Medium |
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package me.meet.leetcode.medium;
2+
3+
public final class MirrorReflection {
4+
private MirrorReflection() {
5+
}
6+
7+
/**
8+
* There is a special square room with mirrors on each of the four walls. Except for the southwest corner, there are receptors on each of the remaining corners, numbered 0, 1, and 2.
9+
* The square room has walls of length p, and a laser ray from the southwest corner first meets the east wall at a distance q from the 0th receptor.
10+
* Return the number of the receptor that the ray meets first. (It is guaranteed that the ray will meet a receptor eventually.)
11+
*
12+
* Example 1:
13+
* Input: p = 2, q = 1
14+
* Output: 2
15+
*
16+
* Explanation: The ray meets receptor 2 the first time it gets reflected back to the left wall.
17+
*
18+
* Note:
19+
* 1 <= p <= 1000
20+
* 0 <= q <= p
21+
*
22+
* 题意:
23+
* 这道题给了我们一个正方形的房间,说是四面都是镜子墙,然后在西南角有一个激光发射器,其余三个角都有接收装置,问我们最终激光会被哪个接收器接收。第一次读题时这句 "Return the number of the receptor that the ray meets first." 让博主理解错误了,以为是让返回接收器的个数,以为接收器也能反射激光到其对角的接收器,那么接收器2和0互相反射,就是返回经过了2个接收器,接收器1返回到反射点,就是返回经过了1个接收点,想的是一套一套的,结果人家让返回的是接收器的标号,
24+
* 个人觉得将 number 改为 index 会减少些歧义。无所谓了,反正最终搞懂了题意就行了。其实这道题的正确解法还挺难想的,因为大家很容易走进的误区就是研究反射角啥的,然后算具体反射到了哪一个位置,再算下一个位置,其实这样都将题目变复杂了。博主把这一类型归为脑筋急转弯 Brain Teaser,一般都有很巧妙的数学解法,并不需要太复杂的算法
25+
*/
26+
static int mirrorReflection(int p, int q) {
27+
while (p % 2 == 0 && q % 2 == 0) {
28+
p /= 2;
29+
q /= 2;
30+
}
31+
if (p % 2 == 0) return 2;
32+
if (q % 2 == 0) return 0;
33+
return 1;
34+
}
35+
36+
private static void testMirrorReflection() {
37+
int p = 2, q = 1;
38+
int res = mirrorReflection(p, q);
39+
System.out.println(res);
40+
41+
}
42+
43+
public static void main(String[] args) {
44+
testMirrorReflection();
45+
}
46+
}

0 commit comments

Comments
 (0)