목록js (166)
N
https://leetcode.com/problems/maximum-units-on-a-truck/ Maximum Units on a Truck - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 정렬을 활용한 문제. 주어진 boxTypes 배열을 정렬하는데, boxTypes[1] 번째 요소의 내림차순으로 정렬을 해준다. 이후 for문을 이용해 truckSize가 채워지는 만큼 박스를 트럭에 채운다. 만약 truckSize - num이 0 이상이라면 모든 박스를 ..
https://leetcode.com/problems/furthest-building-you-can-reach/ Furthest Building You Can Reach - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 최소힙을 이용한 문제풀이. 벽돌과 사다리를 이용해 가장 멀리 갈 수 있는 빌딩의 인덱스를 리턴하면 된다. pq라는 이름으로 우선순위 큐(최소 힙)을 선언한다. for문으로 1부터 heights의 길이만큼 순회. 현재 빌딩의 높이 - 이전 빌딩의 ..
https://leetcode.com/problems/non-decreasing-array/ Non-decreasing Array - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 주어진 nums 배열을 최대 1개의 원소만 바꿔서 오름차순 정렬이 가능한지 확인하는 문제. 변경 여부를 확인할 수 있는 flag인 isModified = false로 선언한다. for문으로 nums를 순회하는데, 인덱스는 1부터 시작한다. 만약 현재 방문한 원소가 이전 원소보다 값이 ..
https://programmers.co.kr/learn/courses/30/lessons/42892?language=javascript 코딩테스트 연습 - 길 찾기 게임 [[5,3],[11,5],[13,3],[3,5],[6,1],[1,3],[8,6],[7,2],[2,2]] [[7,4,6,9,1,8,5,2,3],[9,6,5,8,1,4,3,2,7]] programmers.co.kr 이진 트리를 구성해 전위 순회와 후위순회를 한 결과를 리턴하는 문제. Node 라는 클래스를 정의하여 x, y, num, left, right를 가질 수 있도록 하였다. 이를 이용해 주어진 nodeinfo를 순회하여 노드를 만들어준다. node 라는 배열에 Node 클래스를 가진 node가 있으며, x와 y 좌표를 주어진 조건에..
https://leetcode.com/problems/delete-operation-for-two-strings/ Delete Operation for Two Strings - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com LCS와 DP 를 이용한 문제풀이. 우선 주어진 두 문자열이 같다면 지울 필요가 없기 때문에 0을 바로 리턴한다. 그렇지 않다면 LCS를 확인. 두 문자열 중 길이가 더 긴 문자를 word1에 저장한다. 변수는 word1의 길이와 word2의..
https://leetcode.com/problems/maximum-erasure-value/ Maximum Erasure Value - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com Set과 Two pointer를 활용한 문제 풀이. sliding window 구조 형태로 동작. for문으로 nums를 순회한다. 순회할 시 left, right 변수를 이용한다. 두 변수는 sliding window의 범위를 조절하는 역할을 한다. 만약 obj에서 nums[ri..
https://programmers.co.kr/learn/courses/30/lessons/12927?language=javascript 코딩테스트 연습 - 야근 지수 회사원 Demi는 가끔은 야근을 하는데요, 야근을 하면 야근 피로도가 쌓입니다. 야근 피로도는 야근을 시작한 시점에서 남은 일의 작업량을 제곱하여 더한 값입니다. Demi는 N시간 동안 야근 피로도 programmers.co.kr 최대힙(max heap)을 이용한 문제 풀이. 최대힙을 클래스로 구현하고, 이 힙에 works의 요소를 하나씩 push한다. push하게 되면 알아서 최대힙으로 정렬이 된다. 이후 n만큼 반복하면서 힙을 순회한다. pop()을 하게 되면 가장 큰 수가 앞에 나오게 되며, 해당 일이 0이 아닌 경우 -1을 하여 다..
https://leetcode.com/problems/hamming-distance/ Hamming Distance - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 주어진 x와 y를 이진수로 바꿔서 각 자릿수를 비교했을 때 다른 비트의 수를 구하면 된다. 먼저 x를 이진수로 바꾼 값을 저장할 배열 xArr과 y를 이진수로 바꿔 저장할 배열 yArr을 선언한다. 그리고 리턴할 answer = 0. divide라는 함수를 이용해 인자로 들어온 n을 이진수로 만들어..