2021/04 3

블록 레벨의 유효 범위(Scope)에 대하여

const는 블록 레벨 스코프이고, 재할당이 안된다. 하지만 아래와 같이 하위 블록에서 동일한 이름으로 선언하고, 값을 할당할 수 있다. Case 1 : 상위 블록과 하위 블록에 동일한 이름의 변수가 선언된 경우 function try1() { const a = "cocoa"; console.log("[try1] before", a); // cocoa if (true) { console.log("[if] before", a); // undefined const a = "leaf"; console.log("[if] after", a); // leaf } console.log("[try1] after", a); // cocoa } try1(); 그리고 그 블록에서는 상위 블록의 동일한 이름을 가진 변수는 참..

[HackerRank] Array Manipulation 풀이

문제 입력 값은 행렬의 크기(n*m)와, 쿼리(query, 여기서는 db 쿼리가 아니라 "연산"을 의미한다.)들로 이루어져있는데, 쿼리의 의미는 a = 시작 인덱스, b = 종료 인덱스, k = 값이다. 각 쿼리는 시작 인덱스 ~ 종료 인덱스 사이의 배열 요소에 값을 더하는 작업을 수행한다. 모든 작업을 수행하고 배열의 최대 값을 반환하면 된다. n m a, b, k a, b, k a, b, k ... www.hackerrank.com/challenges/crush/problem Array Manipulation | HackerRank Perform m operations on an array and print the maximum of the values. www.hackerrank.com 풀이 과정 ..

[HackerRank] Minimum Swaps 2 풀이

문제 You are given an unordered array consisting of consecutive integers [1, 2, 3, ..., n] without any duplicates. You are allowed to swap any two elements. Find the minimum number of swaps required to sort the array in ascending order. www.hackerrank.com/challenges/minimum-swaps-2 풀이 과정 1. 문제 해석 오류 : 집합 기호와 영어 해석이 잘못됐다. 배열이 1 부터 n까지의 원소라는 의미인데, 1 부터 포함되어야한다는 의미를 놓치고 말았다. n~m에 해당하는 케이스도 정렬하겠다고 열심히..