주녘공부일지

[프로그래머스 C#] Lv.2 타겟 넘버 본문

Programmers - C#/CodingTest Lv.2

[프로그래머스 C#] Lv.2 타겟 넘버

주녘 2023. 9. 5. 14:48
728x90

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

 

프로그래머스

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

programmers.co.kr

1. 정답코드 및 핵심 아이디어, 유의사항

- 배열 안의 부호를 변경할 수 있는 모든 경우의 수를 체크해야 함 // 배열은 참조형식 자료형

- DFS(Depth First Search) : 깊이우선탐색

https://godgjwnsgur7.tistory.com/47

 

[C#] DFS(Depth First Search), BFS(Breadth First Search)

1. DFS(Depth First Search) - 깊이 우선 탐색 조합류 최단거리로 갈 수 있는 경로의 수, 목적지 도달 여부, 등에 사용 - 스택(LIFO) 사용, 재귀 함수 사용 // 스택을 이용 public void DFS(int index) { Node root = nodes[

godgjwnsgur7.tistory.com

    using System;

    public class Solution
    {
        public int count = 0;

        public int solution(int[] numbers, int target)
        {
            Function(numbers, target, -1);

            return count;
        }

        public void Function(int[] numbers, int target, int index)
        {
            index++;
            
            if (numbers.Length == index)
            {
                int total = 0;
                foreach (int num in numbers)
                    total += num;

                if (total == target)
                    count++;

                return;
            }
            
            Function(numbers, target, index);
            numbers[index] *= -1;
            Function(numbers, target, index);
        }
    }
728x90