Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit cb9aefc

Browse files
solves construct the rectangle
1 parent df01de3 commit cb9aefc

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
| 476 | [Number Complement](https://leetcode.com/problems/number-complement) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/NumberComplement.java) |
128128
| 482 | [License Key Formatting](https://leetcode.com/problems/license-key-formatting) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/LicenseKeyFormatting.java) |
129129
| 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MaxConsecutiveOnes.java) |
130-
| 492 | [Construct the Rectangle](https://leetcode.com/problems/construct-the-rectangle) | Easy | |
130+
| 492 | [Construct the Rectangle](https://leetcode.com/problems/construct-the-rectangle) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ConstructTheRectangle.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/construct_the_rectangle.py)|
131131
| 496 | [Next Greater Element I](https://leetcode.com/problems/next-greater-element-i) | Easy | |
132132
| 500 | [Keyboard Row](https://leetcode.com/problems/keyboard-row) | Easy | |
133133
| 501 | [Find Mode in Binary Search Tree](https://leetcode.com/problems/find-mode-in-binary-search-tree) | Easy | |

python/construct_the_rectangle.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from math import sqrt
2+
from typing import List
3+
4+
5+
class Solution:
6+
def constructRectangle(self, area: int) -> List[int]:
7+
width = int(sqrt(area))
8+
9+
while width > 0:
10+
if area % width == 0:
11+
return [area // width, width]
12+
else:
13+
width -= 1
14+
15+
return [area, 1]

src/ConstructTheRectangle.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class ConstructTheRectangle {
2+
public int[] constructRectangle(int area) {
3+
int width = (int) Math.sqrt(area);
4+
5+
while (width > 0) {
6+
if (area % width == 0) {
7+
return new int[] {area / width , width};
8+
}
9+
width--;
10+
}
11+
12+
return new int[] {area , 1};
13+
}
14+
}

0 commit comments

Comments
 (0)