지난 공부기록/알고리즘 풀이

Python - Algorithm #33

hkl22 2024. 8. 6. 10:22

Python Algorithm

LeetCode

326. Power of Three

  • Given an integer n, return true if it is a power of three. Otherwise, return false.
  • An integer n is a power of three, if there exists an integer x such that n == 3ˣ.
  • Constraints
    • -2³¹   n   2³¹ - 1
class Solution:
    def isPowerOfThree(self, n: int) -> bool:
        # while문 사용
        # n이 1이 될 때까지 3으로 나눠보고
        # 만약 3으로 나눠지지 않는다면 3의 제곱수가 아닌 것
        # n이 0보다 같거나 작은 경우 예외 처리
        if n <= 0:
            return False
        while True:
            # 3으로 나눠떨어지면 3으로 나눠주고
            # 만약 그렇지 않으면 return False
            if n == 1:
                break
            if n % 3 == 0:
                n /= 3
            else:
                return False
        return True
class Solution:
    def isPowerOfThree(self, n: int) -> bool:
        for i in range(31):
            squared_num = math.pow(3, i)
            if n == squared_num:
                return True
            elif n < squared_num:
                return False
        return False
class Solution:
    def isPowerOfThree(self, n: int) -> bool:
        if n <= 0:
            return False
        while n > 0:
            if n == 1:
                return True
            elif n % 3 != 0:
                return False
            n /= 3
  • math.pow(x, y) : x의 y 제곱 반환

2103. Rings and Rods

  • There are n rings and each ring is either red, green, or blue. The rings are distributed across ten rods labeled from 0 to 9.
  • You are given a string rings of length 2n that describes the n rings that are placed onto the rods. Every two characters in rings forms a color-position pair that is used to describe each ring where:
    • The first character of the iᵗʰ pair denotes the iᵗʰ ring's color ('R', 'G', 'B').
    • The second character of the iᵗʰ pair denotes the rod that the iᵗʰ ring is placed on ('0' to '9').
  • For example, "R3G2B1" describes n == 3 rings: a red ring placed onto the rod labeled 3, a green ring placed onto the rod labeled 2, and a blue ring placed onto the rod labeled 1.
  • Return the number of rods that have all three colors of rings on them.
  • Constraints
    • rings.length == 2 ⨉ n
    • 1 n 100
    • rings[i] where i is even is either 'R', 'G', or 'B' (0-indexed).
    • rings[i] where i is odd is a digit from '0' to '9' (0-indexed).
class Solution:
    def countPoints(self, rings: str) -> int:
        # key: rod
        # value: ring color
        d = defaultdict(set)
        for i in range(0, len(rings), 2):
            ring = rings[i]
            rod = rings[i+1]
            # rod 키에다가 ring을 add
            d[rod].add(ring)
        # dictionary를 다 생성하고 나서
        # RGB 세 가지 색상이 모두 포함된 rod의 개수 세어보기
        answer = 0
        for rod, colors in d.items():
            if len(colors) == 3:
                answer += 1
        return answer
class Solution:
    def countPoints(self, rings: str) -> int:
        answer = 0
        d = defaultdict()
        for i in range(0, len(rings), 2):
            if rings[i+1] not in d:
                d[rings[i+1]] = [rings[i]]
            else:
                d[rings[i+1]] += [rings[i]]
        for key, value in d.items():
            if len(set(value)) == 3:
                answer += 1
        return answer

https://leetcode.com/problems/power-of-three/

https://leetcode.com/problems/rings-and-rods/

'지난 공부기록 > 알고리즘 풀이' 카테고리의 다른 글

Python - Algorithm #35  (0) 2024.08.08
Python - Algorithm #34  (0) 2024.08.07
Python - Algorithm #32  (0) 2024.08.05
Python - Algorithm #31  (0) 2024.08.02
Python - Algorithm #30  (0) 2024.08.01