Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- nodejs mongodb
- mongo-native
- node.js 연동
- MacOS
- homebrew
- 파이썬3
- Windows10
- console.log
- util.inspect
- MYSQL
- mongoDB [Object]
- collection.find
- [Object]
- mongodb nodejs driver
- Jupyter notebook
- pip jupyter
- node.js설치
- 맥에 파이썬 설치
- Projection
- Node.js
- 맥
- Installation
- python3
- mongodb
- query
Archives
- Today
- Total
Bon Voyage
TopCoder 문제로 연습해보는 알고리즘: 초급 (1) 시뮬레이션 본문
TopCoder 알고리즘 트레이닝
https://book.naver.com/bookdb/book_detail.nhn?bid=7333164
TopCoder 알고리즘 트레이닝
프로그래밍 실력은 하루 아침에 완성되지 않는다 프로그래밍 콘테스트 탑코더를 대비서. 프로그래밍의 기본인 알고리즘 실력을 탄탄하게 다질 수 있도록 안내한다. 수학 공식을 많이 알아도 문제를 풀어보며 연습하지 않으면 안 되는 것처럼, 알고리즘 역시 연습을 통해 문제 해결력을 키워야 한다. 이 책은 전 세계 프로그래머와 경쟁하는 탑코더에서 최고 등급인 레드 코더가 되도록 이끌어 줄 것이다.
book.naver.com
4장 시뮬레이션 부분의 문제를 정리해 본 내용입니다.
KiwiJuice
Problem
N bottles
capacities[0] ~ capacities[N-1]
bottles[0] ~ bottles[N-1]
redistribution
fromId[0] ~ fromId[M-1]
toId[0]~ toId[M-1]
-> fromId empty or toId full
return int[] of bottles[]
Example Data
0)
capacities = {20, 20}
bottles = {5, 8}
fromId = {0}
toId = {1}
Returns: {0, 13}
1)
capacities = {10, 10}
bottles = {5, 8}
fromId = {0}
toId = {1}
Returns: {3, 10}
2)
capacities = {30, 20, 10}
bottles = {10, 5, 5}
fromId = {0, 1, 2}
toId = {1, 2, 0}
Returns: {10, 10, 0}
3)
capacities = {14, 35, 86, 58, 25, 62}
bottles = {6, 34, 27, 38, 9, 60}
fromId = {1, 2, 4, 5, 3, 3, 1, 0}
toId = {0, 1, 2, 4, 2, 5, 3, 1}
Returns: {0, 14, 65, 35, 25, 35}
Solve
해결 방법
1. fromId\[i\]와 toId\[i\]의 총량은 일정하다
\- int sum = bottles\[fromId\[i\]\] + bottles\[toId\[i\]\]
2. 재분배 후 toId\[i\]는 꽉 차거나 fromId\[i\]의 양을 더한 총량이다
\- bottles\[toId\[i\]\] = capcities\[toId\[i\]\] or sum
3. fromId\[i\]에는 재분배 후 총량에서 toId\[i\]의 양을 제외한 만큼만 가지게 된다
\- bottles\[fromId\[i\]\] = sum - bottles\[toId\[i\]\]
자바 코드
public class KiwiJuice{
public static int[] thePouring
(int[] capacities, int[] bottles, int[] fromId, int[] toId){
int sum = 0;
for (int i=0; i<fromId.length; i++){
sum = bottles[fromId[i]]+bottles[toId[i]];
bottles[toId[i]] = Math.min(capacities[toId[i]], sum);
bottles[fromId[i]] = sum - bottles[toId[i]];
}
return bottles;
}
}
'개념 공부 > 알고리즘' 카테고리의 다른 글
TopCoder 문제로 연습해보는 알고리즘: 중급 (1) 동적 계획법과 메모화 (0) | 2019.10.11 |
---|---|
카탈랑 수 Catalan Number (0) | 2019.10.11 |
TopCoder 문제로 연습해보는 알고리즘: 초급 (2) 전체 탐색 (2/2) (0) | 2019.10.08 |
TopCoder 문제로 연습해보는 알고리즘: 초급 (2) 전체 탐색 (1/2) (0) | 2019.10.08 |
Comments