国科大-计算机算法设计与分析-卜东波作业3

Question2

nums is an array consisting of non-negative integers, please concatenate them in certain permutation such that they can form a smallest new integer number. Input: nums = [1, 4, 13, 2, 25]
Output: 1132254
算法思想:题目是要将一个整数数组连接一个最小的字符串,也就是确定一个连接的顺序,使得连接后的结果最小。用到的方法是对这个数据进行排序,其排序规则为比较两两之间连接后的大小,如排1和13,即为比较113和131的大小,按这种规律进行从小到大的排序

class Solution {
    public String minNumber(int[] nums) {
        Integer[] num = new Integer[nums.length];
            for (int i = 0; i < nums.length; i++) {
                num[i] = nums[i];
            }
            Arrays.sort(num, new Comparator<Integer>() {
                public int compare(Integer o1, Integer o2) {
                    String a = "";
                    String b = "";
                    a += o1;
                    a += o2;
                    b += o2;
                    b += o1;
                    return a.compareTo(b);

                }
            });
            String s = "";
            for (int i = 0; i < nums.length; i++)
                s += num[i];
            return s;

    }
}

Question3

There are n meetings, and the start time and end time of i-th meeting are si and ei. Please design a greedy algorithm to find the minimum number of meeting rooms required to arrange all meetings.
Input: [ [0, 20], [15, 20], [20, 23], [25, 30], [15, 25] ]
Output: 3
算法思想:会场安排问题,1.定义一个结构体(或者类)用来保存需要安排活动的开始时间和结束时间。按照开始时间的先后顺序进行排序, 开始时间早的排在前面。定义一个整数数组(初始化为0,大小为会场个数),用于保存每个会场的结束时间。对于每一个活动遍历会场,直到找到一个会场结束时间小于此次活动开始的时间为止。

import java.util.Scanner;
import java.util.Arrays;
import java.util.Comparator;
public class Main {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc = new Scanner(System.in);
 
            int n = sc.nextInt();
            HuiChang []t = new HuiChang[n+1];
            for(int i=1;i<=n;i++)
                t[i] = new HuiChang();
             
            for(int i = 1;i<=n;i++){
                t[i].s = sc.nextInt();
                t[i].f = sc.nextInt();
            }
            MyComparator cmp = new MyComparator();
            Arrays.sort(t,1,n+1,cmp);
            System.out.println(meeting(t));
    }
    private static int meeting(HuiChang []t){
        int count_chang = 0;
        int n = t.length-1;
        int []a = new int [n+1];
        for(int i=0;i<a.length;i++) {
            a[i] = 0;
        }
        for(int i=1;i<=n;i++) {
            for(int j=1;j<=n;j++) {
                if(a[j]<=t[i].s) {
                    a[j] = t[i].f;
                    if(j>=count_chang+1)
                        count_chang++;
                     
                    break;
                }
            }
        }
        return count_chang;
    }
}
class HuiChang {
    public int s;
    public int f;
}
class MyComparator implements Comparator<HuiChang>{
    @Override
    public int compare(HuiChang o1, HuiChang o2) {
        if(o1.s > o2.s) return 1;
        else return -1;
    }
}

Question5

You are given an integer array nums. You are initially positioned at the array’s first index, and each element in the array represents your maximum jump length at that position. Return “yes“ if you can reach the last index, or “no“ otherwise.
Input: nums = [2,3,1,1,4]
Output: “yes”
算法思想:题目的意思为给你一个数组,里面的数分别表示你可以跳的最远距离,问你能不能跳到最后一块。解决的方法为从后向前判断,分别判断当前台阶是否可以跳到,如果可以,那前面的就不必判断能否到最后一个,只需判断能否到当前即可。


class Solution {
    public boolean canJump(int[] nums) {
        int ans =nums.length-1;
        if(nums.length==1)
            return true;
        for(int i=nums.length-1;i>=0;i--)
        {
            int x =i+nums[i];
            if(x>=ans)
            {
                ans=i;
                if(i==0)
                    return true;
            }
        }
        return false;
    }
        
}


Question6

There is a number k and you can swap two digits at most once. Please design a greedy algorithm to find the maximum value you can get. Input: k = 39748
Output: 93748
算法思想:本题是求交换一次所能得到的最大数字,肯定要先从高位进行判断。判断每一位的后面是否有比它大的数,如果有则取后面最大的与他交换,其得到的数便是交换一次后最大的数。

class Solution {
    public int maximumSwap(int num) {
        char[] s= String.valueOf(num).toCharArray();
        for (int i = 0; i < s.length; i++) {
            char max = s[i];
            int k = i;
            for (int j = i + 1; j < s.length; j++) {
                if (s[j] >= max) {
                    max = s[j];
                    k = j;
                }
            }
//            System.out.println(max);
            if (k != i&&max>s[i]) {
                char c=s[i];
                s[i]=s[k];
                s[k]=c;
                return Integer.parseInt(new String(s));
            }
        }
        return Integer.parseInt(new String(s));
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值