LC周赛237 题解

  1. 判断句子是否为全字母句

https://leetcode-cn.com/problems/check-if-the-sentence-is-pangram/
哈希表

class Solution {
    public boolean checkIfPangram(String sentence) {
        int[] check = new int[26];
        for(int i = 0; i < sentence.length(); i++) {
            char c = sentence.charAt(i);
            check[c - 'a']++;
        }
        for(int i = 0; i < 26; i++) {
            if(check[i] == 0) {
                return false;
            }
        }
        return true;
    }
}
  1. 雪糕的最大数量

https://leetcode-cn.com/problems/maximum-ice-cream-bars/
贪心

class Solution {
    public int maxIceCream(int[] costs, int coins) {
        Arrays.sort(costs);
        int ans = 0;
        for(int i = 0; i < costs.length; i++) {
            if(coins >= costs[i]) {
                ans++;
                coins -= costs[i];
            } else {
                break;
            }
        }
        return ans;
    }
}

5736.雪糕的最大数量
https://leetcode-cn.com/problems/single-threaded-cpu/
优先队列/堆
超时解法:

class Solution {
    public int[] getOrder(int[][] tasks) {
        int n = tasks.length;
        int[][] packageTasks = new int[n][3];
        for(int i = 0; i < n; i++) {
            packageTasks[i][0] = tasks[i][0];
            packageTasks[i][1] = tasks[i][1];
            packageTasks[i][2] = i;
        }
        Arrays.sort(packageTasks, new Comparator<int[]>() {
           public int compare(int[] task1, int[] task2) {
               return task1[0] - task2[0];
           } 
        });

        int[] order = new int[n];
        int orderNextIndex = 0;
        int nowTime = 1;
        int left = 0, right = 0;
        PriorityQueue<int[]> tasksQueue = new PriorityQueue<int[]>(new Comparator<int[]>() {
            public int compare(int[] task1, int[] task2) {
                if(task1[1] == task2[1]) {
                    return task1[2] - task2[2]; 
                } else {
                    return task1[1] - task2[1];
                }
            }
        });
        while(orderNextIndex < n) {
            while(right < n && packageTasks[right][0] <= nowTime) {
                right++;
            }

            // Copy
            int[] copyTask = new int[3];
            for(int i = left; i < right; i++) {
                tasksQueue.offer(packageTasks[i]);
            }
            left = right;
            
            // Run
            if(!tasksQueue.isEmpty()) {
                int[] readyTask = tasksQueue.poll();
                order[orderNextIndex] = readyTask[2];
                orderNextIndex++;
                nowTime += readyTask[1];
            } else {
                nowTime++;
            }
        }
        return order;
    }
}

后续才发现自己的过不了测试用例[10000,10000],nowTime++过于保守,修改为nowTime = orderNextIndex[left]后能过了

class Solution {
    public int[] getOrder(int[][] tasks) {
        int n = tasks.length;
        int[][] packageTasks = new int[n][3];
        for(int i = 0; i < n; i++) {
            packageTasks[i][0] = tasks[i][0];
            packageTasks[i][1] = tasks[i][1];
            packageTasks[i][2] = i;
        }
        Arrays.sort(packageTasks, new Comparator<int[]>() {
           public int compare(int[] task1, int[] task2) {
               return task1[0] - task2[0];
           } 
        });

        int[] order = new int[n];
        int orderNextIndex = 0;
        int nowTime = packageTasks[0][0];
        int left = 0, right = 0;
        PriorityQueue<int[]> tasksQueue = new PriorityQueue<int[]>(new Comparator<int[]>() {
            public int compare(int[] task1, int[] task2) {
                if(task1[1] == task2[1]) {
                    return task1[2] - task2[2]; 
                } else {
                    return task1[1] - task2[1];
                }
            }
        });
        while(orderNextIndex < n) {
            while(right < n && packageTasks[right][0] <= nowTime) {
                right++;
            }

            // Copy
            int[] copyTask = new int[3];
            for(int i = left; i < right; i++) {
                tasksQueue.offer(packageTasks[i]);
            }
            left = right;
            
            // Run
            if(!tasksQueue.isEmpty()) {
                int[] readyTask = tasksQueue.poll();
                order[orderNextIndex] = readyTask[2];
                orderNextIndex++;
                nowTime += readyTask[1];
            } else {
                nowTime = packageTasks[left][0];
            }
        }
        return order;
    }
}

5737.所有数对按位与结果的异或和
https://leetcode-cn.com/problems/find-xor-sum-of-all-pairs-bitwise-and/
莫名其妙找了找规律就过了,也许是有什么运算法则的吧(逃)

class Solution {
    public int getXORSum(int[] arr1, int[] arr2) {
        int m = arr1.length, n = arr2.length;
        long x = -1, y = -1;
        for(int i = 0; i < m; i++) {
            if(x == -1) {
                x = arr1[i];
            } else {
                x ^= arr1[i];
            }
        }
        for(int j = 0; j < n; j++) {
            if(y == -1) {
                y = arr2[j];
            } else {
                y ^= arr2[j];
            }
        }
        return (int)(x & y);
    }
}

你可能感兴趣的:(LeetCode周赛,算法,leetcode,java)