This approach is also slow (O(2^n) runtime. It works, but times out on leetcode. We can do better.
(nums []int, previousVal int, currentIdx int)
| 80 | // It works, but times out on leetcode. |
| 81 | // We can do better. |
| 82 | func explore2(nums []int, previousVal int, currentIdx int) int { |
| 83 | if currentIdx == len(nums) { |
| 84 | return 0 |
| 85 | } |
| 86 | |
| 87 | max1 := 0 |
| 88 | if previousVal < nums[currentIdx] { |
| 89 | max1 = 1 + explore2(nums, nums[currentIdx], currentIdx+1) |
| 90 | } |
| 91 | max2 := explore2(nums, previousVal, currentIdx+1) |
| 92 | |
| 93 | return int(math.Max(float64(max1), float64(max2))) |
| 94 | } |
| 95 | |
| 96 | // This approach is slow (exponential runtime). |
| 97 | // It works, but times out on leetcode. |
nothing calls this directly
no outgoing calls
no test coverage detected