[프로그래머스] 최소 직사각형
Algorithm 2023. 1. 17. 21:29https://school.programmers.co.kr/learn/courses/30/lessons/86491
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace _1406
{
class Program
{
static void Main()
{
int[,] arr =
{
{ 60, 50 },
{ 30, 70 },
{ 60, 30 },
{ 80, 40 }
};
new Solution().solution(arr);
}
}
public class Solution {
public int solution(int[,] sizes) {
int answer = 0;
for (int i = 0; i < sizes.GetLength(0); i++)
{
if (sizes[i, 0] < sizes[i, 1])
{
var temp = sizes[i, 0];
sizes[i, 0] = sizes[i, 1];
sizes[i, 1] = temp;
//(sizes[i, 0], sizes[i, 1]) = (sizes[i, 1], sizes[i, 0]);
}
}
int maxW = int.MinValue;
int maxH = int.MinValue;
for (int i = 0; i < sizes.GetLength(0); i++)
{
if (maxW < sizes[i, 0]) maxW = sizes[i, 0];//가로
if (maxH < sizes[i, 1]) maxH = sizes[i, 1];//세로
}
//Console.WriteLine("{0} {1}", maxW, maxH);
answer = maxW * maxH;
return answer;
}
}
}
제출용
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public int solution(int[,] sizes)
{
List<int[]> list = new List<int[]>();
for(int i = 0; i < sizes.GetLength(0); i++)
{
int temp = 0;
if (sizes[i,0] < sizes[i,1])
{
temp = sizes[i, 1];
sizes[i, 1] = sizes[i, 0];
sizes[i, 0] = temp;
}
list.Add(new int[2] { sizes[i, 0], sizes[i, 1] });
}
return list.Max(x => x[0]) * list.Max(x => x[1]);
}
}
using System;
public class Solution {
public int solution(int[,] sizes) {
int answer = 0;
for (int i = 0; i < sizes.GetLength(0); i++)
{
if (sizes[i, 0] < sizes[i, 1])
{
var temp = sizes[i, 0];
sizes[i, 0] = sizes[i, 1];
sizes[i, 1] = temp;
}
}
int maxW = int.MinValue;
int maxH = int.MinValue;
for (int i = 0; i < sizes.GetLength(0); i++)
{
if (maxW < sizes[i, 0]) maxW = sizes[i, 0];//가로
if (maxH < sizes[i, 1]) maxH = sizes[i, 1];//세로
}
//Console.WriteLine("{0} {1}", maxW, maxH);
answer = maxW * maxH;
return answer;
}
}
'Algorithm' 카테고리의 다른 글
[BOJ] 10866 덱 (0) | 2023.01.17 |
---|---|
[BOJ] 1158 요세푸스 문제 (0) | 2023.01.17 |
[BOJ] 10845 큐 (0) | 2023.01.17 |
[BOJ] 1406 에디터 (0) | 2023.01.17 |
[알고스팟] 록 페스티벌 C# (0) | 2023.01.17 |