题目
Task 2 You are given an array A of N integers, representing the maximum heights of N skyscrapers to be built. Your task is to specify the actual heights of the skyscrapers, given that: The height of the K-th skyscraper should be positive and not bigger than A[K]; No two skyscrapers should be of the same height; The total sum of the skyscrapers’ heights should be the maximum possible. Write a function: class Solution { public int[] solution(int[] A); } 1 that, given an array A of N integers, returns an array B of N integers where B[K] is the assigned height of the K-th skyscraper satisfying the above conditions. If there are several possible answers, the function may return any of them. You may assume that it is always possible to build all skyscrapers while fulfilling all the requirements. Examples: Given A = [1, 2, 3], your function should return [1, 2, 3], as all of the skyscrapers may be built to their maximum height. Given A = [9, 4, 3, 7, 7], your function may return [9, 4, 3, 7, 6]. Note that [9, 4, 3, 6, 7] is also a valid answer. It is not possible for the last two skyscrapers to have the same height. The height of one of them should be 7 and the other should be 6. Given A = [2, 5, 4, 5, 5], your function should return [1, 2, 3, 4, 5]. Write an efficient algorithm for the following assumptions: N is an integer within the range [1…50,000]; Each element of array A is an integer within the range [1…1,000,000,000]; There is always a solution for that given input.
解法1
思路
代码
public class BuildSkyscrapers1 {
public int[] solutionTimeOut(int[] A) {
Map<Integer, Integer> resultMap = new HashMap();
for (int i = 0; i < A.length; i++) {
int tmp = A[i];
while (resultMap.containsKey(tmp)) {
tmp--;
}
resultMap.put(tmp, i);
}
// System.out.println(resultMap); //{3=2, 4=1, 6=4, 7=3, 9=0}
int[] result = new int[A.length];
Iterator iterator = resultMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Integer, Integer> entry = (Map.Entry<Integer, Integer>) iterator.next();
result[entry.getValue()] = entry.getKey();
}
return result;
}
public static void main(String[] args) {
BuildSkyscrapers1 buildSkyscrapers = new BuildSkyscrapers1();
// Test cases
int[] test1 = {1, 2, 3};
int[] result1 = buildSkyscrapers.solutionTimeOut(test1);
// [1, 2, 3],
System.out.println("Test 1: " + Arrays.toString(result1));
int[] test2 = {9, 4, 3, 7, 7};
int[] result2 = buildSkyscrapers.solutionTimeOut(test2);
// [9, 4, 3, 7, 6]. or [9, 4, 3, 6, 7]
System.out.println("Test 2: " + Arrays.toString(result2));
int[] test3 = {2, 5, 4, 5, 5};
int[] result3 = buildSkyscrapers.solutionTimeOut(test3);
// [1, 2, 3, 4, 5].
System.out.println("Test 3: " + Arrays.toString(result3));
int[] test4 = {5, 4, 3, 2, 1};
int[] result4 = buildSkyscrapers.solutionTimeOut(test4);
// [1, 2, 3, 4, 5].
System.out.println("Test 3: " + Arrays.toString(result4));
}
}
解法2
思路
代码
class BuildSkyscrapers {
public int[] solution(int[] A){
int[] results = new int[A.length];
Set<Integer> noDuplicatedSet = new HashSet<>();
for (int i =0; i < A.length; i ++){
Integer number = this.findNoDuplicatedNumber(noDuplicatedSet, A[i]);
if (number==0){
i = i -2;
A[i-1] = A[i-1] -1;
noDuplicatedSet.remove(A[i-1]);
}else{
results[i] = number;
}
}
return results;
}
public int findNoDuplicatedNumber(Set<Integer> noDuplicatedSet, Integer number){
Integer previousSize = noDuplicatedSet.size();
noDuplicatedSet.add(number);
Integer currentSize = noDuplicatedSet.size();
if (previousSize== currentSize){
number = number -1;
if (number <1){
return 0;
}else{
return findNoDuplicatedNumber(noDuplicatedSet, number);
}
}else{
return number;
}
}
public static void main(String[] args) {
BuildSkyscrapers buildSkyscrapers = new BuildSkyscrapers();
// Test cases
int[] test1 = {1, 2, 3};
int[] result1 = buildSkyscrapers.solution(test1);
// [1, 2, 3],
System.out.println("Test 1: " + Arrays.toString(result1));
int[] test2 = {9, 4, 3, 7, 7};
int[] result2 = buildSkyscrapers.solution(test2);
// [9, 4, 3, 7, 6]. or [9, 4, 3, 6, 7]
System.out.println("Test 2: " + Arrays.toString(result2));
int[] test3 = {2, 5, 4, 5, 5};
int[] result3 = buildSkyscrapers.solution(test3);
// [1, 2, 3, 4, 5].
System.out.println("Test 3: " + Arrays.toString(result3));
int[] test4 = {5, 4, 3, 2, 1};
int[] result4 = buildSkyscrapers.solution(test4);
// [1, 2, 3, 4, 5].
System.out.println("Test 3: " + Arrays.toString(result4));
}
}