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

Function explore1

longest_increasing_subsequence_300/solution.go:99–122  ·  view source on GitHub ↗

This approach is slow (exponential runtime). It works, but times out on leetcode. We can do better.

(nums []int)

Source from the content-addressed store, hash-verified

97// It works, but times out on leetcode.
98// We can do better.
99func explore1(nums []int) int {
100 if len(nums) == 0 {
101 return 0
102 }
103
104 if isIncreasing(nums) {
105 return len(nums)
106 }
107
108 maxLen := 0
109 for i := range nums {
110 newNums := append(append([]int{}, nums[0:i]...), nums[i+1:]...)
111 if isIncreasing(newNums) {
112 return len(newNums)
113 }
114
115 max := explore1(newNums)
116 if max > maxLen {
117 maxLen = max
118 }
119 }
120
121 return maxLen
122}
123
124func isIncreasing(nums []int) bool {
125 for i := 1; i < len(nums); i++ {

Callers

nothing calls this directly

Calls 1

isIncreasingFunction · 0.85

Tested by

no test coverage detected