-
[프로그래머스 level.1] 06.19알고리즘 2023. 6. 19. 11:50
최소직사각형
더보기https://school.programmers.co.kr/learn/courses/30/lessons/86491?language=javascript
내가 쓴 코드
function solution(sizes) { let width = []; let height = []; for(let i = 0; i < sizes.length; i++) { const max = Math.max(sizes[i][0], sizes[i][1]); const min = Math.min(sizes[i][0], sizes[i][1]); width.push(max); height.push(min) } const answer = Math.max(...width) * Math.max(...height) return answer }
처음에는 정렬을 해서 첫번째 인덱스와 두번째 인덱스를 구성하는 리스트를 만들고 크고 작은 수를 뽑아내려했는데 생각해보니까 숫자가 2개여서 정렬하는데 Math.max && min을 쓰니깐 그냥 되는것을 깨닫고 처리를 하였다
3진법 뒤집기
https://school.programmers.co.kr/learn/courses/30/lessons/68935
내가 쓴 코드
function solution(n) { var answer = 0; n = n.toString(3); n = [...n].reverse().join("") n = parseInt(n, 3); return n; }
10진법 -> 3진법 n.toString(3); 3진법 -> 10진법 parseInt(n, 3);
같은 숫자는 싫어
https://school.programmers.co.kr/learn/courses/30/lessons/12906?language=javascript
내가 쓴 코드
function solution(arr){ var answer = []; for(var i = 0; i < arr.length; i++){ if(arr[i] !== arr[i + 1]){ answer.push(arr[i]); } } return answer; }
일일히 전개를 하면서 풀었는데 생각해보니 map과 filter를 비롯해서 index값을 가지고 있는 녀석들이 있었다 이녀석들을 이용하니깐 밑에처럼 엄청 간단하게 결과가 나왔다 충격... ㄷㄷ
function solution(arr){ return arr.filter((val,index) => val != arr[index+1]); }
'알고리즘' 카테고리의 다른 글
[프로그래머스 level.1] filter , includes (0) 2023.06.20 [프로그래머스 level.1] 이중반복문, 중복제거, 정렬 (0) 2023.06.20 알고리즘 Day-1 (0) 2023.06.16 알고리즘 & 자료구조 스터디(10조) 8일차 (0) 2023.05.30 알고리즘 & 자료구조 스터디(10조) 7일차 (0) 2023.05.29