Skip to content

1183. Maximum Number of Ones 👍

Approach 1: Heap

  • Time: $O(\texttt{width} \cdot \texttt{height} + \texttt{sideLength} \log\texttt{sideLength})$
  • Space: $O(\texttt{sideLength}^2)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
 public:
  int maximumNumberOfOnes(int width, int height, int sideLength, int maxOnes) {
    int ans = 0;
    vector<vector<int>> submatrix(sideLength, vector<int>(sideLength));
    priority_queue<int> maxHeap;

    for (int i = 0; i < width; ++i)
      for (int j = 0; j < height; ++j)
        ++submatrix[i % sideLength][j % sideLength];

    for (const vector<int>& row : submatrix)
      for (const int a : row)
        maxHeap.push(a);

    for (int i = 0; i < maxOnes; ++i)
      ans += maxHeap.top(), maxHeap.pop();

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
  public int maximumNumberOfOnes(int width, int height, int sideLength, int maxOnes) {
    int ans = 0;
    int[][] submatrix = new int[sideLength][sideLength];
    Queue<Integer> maxHeap = new PriorityQueue<>(Comparator.reverseOrder());

    for (int i = 0; i < width; ++i)
      for (int j = 0; j < height; ++j)
        ++submatrix[i % sideLength][j % sideLength];

    for (int[] row : submatrix)
      for (final int a : row)
        maxHeap.offer(a);

    for (int i = 0; i < maxOnes; ++i)
      ans += maxHeap.poll();

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution:
  def maximumNumberOfOnes(
      self,
      width: int,
      height: int,
      sideLength: int,
      maxOnes: int,
  ) -> int:
    submatrix = [[0] * sideLength for _ in range(sideLength)]

    for i in range(width):
      for j in range(height):
        submatrix[i % sideLength][j % sideLength] += 1

    return sum(heapq.nlargest(maxOnes, [a for row in submatrix for a in row]))

Approach 2: Math

  • Time: $O(\texttt{sort}(k))$, where $k = \texttt{sideLength}^2$
  • Space: $O(\texttt{sideLength}^2)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
 public:
  int maximumNumberOfOnes(int width, int height, int sideLength, int maxOnes) {
    vector<int> subCount;

    auto getCount = [sideLength](int length, int index) -> int {
      return (length - index - 1) / sideLength + 1;
    };

    for (int i = 0; i < sideLength; ++i)
      for (int j = 0; j < sideLength; ++j)
        subCount.push_back(getCount(width, i) * getCount(height, j));

    ranges::sort(subCount, greater<>());
    return accumulate(subCount.begin(), subCount.begin() + maxOnes, 0);
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
  public int maximumNumberOfOnes(int width, int height, int sideLength, int maxOnes) {
    int ans = 0;
    List<Integer> subCount = new ArrayList<>();

    for (int i = 0; i < sideLength; ++i)
      for (int j = 0; j < sideLength; ++j)
        subCount.add(getCount(width, i, sideLength) * getCount(height, j, sideLength));

    Collections.sort(subCount, Collections.reverseOrder());

    for (int i = 0; i < maxOnes; ++i)
      ans += subCount.get(i);

    return ans;
  }

  private int getCount(int length, int index, int sideLength) {
    return (length - index - 1) / sideLength + 1;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution:
  def maximumNumberOfOnes(
      self,
      width: int,
      height: int,
      sideLength: int,
      maxOnes: int,
  ) -> int:
    subCount = []

    def getCount(length: int, index: int) -> int:
      return (length - index - 1) // sideLength + 1

    for i in range(sideLength):
      for j in range(sideLength):
        subCount.append(getCount(width, i) * getCount(height, j))

    return sum(sorted(subCount, reverse=True)[:maxOnes])