Study Anything 🧐

1637. Widest Vertical Area Between Two Points Containing No Points λ³Έλ¬Έ

μ•Œκ³ λ¦¬μ¦˜λ¬Έμ œ 풀이/LeetCode

1637. Widest Vertical Area Between Two Points Containing No Points

솔 2022. 2. 11. 23:36

#Array #Sorting

 

n개의 점이 2D 평면에 points[i] = [xi, yi] 으둜 주어진닀.

μ˜μ—­ μ•ˆμ— λ‹€λ₯Έ 점이 μ—†λŠ” κ°€μž₯ 넓은 μˆ˜μ§μ˜μ—­μ„ λ°˜ν™˜ν•˜λΌ.

 

μˆ˜μ§μ˜μ—­μ€ λ„ˆλΉ„κ°€ κ³ μ •λœ, Y좕을 따라 λ¬΄ν•œν•˜κ²Œ ν™•μž₯ν•˜λŠ” μ˜μ—­μ΄λ‹€.

κ°€μž₯ 넓은 μˆ˜μ§μ˜μ—­μ€ κ°€μž₯ κΈ΄ λ„ˆλΉ„λ₯Ό 가진 μ˜μ—­μ΄λ‹€.

 

μ˜μ—­μ˜ λͺ¨μ„œλ¦¬ μœ„μ˜ 점은 μ˜μ—­μ— ν¬ν•¨λ˜μ§€ μ•ŠλŠ”λ‹€.

 


Trial 1 : Python3, 22/02/11

class Solution:
    def maxWidthOfVerticalArea(self, points: List[List[int]]) -> int:
        points.sort()
        re=0
        for i in range(1,len(points)):
            re = max(re, points[i][0]-points[i-1][0])
        return re

풀이:

내뢀에 λ‹€λ₯Έ 점이 μ—†μ–΄μ•Ό ν•˜κ³ , λͺ¨μ„œλ¦¬ μœ„μ˜ 점은 신경쓰지 μ•ŠμœΌλ©°, κ°€λ‘œμ˜ 길이둜 μ˜μ—­ 넓이가 κ²°μ •λœλ‹€.

λ”°λΌμ„œ points[] λ°°μ—΄μ˜ 점듀 쀑 x μ’Œν‘œλ§Œ μ΄μš©ν•˜λ©΄ λœλ‹€.

λ¨Όμ €, points[] 배열을 x μ’Œν‘œλ₯Ό κΈ°μ€€μœΌλ‘œ μ •λ ¬ν•œλ‹€. νŒŒμ΄μ¬μ—μ„œλŠ” sort() λ©”μ†Œλ“œλ₯Ό μ΄μš©ν•˜λ©΄ 각 μš”μ†Œμ˜ 0번째 인덱슀둜 μ •λ ¬ν•˜λ―€λ‘œ x μ’Œν‘œλ₯Ό κΈ°μ€€μœΌλ‘œ 배열을 μ‰½κ²Œ μ •λ ¬ν•  수 μžˆλ‹€.

λ˜ν•œ 쀑간에 ν¬ν•¨λ˜λŠ” 점이 μ—†μ–΄μ•Ό ν•˜λ―€λ‘œ ν•œλ‹¨κ³„μ”©μ˜ κ°€λ‘œ 길이λ₯Ό μΈ‘μ •ν•˜κ³  κ°€μž₯ κΈ΄ κ°€λ‘œκ°€ 곧 λ°˜ν™˜κ°’μ΄ λœλ‹€.

λ”°λΌμ„œ for λ°˜λ³΅λ¬Έμ„ 톡해 각 점의 κ°€λ‘œ 길이λ₯Ό μΈ‘μ •ν•˜κ³  κ°€μž₯ 큰 값을 κ°±μ‹ ν•΄μ„œ λ°˜ν™˜ν•œλ‹€.

 

 

리뷰: 

λŸ°νƒ€μž„μ€ 46.82%, λ©”λͺ¨λ¦¬λŠ” 57.63% 의 κ²°κ³Όλ₯Ό μ–»μ—ˆλ‹€.

파이썬으둜 ν•΄κ²°ν•œ 방법은 κ°„λ‹¨ν–ˆμ§€λ§Œ λ‹€λ₯Έ μ–Έμ–΄λ‘œ ν‘ΈλŠ” κ²½μš°μ— μ‹œκ°„κ³Ό 곡간을 더 μ•„λ‚„ 수 μžˆλŠ” 것 κ°™λ‹€.

 

 

 

 

728x90

'μ•Œκ³ λ¦¬μ¦˜λ¬Έμ œ 풀이 > LeetCode' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

413. Arithmetic Slices  (0) 2022.03.04
804. Unique Morse Code Words  (0) 2022.02.27
1108. Defanging an IP Address  (0) 2022.02.10
438. Find All Anagrams in a String - Trial 2  (0) 2022.02.03
438. Find All Anagrams in a String  (0) 2022.02.02
Comments