# [2335. Minimum Amount of Time to Fill Cups](https://leetcode.com/problems/minimum-amount-of-time-to-fill-cups/description/) ## 1. 題目說明 - **你需要裝滿指定杯數的==三種水==** - **請找出==最短的==裝水次數,並==回傳==該次數** - **裝水規則:** - **每次==最多==只能裝滿==兩個不同種類的水==** ## 2. 解題思路1 - **最快的裝水方法是,每次都裝指定==數量最多==的==兩杯水==** 1. **每次裝水前先排序,找出最多的兩種水** 2. **執行裝水動作,兩種水==次數都 - 1==** 3. **紀錄==裝水次數 + 1==** 4. **直到所有水裝滿,回傳裝水次數** ### ● 程式碼 ```cpp= void sorting(int* obj) { for (int i = 0; i < 2; i++) { int reg = obj[i]; for (int j = i + 1; j < 3; j++) { if (reg < obj[j]) { obj[i] = obj[j]; obj[j] = reg; reg = obj[i]; } } } } int fillCups(int* amount, int amountSize) { sorting(amount); int ans = 0; while (amount[0] > 0) { if (amount[1]){ ans++; amount[0]--; amount[1]--; sorting(amount); } else{ ans += amount[0]; amount[0] = 0; } } return ans; } ``` ---
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up