[릿코드] 1724 Number of Rectangles That From The Largest Square (21.06.06)
오늘도 역시
기출문제 푸는 날이므로
릿코드!!!!!!!!!!!!!
지금은 다른 스터디원들의 시.험.기.간.
이므로 조금 쉬운 문제를 풀기로 했다!!!!!!
그래서
찾은
문제는
.
.
.
#easy #greedy
문제인 !!
.
.
.
LeetCode 21725. Number of Rectangles That Form The Largest Square
You are given an array rectangles where rectangles[i] = [li, wi] represents the ith rectangle of length li and width wi.
You can cut the ith rectangle to form a square with a side length of k if both k <= li and k <= wi. For example, if you have a rectangle [4,6], you can cut it to get a square with a side length of at most 4.
Let maxLen be the side length of the largest square you can obtain from any of the given rectangles.
Return the number of rectangles that can make a square with a side length of maxLen.
Constraints:
- 1 <= rectangles.length <= 1000
- rectangles[i].length == 2
- 1 <= li, wi <= 109
- li != wi
문제 사이트 : https://leetcode.com/problems/number-of-rectangles-that-can-form-the-largest-square/
Number Of Rectangles That Can Form The Largest Square - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
사실 ... 1725번은 문제 해석을 하다보면 답이 나온다!
그래서 문제 설명은 생략!!
직접 작성해본 소스코드 및 간단한 설명
class Solution {
public int countGoodRectangles(int[][] rectangles) {
int size = rectangles.length;
int[] minValues = new int[size];
for(int i = 0; i< rectangles.length; i++){
if(rectangles[i][0] < rectangles[i][1]){
minValues[i] = rectangles[i][0];
}
else{
minValues[i] = rectangles[i][1];
}
}
int max = minValues[0];
for(int i = 1; i< minValues.length; i++){
if(max < minValues[i]){
max = minValues[i];
}
}
int count = 0;
for(int i = 0; i< minValues.length; i++){
if(max == minValues[i]){
count++;
}
}
return count;
}
}
간단히 설명하자면,
- 행마다 min 값 찾기
- min 들 중에서 max값 찾기
- max 값의 개수 찾기
이렇게가 끝이다!!!
이를 구현하는 방법은 여러 개가 있지만,
내가 작성한 방법은 3가지다!!!!
1. ArrayList를 사용해서 각각의 list 값을 구하기!!
2. (현재 코드) int 배열을 사용해서 list로 구하기!!
3. Math.min() 과 Math.max() 사용해서 구하기!!
( 3번 코드 확인하기! )
class Solution {
public int countGoodRectangles(int[][] rectangles) {
int size = rectangles.length;
int max = 0;
int count = 0;
for(int i = 0; i< rectangles.length; i++){
int length = Math.min(rectangles[i][0], rectangles[i][1]);
if(max < length){
max = length;
count = 1;
}else if(max == length){
count++;
}
}
return count;
}
}
구현 결과는 놀랍게도
2 < 3 < 1
순으로 비효율적이었다!!!
코드 수는 3이 더 적었지만, Math 클래스의 함수를 사용하는 것으로부터 조금 더 시간이 걸린 것 같다!
이렇게 코드가 완성되었다!!!
LeetCode 릿코드
SUBMIT 결과 !!!!
ACCEPTED !!!!!!!
스터디하면서 얻은 것들
- Vector를 사용하여, 정렬 후 맨 뒤의 값은 Max 값이 된다!!
이번 스터디를 하면서
알게 된 것 중 가장 큰 것은 !!!!!!!
LeetCode의 큰 장점을 알게된 것이다!!!!!!
밑에 보이는 화면처럼 LeetCode는 Accepted 된 것들에 대해
코드를 이전까지의 결과와 함께 비교 분석을 알아서 해주고 있다는 것이다!!!
이를 통해 내가 지금 어디쯤인지 확인할 수 있는 게 가장 좋은 것 같다!!!!
이상으로 이번 학기 스터디는 끝!!!!!!
아쉽지만, 친구들이 종강한 후 계속될 것 같다!!!!!!!!!!!!!