【Week】No.179

C_01 生成每种字符都是奇数个的字符串

Given an integer n, return a string with n characters such that each character in such string occurs an odd number of times.

The returned string must contain only lowercase English letters. If there are multiples valid strings, return any of them.

输入:n = 4
输出:"pppz"
解释:"pppz" 是一个满足题目要求的字符串,因为 'p' 出现 3 次,且 'z' 出现 1 次。
当然,还有很多其他字符串也满足题目要求,比如:"ohhh""love"

方法一:奇偶判断

public String generateTheString(int n) {
  char[] res = new char[n];
  if (n % 2 == 1) {
      Arrays.fill(res, 'a');
  }
  else {
      Arrays.fill(res,'a');
      res[n-1] = 'b';
  }
  return new String(res);
}

复杂度分析

  • 时间复杂度: O ( n ) O(n)
  • 空间复杂度: O ( n ) O(n)

B_02 Bulb Switcher III

There is a room with n bulbs, numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off.

At moment k (for k from 0 to n - 1), we turn on the light[k] bulb. A bulb change color to blue only if it is on and all the previous bulbs (to the left) are turned on too.

Return the number of moments in which all turned on bulbs are blue.
在这里插入图片描述

Input: light = [2,1,3,5,4]
Output: 3
Explanation: All bulbs turned on, are blue at the moment 1, 2 and 4.

方法一:记录较先点亮的灯泡的位置

这题记录最大值有很多方法,比如用大根堆记录已经开启的灯泡里面的最大的位置 max,如果新开的灯泡的位置达不到这个 max,则认为打开的灯泡是不连续的。

这是最朴素的做法:

public int numTimesAllBlue2(int[] light) {
  int count = 0;
  int l = 1, r = -1;
  int i;
  boolean[] isLight = new boolean[light.length + 1];

  for (int pos : light) {
    if (pos > r)
      r = pos;
    isLight[pos] = true;
    for (i = l; i <= r; i++) {
      if (isLight[i] == false)
        break;
    }
    if (i == r + 1) {
      count++;
      l = r;
    }
  }
  return count;
}

复杂度分析

  • 时间复杂度: O ( n ) O(n)
  • 空间复杂度: O ( n ) O(n)

B_03

公司里有 n 名员工,每个员工的 ID 都是独一无二的,编号从 0 到 n - 1。公司的总负责人通过 headID 进行标识。

在 manager 数组中,每个员工都有一个直属负责人,其中 manager[i] 是第 i 名员工的直属负责人。对于总负责人,manager[headID] = -1。题目保证从属关系可以用树结构显示。

公司总负责人想要向公司所有员工通告一条紧急消息。他将会首先通知他的直属下属们,然后由这些下属通知他们的下属,直到所有的员工都得知这条紧急消息。

第 i 名员工需要 informTime[i] 分钟来通知它的所有直属下属(也就是说在 informTime[i] 分钟后,他的所有直属下属都可以开始传播这一消息)。

返回通知所有员工这一紧急消息所需要的 分钟数 。

在这里插入图片描述

输入:n = 1, headID = 0, manager = [-1], informTime = [0]
输出:0
解释:公司总负责人是该公司的唯一一名员工。
输入:n = 7, headID = 6, manager = [1,2,3,4,5,6,-1], informTime = [0,6,5,4,3,2,1]
输出:21
解释:总负责人 id = 6。他将在 1 分钟内通知 id = 5 的员工。
id = 5 的员工将在 2 分钟内通知 id = 4 的员工。
id = 4 的员工将在 3 分钟内通知 id = 3 的员工。
id = 3 的员工将在 4 分钟内通知 id = 2 的员工。
id = 2 的员工将在 5 分钟内通知 id = 1 的员工。
id = 1 的员工将在 6 分钟内通知 id = 0 的员工。
所需时间 = 1 + 2 + 3 + 4 + 5 + 6 = 21

方法一:dfs(自底向上)

题目大意:每一个员工都有一个管理者来通知自己,每一个管理者自己同事也可能是被管理者。计算出通知完毕所有员工的最长分钟数。实质上是求有向带权图的遍历的最大路径和。

public int numOfMinutes1(int n, int headID, int[] manager, int[] informTime) {

  boolean[] visited = new boolean[100005];
  int minute = 0;
  for (int i = 0; i < n; i++) {
    if (visited[i])
      continue;
    int id = i;
    int curMinute = 0;
    while (manager[id] != -1) {
      id = manager[id];
      visited[id] = true;
      curMinute += informTime[id];
    }
    if (curMinute > minute) minute = curMinute;
  }
  return minute;
}

复杂度分析

  • 时间复杂度: O ( n ) O(n)
  • 空间复杂度: O ( n ) O(n)

方法二:bfs(没想到)

对于每一个管理者,我们将其所有员工加到一个列表 list 中,然后让管理者的 id 和这些个员工映射起来。然后让超级管理者 headID 优先入队,开始 bfs 层次遍历这棵树。

bfs 中,对于每一层的每一个子结点,都将子节点的 id 和其管理者通知自己的时间和管理者被通知的时间累加到结点中记录着。

public int numOfMinutes(int n, int headID, int[] manager, int[] informTime) {
  int minute = 0;
  HashMap<Integer, List<Integer>> edges = new HashMap<>();

  for (int i = 0; i < n; i++) {
    List<Integer> list = edges.getOrDefault(manager[i], new LinkedList<>());
    list.add(i);
    edges.put(manager[i], list);
  }
  Queue<Node> queue = new LinkedList<>();
  queue.add(new Node(headID, 0));

  while (!queue.isEmpty()) {
    Node emp = queue.poll();
    int empId = emp.id;
    //层次遍历每一个员工的下属,把自己的通知时间 + 通知empId号员工需要的时间,注boss是不需要通知时间的
    List<Integer> level = edges.getOrDefault(empId, new LinkedList<>());
    for (int sonId : level) {
      int total_time = emp.time + informTime[empId];
      queue.add(new Node(sonId, total_time));
//      System.out.print(total_time); //1 3 6 10 15 21
    }
    minute = Math.max(minute, emp.time);
  }
  return minute;
}

A_04 T 秒后青蛙的位置(没做)

方法一:


复杂度分析

  • 时间复杂度: O ( ) O()
  • 空间复杂度: O ( ) O()

发布了461 篇原创文章 · 获赞 102 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_43539599/article/details/104739719
179