// Process
// 1. Input
// 2. Sort people in ascending way
// 3. Iterate till front and rear index are met
// 3.1. Check if front and rear index value can take the same boat
// 3.1.1. If so -> move both indices
// 3.1.2. If not -> move rear index
// 3.2. ++boatCount
// 4. Return boatCount
import java.util.*;
class Solution {
public int solution(int[] people, int limit) {
int boatCount = 0;
int frontIndex = 0;
int rearIndex = people.length-1;
// 2.
// 내부라이브러리 (오름차순만 됨)
Arrays.sort(people);
// Selection sort (내림차순이 라이브러리 안돼서 그냥 만들었었음)
// for (int i = 0; i < people.length-1; ++i) {
// int maxIndex = i;
// int max = people[i];
// for (int j = i+1; j < people.length; ++j) {
// if (max < people[j]) {
// max = people[j];
// maxIndex = j;
// }
// }
// int temp = people[i];
// people[i] = max;
// people[maxIndex] = temp;
// }
// 3.
while (frontIndex <= rearIndex) {
if (people[frontIndex] + people[rearIndex] <= limit) {
++frontIndex;
--rearIndex;
} else {
--rearIndex;
}
++boatCount;
}
return boatCount;
}
}
탐욕법 03 - 큰수만들기, 프로그래머스, 알고리즘, Programmers, Stack, Queue, Hash, 코딩테스트, Algorithm,
// 문제에서 예를 들 때 전체 조합을 나열하고 가장 큰 것을 골라서 순서가 변해도 상관 없을 것 같으나, // 실제로 답은 순서는 건드리지 않고 찾아나가야함. // Process // 1. Input
itdar.tistory.com
스택/큐 02 - 주식가격, 프로그래머스, 알고리즘, Programmers, Stack, Queue, Hash, 코딩테스트, Algorithm, 풀
# Process (list) # 1. Input prices list # 2. Iterate i from begin to the end # 2.1. Iterate j from i to the end # 2.1.1. Check if [j] is smaller than [i] # ..
itdar.tistory.com
LeetCode #888 FairCandySwap. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래
LeetCode #888 FairCandySwap. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techinterview,coding,코딩인터뷰,..
itdar.tistory.com
CodeSignal Intro Databases #2 CountriesSelection. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드
CodeSignal Intro Databases #2 CountriesSelection. Algorithm,알고리즘,LeetCode,Codefights,CodeSignal,코드파이트,코드시그널,예제,그래프,Graph,example,c++,java,재귀,recursive,datastructure,techintervie..
itdar.tistory.com