8000 feat: fibfrog · kimi0230/LeetcodeGolang@60db9d0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 60db9d0

Browse files
committed
feat: fibfrog
1 parent cbe328c commit 60db9d0

File tree

3 files changed

+10
-12
lines changed

3 files changed

+10
-12
lines changed

Codility/Lesson/0013.Fibonacci-Numbers/FibFrog/FibFrog.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package fibfrog
22

3-
import "fmt"
4-
53
/**
64
* @description: 產生不大於n的斐波那契數的列表
75
* @param {int} N
@@ -29,16 +27,17 @@ func Solution(A []int) int {
2927
return 1
3028
}
3129
fibArr = fibArr[1 : len(fibArr)-1]
32-
fmt.Println(fibArr)
30+
// fmt.Println(fibArr)
3331

3432
// get the leafs that can be reached from the starting shore
3533
reachable := make([]int, N)
3634
for _, v := range fibArr {
3735
if A[v-1] == 1 {
36+
// 找到樹葉
3837
reachable[v-1] = 1
3938
}
4039
}
41-
// 一開始只能到 index: 4
40+
// 一開始只能跳到 index: 4 , fib是 [1 1 2 3 5 8], 會使用到 5
4241
// fmt.Println("re", reachable) // [0 0 0 0 1 0 0 0 0 0 0 0]
4342

4443
// iterate all the positions until you reach the other shore
@@ -58,13 +57,13 @@ func Solution(A []int) int {
5857
previousIdx := i - f
5958

6059
if previousIdx < 0 || reachable[previousIdx] == 0 {
61-
fmt.Printf("[No] %d :previousIdx = %d reachable = %v \n", i, previousIdx, reachable)
60+
// fmt.Printf("[No] %d :previousIdx = %d reachable = %v \n", i, previousIdx, reachable)
6261
continue
6362
}
64-
fmt.Printf("%d :previousIdx = %d reachable = %v \n", i, previousIdx, reachable)
6563

6664
if minJump > reachable[previousIdx] {
6765
// 此 previousIdx 位置可以到達
66+
// fmt.Printf("%d :previousIdx = %d reachable = %v \n", i, previousIdx, reachable)
6867
minJump = reachable[previousIdx]
6968
canJump = true
7069
}
@@ -73,12 +72,11 @@ func Solution(A []int) int {
7372
reachable[i] = minJump + 1
7473
}
7574
}
76-
fmt.Printf("i=%d , reachable = %v \n", i, reachable)
75+
// fmt.Printf("i=%d , reachable = %v \n", i, reachable)
7776
}
7877

79-
if reachable[len(reachable)-1] < N {
80-
return reachable[len(reachable)-1]
81-
} else {
78+
if reachable[len(reachable)-1] == 0 {
8279
return -1
8380
}
81+
return reachable[len(reachable)-1]
8482
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,8 @@
498498
<a href="https://github.com/kimi0230/LeetcodeGolang/tree/master/Codility/Lesson/0013.Fibonacci-Numbers/FibFrog"> Go <br/></a>
499499
</th>
500500
<th style="color:red;"> Respectable </th>
501-
<th> </th>
502-
<th> </th>
501+
<th> O(N * log(N)) </th>
502+
<th> O(N) </th>
503503
</tr>
504504
<tr>
505505
<th>

asset/images/fibfrog.jpg

1.78 MB
Loading

0 commit comments

Comments
 (0)
0