Skip to content

Instantly share code, notes, and snippets.

@Israel-Miles
Israel-Miles / mtx.go
Last active September 27, 2021 15:07
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
}
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
var cache = map[int]int{}
package main
import (
"fmt"
"time"
"sync"
)
func main() {
var wg sync.WaitGroup
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) {
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)
package main
import (
"fmt"
"time"
)
func main() {
start := time.Now()
for i := 1; i <= 10; i++ {
package heap
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestKClosest(t *testing.T) {
testCases := map[string]struct {
input [][]int
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]
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
func buildHeap(heap []int) {
n := len(heap)
startIdx := n / 2 - 1
for i := startIdx; i >= 0; i-- {
heapify(&heap, i)
}
}