(sort)VoteRigging --a simple problem

VoteRigging http://www.topcoder.com/tc?module=Static&d1=match_editorials&d2=srm388

关键点:favoriteNumber每次+1都是从otherNumbers 最大的那个-1。

package srm388;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class VoteRigging {
	int favoriteNumber;
	List<Integer> otherNumbers = new ArrayList<Integer>();
	public int minimumVotes(int[] votes){
		int res = 0;
		
		if(votes.length == 1){
			return 0;
		}
		
		favoriteNumber = votes[0];
		for(int i=1;i<votes.length;i++){
			otherNumbers.add(votes[i]);
		}
		
		Collections.sort(otherNumbers);
		Collections.reverse(otherNumbers);
		while(otherNumbers.get(0)>=favoriteNumber){
				favoriteNumber += 1;
				res += 1;
				otherNumbers.set(0, otherNumbers.get(0)-1);
				Collections.sort(otherNumbers);
				Collections.reverse(otherNumbers);
		}
		
		return res;
	}
	
	public static void main(String[] args){
		VoteRigging v = new VoteRigging();
		int[] votes ={5, 10, 7, 3, 8};
		System.out.println(v.minimumVotes(votes));
	}
}

你可能感兴趣的:(java)