
n개의 음이 아닌 정수들이 있습니다. 이 정수들을 순서를 바꾸지 않고 적절히 더하거나 빼서 타겟 넘버를 만들려고 합니다. 예를 들어 [1, 1, 1, 1, 1]로 숫자 3을 만들려면 다음 다섯 방법을 쓸 수 있습니다.
-1+1+1+1+1 = 3
+1-1+1+1+1 = 3
+1+1-1+1+1 = 3
+1+1+1-1+1 = 3
+1+1+1+1-1 = 3
사용할 수 있는 숫자가 담긴 배열 numbers, 타겟 넘버 target이 매개변수로 주어질 때 숫자를 적절히 더하고 빼서 타겟 넘버를 만드는 방법의 수를 return 하도록 solution 함수를 작성해주세요.
주어지는 숫자의 개수는 2개 이상 20개 이하입니다.
각 숫자는 1 이상 50 이하인 자연수입니다.
타겟 넘버는 1 이상 1000 이하인 자연수입니다.
function solution(numbers, target) {
var answer = 0;
function dfs(index, sum) {
if (index == numbers.length) {
if (sum == target){
answer++;
}
return;
}
dfs(index + 1, sum + numbers[index]);
dfs(index + 1, sum - numbers[index]);
}
dfs(0, 0);
return answer;
}
DFS알고리즘을 이용해 풀었다.
우선 DFS 재귀함수의 탈출 조건으로 index와 numbers.length가 같아지면 리턴하는 조건문을 만들고 해당 조건문 안에서 sum과 target값이 같아지면 answer에 1을 더해주는 조건문을 만든다.
이제 수행동작을 만들어 줘야 하는데 수행동작은
가 된다.
1번 동작의 경우에는 누적 합계에 현재 인덱스 번째 숫자를 더해서 다음 dfs함수를 call해주고,
2번 동작의 경우에는 누적 합계에 현재 인덱스 번째 숫자를 빼서 다음 dfs함수를 call해줬다.
var a = 5;
console.log(a); // 5
var a = 10;
console.log(a); // 10
a = 15;
console.log(a); // 15
function hello(){
var a = 10;
console.log(a);
}
hello(); // 10
console.log(a); //ReferenceError: a is not defined
console.log(a); // undefined :: 변수 선언 이전에 변수 참조 가능
var a // 초기화
console.log(a); // undefined
a = 10 // 할당
console.log(a); // 10
let a = 5;
let a = 10;
cnosole.log(a); // SyntaxError: Identifier 'a' has already been declared
let a = 5;
console.log(a); // 5
a = 10;
console.log(a); // 10
function hello() {
let a = 5;
console.log(a); // 5
}
console.log(a); // ReferenceError: a is not defined
console.log(a); // ReferenceError :: 변수 선언 이전에 변수 참조 불가능
let a // 초기화
console.log(a); // undefined
a = 10 // 할당
consloe.log(a); // 10
const a = 5;
const a = 10;
cnosole.log(a); // SyntaxError: Identifier 'a' has already been declared
ㅡㅡ
const a = 5;
console.log(a); // 5
a = 10;
console.log(a); // TypeError: Assignment to constant variable.
function hello() {
const a = 5;
console.log(a); // 5
}
console.log(a); // ReferenceError: a is not defined
console.log(a); // ReferenceError :: 변수 선언 이전에 변수 참조 불가능
const a // 초기화
console.log(a); // undefined
a = 10 // 할당
consloe.log(a); // 10
| var | let | const | |
|---|---|---|---|
| 중복 선언과 재할당 | 중복 선언과 재할당 모두 o | 중복 선언 x 재할당 o | 중복 선언과 재할당 모두 x |
| 스코프 범위 | 함수 단위 스코프(function-level scope) | 블록 단위 스코프(block-level scope) | 블록 단위 스코프(block-level scope) |
| 호이스팅 | 발생 | 발생하지만 값을 참조할 수 없음 | 발생하지만 값을 참조할 수 없음 |
팀원이 6명인데 어제부터 해서 아직 2명이 접속을 하지 않았다.
일단 Project Convention 등 프로젝트에 관한 내용은 내일 작성해보기로 하고 오늘은 Github Rules, Code Convention 등 개발환경과 그룹 규칙에 대한 것들을 작성했다.