Back to course home
0% completed
Vote For New Content
Time complexity incorrect for Golang solution
Kurt Medley
Aug 17, 2024
According to GPT
```go for len(stack) > 0 { pop := stack[len(stack)-1] // Pop the top element from the stack. stack = stack[:len(stack)-1] // Remove the popped element from the stack. sb += fmt.Sprintf("%d", pop) // Append the popped binary digit to the result string. }
**Time Complexity:**
- **Stack Operations:** Popping from a slice is O(1).
- **String Concatenation:** Each `sb += fmt.Sprintf("%d", pop)` operation involves concatenating a string to `sb`. Concatenating strings in Go with `+=` within a loop has O(k) complexity where `k` is the length of the string being concatenated, leading to O(log₂(num)²) due to repeated reallocation and copying of the string.
0
0
Comments
Comments
On this page