跳跃游戏
解法一
贪心
class Solution { public boolean canJump(int[] nums) { int maxPosition = 0; for(int i = 0;i < nums.length;i++){ // maxPosition 为到目前为止能够到最右边的距离 if(i > maxPosition) return false; maxPosition = Math.max(maxPosition,i + nums[i]); if(maxPosition >= nums.length-1) return true; } return false; } }
根据身高重建队列
class Solution { public int[][] reconstructQueue(int[][] people) { Arrays.sort(people,(int[] p1,int p2[])->{ if(p1[0] != p2[0]) return p2[0] - p1[0]; return p1[1] - p2[1]; }); List<int []> res = new ArrayList<int[]>(); for(int[] p : people){ res.add(p[1],p); } return res.toArray(new int[people.length][2]); } }
任务调度器
class Solution { public int leastInterval(char[] tasks, int n) { int [] task = new int[26]; for(int i = 0;i < tasks.length;i++){ task[tasks[i]-'A']++; } int max = 0; int maxCount = 0; for(int i = 0;i < 26;i++){ if(task[i] > max){ max = task[i]; maxCount = 1; }else if(task[i] == max){ maxCount++; } } return Math.max(tasks.length,(max-1)*(n+1)+maxCount); } }