Developing Myself Everyday
article thumbnail
Published 2022. 12. 3. 20:14
삼총사 프로그래머스 - kotlin/LEVEL 1

문제 출처: https://school.programmers.co.kr/learn/courses/30/lessons/92341?language=kotlin

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

문제

나의 풀이

정수가 몇가지 주어졌을때 그중에서 3개를 고르고 그 수들의 합이 0이 되면 삼총사가 되는 간단한 조합 문제이다.

 

순열 (Permutation) & 조합 (Combination)

순열이란? 수학에서 순열(Permutation) 또는 치환은 순서가 부여된 임의의 집합을 다른 순서로 뒤섞는 연산이다. 즉, 순열은 정의역과 공역이 같은 일대일 대응이다. n개의 원소의 순서를 뒤섞는 순

everyday-develop-myself.tistory.com

이전에 정리 해놓았던 게시글을 참고해서 쉽게 구현했다.

class Solution {
    var num = ArrayList<Int>()
    val pickednum = mutableListOf<Int>()
    var count = 0
    fun solution(number: IntArray): Int {
        for(i in number.indices) num.add(number[i])
        return combination(0, 3, 0)
    }
    
    fun combination(cnt: Int, depth: Int, beginWith: Int): Int{
        if(cnt == depth) {
            if(pickednum.sum() == 0) {
                println(pickednum)
                count++ 
            }
        }
        for(i in beginWith until num.size) {
            pickednum.add(num[i])
            combination(cnt + 1, depth, i + 1)
            pickednum.removeAt(pickednum.lastIndex)
        }
        return count
    }
}

'프로그래머스 - kotlin > LEVEL 1' 카테고리의 다른 글

개인정보 수집 유효기간 - Kotlin  (0) 2023.05.24
콜라 문제  (0) 2022.12.04
profile

Developing Myself Everyday

@배준형

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!