Initial version of the README.MD
Browse files
README.md
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: llama2
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
tags:
|
| 6 |
+
- Python
|
| 7 |
+
- Leetcode
|
| 8 |
+
- Problem Solving
|
| 9 |
+
- CP
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
```
|
| 13 |
+
Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
|
| 14 |
+
|
| 15 |
+
### Instruction:
|
| 16 |
+
Solve the problem using Python.
|
| 17 |
+
|
| 18 |
+
### Input:
|
| 19 |
+
Consider a matrix M with dimensions width * height, such that every cell has value 0 or 1, and any square sub-matrix of M of size sideLength * sideLength has at most maxOnes ones.
|
| 20 |
+
|
| 21 |
+
Return the maximum possible number of ones that the matrix M can have.
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
Example 1:
|
| 26 |
+
|
| 27 |
+
Input: width = 3, height = 3, sideLength = 2, maxOnes = 1
|
| 28 |
+
Output: 4
|
| 29 |
+
Explanation:
|
| 30 |
+
In a 3*3 matrix, no 2*2 sub-matrix can have more than 1 one.
|
| 31 |
+
The best solution that has 4 ones is:
|
| 32 |
+
[1,0,1]
|
| 33 |
+
[0,0,0]
|
| 34 |
+
[1,0,1]
|
| 35 |
+
|
| 36 |
+
Example 2:
|
| 37 |
+
|
| 38 |
+
Input: width = 3, height = 3, sideLength = 2, maxOnes = 2
|
| 39 |
+
Output: 6
|
| 40 |
+
Explanation:
|
| 41 |
+
[1,0,1]
|
| 42 |
+
[1,0,1]
|
| 43 |
+
[1,0,1]
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
Constraints:
|
| 48 |
+
|
| 49 |
+
1 <= width, height <= 100
|
| 50 |
+
1 <= sideLength <= width, height
|
| 51 |
+
0 <= maxOnes <= sideLength * sideLength
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
### Response:
|
| 56 |
+
Sure, here you have a possible implementation to solve the problem.
|
| 57 |
+
|
| 58 |
+
'''python
|
| 59 |
+
class Solution:
|
| 60 |
+
def maximumNumberOfOnes(self, C, R, K, maxOnes):
|
| 61 |
+
# every K*K square has at most maxOnes ones
|
| 62 |
+
count = [0] * (K*K)
|
| 63 |
+
for r in range(R):
|
| 64 |
+
for c in range(C):
|
| 65 |
+
code = (r%K) * K + c%K
|
| 66 |
+
count[code] += 1
|
| 67 |
+
count.sort()
|
| 68 |
+
ans = 0
|
| 69 |
+
for _ in range(maxOnes):
|
| 70 |
+
ans += count.pop()
|
| 71 |
+
return ans
|
| 72 |
+
'''
|
| 73 |
+
```
|