高频刷题一

题目1

给定数组hard和money,长度都为N,hard[i]表示i号的难度, money[i]表示i号工作的收入。

给定数组ability,长度都为M, ability[j]表示j号人的能力,每一号工作,都可以提供无数的岗位,难度和收入都一样,但是人的能力必须>=这份工作的难度,才能上班返回一个长度为M的数组ans,ans[j]表示j号人能获得的最好收入

数组hard:        [7,  1, 1, 3]

数组money:    [13, 5, 9, 6]

将hard和money按照hard排序

(1, 5) (1, 9) (3, 6) (7, 13) 

对于上面的排序,如果能力相同,那么肯定选择能力大的,因此能力相同的时候,去掉money小的

因此:(1, 9) (3, 6) (7, 13) 

如果难度增加了,但是钱变小了,那么没必要选择钱少的

因此:(1, 9) (7, 13) 

所以题目思路:先排序,然后放入TreeMap中,如果相同的,只放最大值,不同的,只放递增的

package com.zy.class002;

import java.util.Arrays;
import java.util.Comparator;
import java.util.TreeMap;

public class ChooseWork {
    public static int[] getMoneys(int[] hard, int[] money, int[] ability) {
        int len = hard.length;
        Job[] jobs = new Job[len];
        for (int i = 0; i < len; i++) {
            jobs[i] = new Job(money[i], hard[i]);
        }
        Arrays.sort(jobs, new JobComparator());

        // key : 难度   value:报酬
        TreeMap<Integer, Integer> map = new TreeMap<>();
        map.put(jobs[0].hard, jobs[0].money);
        // pre : 上一份进入map的工作
        Job pre = jobs[0];
        for (int i = 1; i < jobs.length; i++) {
            if (jobs[i].hard != pre.hard && jobs[i].money > pre.money) {
                pre = jobs[i];
                map.put(pre.hard, pre.money);
            }
        }
        int[] ans = new int[ability.length];
        for (int i = 0; i < ability.length; i++) {
            // ability[i] 当前人的能力 <= ability[i]  且离它最近的
            Integer key = map.floorKey(ability[i]);
            ans[i] = key != null ? map.get(key) : 0;
        }
        return ans;
    }
}

class Job {
    public int money;
    public int hard;

    public Job(int m, int h) {
        money = m;
        hard = h;
    }
}

class JobComparator implements Comparator<Job> {
    @Override
    public int compare(Job o1, Job o2) {
        return o1.hard != o2.hard ? (o1.hard - o2.hard) : (o2.money - o1.money);
    }
}

题目2:

给定一个数组arr,只能对arr中的一个子数组排序,但是想让arr整体都有序,返回满足这一设定的子数组中,最短的多长?

从左遍历,找出不是递增的最后一个数

从右遍历,找出最后一个不是递减的最后一个数,

两个数之间的数字就是最短长:

package com.zy.class002;

public class MinLengthForSort {
    public static int getMinLength(int[] arr) {
        if (arr == null || arr.length < 2) {
            return 0;
        }
        // 从右到左
        int min = arr[arr.length - 1];
        int nonMinIndex = -1;
        for (int i = arr.length - 2; i >= 0; i--) {
            if (arr[i] > min) {
                nonMinIndex = i;
            } else {
                min = Math.min(arr[i], min);
            }
        }

        if (nonMinIndex == -1) {
            return 0;
        }

        int max = arr[0];
        int nonMaxIndex = -1;
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] < max) {
                nonMaxIndex = i;
            } else {
                max = Math.max(arr[i], max);
            }
        }
        return nonMaxIndex - nonMinIndex + 1;
    }

    public static void main(String[] args) {
        int[] arr = { 1, 2, 4, 7, 10, 11, 7, 12, 6, 7, 16, 18, 19 };
        System.out.println(getMinLength(arr));

    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值