[LeetCode] 1395. Count Number of Teams
Updated:
1395. Count Number of Teams
μ£Όμ΄μ§ λ°°μ΄μμ 3 λͺ μ λ½λλ° λ€μκ³Ό κ°μ 쑰건μ λ§μ‘± μν€λ©΄μ λ½μΌλ©΄ λλ€.
index λ i < j < k, κ°μ rating[i] < rating[j] < rating[k] or rating[i] > rating[j] > rating[k]
μ΄λ λ½μ μ μλ κ²½μ°μ μλ₯Ό λͺ¨λ ꡬνλ λ¬Έμ μ΄λ€.
μ²μμ combination μΌλ‘ νμλ€κ° λ λ²μ§Έλ‘λ index λ₯Ό μ§μ μ κ·Όνμ¬ νμλ€.
from typing import List
from itertools import combinations
class Solution:
def solution1(self, rating: List[int]) -> int:
answer = 0
for cur in combinations(rating, 3):
if cur[0] < cur[1] and cur[1] < cur[2]:
answer += 1
elif cur[0] > cur[1] and cur[1] > cur[2]:
answer += 1
return answer
def solution2(self, rating: List[int]) -> int:
answer = 0
for i in range(len(rating) - 2):
for j in range(i + 1, len(rating) - 1):
for k in range(j + 1, len(rating)):
if rating[i] > rating[j] > rating[k]:
answer += 1
elif rating[i] < rating[j] < rating[k]:
answer += 1
return answer
λ¬Έμ μμ rating μ κΈΈμ΄κ° 200 μ΄λΌ μλ λ©΄μμ μ°¨μ΄κ° μμ§λ§ κΈΈμ΄κ° 1000 λ¨μλ‘ λμ΄κ°λ©΄ solution1 μ΄ 10μ΄ κ°λ λ λΉ λ₯΄λ€.
κ·Έλλ λ λ€ κΈΈμ΄κ° κΈ΄ rating μ΄ μ¨λ€λ©΄ μ±λ₯λ©΄μμ κ½μ΄λ€. combination μ μ λΆ κ΅¬νκ³ .. O(n^3) μ΄κ³ ..
κ·Έλμ λ€λ₯Έ μ¬λλ€μ O(n^2), O(nlogn) 볡μ‘λλ₯Ό κ°μ§ μ½λλ₯Ό λ΄€λλ° κ·Έκ³³μ λ΄κ° κ°λΉν κ³³μ΄ μλμλ€..
Leave a comment