LeetCode Week Contest 142

1093. Statistics from a Large Sample
问题比较简单,主要是需要处理计算均值求和时溢出的问题。

class Solution {
    public double[] sampleStats(int[] count) {
        int sum=0,i=0,j=count.length-1,n_sample=0;
        double[] ans=new double[5];
        
        while(i<count.length)
        {
            if(count[i]!=0)
                break;
            i++;
        }
        ans[0]=(double)i;
        while(j>=i)
        {
            if(count[j]!=0)
                break;
            j--;
        }
        ans[1]=(double)j;
        int mode=i,frequency=count[i];
        
        for(int k=i;k<=j;k++){
            n_sample+=count[k];
            if(count[k]>frequency)
            {
                mode=k;
                frequency=count[k];
            }
        }
        
        for(int k=i;k<=j;k++){
            if(count[k]==0)
                continue;
            ans[2]+=(double)count[k]*k/n_sample;
        }
        
        ans[4]=(double)mode;
        if(n_sample%2==0)
        {
            int left_index=n_sample/2;
            int right_index=left_index;
            for(int k=i;k<=j;k++){
                if(left_index<=count[k])
                {
                    left_index=k;
                    break;
                }
                left_index-=count[k];
            }
            
            for(int k=j;k>=i;k--){
                if(right_index<=count[k])
                {
                    right_index=k;
                    break;
                }
                right_index-=count[k];
            }
            ans[3]=((double)left_index+(double) right_index)/2;
        }
        else
        {
            int index=n_sample/2+1;
            for(int k=i;k<=j;k++)
            {
                if(index<=count[k])
                {
                    ans[3]=(double)k;
                    break;
                }
                index-=count[k];
            }
        }
        return ans;
    }
}

1094. Car Pooling

class Solution {
    public boolean carPooling(int[][] trips, int capacity) {
        
        int[] stops=new int[1001];
        for(int[] tmp:trips){
            stops[tmp[2]]-=tmp[0];
            stops[tmp[1]]+=tmp[0];
        }
        
        for(int i=1;i<1001 && capacity>=0;i++){
            
            capacity-=stops[i];
            if(capacity<0)
                return false;
        }
        return true;
    }
}

1095. Find in Mountain Array
使用三次二分,第一次二分求峰值位置,然后对左右子区间进行二分查找。

class Solution {
    public int findInMountainArray(int target, MountainArray mountainArr) {
        
        int left=0,right=mountainArr.length()-1;
        while(left<right){
            int mid=left+(right-left)/2;
            if(mountainArr.get(mid)<mountainArr.get(mid+1))
                left=mid+1;
            else
                right=mid;
        }
        if(mountainArr.get(left)==target)
            return left;
        int l=0,r=left;
        while(l<r){
            int m=l+(r-l)/2;
            if(mountainArr.get(m)<target)
                l=m+1;
            else
                r=m;
        }
        if(mountainArr.get(l)==target)
            return l;
        
        l=left;
        r=mountainArr.length();
        
        while(l<r){
            int m=l+(r-l)/2;
            if(mountainArr.get(m)<target)
                r=m-1;
            else if(mountainArr.get(m)>target)
                l=m+1;
            else
                return m;
        }
        System.out.print(l);

        return -1;
    }
}

1096. Brace Expansion II

class Solution {
    public static int idx;

    public List<String> braceExpansionII(String expression) {

        idx=0;
        Set<String> ans=parseRule1(expression);
        List<String> list=new ArrayList<>();
        for(String s:ans)
            list.add(s);
        Collections.sort(list);
        return list;
    }

    public Set<String> merge(Set<String> set1, Set<String> set2) {

        if(set1.size()==0)
            return set2;
        if(set2.size()==0)
            return set1;
        HashSet<String> ans = new HashSet<>();
        for (String str1 : set1) {
            for (String str2 : set2) {
                ans.add(str1 + str2);
            }
        }
        return ans;
    }

    public Set<String> parseRule1(String str) {
        Set<String> ans = new HashSet<>();
        while (idx < str.length()) {
            char c = str.charAt(idx);
            if (c == '}' || c == ',')
                break;
            if (c == '{') {
                idx++;
                Set<String> tmp = parseRule2(str);
                idx++;
                ans = merge(ans, tmp);
            } else {
                Set<String> tmp = new HashSet<>();
                StringBuilder sb = new StringBuilder();
                while (idx < str.length() && str.charAt(idx) >= 'a' && str.charAt(idx) <= 'z') {
                    sb.append(str.charAt(idx));
                    idx++;
                }
                tmp.add(sb.toString());
                ans = merge(ans, tmp);
            }
        }
        return ans;
    }

    public Set<String> parseRule2(String str) {
        Set<String> ans;
        ans = parseRule1(str);
        while (idx < str.length()) {
            char c = str.charAt(idx);
            if (c != ',')
                break;
            idx++;
            Set<String> tmp = parseRule1(str);
            for (String s : tmp)
                ans.add(s);
        }
        return ans;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值