【英雄算法六月集训】Day16

【英雄算法六月集训】Day16

672. 灯泡开关 Ⅱ

class Solution {
    int[][] list = new int[][]{
            {0,1},{0,2},{1,2},{0,3}
    };
    List<List<String>> ans= new ArrayList<List<String>>();
    //静态代码块,初始化ans
    {
        ans.add(new ArrayList<String>());
        ans.add(new ArrayList<String>());
        ans.add(new ArrayList<String>());
        ans.add(new ArrayList<String>());
        ans.add(new ArrayList<String>());
    }
    Map<String,Integer> hash= new HashMap<String,Integer>();
    String gen(String ori,int start,int step){
        char[] s = ori.toCharArray();
        int i;
        for(i=start;i < s.length;i+=step){
            s[i] = (char) ('1'-(s[i]-'0'));
        }

        return new String(s);
    }
    void dfs(int dep,String s){
        ans.get(dep).add(s);
        if(dep>=4){
            return;
        }
        for(int i=0;i<4;++i){
            dfs(dep+1,gen(s,list[i][0],list[i][1]));
        }
    }
    public int flipLights(int n, int presses) {
        int i;
        StringBuilder start= new StringBuilder();

        for(i=0;i<n;++i){
            start.append('0');
        }
        dfs(0, start.toString());
        if(presses>4){
            if((presses&1)==1){
                presses=3;
            }else{
                presses=4;
            }
        }
        for(i=0;i<ans.get(presses).size();++i){
            hash.put(ans.get(presses).get(i),1);
        }
        return hash.size();
    }
}

2059. 转化数字的最小运算数

class Solution {
    public int minimumOperations(int[] nums, int start, int goal) {
        int maxn=1010;
        LinkedList<Integer> q=new LinkedList<Integer>();
        int[] step = new int[maxn];
        for(int a=0;a<maxn;++a) step[a]=-1;
        int x,nextx,i;
        q.push(start);
        step[start]=0;

        while(!q.isEmpty()){
            x=q.peekFirst();
            q.pop();
            for (i = 0; i < nums.length; ++i) {
                nextx = x+nums[i];
                if(nextx == goal){
                    return step[x]+1;
                }
                if(nextx<=1000&&nextx>=0){
                    if(step[nextx] == -1){
                        step[nextx] = step[x]+1;
                        q.add(nextx);
                    }
                }

                nextx = x-nums[i];
                if(nextx == goal){
                    return step[x]+1;
                }
                if(nextx<=1000&&nextx>=0){
                    if(step[nextx] == -1){
                        step[nextx] = step[x]+1;
                        q.add(nextx);
                    }
                }

                nextx = (x^nums[i]);
                if(nextx == goal){
                    return step[x]+1;
                }
                if(nextx<=1000&&nextx>=0){
                    if(step[nextx] == -1){
                        step[nextx] = step[x]+1;
                        q.add(nextx);
                    }
                }
            }
        }
        return -1;
    }
}

690. 员工的重要性

/*
// Definition for Employee.
class Employee {
    public int id;
    public int importance;
    public List<Integer> subordinates;
};
*/

class Solution {
    int theImportance;
    Map<Integer,Employee> id2Map= new HashMap<Integer,Employee>();
    int dfs(int id){
        Employee employee = id2Map.get(id);
        if(employee == null){
            return 0;
        }
        int sum = employee.importance;

        for(Integer eId:employee.subordinates){
            sum+= dfs(eId);
        }

        return sum;
    }
    public int getImportance(List<Employee> employees, int id) {
         
         int i=0;
         for(Employee e:employees){
             id2Map.put(e.id,e);
         }
         return dfs(id);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值