LeetCode Week Contest 143

1103. Distribute Candies to People
暴力模拟

class Solution {
    public int[] distributeCandies(int candies, int num_people) {
        
        int i=0;
        int[] ans=new int[num_people];
        while(candies>0){
            ans[i%num_people]+=candies>=i+1?i+1:candies;
            candies-=i+1;
            i++;
        }
        return ans;
    }
}

1104. Path In Zigzag Labelled Binary Tree
这个问题我写的复杂了,可以精简。就是说对于某一行中心对称位置上的两个节点上的和是一定的,对于偶数行采用正常计算的值,而奇数采用正常值的补值。

class Solution {
    public List<Integer> pathInZigZagTree(int label) {
        
        ArrayList<Integer> left=new ArrayList<>();
        ArrayList<Integer> right=new ArrayList<>();
        List<Integer> ans=new LinkedList<>();
        int cur=1,i=0,val=0;
        while((cur<<i)<=label)
        {
            left.add(cur<<i);
            right.add((cur<<(i+1))-1);
            i++;
        }
        int level=left.size()-1,indexInLevel=0,indexInTree=0;
        System.out.println(level);
        if((level & 1)==0)
        {
            indexInLevel=label-left.get(level);
            indexInTree=left.get(level)+indexInLevel;
        }
        else{
            indexInLevel=right.get(level)-label;
            indexInTree=left.get(level)+indexInLevel;
        }
        System.out.println(indexInTree);
        ans.add(label);
        while(indexInTree>=2){
            indexInTree/=2;
            level--;
            if(((level) & 1)==0){
                val=indexInTree;
            }
            else
            {
                val=right.get(level)-(indexInTree-left.get(level));
            }
            System.out.println(val);
            ans.add(0,val);
            
        }
        return ans;
    }
}

1105. Filling Bookcase Shelves
dp[i]表示前i件箱子的最小高度

class Solution {
    public int minHeightShelves(int[][] books, int shelf_width) {
        int n=books.length;
        int[] dp=new int[n+1];
        Arrays.fill(dp,Integer.MAX_VALUE);
        dp[0]=0;
        for(int i=1;i<=n;i++){
            int w=books[i-1][0];
            int b=books[i-1][1];
            dp[i]=dp[i-1]+b;
            for(int j=i-1;j>0;j--){
                w+=books[j-1][0];
                b = Math.max(b, books[j-1][1]);
                if(w>shelf_width)
                    break;
                dp[i]=Math.min(dp[i],dp[j-1]+b);
            } 
        }   
        return dp[n];
    }
}

1106. Parsing A Boolean Expression

class Solution {
    public static int idx;

    public static boolean parseBoolExpr(String exp) {
        idx = 0;
        return passExpr(exp);
    }


    public static int[] passMore(String exp) {

        idx++;
        int[] ans = new int[2];
        if (passExpr(exp))
            ans[0]++;
        else
            ans[1]++;
        while (exp.charAt(idx) == ',') {
            idx++;
            if (passExpr(exp))
                ans[0]++;
            else
                ans[1]++;
        }
        idx++;
        return ans;

    }

    public static boolean passExpr(String exp) {
        if (idx >= exp.length())
            return true;
        boolean ret = true;
        int[] tmp;
        char c = exp.charAt(idx++);
        switch (c) {
            case 't':
                ret = true;
                break;
            case 'f':
                ret = false;
                break;
            case '|':
                tmp = passMore(exp);
                ret = tmp[0] > 0;
                break;
            case '&':
                tmp = passMore(exp);
                ret = (tmp[1] == 0);
                break;
            case '!':
                tmp = passMore(exp);
                ret = tmp[0] > 0 ? false : true;
                break;
        }
        return ret;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值