TIL 2023. 6. 14.

TIL #056

오늘 한 것

알고리즘

그리디

  • 프로그래머스 : 체육복
  • const solution = (n, lost, reserve) => { lost = lost.sort((a, b) => a - b); reserve = reserve.sort((a, b) => a - b); lost = lost.filter(v => { const same = reserve.findIndex(resV => resV === v); if(same !== -1) { reserve[same] = null; return false; }; return true; }); lost = lost.map(losV => { const small = reserve.findIndex(v => v === losV - 1); const large = reserve.findIndex(v => v === losV + 1); if(small !== -1) { reserve[small] = null; return null; } else if (large !== -1) { reserve[large] = null; return null; } else { return losV; } }); return n - lost.filter(v => !!v).length; }
  • 프로그래머스 : 큰 수 만들기
  • const solution = (number, k) => { const stack = []; number.split('').forEach(str => { stack.push(str * 1); while(stack[stack.length - 1] > stack[stack.length - 2] && !!k) { k -= 1; stack[stack.length - 2] = stack[stack.length - 1]; stack.pop(); } }); let answer = stack.join(''); if(!!k) answer = answer.slice(0, -k); return answer; }
  • 프로그래머스 : 구명보트
  • const solution = (people, limit) => { people = people.sort((a, b) => b - a); let left = 0; let right = people.length - 1; let count = 0; while(left <= right) { let sum = people[left]; if(people[left] + people[right] <= limit) { right -= 1; } left += 1; count += 1; } return count; }
  • 섬 연결하기
  • 가장 비용이 적은 간선부터 연결해 나가면서, 이미 연결되어 있는 섬은 연결하지 않는다.

기술 블로그

오늘 느낀 점

오늘 몸 상태가 좋지 않다,,, ㅠㅠ.

그래도 큰 수 만들기 문제를 푸는 건 정말 재미있었다. 알고리즘 ㅎㅎ

'TIL' 카테고리의 다른 글

TIL #058  (0) 2023.06.21
TIL #057  (0) 2023.06.15
TIL #055  (0) 2023.06.13
TIL #054  (0) 2023.06.13
TIL #053  (0) 2023.06.06