返回介绍

solution / 0900-0999 / 0927.Three Equal Parts / README_EN

发布于 2024-06-17 01:03:33 字数 5423 浏览 0 评论 0 收藏 0

927. Three Equal Parts

中文文档

Description

You are given an array arr which consists of only zeros and ones, divide the array into three non-empty parts such that all of these parts represent the same binary value.

If it is possible, return any [i, j] with i + 1 < j, such that:

  • arr[0], arr[1], ..., arr[i] is the first part,
  • arr[i + 1], arr[i + 2], ..., arr[j - 1] is the second part, and
  • arr[j], arr[j + 1], ..., arr[arr.length - 1] is the third part.
  • All three parts have equal binary values.

If it is not possible, return [-1, -1].

Note that the entire part is used when considering what binary value it represents. For example, [1,1,0] represents 6 in decimal, not 3. Also, leading zeros are allowed, so [0,1,1] and [1,1] represent the same value.

 

Example 1:

Input: arr = [1,0,1,0,1]
Output: [0,3]

Example 2:

Input: arr = [1,1,0,1,1]
Output: [-1,-1]

Example 3:

Input: arr = [1,1,0,0,1]
Output: [0,2]

 

Constraints:

  • 3 <= arr.length <= 3 * 104
  • arr[i] is 0 or 1

Solutions

Solution 1

class Solution:
  def threeEqualParts(self, arr: List[int]) -> List[int]:
    def find(x):
      s = 0
      for i, v in enumerate(arr):
        s += v
        if s == x:
          return i

    n = len(arr)
    cnt, mod = divmod(sum(arr), 3)
    if mod:
      return [-1, -1]
    if cnt == 0:
      return [0, n - 1]

    i, j, k = find(1), find(cnt + 1), find(cnt * 2 + 1)
    while k < n and arr[i] == arr[j] == arr[k]:
      i, j, k = i + 1, j + 1, k + 1
    return [i - 1, j] if k == n else [-1, -1]
class Solution {
  private int[] arr;

  public int[] threeEqualParts(int[] arr) {
    this.arr = arr;
    int cnt = 0;
    int n = arr.length;
    for (int v : arr) {
      cnt += v;
    }
    if (cnt % 3 != 0) {
      return new int[] {-1, -1};
    }
    if (cnt == 0) {
      return new int[] {0, n - 1};
    }
    cnt /= 3;

    int i = find(1), j = find(cnt + 1), k = find(cnt * 2 + 1);
    for (; k < n && arr[i] == arr[j] && arr[j] == arr[k]; ++i, ++j, ++k) {
    }
    return k == n ? new int[] {i - 1, j} : new int[] {-1, -1};
  }

  private int find(int x) {
    int s = 0;
    for (int i = 0; i < arr.length; ++i) {
      s += arr[i];
      if (s == x) {
        return i;
      }
    }
    return 0;
  }
}
class Solution {
public:
  vector<int> threeEqualParts(vector<int>& arr) {
    int n = arr.size();
    int cnt = accumulate(arr.begin(), arr.end(), 0);
    if (cnt % 3) return {-1, -1};
    if (!cnt) return {0, n - 1};
    cnt /= 3;

    auto find = [&](int x) {
      int s = 0;
      for (int i = 0; i < n; ++i) {
        s += arr[i];
        if (s == x) return i;
      }
      return 0;
    };
    int i = find(1), j = find(cnt + 1), k = find(cnt * 2 + 1);
    for (; k < n && arr[i] == arr[j] && arr[j] == arr[k]; ++i, ++j, ++k) {}
    return k == n ? vector<int>{i - 1, j} : vector<int>{-1, -1};
  }
};
func threeEqualParts(arr []int) []int {
  find := func(x int) int {
    s := 0
    for i, v := range arr {
      s += v
      if s == x {
        return i
      }
    }
    return 0
  }
  n := len(arr)
  cnt := 0
  for _, v := range arr {
    cnt += v
  }
  if cnt%3 != 0 {
    return []int{-1, -1}
  }
  if cnt == 0 {
    return []int{0, n - 1}
  }
  cnt /= 3
  i, j, k := find(1), find(cnt+1), find(cnt*2+1)
  for ; k < n && arr[i] == arr[j] && arr[j] == arr[k]; i, j, k = i+1, j+1, k+1 {
  }
  if k == n {
    return []int{i - 1, j}
  }
  return []int{-1, -1}
}
/**
 * @param {number[]} arr
 * @return {number[]}
 */
var threeEqualParts = function (arr) {
  function find(x) {
    let s = 0;
    for (let i = 0; i < n; ++i) {
      s += arr[i];
      if (s == x) {
        return i;
      }
    }
    return 0;
  }
  const n = arr.length;
  let cnt = 0;
  for (const v of arr) {
    cnt += v;
  }
  if (cnt % 3) {
    return [-1, -1];
  }
  if (cnt == 0) {
    return [0, n - 1];
  }
  cnt = Math.floor(cnt / 3);
  let [i, j, k] = [find(1), find(cnt + 1), find(cnt * 2 + 1)];
  for (; k < n && arr[i] == arr[j] && arr[j] == arr[k]; ++i, ++j, ++k) {}
  return k == n ? [i - 1, j] : [-1, -1];
};

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

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

发布评论

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