0


国科大-计算机算法设计与分析-卜东波作业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的大小,按这种规律进行从小到大的排序

classSolution{publicStringminNumber(int[] nums){Integer[] num =newInteger[nums.length];for(int i =0; i < nums.length; i++){
                num[i]= nums[i];}Arrays.sort(num,newComparator<Integer>(){publicintcompare(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,大小为会场个数),用于保存每个会场的结束时间。对于每一个活动遍历会场,直到找到一个会场结束时间小于此次活动开始的时间为止。

importjava.util.Scanner;importjava.util.Arrays;importjava.util.Comparator;publicclassMain{publicstaticvoidmain(String[] args){// TODO Auto-generated method stubScanner sc =newScanner(System.in);int n = sc.nextInt();HuiChang[]t =newHuiChang[n+1];for(int i=1;i<=n;i++)
                t[i]=newHuiChang();for(int i =1;i<=n;i++){
                t[i].s = sc.nextInt();
                t[i].f = sc.nextInt();}MyComparator cmp =newMyComparator();Arrays.sort(t,1,n+1,cmp);System.out.println(meeting(t));}privatestaticintmeeting(HuiChang[]t){int count_chang =0;int n = t.length-1;int[]a =newint[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;}}classHuiChang{publicint s;publicint f;}classMyComparatorimplementsComparator<HuiChang>{@Overridepublicintcompare(HuiChang o1,HuiChang o2){if(o1.s > o2.s)return1;elsereturn-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”
算法思想:题目的意思为给你一个数组,里面的数分别表示你可以跳的最远距离,问你能不能跳到最后一块。解决的方法为从后向前判断,分别判断当前台阶是否可以跳到,如果可以,那前面的就不必判断能否到最后一个,只需判断能否到当前即可。

classSolution{publicbooleancanJump(int[] nums){int ans =nums.length-1;if(nums.length==1)returntrue;for(int i=nums.length-1;i>=0;i--){int x =i+nums[i];if(x>=ans){
                ans=i;if(i==0)returntrue;}}returnfalse;}}

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
算法思想:本题是求交换一次所能得到的最大数字,肯定要先从高位进行判断。判断每一位的后面是否有比它大的数,如果有则取后面最大的与他交换,其得到的数便是交换一次后最大的数。

classSolution{publicintmaximumSwap(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;returnInteger.parseInt(newString(s));}}returnInteger.parseInt(newString(s));}}

本文转载自: https://blog.csdn.net/weixin_43731005/article/details/127960952
版权归原作者 稚念.. 所有, 如有侵权,请联系我们删除。

“国科大-计算机算法设计与分析-卜东波作业3”的评论:

还没有评论