Leetcode Weekly Contest 272(双指针、贪心、二分查找)

题目链接: Leetcode Weekly Contest 272

1、2108. Find First Palindromic String in the Array

难度:Easy

代码:

class Solution {
    public String firstPalindrome(String[] words) {
        for(String s:words){
            int l=0,r=s.length()-1;
            boolean flag=true;
            while(l<r){
                if(s.charAt(l)!=s.charAt(r)){
                    flag=false;
                    break;
                }
                l++;
                r--;
            }
            if(flag){
                return s;
            }
        }
        return "";
    }
}

2、2109. Adding Spaces to a String

难度:Medium

代码:

class Solution {
    public String addSpaces(String s, int[] spaces) {
        StringBuilder sb=new StringBuilder(s);
        int offset=0;
        for(int i:spaces){
            sb.insert(i+offset," ");
            offset++;
        }
        return new String(sb);
    }
}

3、2110. Number of Smooth Descent Periods of a Stock

难度:Medium

思路:

双指针,注意计算过程中也要用long类型。

代码

class Solution {
    public long getDescentPeriods(int[] prices) {
        int l=0,r=0;
        int n=prices.length;
        long res=0;
        while(l<n){
            while(r<n-1&&prices[r+1]-prices[r]==-1){
                r++;
            }
            res+=((long)(r-l+1)*(long)(r-l+2)/2);//这里要转化成long
            r++;
            l=r;
        }
        return res;
    }
}

4、2111. Minimum Operations to Make the Array K-Increasing

难度:Hard

思路

将数组分成k组进行求解,然后转化成最长非递减子序列问题,参考Leetcode 300 最长递增子序列问题,用贪心+二分查找求解。

代码

class Solution {
    public int kIncreasing(int[] arr, int k) {
        int n=arr.length;
        int size=(n%k==0?n/k:n/k+1);
        int[] nums=new int[size];
        int start=0;
        int res=0;
        for(int i=0;i<k;i++){
            for(int j=0;j<size;j++){
                if(k*j+start<n){
                    nums[j]=arr[k*j+start];
                }
                else{
                    nums[j]=0;
                }
            }
            int len=lengthOfLIS(nums);
            if(nums[size-1]!=0){
                res+=(size-len);
            }
            else{
                res+=(size-1-len);
            }
            start++;
        }
        return res;
    }
    public int lengthOfLIS(int[] nums) {//求最长非递减子序列的长度,参考Leetcode 300 最长递增子序列
        int n=nums.length;
        while(nums[n-1]==0){
            n--;
        }
        int[] d=new int[n+1];
        //d[i]表示长度为i的最长非递减子序列的最后一个元素的最小值
        int len=1;//最长非递减子序列的长度
        d[1]=nums[0];
        for(int i=1;i<n;i++){
            if(nums[i]>=d[len]){
                len++;
                d[len]=nums[i];
            }
            else{
                int l=0,r=len;
                while(l<r){
                    //寻找小于等于nums[i]的元素的最大下标
                    int mid=l+(r-l+1)/2;
                    if(d[mid]>nums[i]){
                        r=mid-1;
                    }
                    else{
                        l=mid;
                    }
                }
                d[l+1]=nums[i];
            }
        }
        return len;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值