분류 전체보기
-
프로그래머스 - 짝지어 제거하기Algorithm 2023. 5. 2. 18:08
문제 풀이 풀고자 했던 의도는 입력받은 문자열을 배열로 변환한 뒤 해당 배열을 순회하면서 배열의 idx번째 값과 idx+1 값을 비교해서 같다면 splice(idx, 2)로 제거한 뒤 idx값을 0으로 초기화 하려고 했다. 하지만 예상치 못한 문제가 있었는데, 그것은 코드는 제대로 작동 할지라도 비교할 배열 인덱스를 0으로 초기화하는 덕에 최악의 경우 O(n^2)의 시간복잡도를 가지게 된다는 것이었다. 다른 풀이방법을 모색하던 도중 stack 방식으로 풀면 O(n)의 시간복잡도로 해결할 수 있다는 힌트를 얻어 다시 풀어보았다. 입력받은 문자열을 splitedString 배열로 변환하고, 새로운 빈 answer 배열에다가 하나씩 push하면서 splitedString배열의 요소와 answer배열의 마지막 ..
-
프로그래머스 - 추억점수Algorithm 2023. 4. 27. 18:47
문제 풀이 풀고자 했던 의도는 받은 2차원 photo 배열의 각 배열의 요소들이 name 배열의 요소와 일치하는지 확인, 일치한다면 해당 name 배열의 index값들을 가져와 yearning 배열에서 가져온 index들에 일치하는 값들의 합을 answer배열에 하나씩 대입하는 것이었다. 의도대로 하드코딩을 해본 결과 답은 나왔지만 심히 지저분해서 화가 나지 않을 수가 없는 코드다. 지저분한 코드 줄이기 작업을 해보자... photo 배열의 요소를 name 배열의 요소와 비교해 얻은 인덱스값을 yearning 배열에 대입해 값을 뽑아내는게 키포인트다. reduce와 indexOf 함수를 이용해 photoArray의 요소를 name과 비교하고 그 결과값을 yearning에 바로 대입해주었다. 주목할 포인트..