Leetcode Weekly Contest 233(前三道)

题目链接: Leetcode Weekly Contest 233

写在前面:

本次周赛只做出了第一题,还需多加努力。

1、1800. Maximum Ascending Subarray Sum

难度:Easy

题目大意:

求数组中升序子数组的和的最大值。

思路:

遍历数组,记录下每隔升序子数组的和,取最大值

代码

class Solution {
    public int maxAscendingSum(int[] nums) {
        int sum=nums[0],max=sum;
        for(int i=1;i<nums.length;i++){
            if(nums[i]>nums[i-1]){
                sum+=nums[i];
            }
            else{
                if(sum>max){
                    max=sum;
                }
                sum=nums[i];//重新开始计算新一个升序子数组的和
            }
        }
        if(sum>max){//最后一个升序子数组
            max=sum;
        }
        return max;
    }
}

2、1801. Number of Orders in the Backlog

难度:Medium

题目大意:

详见题目。

思路:

按照题意一步步模拟即可,不难,但是题目比较长需要细心。

代码

class Solution {
    public int getNumberOfBacklogOrders(int[][] orders) {
        Queue<int[]> sell=new PriorityQueue<>((a,b)->a[0]-b[0]);
        Queue<int[]>  buy=new PriorityQueue<>((a,b)->b[0]-a[0]);
        int res=0,M=1000000007;
        for(int[] order:orders){
            if(order[2]==0){
                int price=order[0];
                int amount=order[1];
                while(amount>0){
                    if(!sell.isEmpty()&&sell.peek()[0]<=price){
                        int[] s=sell.peek();
                        if(s[1]>amount){
                            sell.poll();
                            sell.offer(new int[]{s[0],s[1]-amount});
                            amount=0;
                        }
                        else{
                            sell.poll();
                            amount-=s[1];
                        }
                    }
                    else{
                        buy.offer(new int[]{price,amount});
                        break;
                    }
                }
            }
            else{
                int price=order[0];
                int amount=order[1];
                while(amount>0){
                    if(!buy.isEmpty()&&buy.peek()[0]>=price){
                        int[] b=buy.peek();
                        if(b[1]>amount){
                            buy.poll();
                            buy.offer(new int[]{b[0],b[1]-amount});
                            amount=0;
                        }
                        else{
                            buy.poll();
                            amount-=b[1];
                        }
                    }
                    else{
                        sell.offer(new int[]{price,amount});
                        break;
                    }
                }
            }
        }
        for(int[] s:sell){
            res=(res%M+s[1]%M)%M;
        }
        for(int[] b:buy){
            res=(res%M+b[1]%M)%M;
        }
        return res;
    }
}
 

3、1802. Maximum Value at a Given Index in a Bounded Array

难度:Medium

题目大意:

详见题目。

思路:

参考高赞回答,最优解情况是从index处开始,左右两边的元素依次递减1。对index处的值进行枚举,判断是否满足题目限制,用二分查找来提高效率。

代码

class Solution {
    boolean check(int n,int index,int maxSum,int m){//m为index处的最大值
        int a=Math.max(m-index,0);//要保证所有数值大于0
        int b=m;
        long leftSum=(long)(a+b)*(b-a+1)/2;
        a=Math.max(m+index-(n-1),0);
        long rightSum=(long)(a+b)*(b-a+1)/2;
        long sum=leftSum+rightSum-m;//m被加了两次
        if(maxSum>=sum){
            return true;
        }
        else{
            return false;
        }
    }
    public int maxValue(int n, int index, int maxSum) {
        maxSum-=n;//每个元素先都置为1,保证所有元素都是正数
        int l=0,r=maxSum;
        while(l<r){
            int mid=(l+r+1)/2;
            if(check(n,index,maxSum,mid)){
                l=mid;
            }
            else{
                r=mid-1;
            }
        }
        return l+1;//开始的时候每个元素都置为1,所以最终的结果还要加1
    }
}

4、1803. Count Pairs With XOR in a Range

难度:Hard

题目大意:

思路

暂时做不出来。

代码

 //
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值