MCPcopy Create free account
hub / github.com/austingebauer/go-leetcode / explore2

Function explore2

longest_increasing_subsequence_300/solution.go:82–94  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

80// It works, but times out on leetcode.
81// We can do better.
82func 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.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected