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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值