【Lintcode】1736. Throw Garbage
題目地址:
https://www.lintcode.com/problem/throw-garbage/description
給定一個浮點型長 n n n陣列 A A A,題目保證每個數的範圍是 [ 1.01 , 3.00 ] [1.01,3.00] [1.01,3.00]。每次可以挑若干數使得和小於等於 3 3 3,問最少多少次挑完。
思路是貪心 + 雙指標。可以看出每次最多挑兩個數。先對 A A A排序,開左右兩個指標 l l l和 r r r分別賦值為 0 0 0和 n − 1 n-1 n−1,維護區間 [ l , r ] [l,r] [l,r]為接下來要選的數。考慮 A [ r ] A[r] A[r],如果 A [ l ] + A [ r ] > 3 A[l]+A[r]>3 A[l]+A[r]>3,那 A [ r ] A[r] A[r]只能單獨挑了;否則的話可以將 A [ l ] A[l] A[l]和 A [ r ] A[r] A[r]都挑出來。挑完了之後計數加 1 1 1,接著繼續迴圈考慮剩餘區間。演算法證明可以用數學歸納法。程式碼如下:
import java.util.Arrays;
public class Solution {
/**
* @param A: the weight of all garbage bags.
* @return: an integer represent the minimum number of times.
*/
public int Count_ThrowTimes(float[] A) {
// Write your code here.
// 先排序
Arrays.sort(A);
int res = 0, l = 0, r = A.length - 1;
float sum = 0;
// 先只考慮挑多於1個數的情況
while (l < r) {
sum = A[l] + A[r];
// 如果挑的兩個數之和大於3了,那隻能A[r]自成一個
if (sum > 3) {
r--;
res++;
} else {
// 如果兩個數之和小於等於3,則就挑掉這兩個
r--;
l++;
// 更新計數
res++;
}
}
// 如果還剩一個數,則還需要另外加1
return res + (l == r ? 1 : 0);
}
}
時間複雜度 O ( n log n ) O(n\log n) O(nlogn),空間 O ( 1 ) O(1) O(1)。
相關文章
- [Javascript] garbage collectionJavaScript
- throw關鍵字
- throw er; // Unhandled 'error' eventError
- python ref counting based garbage collectionPython
- throw和throws的區別
- Java異常十一:使用throw丟擲異常物件;throw和throws的區別Java物件
- [Memory leak] 3. Garbage collection in Closure
- C語言垃圾回收(Garbage Collection)C語言
- [LintCode] Daily TemperaturesAI
- [LintCode] Permutation in String
- (十四).try-throw-catch機制
- Solon 用 throw 丟擲資料
- They just wanted to find small ways to throw people
- 深入理解Java中的Garbage CollectionJava
- Java的垃圾回收(Garbage Collection)機制Java
- [LintCode/LeetCode] Meeting RoomsLeetCodeOOM
- Lintcode 1263. Is Subsequence
- 【Lintcode】1189. Minesweeper
- try throw catch 語句檢測input值
- c++中的try-catch及throwC++
- Java基礎:throw和throws的詳解Java
- [LeetCode/LintCode] Largest Palindrome ProductLeetCode
- [LintCode/LeetCode] Contains Duplicate IIILeetCodeAI
- [LintCode] Check Full Binary Tree
- [LintCode/LeetCode] Remove Duplicate LettersLeetCodeREM
- [LintCode] 3Sum Smaller
- 【Lintcode】1615. The Result of Investment
- [LintCode] Binary Tree Level Order
- 【Lintcode】1665. Calculate Number
- 【Lintcode】1789. Distinguish UsernameNGUI
- 【Lintcode】1562. Number of RestaurantsREST
- 【Lintcode】576. Split Array
- 【Lintcode】1267. Lexicographical Numbers
- 【Lintcode】141. Sqrt(x)
- 【Lintcode】1415. Residual Product
- 【Lintcode】1230. Assign CookiesCookie
- 【Lintcode】1732. Snakes and Ladders
- 【Lintcode】1218. Number Complement