πŸ“—
JunegLee's TIL
  • TIL
  • python
    • class
    • String Basic
    • regularExpression
    • String function
    • Generator
    • String format
    • getset
    • module
    • while
    • numpy
    • print()
    • matplotlib
    • for
    • Boolean
    • tuple
    • package
    • input(variable)
    • list
    • if
    • file
    • type()
    • pandas
    • function
    • dictionary
    • ꡬ문 였λ₯˜μ™€ μ˜ˆμ™Έ
    • builtinFunction
    • Constructor
  • algorithm
    • sort
      • mergeSort
      • insertionSort
      • bubbleSort
      • heapSort
      • quickSort
      • selectionSort
    • recursion
    • Greedy
    • DepthFirstSearch
    • basic
      • DataStructure
    • hash
    • BreadthFirstSearch
  • tensorflow
    • keras
      • layers
        • Flatten
        • Flatten
        • Dense
        • Dense
        • Conv2D
        • Conv2D
    • tensorflow1x
    • tensorflow2x
  • DB
    • setting
    • join
    • subQuery
    • overview
  • deep-learning
    • neuralNetwork
    • perceptron
    • neuralNetworkLearning
    • convolution neural network
    • Gradient Descent
    • Linear Regression
    • backPropagation
    • logistic regression
    • overview
  • textPreprocessing
    • overview
  • java
    • basics
      • generic
      • Variable
      • String
    • theory
      • Object Oriented Programing
  • NLP
    • Embedding
    • Natural Language Processing
Powered by GitBook
On this page
  • Greedy
  • 그리디(νƒμš•) μ•Œκ³ λ¦¬μ¦˜ (Greedy)
  • μ΅œμ†Œ μ‹ μž₯ 트리 (MinimumSpanningTree, MST)
  • MST의 νŠΉμ§•
  • 그리디 μ•Œκ³ λ¦¬μ¦˜ 예제(Baek5585)

Was this helpful?

  1. algorithm

Greedy

Greedy

그리디(νƒμš•) μ•Œκ³ λ¦¬μ¦˜ (Greedy)

: κ·Έ μˆœκ°„μˆœκ°„λ§ˆλ‹€ 졜적이라고 μƒκ°λ˜λŠ” 결정을 ν•˜λŠ” λ°©μ‹μœΌλ‘œ μ§„ν–‰ν•˜μ—¬ μ΅œμ’… 해닡에 λ„λ‹¬ν•˜λŠ” 문제 ν•΄κ²° 방식 경우의 μˆ˜κ°€ μ‘΄μž¬ν•  경우, 경우λ₯Ό 선택해야할 λ•Œ μ΅œμ„ μ΄λΌκ³  μƒκ°ν•˜λŠ” 경우λ₯Ό μ„ νƒν•˜λŠ” μ•Œκ³ λ¦¬μ¦˜

  • 그리디 μ•Œκ³ λ¦¬μ¦˜ 예

    : μ§€λΆˆν•΄μ•Ό ν•˜λŠ” 값이 μžˆμ„λ•Œ, λ™μ „μœΌλ‘œ 동전 μˆ˜κ°€ κ°€μž₯ 적게 μ§€λΆˆν•˜λŠ” 방식

μ΅œμ†Œ μ‹ μž₯ 트리 (MinimumSpanningTree, MST)

: μ‚¬μš©λœ κ°„μ„ λ“€μ˜ κ°€μ€‘μΉ˜ 합이 μ΅œμ†ŒμΈ 트리

MST의 νŠΉμ§•

  • κ°„μ„ μ˜ κ°€μ€‘μΉ˜μ˜ 합이 μ΅œμ†Œμ—¬μ•Όν•œλ‹€

  • n개의 정점을 κ°€μ§€λŠ” κ·Έλž˜ν”„μ— λŒ€ν•΄ λ°˜λ“œμ‹œ (n-1)개의 κ°„μ„ λ§Œμ„ μ‚¬μš©ν•΄μ•Ό ν•œλ‹€

  • 사이클이 ν¬ν•¨λ˜μ–΄μ„œλŠ” μ•ˆλœλ‹€

MST의 μ’…λ₯˜ : Greedy, Prim's, Kruskal's

그리디 μ•Œκ³ λ¦¬μ¦˜ 예제(Baek5585)

  • νƒ€λ‘œλŠ” 자주 JOIμž‘ν™”μ μ—μ„œ 물건을 μ‚°λ‹€.

  • JOIμž‘ν™”μ μ—λŠ” μž”λˆμœΌλ‘œ 500μ—”, 100μ—”, 50μ—”, 10μ—”, 5μ—”, 1엔이 μΆ©λΆ„νžˆ 있고,

  • μ–Έμ œλ‚˜ κ±°μŠ€λ¦„λˆ κ°―μˆ˜κ°€ κ°€μž₯ 적게 μž”λˆμ„ μ€€λ‹€. νƒ€λ‘œκ°€ JOIμž‘ν™”μ μ—μ„œ 물건을 사고 μΉ΄μš΄ν„°μ—μ„œ 1000μ—” 지폐λ₯Ό ν•œμž₯ λƒˆμ„ λ•Œ,

  • 받을 μž”λˆμ— ν¬ν•¨λœ μž”λˆμ˜ 갯수λ₯Ό κ΅¬ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜μ‹œμ˜€

// μž…λ ₯ : 380 좜λ ₯ : 4
public class Baek5585 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in); 
        int money = 1000 - sc.nextInt();
        int[] array = { 500, 100, 50, 10, 5, 1 };
        int idx = 0;
        int ans = 0;
        while (money != 0) {
            int change = money / array[idx];
            money -= change * array[idx++];
            ans += change;
        }
        System.out.println(ans);
    }
}
a = 1000 - int(input())
b = [500, 100, 50, 10, 5, 1]
count = 0
for i in b:
    count += a // i
    a %= i
print(count)
PreviousrecursionNextDepthFirstSearch

Last updated 3 years ago

Was this helpful?