Skip to content

Instantly share code, notes, and snippets.

View nleiva's full-sized avatar
☠️
Working from somewhere

Nicolas Leiva nleiva

☠️
Working from somewhere
View GitHub Profile
func findMaxConsecutiveOnes(nums []int) int {
total := 0
max := 0
for i := 0; i < len(nums); i++ {
if nums[i] == 1 {
total++
if total > max {
max = total
}
} else {
@nleiva
nleiva / combinations.go
Created February 26, 2018 21:16
Combinations (77) from https://leetcode.com/problems/combinations/description/ ....too compex :-(
// Slice implements sort.Interface
type Slice []int
func (a Slice) Len() int { return len(a) }
func (a Slice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a Slice) Less(i, j int) bool { return a[i] < a[j] }
func combine(n int, k int) [][]int {
a := []int{}
for i := 1; i <= n; i++ {
@nleiva
nleiva / shrange.go
Created February 28, 2018 21:00
SHORTEST RANGE IN K SORTED LISTS from https://www.youtube.com/watch?v=zplklOy7ENo, first take (not optimal)
package main
import (
"fmt"
"sort"
)
type Slice []int
func (a Slice) Len() int { return len(a) }
@nleiva
nleiva / shrange.go
Created March 1, 2018 03:28
SHORTEST RANGE IN K SORTED LISTS from https://www.youtube.com/watch?v=zplklOy7ENo, solution explained in the video
package main
import (
"container/heap"
"fmt"
)
type mHeap struct {
value int
array int
func threeSum(nums []int) [][]int {
result := [][]int{}
check := make(map[int]bool)
for i := 0; i < len(nums)-2; i++ {
if check[nums[i]] {
continue
}
for _, v := range twoSum(nums[i+1:], -nums[i]) {
if !(check[v[0]] || check[v[1]]) {
result = append(result, append(v, nums[i]))
func maxDepth(root *TreeNode) int {
if root == nil {
return 0
}
d := 1 + max(maxDepth(root.Right), maxDepth(root.Left))
return d
}
func max(a, b int) int {
if a > b {
func main() {
ipn := &net.IPNet{
IP: net.IPv4(10,200,0,0),
Mask: net.CIDRMask(24, 32),
}
gw := calcGatewayIP(ipn)
fmt.Printf("%v", gw )
}
@nleiva
nleiva / ec2-go.sh
Last active July 2, 2019 20:48
Install Go in EC2
#!/bin/bash
sudo apt-get update
sudo apt-get -y upgrade
sudo apt-get -y install make
curl -O https://storage.googleapis.com/golang/go1.12.6.linux-amd64.tar.gz
tar -xvf go1.12.6.linux-amd64.tar.gz
sudo mv go /usr/local
echo 'export PATH=$PATH:/usr/local/go/bin
// Client
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
// Server
s := grpc.NewServer()
// ... register gRPC services ...
// Client
config := &tls.Config{
InsecureSkipVerify: true,
}
conn, err := grpc.Dial(address, grpc.WithTransportCredentials(credentials.NewTLS(config)))
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()