This file contains hidden or 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
func GetValue(id int, wg *sync.WaitGroup, mtx *sync.RWMutex) (int, bool) { | |
defer wg.Done() | |
// Check the cache | |
mtx.RLock() | |
val, ok := cache[id] | |
mtx.RUnlock() | |
if ok { | |
return val, ok | |
} |
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"sync" | |
"time" | |
) | |
var cache = map[int]int{} |
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"time" | |
"sync" | |
) | |
func main() { | |
var wg sync.WaitGroup |
This file contains hidden or 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
func main() { | |
start := time.Now() | |
for i := 1; i <= 10; i++ { | |
go executeTask(i) | |
} | |
time.Sleep(300 * time.Millisecond) | |
fmt.Println("Total execution time:", time.Since(start)) | |
} | |
func executeTask(i int) { |
This file contains hidden or 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
func main() { | |
start := time.Now() | |
for i := 1; i <= 10; i++ { | |
go executeTask(i) | |
} | |
fmt.Println("Total execution time:", time.Since(start)) | |
} | |
func executeTask(i int) { | |
fmt.Println("Executing task", i) |
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
func main() { | |
start := time.Now() | |
for i := 1; i <= 10; i++ { |
This file contains hidden or 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
package heap | |
import ( | |
"github.com/stretchr/testify/assert" | |
"testing" | |
) | |
func TestKClosest(t *testing.T) { | |
testCases := map[string]struct { | |
input [][]int |
This file contains hidden or 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
package heap | |
func KClosest(points [][]int, k int) [][]int { | |
if len(points) == 0 { | |
return [][]int{} | |
} | |
var heap [][]int | |
for _, pair := range points { | |
x, y := pair[0], pair[1] |
This file contains hidden or 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
func pop(heap *[]int) []int { | |
root := (*heap)[0] | |
n := len(*heap) | |
lastElement := (*heap)[n - 1] | |
(*heap)[0] = lastElement | |
*heap = (*heap)[:n-1] | |
heapify(heap, 0) | |
return root |
This file contains hidden or 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
func buildHeap(heap []int) { | |
n := len(heap) | |
startIdx := n / 2 - 1 | |
for i := startIdx; i >= 0; i-- { | |
heapify(&heap, i) | |
} | |
} |