Skip to content

Codility – Peaks

May 8, 2018

This problem is under the learning lesson “Prime and Composite numbers” . I am trying to do all the problems marked as Respectable. Here’s my solution for this one:

package solution

// you can also use imports, for example:
//import “fmt”

// import “os”
//import “math”

// you can write to stdout for debugging purposes, e.g.
// fmt.Println(“this is a debug message”)

func Solution(A []int) int {
// write your code in Go 1.4
N := len(A)
var peaks []int
num_peaks := 0
for i := 1; i A[i-1] && A[i] > A[i+1] {
peaks = append(peaks, i)
num_peaks++
}
}

if num_peaks == 0 {
return 0
}

for parts := num_peaks; parts >= 1; parts– {
if N%parts != 0 {
continue
}
k := N / parts
//fmt.Printf(“k: %v\n”, k)
peak_ind := 0
cur_peak := peaks[peak_ind]
//fmt.Printf(“cur_peak: %v\n”, cur_peak)
peak_missing := false
for i := 0; i+k-1 = i && cur_peak < i+k && peak_ind < num_peaks {
// peak found in block
peak_found = true
peak_ind++
if peak_ind < num_peaks {
cur_peak = peaks[peak_ind]
//fmt.Printf("cur_peak: %v\n", cur_peak)
}
}

//fmt.Printf("block %v, peak_found: %v\n", i, peak_found)
if !peak_found {
peak_missing = true
break
}
}

if !peak_missing {
return parts
}
}

return 0
}

Was glad to get Another 100% result. Albeit, in my 2nd try.
codility_peaks_2

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: