728x90
SMALL

using System;
using System.Collections.Generic;
using System.Linq;

public class Solution
        {
            public int solution(int[,] dots)
            {
                List<float> slopes = new List<float>();                
                int length = dots.GetLength(0);


                for (int i = 0; i < length; i++)
                {
                    for (int j = i + 1; j < length; j++)
                    {
                        slopes.Add((dots[i, 1] - dots[j, 1]) / (float)(dots[i, 0] - dots[j, 0]));

/// 경우의 수가 length * (length -1) / 2
                    }
                }

                return slopes.Distinct().Count() == length * (length -1) / 2 ? 0 : 1;
            }
        }

 

/// Distinct로 기울기를 집어 넣은 게 중복으로 사라지게 되면, 기울기가 같은 게 있으면 1을 출력 (평행) 그렇지 않으면 0을 출력

 

 

자바

 

class Solution {
    public int solution(int[][] dots) {
        int x1 = dots[3][0];
        int y1 = dots[3][1];
        
        for (int i = 0; i < 3; i++) 
        {
            int x2 = dots[i][0];
            int y2 = dots[i][1];
            int x3 = dots[(i+1)%3][0];
            int y3 = dots[(i+1)%3][1];
            int x4 = dots[(i+2)%3][0];
            int y4 = dots[(i+2)%3][1];
            
            if(x1 == x2 || x3 == x4)
            {
                if(x1 == x2 && x3 == x4)
                {
                    return 1;
                }
                continue;
            }
            
            if((y2-y1)/(double)(x2-x1)==(y4-y3)/(double)(x4-x3))
            {
                return 1;
            }
        }
        return 0;
    }
}

 

728x90

설정

트랙백

댓글