ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[Leetcode Weekly Contest]264

2021-10-28 22:02:34  阅读:206  来源: 互联网

标签:count Contest int list Leetcode parents 264 new 节点


链接:LeetCode

[Leetcode]2047. 句子中的有效单词数

句子仅由小写字母('a' 到 'z')、数字('0' 到 '9')、连字符('-')、标点符号('!'、'.' 和 ',')以及空格(' ')组成。每个句子可以根据空格分解成 一个或者多个 token ,这些 token 之间由一个或者多个空格 ' ' 分隔。

如果一个 token 同时满足下述条件,则认为这个 token 是一个有效单词:

仅由小写字母、连字符和/或标点(不含数字)。
至多一个 连字符 '-' 。如果存在,连字符两侧应当都存在小写字母("a-b" 是一个有效单词,但 "-ab" 和 "ab-" 不是有效单词)。
至多一个 标点符号。如果存在,标点符号应当位于 token 的 末尾 。
这里给出几个有效单词的例子:"a-b."、"afad"、"ba-c"、"a!" 和 "!" 。

给你一个字符串 sentence ,请你找出并返回 sentence 中 有效单词的数目 。

遍历即可。

class Solution {
    public int countValidWords(String sentence) {
        String[] words = sentence.split(" ");
        int res = 0;
        for(String word : words) {
            if(isValid(word)) {
                res+=1;
            }
        }
        return res;
    }

    public boolean isValid(String word) {
        if(word.isBlank()) return false;
        int dash = -1, n = word.length();
        for(int idx=0; idx<n; idx++) {
            var ch = word.charAt(idx);
            if (ch == '-') {
                if(dash != -1) return false;
                else dash = idx;
            }
            else if(ch == '!' || ch == '.' || ch == ',') {
                if(idx != n-1) return false;
            }
            else if(Character.isDigit(ch)) return false;
        }
        if(dash!=-1 && (dash==0 || dash==n-1||!Character.isLowerCase(word.charAt(dash-1))||!Character.isLowerCase(word.charAt(dash+1)))) return false;
        return true;
    }
}

[Leetcode]2048. 下一个更大的数值平衡数

如果整数 x 满足:对于每个数位 d ,这个数位 恰好 在 x 中出现 d 次。那么整数 x 就是一个 数值平衡数 。

给你一个整数 n ,请你返回 严格大于 n 的 最小数值平衡数 。

暴力即可。

class Solution {
    public int nextBeautifulNumber(int n) {
        while(true) {
            n++;
            if(isBeautifulNumber(n)) {
                return n;
            }
        }
    }

    public boolean isBeautifulNumber(int n) {
        String strN = String.valueOf(n);
        HashMap<Character,Integer> count = new HashMap<>();
        for(var ch:strN.toCharArray()) {
            count.put(ch,count.getOrDefault(ch,0)+1);
        }
        for(var pair:count.entrySet()) {
            // int key = (int)(pair.getKey()) - (int)('0');
            int key = Integer.parseInt(String.valueOf(pair.getKey()));
            if(key != pair.getValue()) return false;
        }
        return true;
    }
}

[Leetcode]2049. 统计最高分的节点数目

给你一棵根节点为 0 的 二叉树 ,它总共有 n 个节点,节点编号为 0 到 n - 1 。同时给你一个下标从 0 开始的整数数组 parents 表示这棵树,其中 parents[i] 是节点 i 的父节点。由于节点 0 是根,所以 parents[0] == -1 。

一个子树的 大小 为这个子树内节点的数目。每个节点都有一个与之关联的 分数 。求出某个节点分数的方法是,将这个节点和与它相连的边全部 删除 ,剩余部分是若干个 非空 子树,这个节点的 分数 为所有这些子树 大小的乘积 。

请你返回有 最高得分 节点的 数目 。

1.使用Map存储当前节点,以及它的子节点。
2.利用DFS查询并存储当前节点下的节点个数。
3.遍历计算各个节点删除后的得分情况(0号节点没有父节点)。

class Solution {
    public int countHighestScoreNodes(int[] parents) {
        Map<Integer, List<Integer>> map = new HashMap();
        int[] count = new int[parents.length];
        for(int i = 1; i < parents.length; i++){
            List<Integer> list = new ArrayList(map.getOrDefault( parents[i], new ArrayList()));
            list.add(i);
            map.put(parents[i], list);
        }
        DFS(0, map, count);
        long scoreMax = Integer.MIN_VALUE;
        int nodes = 0;
        for(int i = 0; i < parents.length; i++){
            long score = 1;
            if(parents[i] == -1){
                List<Integer> list = new ArrayList( map.getOrDefault( i, new ArrayList() ) );
                for(int num : list)
                    score *= count[num];
            }else{
                score = count[0] - count[i];
                List<Integer> list = new ArrayList( map.getOrDefault( i, new ArrayList() ) );
                for(int num : list)
                    score *= count[num];
            }
            if(scoreMax < score){
                scoreMax = score;
                nodes = 1;
            }else if(score == scoreMax) ++nodes;
        }
        return nodes;
    }

    public void DFS(int index,  Map<Integer, List<Integer>> map, int[] count){
        List<Integer> list = new ArrayList( map.getOrDefault( index, new ArrayList() ) );
        if(list.size() == 0){
            count[index] = 1;
            return;
        }
        for(int num : list){
            DFS(num, map, count);
            count[index] += count[num];
        }
        ++count[index];
    }
}

[Leetcode]2050. 并行课程 III

给你一个整数 n ,表示有 n 节课,课程编号从 1 到 n 。同时给你一个二维整数数组 relations ,其中 relations[j] = [prevCoursej, nextCoursej] ,表示课程 prevCoursej 必须在课程 nextCoursej 之前 完成(先修课的关系)。同时给你一个下标从 0 开始的整数数组 time ,其中 time[i] 表示完成第 (i+1) 门课程需要花费的 月份 数。

请你根据以下规则算出完成所有课程所需要的 最少 月份数:

如果一门课的所有先修课都已经完成,你可以在 任意 时间开始这门课程。
你可以 同时 上 任意门课程 。
请你返回完成所有课程所需要的 最少 月份数。

注意:测试数据保证一定可以完成所有课程(也就是先修课的关系构成一个有向无环图)。

这是一道非常经典的拓扑排序的应用题目, 我们可以在拓扑排序的过程中,维护一个 dp[i] 数组来表示当前到达节点 i 的最长时间即可。另外,我们也可用DFS的方法来做,维护一个最长时间即可。

class Solution {
public int minimumTime(int n, int[][] relations, int[] time) {
        int[] inDegree = new int[n];
        List<Integer>[] graph = new List[n];
        for (int i = 0; i < n; i++) {
            graph[i] = new ArrayList<>();
        }
        for (int[] edge : relations) {
            int u = edge[0] - 1;
            int v = edge[1] - 1;
            graph[u].add(v);
            inDegree[v]++;
        }

        int[] dist = new int[n];
        Queue<Integer> queue = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            if (inDegree[i] == 0) {
                queue.add(i);
                dist[i] = time[i];
            }
        }
        while (!queue.isEmpty()) {
            int index = queue.poll();
            for (int next : graph[index]) {
                dist[next] = Math.max(dist[next], dist[index] + time[next]);
                if (--inDegree[next] == 0) {
                    queue.add(next);
                }
            }
        }
        return Arrays.stream(dist).max().getAsInt();
    }
}

Leetcode

标签:count,Contest,int,list,Leetcode,parents,264,new,节点
来源: https://www.cnblogs.com/hellojamest/p/15477955.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有