Maximum Number of Ones Solutions in C++
Number 1183
Difficulty Hard
Acceptance 54.4%
Link LeetCode
Other languages —
Solutions
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/maximum-number-of-ones//// Author : liuyubobobo/// Time : 2019-09-07#include <iostream>#include <vector>#include <numeric>using namespace std;/// Mathematics/// Time Complexity: O(width * height + (side ^ 2)log(side^2) + maxOnes)/// Space Complexity: O((side ^ 2)log(side^2))class Solution {public:int maximumNumberOfOnes(int width, int height, int side, int maxOnes) {vector<int> pattern(side * side, 0);for(int i = 0; i < width; i ++)for(int j = 0; j < height; j ++)pattern[i % side * side + j % side] ++;sort(pattern.begin(), pattern.end(), greater<int>());return accumulate(pattern.begin(), pattern.begin() + maxOnes, 0);}};int main() {cout << Solution().maximumNumberOfOnes(3, 3, 2, 1) << endl;// 4cout << Solution().maximumNumberOfOnes(3, 3, 2, 2) << endl;// 6cout << Solution().maximumNumberOfOnes(68, 86, 63, 1474) << endl;// 3178cout << Solution().maximumNumberOfOnes(86, 32, 3, 4) << endl;// 1276return 0;}