Two Oldest Ages Problem
ผมอยากเขียน Kolin เลยไปลองเล่นโจทย์ Codewar
Two Oldest Ages Problem
ผมอยากเขียน Kolin เลยไปลองเล่นโจทย์ Codewar

คุณมีเพื่อนอยู่ 5 คน มีโจทย์ว่าจงหาคนที่อายุมากที่สุด 2 และจงบอกอายุตามลำดับคุณจะหายังไง ?
โจทย์ง่ายๆ ที่ถ้าถามในชีวิตประจำวันก็คงตอบได้ไม่ยาก “ทุกคนบอกอายุสิ แล้วฉันจะบอกเองว่าใครอายุมากที่สุด”
งั้นลองเพิ่มความซับซ้อนเข้าไป ลองอธิบายวิธีคิดของคุณที อะไรที่ทำเราบอกได้ว่าใครอายุมากกว่ากัน
ตัวอย่างเช่น
A: 16, B: 20, C: 21, D: 45, E: 23
การจะตอบได้ว่า E และ D อายุมากที่สุดได้มาจากกระบวนการคิดแบบใดนั้นอย่างแรกที่คุณต้องทำได้คือคุณต้องนับเลขเป็นถูกมะ
คุณต้องรู้ว่าระหว่าง 16 และ 20 ค่าไหนมากกว่ากัน ซึ่งนำมาสู่ Solution แรก
เทียบค่ารายคนไปเรื่อยๆ จนครบทุกคน
ถ้าเราเทียบอายุรายคนไปเรื่อยๆ จนครบทุกคนสุดท้ายเราก็จะรู้ว่า อ่อ A อายุน้อยสุดนะ และก็ทำกับ B โดยเริ่มเทียบจาก C จนถึง E ทำไปเรื่อยๆ สุดท้ายเราจะอธิบายได้ว่าทำไมคนที่อายุมากสุดคือ E และ D เพราะเราเทียบอายุทุกคนและรู้ว่า D อายุมากสุด ส่วน E อายุรองลงมา

เทียบค่าไปเรื่อยๆ
สังเกตอะไรมั้ย? ตอนนี้เราเทียบค่าไปเรื่อยๆ อย่างเดียวมันทำให้เรารู้ว่าใครมีอายุมากกว่าหรือน้อยกว่า ถ้าเราทำเพิ่มอีกนิดนึง ถ้าใครอายุมากกว่าให้สลับลำดับกันคนที่อายุมากกว่าจะอยู่ทางขวาเสมอเราจะพบว่ามี 3 คนที่ไม่ได้เปลี่ยนลำดับเลย

A, B, C ไม่จำเป็นต้องสลับลำดับ
ถึงอย่างงั้น A ก็ต้องเทียบอายุกับทุกคนอยู่ดีเพราะเราไม่รู้ว่า ถัดจาก B จะมีคนอายุน้อยกว่า A รึเปล่า แต่หลังจากทุกคนยืนเรียงกันตามลำดับอายุน้อยไปมากแล้วเราจะตอบได้ทันทีว่าสองคนนับจากขวามือสุดคือคนที่อายุมากที่สุดตามลำดับ!
เราจะเห็นว่าถ้าเราคิดเป็นอธิบายความคิดเราได้ อย่างชัดเจนการเขียนโค้ดจะง่ายมาก (ถ้าเป็น Kotlin)
เขียนออกมาหน้าตาจะประมาณนี้
fun twoOldestAges(ages: List<Int>): List<Int> {
return ages.sorted().takeLast(2)
}
แต่กว่าผมจะคิดได้ว่าควรจะเป็นยังไงนี้คือสิ่งที่ผมเขียน
fun twoOldestAges(ages: List<Int>): List<Int> {
var agesMutableList = ages.toMutableList();
var oldestList:MutableList<Int> = mutableListOf();
val middle = agesMutableList.size / 2
//split list to two list
val firstPart = agesMutableList.subList(0, middle).toMutableList()
val secondPart = agesMutableList.subList(middle, agesMutableList.size).toMutableList()
//find a highest number for each list
//temp first value
var temp:Int = 0;
//compare temp with next value
println("firstPart: " + firstPart)
for (i in firstPart) {
if (temp < i) {
//if next value grather than temp replace temp util reach the eage
temp = i
}
}
println("first highest" + temp)
oldestList.add(temp)
//compare temp with next value
println("secondPart: " + secondPart)
temp = 0
for (i in secondPart) {
if (temp < i) {
//if next value grather than temp replace temp util reach the eage
temp = i
}
}
println("second highest" + temp)
//add two hightest number to list
oldestList.add(temp)
//reorder acs order
oldestList.sort()
return oldestList
}
และอีกรอบเพราะอยากลอง Recursive
fun highestNum(numList: MutableList<Int>): Int {
//compare 0 and max index
//temp
//if size != 0 call it own function
//else return
val listSize = numList.size
//compare temp with next value
if (listSize != 1 && numList[0] < numList[listSize-1]) {
numList.removeAt(0)
return findingHighestNum(numList)
}
return numList[0]
}
fun twoOldestAges(ages: List<Int>): List<Int> {
var agesMutableList = ages.toMutableList();
var oldestList:MutableList<Int> = mutableListOf();
val middle = agesMutableList.size / 2
//split list to two list
val firstPart = agesMutableList.subList(0, middle).toMutableList()
val secondPart = agesMutableList.subList(middle, agesMutableList.size).toMutableList()
//find a highest number for each list
//compare temp with next value
//add two hightest number to list
val firstResult = highestNum(firstPart)
val secondResult = highestNum(secondPart)
//reorder acs order
if (firstResult > secondResult) {
oldestList.add(secondResult)
oldestList.add(firstResult)
} else {
oldestList.add(firstResult)
oldestList.add(secondResult)
}
return oldestList
}
ถ้าอ่านก่อนหน้านี้เข้าใจและพออ่านโค้ดได้ จะพอรู้ละว่าโค้ดของผมมีจุดที่บัคตรงไหน และถ้าเขียนโค้ดเป็นจะเห็นเข้าไปอีกว่าไม่ใช้แค่ Bussiness logic ที่บัค ตัวโค้ดเองมันจะพังด้วยกับ Usecase ที่หลากหลาย ขอละไว้เท่านี้ในบทความนี้เจอกันครั้งหน้า
ใครมองออกสามารถรีวิวโค้ดกันได้เลยนะครับ !
메타데이터
- post_id
- 2d98cfd22fa8
- slug
- two-oldest-ages-problem-2d98cfd22fa8
- url
- https://medium.com/@khing78/two-oldest-ages-problem-2d98cfd22fa8
- canonical_url
- https://medium.com/@khing78/two-oldest-ages-problem-2d98cfd22fa8
- author_url
- https://medium.com/@khing78
- status
- ok
- fetched_at
- 2026-09-05 12:20:41