返回介绍

lcci / 17.18.Shortest Supersequence / README_EN

发布于 2024-06-17 01:04:42 字数 5075 浏览 0 评论 0 收藏 0

17.18. Shortest Supersequence

中文文档

Description

You are given two arrays, one shorter (with all distinct elements) and one longer. Find the shortest subarray in the longer array that contains all the elements in the shorter array. The items can appear in any order.

Return the indexes of the leftmost and the rightmost elements of the array. If there are more than one answer, return the one that has the smallest left index. If there is no answer, return an empty array.

Example 1:


Input:

big = [7,5,9,0,2,1,3,5,7,9,1,1,5,8,8,9,7]

small = [1,5,9]

Output: [7,10]

Example 2:


Input:

big = [1,2,3]

small = [4]

Output: []

Note:

  • big.length <= 100000
  • 1 <= small.length <= 100000

Solutions

Solution 1

class Solution:
  def shortestSeq(self, big: List[int], small: List[int]) -> List[int]:
    need = Counter(small)
    window = Counter()
    cnt, j, k, mi = len(small), 0, -1, inf
    for i, x in enumerate(big):
      window[x] += 1
      if need[x] >= window[x]:
        cnt -= 1
      while cnt == 0:
        if i - j + 1 < mi:
          mi = i - j + 1
          k = j
        if need[big[j]] >= window[big[j]]:
          cnt += 1
        window[big[j]] -= 1
        j += 1
    return [] if k < 0 else [k, k + mi - 1]
class Solution {
  public int[] shortestSeq(int[] big, int[] small) {
    int cnt = small.length;
    Map<Integer, Integer> need = new HashMap<>(cnt);
    Map<Integer, Integer> window = new HashMap<>(cnt);
    for (int x : small) {
      need.put(x, 1);
    }
    int k = -1, mi = 1 << 30;
    for (int i = 0, j = 0; i < big.length; ++i) {
      window.merge(big[i], 1, Integer::sum);
      if (need.getOrDefault(big[i], 0) >= window.get(big[i])) {
        --cnt;
      }
      while (cnt == 0) {
        if (i - j + 1 < mi) {
          mi = i - j + 1;
          k = j;
        }
        if (need.getOrDefault(big[j], 0) >= window.get(big[j])) {
          ++cnt;
        }
        window.merge(big[j++], -1, Integer::sum);
      }
    }
    return k < 0 ? new int[0] : new int[] {k, k + mi - 1};
  }
}
class Solution {
public:
  vector<int> shortestSeq(vector<int>& big, vector<int>& small) {
    int cnt = small.size();
    unordered_map<int, int> need;
    unordered_map<int, int> window;
    for (int x : small) {
      need[x] = 1;
    }
    int k = -1, mi = 1 << 30;
    for (int i = 0, j = 0; i < big.size(); ++i) {
      window[big[i]]++;
      if (need[big[i]] >= window[big[i]]) {
        --cnt;
      }
      while (cnt == 0) {
        if (i - j + 1 < mi) {
          mi = i - j + 1;
          k = j;
        }
        if (need[big[j]] >= window[big[j]]) {
          ++cnt;
        }
        window[big[j++]]--;
      }
    }
    if (k < 0) {
      return {};
    }
    return {k, k + mi - 1};
  }
};
func shortestSeq(big []int, small []int) []int {
  cnt := len(small)
  need := map[int]int{}
  window := map[int]int{}
  for _, x := range small {
    need[x] = 1
  }
  j, k, mi := 0, -1, 1<<30
  for i, x := range big {
    window[x]++
    if need[x] >= window[x] {
      cnt--
    }
    for cnt == 0 {
      if t := i - j + 1; t < mi {
        mi = t
        k = j
      }
      if need[big[j]] >= window[big[j]] {
        cnt++
      }
      window[big[j]]--
      j++
    }
  }
  if k < 0 {
    return []int{}
  }
  return []int{k, k + mi - 1}
}
function shortestSeq(big: number[], small: number[]): number[] {
  let cnt = small.length;
  const need: Map<number, number> = new Map();
  const window: Map<number, number> = new Map();
  for (const x of small) {
    need.set(x, 1);
  }
  let k = -1;
  let mi = 1 << 30;
  for (let i = 0, j = 0; i < big.length; ++i) {
    window.set(big[i], (window.get(big[i]) ?? 0) + 1);
    if ((need.get(big[i]) ?? 0) >= window.get(big[i])!) {
      --cnt;
    }
    while (cnt === 0) {
      if (i - j + 1 < mi) {
        mi = i - j + 1;
        k = j;
      }
      if ((need.get(big[j]) ?? 0) >= window.get(big[j])!) {
        ++cnt;
      }
      window.set(big[j], window.get(big[j])! - 1);
      ++j;
    }
  }
  return k < 0 ? [] : [k, k + mi - 1];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文