핵심 아이디어: "지금 당장 가장 좋아 보이는 것을 선택한다."
매 단계마다 그 순간 가장 최적인(local optimum) 선택을 반복하여 전체적으로도 최적인(global optimum) 해답을 기대하는 방식
적용 분야:
최적화 문제(optimization problem)에서 종종 사용.
단순히 해결책이 아닌 최적의 해결책을 찾는 데 목적이 있다.
지역 최적해(Local Optimum)에 빠질 수 있음: 탐욕적으로 선택한 해가 전체 문제의 최적해가 아닐 수 있습니다.
이 과정을 반복하여 전체 해를 구성
결과적으로 최적해를 보장하지 못함
탐욕 알고리즘은 간단하고 빠르지만, 항상 최적의 해를 보장하지는 않는다. 적절한 문제에만 사용해야 하며, 적용 전 문제의 구조를 충분히 이해하는 것이 중요하다.
Problem Description
You are given a set of ropes, each with a different maximum weight capacity. The objective is to determine the maximum weight of an object that can be lifted using any subset of these ropes.
Each rope in a selected subset must evenly share the weight of the object. This means that if k ropes are used to lift an object of weight w, then each rope must endure w/k. However, no rope can endure more than its maximum capacity.
Example
Given ropes = [10, 20, 20], let's analyze why the maximum weight is 40:
API
public int maxWeight(int[] ropes)
Input:
ropes: An integer array representing the maximum weight each rope can endure.
Output:
An integer representing the maximum weight that can be lifted using a subset of ropes.
Constraints:
1 ≤ ropes.length ≤ 100, 1 ≤ ropes[i] ≤ 1000
Main method for Test
public class Test {
public static void main(String[] args) {
Itm itm = new Itm(); // Create an instance of the Itm class
// Define an integer array containing the maximum weight each rope can endure
int[] ropes = {10, 20, 20};
// Compute the maximum weight that can be lifted
int result = itm.maxWeight(ropes);
// Print the result, expected output is 40
System.out.println("Maximum weight that can be lifted: " + result);
}
}