목록Greedy (20)
N
https://school.programmers.co.kr/learn/courses/30/lessons/161989 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 문제를 단순하게 접근하면 된다. section에 있는 범위만 덧칠하면 되기 때문에 greedy하게 접근한다. 우선 start 변수를 선언하여 section의 마지막 원소를 가져온다. 한번 덧칠하는 단계이기 때문에 answer = 1이며, 이 start에서 m 만큼 이동한다. while문으로 section을 비울 때까지 반복 현재 위치를 section.pop()을 이용해 가져오며, 만약 curre..
https://school.programmers.co.kr/learn/courses/30/lessons/150369 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr greedy 알고리즘으로 접근. 택배 배달과 수거를 앞에서가 아닌 뒤에서부터 진행을 하면 된다. deveriesIndex와 pickupsIndex를 선언하여 n - 1로 저장하고, 이 두 변수를 이용해서 더 큰 값이 사실상 출발 위치가 된다. while문을 이용해 두 인덱스가 모든 배열을 순회한 -1 값이 될 때까지 반복한다. (뒤에서부터 순회했기 때문에 n - 1 ~ 0 으로 간다.) deli..
https://leetcode.com/problems/candy/ Candy - 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 var candy = function(ratings) { const { length } = ratings; const fromLeft = Array.from({length}, () => 1); const fromRight = Array.from({length}, () => 1); let answer = 0; for(let i = 1; i..
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/can-place-flowers/ Can Place Flowers - 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 그리디 알고리즘. n개의 꽃을 flowerbed에 심어야 하는데, 근접한 위치에 꽃이 없어야 한다. for문을 이용해 flowerbed를 순회. 만약 현재 n이 0이라면 모두 심었기 때문에 바로 true를 리턴한다. 그렇지 않고 n이 남아있다면 최대한 꽃을 심어본다. 맨 앞에서부터 꽃을 심으면 ..
https://leetcode.com/problems/assign-cookies/ Assign Cookies - 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 그리드 알고리즘 주어진 두 배열을 오름차순으로 정렬하여 시작. 이중 for문을 적용. sIndex를 0으로 시작하여, for문을 돌면서 sIndex가 s.length와 같아지면 바로 종료할 수 있도록 한다. g와 s를 순회하면서 g[i] a - b); s.sort((a, b) => a - b); for(l..
https://leetcode.com/problems/find-original-array-from-doubled-array/ Find Original Array From Doubled 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 Greedy 알고리즘 적용. changed 배열을 오름차순으로 정렬한 후 순회를 한다. 순회를 하면서 number배열에 changed[i]의 갯수를 카운팅한다. 다음 for문으로 다시 changed를 순회한다. 현재 ch..
134. Gas Station There are n gas stations along a circular route, where the amount of gas at the ith station is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from the ith station to its next (i + 1)th station. You begin the journey with an empty tank at one of the gas stations. Given two integer arrays gas and cost, return the starting gas station's inde..