Greedy选编2
502. IPO
1 | //not clever enough! |
1 | class Solution { |
135. Candy
1 | public int candy(int[] ratings) { |
2141. Maximum Running Time of N Computers
法一:sorting
思路:
- use up biggest batteries first
- spare the extra to smallest battery, raise them as much as possible
- if still has extra, spare them to all batteries
1 | public long maxRunTime(int n, int[] batteries) { |
法二:binary_search
思路:
- find max
- how to validate:
- a large battery cannot be fully utilized, because it cannot serve 2 computers simultaneously
- a small battery can be fully utilized
- only need to check total power>=target or not
1 | public long maxRunTime(int n, int[] batteries) { |
2808. Minimum Seconds to Equalize a Circular Array
1 | public int minimumSeconds(List<Integer> nums) { |
2551. Put Marbles in Bags
1 | //k=1: 0,n-1 |
2745. Construct the Longest New String
brainteaser
1 | public int longestString(int x, int y, int z) { |
1199. Minimum Time to Build Blocks
huffman code:
- construct the optimal tree
- always merge the 2 smallest node
1 | public int minBuildTime(int[] blocks, int split) { |
1727. Largest Submatrix With Rearrangements
1 | public int largestSubmatrix(int[][] matrix) { |