Skip to content

Commit d955d2b

Browse files
add 755
1 parent 486f447 commit d955d2b

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2424
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
25+
|755|[Reach a Number](https://leetcode.com/problems/reach-a-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_755.java) | O(n) | O(1) | |Medium| Math
2526
|750|[Number Of Corner Rectangles](https://leetcode.com/problems/number-of-corner-rectangles/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_750.java) | O((m*n)^2) | O(1) | |Medium|
2627
|748|[Shortest Completing Word](https://leetcode.com/problems/shortest-completing-word/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_748.java) | O(n) | O(1) | |Easy|
2728
|747|[Largest Number Greater Than Twice of Others](https://leetcode.com/problems/largest-number-greater-than-twice-of-others/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_747.java) | O(n) | O(1) | |Easy|
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 755. Reach a Number
5+
*
6+
* You are standing at position 0 on an infinite number line. There is a goal at position target.
7+
* On each move, you can either go left or right. During the n-th move (starting from 1), you take n steps.
8+
* Return the minimum number of steps required to reach the destination.
9+
10+
Example 1:
11+
Input: target = 3
12+
Output: 2
13+
Explanation:
14+
On the first move we step from 0 to 1.
15+
On the second step we step from 1 to 3.
16+
17+
Example 2:
18+
Input: target = 2
19+
Output: 3
20+
Explanation:
21+
On the first move we step from 0 to 1.
22+
On the second move we step from 1 to -1.
23+
On the third move we step from -1 to 2.
24+
25+
Note:
26+
target will be a non-zero integer in the range [-10^9, 10^9].
27+
*/
28+
29+
public class _755 {
30+
public static class Solution1 {
31+
/**Two case:
32+
* 1. go to the right, and reach the goal exactly.
33+
* 2. go over the goal by several steps:
34+
* by even number, then you can choose one of the steps that went right to go back to the left (the step is half of what you went over)
35+
* by odd number, then you keep going until you are over by an even number.*/
36+
public int reachNumber(int target) {
37+
int absTarget = Math.abs(target);
38+
int steps = 1;
39+
int sum = 0;
40+
while (sum < absTarget || (sum - absTarget) % 2 == 1) {
41+
sum += steps;
42+
steps++;
43+
}
44+
return steps - 1;
45+
}
46+
}
47+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._755;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _755Test {
10+
private static _755.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _755.Solution1();
15+
}
16+
17+
@Test
18+
public void test4() {
19+
assertEquals(1, solution1.reachNumber(1));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(3, solution1.reachNumber(2));
25+
}
26+
27+
@Test
28+
public void test1() {
29+
assertEquals(2, solution1.reachNumber(3));
30+
}
31+
32+
@Test
33+
public void test3() {
34+
assertEquals(3, solution1.reachNumber(4));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(5, solution1.reachNumber(5));
40+
}
41+
42+
@Test
43+
public void test6() {
44+
assertEquals(3, solution1.reachNumber(6));
45+
}
46+
47+
@Test
48+
public void test7() {
49+
assertEquals(5, solution1.reachNumber(7));
50+
}
51+
52+
@Test
53+
public void test8() {
54+
assertEquals(4, solution1.reachNumber(8));
55+
}
56+
57+
@Test
58+
public void test9() {
59+
assertEquals(5, solution1.reachNumber(9));
60+
}
61+
62+
@Test
63+
public void test10() {
64+
assertEquals(4, solution1.reachNumber(10));
65+
}
66+
67+
@Test
68+
public void test11() {
69+
assertEquals(15, solution1.reachNumber(100));
70+
}
71+
72+
@Test
73+
public void test12() {
74+
assertEquals(47, solution1.reachNumber(1000));
75+
}
76+
77+
@Test
78+
public void test13() {
79+
assertEquals(143, solution1.reachNumber(10000));
80+
}
81+
82+
@Test
83+
public void test14() {
84+
assertEquals(447, solution1.reachNumber(100000));
85+
}
86+
87+
@Test
88+
public void test15() {
89+
assertEquals(1415, solution1.reachNumber(1000000));
90+
}
91+
92+
@Test
93+
public void test16() {
94+
assertEquals(4472, solution1.reachNumber(10000000));
95+
}
96+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy