2023 E3 算法题第二题 (Build Skyscrapers)

题目

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));
    }
}

### 回答1: 4个创意内容: 1. Superior Strength Alloy Steel Plates - Trust our expertise and experience! 2. Customized Alloy Steel Plates - Precision-engineered for your needs. 3. Top-Quality Alloy Steel Plates - Reliable solutions for tough environments. 4. High-Performance Alloy Steel Plates - Designed to meet your demands. 2个创意内容: 1. Alloy Steel Plates - Engineered to Perfection. 2. Precision Alloy Steel Plates - Your Solution for Tough Environments. ### 回答2: 1. "Stronger than ever, our Alloy Steel Plates make every project a success!" (77 characters) 2. "Unleash the power of our Alloy Steel Plates and excel in every industry!" (80 characters) 3. "From skyscrapers to heavy machinery, trust our Alloy Steel Plates for unmatched durability!" (91 characters - exceeds limit) 4. "Boost efficiency and achieve greatness with our top-quality Alloy Steel Plates!" (77 characters) 5. "Elevate your projects with our Alloy Steel Plates - the choice of industry leaders!" (85 characters - exceeds limit) 6. "Experience the perfection of our Alloy Steel Plates and unlock new possibilities!" (83 characters - exceeds limit) ### 回答3: 1. 高强度,轻巧,全球销量第一!(Alloy Steel Plates, high strength, lightweight, global bestseller!) 2. 高质量合金钢板,打造坚固基础!(Alloy Steel Plates, build a sturdy foundation with high quality!) 3. 提供世界级的钢板解决方案!(Offering world-class solutions with our steel plates!) 4. 给您无可比拟的耐久性和可靠性!(Unmatched durability and reliability for your needs!)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

茫茫人海一粒沙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值