zoukankan      html  css  js  c++  java
  • [Leetcode Weekly Contest]264

    链接: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

  • 相关阅读:
    Swift学习 --- 2.1基础部分
    【oracle案例】ORA-01722
    一种适合于大数据的并行坐标下降法
    【机器学习算法-python实现】svm支持向量机(3)—核函数
    让Editplus支持sql语法高亮显示
    CF986F Oppa Funcan Style Remastered
    HashMap和Hashtable的区别
    ArrayList和Vector的区别
    基本概念
    java编程规范
  • 原文地址:https://www.cnblogs.com/hellojamest/p/15477955.html
Copyright © 2011-2022 走看看