[Kotlin] ์Œ์–‘ ๋”ํ•˜๊ธฐ (ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜)
๋ฐ˜์‘ํ˜•

 

๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/76501

 

 


 

์ ‘๊ทผ

 

  • ๋จผ์ € absoulutes[i] ๊ฐ’์— signs[i]์˜ ๋ถ€ํ˜ธ๊ฐ’์„ ๋”ํ•œ ๋ฐฐ์—ด์„ ๋งŒ๋“ ๋‹ค.
  • ๊ทธ๋ฆฌ๊ณ  ๋งŒ๋“ค์–ด์ง„ ๋ฐฐ์—ด ์›์†Œ์˜ ํ•ฉ์„ ๊ตฌํ•˜๊ณ  ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.
//์˜ค๋ฅ˜ ์ฝ”๋“œ
// 1. absoulutes[i] ๊ฐ’์— signs[i]์˜ ๋ถ€ํ˜ธ๊ฐ’์„ ๋”ํ•œ ๋ฐฐ์—ด์„ ๋งŒ๋“ฆ
// 2. ํ•ด๋‹น ๋ฐฐ์—ด ์›์†Œ์˜ ํ•ฉ์„ ๊ตฌํ•˜๊ณ  ์ถœ๋ ฅ

class Solution {
    fun solution(absolutes: IntArray, signs: BooleanArray): Int {
        
        var sumArray = intArrayOf()
        for(i in 0..absolutes.size){
            if(signs[i] == true) sumArray += absolutes[i]
            else  sumArray += -absolutes[i]
        }
        return sumArray.sum()       
    }
}
  • ์˜ค๋ฅ˜: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
  • absolutes.size ๋งŒํผ for๋ฌธ์„ ๋ฐ˜๋ณตํ•˜๋ฉด ๋งˆ์ง€๋ง‰์— ์ธ๋ฑ์Šค ์ตœ๋Œ€๊ฐ’์„ ์ดˆ๊ณผํ•˜๋ฏ€๋กœ, -1์„ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 


 

์„ฑ๊ณต ์ฝ”๋“œ

 

class Solution {
    fun solution(absolutes: IntArray, signs: BooleanArray): Int {
        
        var sumArray = intArrayOf()
        for(i in 0..absolutes.size-1){
            if(signs[i] == true) sumArray += absolutes[i]
            else  sumArray += -absolutes[i]
        }
        return sumArray.sum()       
    }
}

 


 

์„ค๋ช…

 

  1. ๊ฒฐ๊ณผ๊ฐ’์„ ๋„ฃ์„ ๋ฐฐ์—ด์„ ๋งŒ๋“ ๋‹ค.
  2. absolutes ๋ฐฐ์—ด์˜ ๊ธธ์ด - 1๋งŒํผ for๋ฌธ์„ ๋ฐ˜๋ณตํ•ด์ฃผ๋ฉด์„œ, absolutes ๊ฐ ์›์†Œ์— ๋ถ€ํ˜ธ๋ฅผ ๋ถ™์—ฌ ๊ฒฐ๊ณผ ๋ฐฐ์—ด์— ๋„ฃ๋Š”๋‹ค.
  3. ๊ฒฐ๊ณผ ๋ฐฐ์—ด ์›์†Œ์˜ ํ•ฉ์„ .sum()์œผ๋กœ ์ถœ๋ ฅํ•œ๋‹ค.

 


 

๋‹ค๋ฅธ ๋ฐฉ๋ฒ•

 

class Solution {
    fun solution(absolutes: IntArray, signs: BooleanArray): Int {

        val array = IntArray(signs.size)
        repeat(signs.size) {
            if(signs[it]) array[it] = absolutes[it]
            else array[it] = -(absolutes[it])
        }
        return array.sum()
    }
}
  • for๋ฌธ ๋Œ€์‹  repeat๋ฅผ ์ด์šฉํ•œ ํ’€์ด์ด๋‹ค.

 

class Solution {
    fun solution(absolutes: IntArray, signs: BooleanArray) =
        absolutes.foldIndexed(0) { idx, acc, num -> acc + if (signs[idx]) num else -num }
}
  • foldIndexed()๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ ์ •์„์ ์ธ ํ’€์ด๋‹ค.
  • foldIndexed ํ•จ์ˆ˜
    - foldIndexed ํ•จ์ˆ˜๋Š” ์ปฌ๋ ‰์…˜์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ง€์ •๋œ ์ดˆ๊ธฐ๊ฐ’๊ณผ ๋žŒ๋‹ค ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•จ
    - ๋žŒ๋‹ค ํ•จ์ˆ˜์˜ ์ฒซ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์ธ๋ฑ์Šค, ๋‘ ๋ฒˆ์งธ๋Š” ๋ˆ„์‚ฐ๊ธฐ(accumulator), ์„ธ ๋ฒˆ์งธ๋Š” ์ปฌ๋ ‰์…˜์˜ ํ˜„์žฌ ์š”์†Œ
  • acc + if (signs[idx]) num else -num
    - ์ด ๋ถ€๋ถ„์€ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•œ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰
    - if ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜์—ฌ signs[idx]์˜ ๊ฐ’์— ๋”ฐ๋ผ num์„ ๋”ํ• ์ง€ ๋นผ์ค„์ง€๋ฅผ ๊ฒฐ์ •
    - signs[idx]๊ฐ€ true์ด๋ฉด num์„ ๋”ํ•จ
    - signs[idx]๊ฐ€ false์ด๋ฉด num์„ ๋บŒ
  • ์ „์ฒด ํ‘œํ˜„์‹ ์„ค๋ช…
    - foldIndexed ํ•จ์ˆ˜๋Š” ์ฃผ์–ด์ง„ ์ดˆ๊ธฐ๊ฐ’์ธ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ ๋ฐฐ์—ด absolutes์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ๋žŒ๋‹ค ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰
    - ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ˆ„์‚ฐ๊ธฐ์— ๋”ํ•˜์—ฌ ์ตœ์ข…์ ์ธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜
    - ์ตœ์ข…์ ์œผ๋กœ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ’์€ ๋ชจ๋“  ์š”์†Œ์— ๋Œ€ํ•ด acc + if (signs[idx]) num else -num ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ ๊ฒฐ๊ณผ

 

๐Ÿ’ก fold() ํ•จ์ˆ˜

val absolutes = intArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

val sumNumber = absolutes.fold(100) { sum, element ->
    sum + element
}
println(sumNumber) // ์ถœ๋ ฅ๊ฐ’: 155
// ์ดˆ๊ธฐ๊ฐ’ 100์— absolutes ๋ฐฐ์—ด์˜ element ๋“ค์˜ ํ•ฉ 55๊ฐ€ ๋”ํ•ด์ง„ ๊ฐ’ 155๊ฐ€ ์ถœ๋ ฅ
  • ์ฃผ์–ด์ง„ Int๋กœ๋งŒ ๊ตฌ์„ฑ๋œ Array ํ˜น์€ Collection ์˜ ๋ชจ๋“  element๋“ค์„ ์—ฐ์†์ ์œผ๋กœ ๋ถˆ๋Ÿฌ์™€ ๊ณ„์‚ฐ
  • fold ํ•จ์ˆ˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ initial๊ฐ’(์ดˆ๊ธฐ๊ฐ’)์„ ๋ฐ›์Œ

 

๐Ÿ’ก foldIndexed() ํ•จ์ˆ˜

val absolutes = intArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val signs = booleanArrayOf(false, false, true, true, false, false, true, true, true, true)

val sumWithIndex = absolutes.foldIndexed(0) {
    idx, sum, element ->
    println("index::: $idx \nsum::: $sum \nelement::: $element")
    sum + if (signs[idx]) element else -element
}
  • ๋‹ค๋ฅธ ๊ฑด fold()ํ•จ์ˆ˜์™€ ๋˜‘๊ฐ™๊ณ , index๋ฅผ ๊ฐ™์ด ๊ฐ€์ ธ์˜จ๋‹ค๋Š” ๊ฒƒ์ด fold ํ•จ์ˆ˜์™€ ๋‹ค๋ฅธ ์ 

 

 

 

 

๋ฐ˜์‘ํ˜•
 ๐Ÿ’ฌ C O M M E N T