SW Test/Programmers

[Programmers] Lv2. ์—ฐ์† ๋ถ€๋ถ„ ์ˆ˜์—ด(kotlin)

An effort will never betray ๐Ÿ˜Ž 2022. 10. 22. 18:20
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

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

 

 

 

์˜ˆ์‹œ

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

 

 

 

ํ’€์ด

  • ์›ํ˜•์˜ ๋ชจ์–‘์„ ๊ตณ์ด ์ƒ๊ฐํ•  ํ•„์š” ์—†์ด ํ•˜๋‚˜์˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์—ฐ์†์œผ๋กœ ๋ถ™์ธ ํ›„ ์ฒ˜์Œ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐฏ์ˆ˜๋งŒํผ๋งŒ ๋ถ€๋ถ„ ์ˆ˜์—ด์„ ๋ณด์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
class Solution {
    fun solution(elements: IntArray): Int {
        var answer = mutableSetOf<Int>()
        val list = elements.toList() + elements.toList()
        
        for(i in 1..elements.size){
            for(j in 0..(list.size -i)){
                answer.add(list.subList(j, j+i).sum())
            }
        }
        
        return answer.size
    }
}

 

 

 

์ฐธ๊ณ 

  • ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ธ kotlin์˜ ํŠน์„ฑ์— ๋งž๊ฒŒ ํ•จ์ˆ˜๋ฅผ ๋งŽ์ด ์ตํžˆ์ž ๐Ÿ˜ฝ
  • mutableSetOf๋Š” Java์˜ LinkedHashSet ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. LinkedHashSet์€ LinkedHashMap๊ณผ ์œ ์‚ฌํ•˜๋ฉฐ ์ˆœ์„œ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•ด Double Linked List๋ฅผ ๋ณ„๋„ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
๋ฐ˜์‘ํ˜•