beeimp
BeeImp
beeimp
전체 방문자
오늘
어제
  • 분류 전체보기 (110)
    • Program_Language (17)
      • Python (13)
      • Go (0)
      • JavaScript (4)
      • TypeScript (0)
      • Rust (0)
      • Solidity (0)
    • OS (8)
      • UNIX&LINUX (7)
      • Windows (0)
      • MacOS (1)
    • Front-End (19)
      • Svelte (19)
      • React (0)
    • Blockchain (6)
      • Bitcoin (0)
      • Ethereum (1)
      • Klaytn (0)
      • Project (5)
    • Data Structure&Algorithm (11)
      • Greedy (7)
      • Dynamic Programming (1)
      • Sort (0)
      • DFS & BFS (2)
      • Recursive (1)
    • Security (0)
      • SDP (0)
      • Authentication (0)
    • Network (3)
      • OpenWrt (0)
      • SDN&NFV (1)
    • Git (5)
    • IT_News (0)
    • 베타 학습단 (12)
      • SQL (12)
    • Project (1)
    • Issues (1)
    • Reviews (3)
    • I Learned (23)
      • TIL (23)
      • WIL (0)
    • Other (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • jenkins
  • sql
  • 기초
  • 블록체인
  • blockchain
  • ubuntu
  • 탐욕법
  • Ethereum
  • mysql
  • greedy
  • Docker
  • Nest.js
  • github
  • svelte
  • PYTHON
  • solidity
  • javascript
  • Git
  • typescript
  • react

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
beeimp

BeeImp

Data Structure&Algorithm/Greedy

배 - [백준 1092번]

2021. 7. 14. 17:45

배 - 백준 1092번

문제

  • 항구에서 배에 화물을 실어야함
  • 모든 화물은 박스에 들어있음
  • 항구에는 크레인 N대, 1분에 크레인당 한개씩만 실을 수 있음
  • 모든 크레인은 동시에 움직임

입력

  • 첫째 줄 - N : 크레인 수
  • 둘째 줄 - 각 크레인의 무게 제한 ( 1,000,000 보다 작거나 같음)
  • 셋째 줄 - M : 박스의 수
  • 넷째 줄 - 각 박스의 무게 ( 1,000,000 보다 작거나 같음)

출력

  • 모든 박스를 배로 옮기는데 드는 시간의 최솟값 출력

코드

import sys

def solution(craneWeightLimits, boxWeights):
  result = 0

  craneWeightLimits.sort(reverse=True) # 내림차순
  boxWeights.sort(reverse=True) # 내림차순

  if craneWeightLimits[0] < boxWeights[0] : # 모든 박스를 배로 옮길 수 없을 때 -1 반환
    return -1

  # 모든 크레인이 동시에 움직일 경우 최솟값
  normal = len(boxWeights) // len(craneWeightLimits)
  normal = normal if len(boxWeights) % len(craneWeightLimits) == 0 else normal + 1
  result = normal

  i, j, count = 0, 0, 0 # craneWeightLimits 인덱스, boxWeights 인덱스, 카운터
  while len(boxWeights) > j: # 전체 박스 반복문
    if count >= result: # 해당 크레인이 할당량을 채울 경우 다음 크레인으로 이동
      count = 0
      i += 1
    if boxWeights[j] > craneWeightLimits[i]: # 박스 무게가 크레인이 들지 못하는 경우. 할당량을 채운 크레인들이 박스를 옮겨줌
      j += i # 할당량 채운 크레인 모두가 박스를 옮김
      result += 1 # 1분 추가
      continue
    count += 1 # 할당량 1 증가

    j += 1 # 다음 박스로 이동

  return result

N = int(sys.stdin.readline())
craneWeightLimits = list(map(int, sys.stdin.readline().split()))
M = int(sys.stdin.readline())
boxWeights = list(map(int, sys.stdin.readline().split()))
print(solution(craneWeightLimits, boxWeights))

'Data Structure&Algorithm > Greedy' 카테고리의 다른 글

전자레인지 - [백준 10162번]  (0) 2021.07.14
세탁소 사장 동혁 - [백준 2720번]  (0) 2021.07.14
신입사원 - [백준 1946번]  (0) 2021.07.14
도서관 - [백준 1461번]  (0) 2021.07.14
강의실 - [백준 1374번]  (0) 2021.07.14
    'Data Structure&Algorithm/Greedy' 카테고리의 다른 글
    • 세탁소 사장 동혁 - [백준 2720번]
    • 신입사원 - [백준 1946번]
    • 도서관 - [백준 1461번]
    • 강의실 - [백준 1374번]

    티스토리툴바