คือ ผมทำ CodeSignal อยู่เรื่อยๆ พยายามแบ่งเวลามาทำเรื่อยๆ
แต่ปัญหาก็คือ บางครั้งโจทย์ยาวมาก และผมแปลผิด นั้นทำให้เขียนโปรแกรมเท่าไหร่ก็ไม่ผ่าน

แล้วผมบ้าบอ อะไรก็ไม่รู้คิดไปเองหลายๆอย่าง และนี่ไม่ใช่ครั้งแรก
พอมานึกดูดีๆ ผมเจอแบบนี้บ่อย และข้อนี้ผมก็จำได้ว่า ให้หา number of candidate ไม่ใช่ index แต่พอเราหยุดเขียน เพราะติดงาน หรือติดอื่นๆ กลับมาเขียนใหม่ มันก็จะลืม แล้วก็หา index เหมือนเดิมมมม บ้าาาาาาา
พอผมเข้าใจโจทย์ ผมเขียนรอบเดียวผ่าน ผ่านแบบไม่ได้ทดสอบ test อะไรเลย
ผมเลยหงิดหงุดกับภาษาอังกฤษตัวเองมากกกก ทำไมถึงแย่ขนาดนี้
func electionsWinners(votes []int, k int) int {
count:=0
for i:=0;i<len(votes);i++{
ik:=votes[i]+k
flagK:=true
for j:=0;j<len(votes);j++{
if i == j{
continue
}
if ik <= votes[j]{
flagK=false
break
}
}
if flagK{
count++
}
}
return count
}
func electionsWinners2(votes []int, k int) int {
maxValue:=votes[0]
maxIndex:=0
for i:=0;i<len(votes);i++{
if votes[i] >= maxValue {
maxValue = votes[i]
maxIndex = i
}
}
fmt.Println(maxValue,maxIndex+1)
max2Value:=votes[0]
max2Index:=0
for i:=0;i<len(votes);i++{
if max2Value != maxValue && votes[i] > max2Value && votes[i] != maxValue {
max2Value = votes[i]
max2Index = i
}
}
fmt.Println(max2Value,max2Index+1)
// Test 1
if max2Value+k > maxValue && max2Value != maxValue{
return max2Index+1
}
if maxValue+k == max2Value+k {
// Test 3
if maxIndex == max2Index{
return maxIndex+1
}
// Test 5
if maxValue+k==maxValue && max2Value+k == max2Value{
return 0
}
// Test 4
if maxIndex > max2Index{
return maxIndex+1
}
}
return 0
}
func electionsWinners1(votes []int, k int) int {
if k == 0 {
maxValue:=0
maxvalueIndex:=0
dup:=0
for i:=0;i<len(votes);i++{
if votes[i] > maxValue {
maxValue = votes[i]
maxvalueIndex=i
}
if maxValue == votes[i] {
dup++
}
}
if dup >= 2{
return 0
}else{
return maxvalueIndex+1
}
}
// case dup all
s:=votes[0]
flag:=true
for _,v:=range votes{
if v != s{
flag = false
break
}
}
if flag {
return len(votes)
}
// step 1 find max
maxValue:=0
maxValueIndex:=0
for i:=0;i<len(votes);i++{
if votes[i] > maxValue {
maxValue = votes[i]
maxValueIndex = i
}
}
// step 2 compare with out maxValue
plusVotes:=make([]int,len(votes))
saveIndex:=[]int{}
for i,v:=range votes{
if i != maxValueIndex{
// TODO: fix
if maxValue == votes[i]{
plusVotes[i] = v
saveIndex = append(saveIndex,i)
continue
}
plusVotes[i] = v+k
}else{
plusVotes[i] = v
saveIndex = append(saveIndex,i)
}
}
// fmt.Println(maxValue,maxValueIndex+1,plusVotes)
// step 3 find max
maxValue=0
maxValueIndex=0
for i:=0;i<len(plusVotes);i++{
if plusVotes[i] >= maxValue {
if maxValue != plusVotes[i]{
f:=true
for _,si:=range saveIndex {
if si == i {
f = false
break
}
}
if f {
maxValue = plusVotes[i]
maxValueIndex = i
}
}
}
}
return maxValueIndex+1
}
ไอที่ยาวๆ คือผิด ส่วนสั้นๆ func เดียว ทีเดียวผ่านนนน บ้าบอออออออ