Skip to content

Commit 44a1cfd

Browse files
add 1344
1 parent e36ccb1 commit 44a1cfd

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1344|[Angle Between Hands of a Clock](https://leetcode.com/problems/angle-between-hands-of-a-clock/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1344.java) | |Medium|Math|
1112
|1343|[Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold](https://leetcode.com/problems/number-of-sub-arrays-of-size-k-and-average-greater-than-or-equal-to-threshold/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1343.java) | |Medium|Array|
1213
|1342|[Number of Steps to Reduce a Number to Zero](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1342.java) | |Easy|Bit Manipulation|
1314
|1341|[The K Weakest Rows in a Matrix](https://leetcode.com/problems/the-k-weakest-rows-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1341.java) | |Easy||
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 1344. Angle Between Hands of a Clock
5+
*
6+
* Given two numbers, hour and minutes. Return the smaller angle (in sexagesimal units) formed between the hour and the minute hand.
7+
*
8+
* Example 1:
9+
* Input: hour = 12, minutes = 30
10+
* Output: 165
11+
*
12+
* Example 2:
13+
* Input: hour = 3, minutes = 30
14+
* Output: 75
15+
*
16+
* Example 3:
17+
* Input: hour = 3, minutes = 15
18+
* Output: 7.5
19+
*
20+
* Example 4:
21+
* Input: hour = 4, minutes = 50
22+
* Output: 155
23+
*
24+
* Example 5:
25+
* Input: hour = 12, minutes = 0
26+
* Output: 0
27+
*
28+
* Constraints:
29+
* 1 <= hour <= 12
30+
* 0 <= minutes <= 59
31+
* Answers within 10^-5 of the actual value will be accepted as correct.
32+
* */
33+
public class _1344 {
34+
public static class Solution1 {
35+
public double angleClock(int hour, int minutes) {
36+
double minAngle = minutes * 360 / 60;
37+
double hourAnglePart1 = hour != 12 ? (hour * 360) / 12 : 0;
38+
double hourAnglePart2 = (double) (30 * minutes) / (double) 60;
39+
double hourAngle = hourAnglePart1 + hourAnglePart2;
40+
double preResult = Math.abs(minAngle - (hourAngle));
41+
return preResult > 180 ? 360 - preResult : preResult;
42+
}
43+
}
44+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1344;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1344Test {
10+
private static _1344.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1344.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(165, solution1.angleClock(12, 30), 0);
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(75, solution1.angleClock(3, 30), 0);
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(155, solution1.angleClock(4, 50), 0);
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(0, solution1.angleClock(12, 0), 0);
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(76.5, solution1.angleClock(1, 57), 0);
40+
}
41+
42+
}

0 commit comments

Comments
 (0)