-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #878 from 0xff-dev/1553
Add solution and test-cases for problem 1553
- Loading branch information
Showing
3 changed files
with
67 additions
and
23 deletions.
There are no files selected for viewing
40 changes: 26 additions & 14 deletions
40
leetcode/1501-1600/1553.Minimum-Number-of-Days-to-Eat-N-Oranges/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 34 additions & 2 deletions
36
leetcode/1501-1600/1553.Minimum-Number-of-Days-to-Eat-N-Oranges/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,37 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
return x | ||
func Solution(n int) int { | ||
// dfs内存爆了,试试bfs | ||
in := make(map[int]struct{}) | ||
queue := [][2]int{{n, 0}} | ||
for len(queue) > 0 { | ||
nq := make([][2]int, 0) | ||
for _, cur := range queue { | ||
if cur[0] == 1 { | ||
return cur[1] + 1 | ||
} | ||
next := cur[0] - 1 | ||
if _, ok := in[next]; !ok { | ||
in[next] = struct{}{} | ||
nq = append(nq, [2]int{next, cur[1] + 1}) | ||
} | ||
if cur[0]%2 == 0 { | ||
next = cur[0] / 2 | ||
if _, ok := in[next]; !ok { | ||
in[next] = struct{}{} | ||
nq = append(nq, [2]int{next, cur[1] + 1}) | ||
} | ||
} | ||
|
||
if cur[0]%3 == 0 { | ||
next = cur[0] / 3 | ||
if _, ok := in[next]; !ok { | ||
in[next] = struct{}{} | ||
nq = append(nq, [2]int{next, cur[1] + 1}) | ||
} | ||
} | ||
} | ||
queue = nq | ||
} | ||
return -1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters