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

Python - Algorithm #57

hkl22 2024. 9. 25. 10:08

Python Algorithm

LeetCode

3285. Find Indices of Stable Mountains

  • There are n mountains in a row, and each mountain has a height. You are given an integer array [height] where height[i] represents the height of mountain i, and an integer threshold.
  • A mountain is called stable if the mountain just before it (if it exists) has a height strictly greater than threshold. Note that mountain 0 is not stable.
  • Return an array containing the indices of all stable mountains in any order.
  • Constraints
    • 2 n == height.length 100
    • 1 height[i] 100
    • 1 threshold 100
class Solution:
    def stableMountains(self, height: List[int], threshold: int) -> List[int]:
        answer = []
        for i in range(1, len(height)):
            # 바로 앞에 위치한 수가 threshold보다 큰지 체크
            if height[i-1] > threshold:
                answer.append(i)
        return answer
class Solution:
    def stableMountains(self, height: List[int], threshold: int) -> List[int]:
        return [i for i in range(1, len(height)) if height[i-1] > threshold]

1652. Defuse the Bomb

  • You have a bomb to defuse, and your time is running out! Your informer will provide you with a circular array [code] of length of n and a key k.
  • To decrypt the code, you must replace every number. All the numbers are replaced simultaneously.
    • If k > 0, replace the iᵗʰ number with the sum of the next k numbers.
    • If k < 0, replace the iᵗʰ number with the sum of the previous k numbers.
    • If k == 0, replace the iᵗʰ number with 0.
  • As code is circular, the next element of code[n-1] is code[0], and the previous element of code[0] is code[n-1].
  • Given the circular array code and an integer key k, return the decrypted code to defuse the bomb!
  • Constraints
    • n == code.length
    • 1 n 100
    • 1 code[i] 100
    • -(n - 1) k n - 1
class Solution:
    def decrypt(self, code: List[int], k: int) -> List[int]:
        answer = []
        for i, num in enumerate(code):
            cur_sum = 0
            if k > 0:
                # 현재 위치에서 k개만큼 이후의 숫자들의 합 계산해서 추가
                for j in range(k):
                    cur_sum += code[(i + j + 1) % len(code)]
            else:
                # 현재 위치에서 k개만큼 이전 숫자들 합 계산해서 추가
                for j in range(-k):
                    cur_sum += code[i - j -1]
            answer.append(cur_sum)
        return answer
class Solution:
    def decrypt(self, code: List[int], k: int) -> List[int]:
        n = len(code)
        code = code * 2
        if k == 0:
            return [0] * n
        elif k > 0:
            for i in range(n):
                code[i] = sum(code[i+1:i+1+k])
            return code[:n]
        else:
            for i in range(n):
                code[i] = sum(code[i+n+k:i+n])
            return code[:n]
class Solution:
    def decrypt(self, code: List[int], k: int) -> List[int]:
        answer = []
        n = len(code)
        if k == 0:
            return [0] * n
        elif k > 0:
            for i in range(n):
                new = 0
                for j in range(1, k+1):
                    new += code[(i + j) % n]
                answer.append(new)
        else:
            for i in range(n):
                new = 0
                for j in range(1, abs(k)+1):
                    new += code[i - j]
                answer.append(new)
        return answer

https://leetcode.com/problems/find-indices-of-stable-mountains/

https://leetcode.com/problems/defuse-the-bomb/

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

Python - Algorithm #59  (0) 2024.09.27
Python - Algorithm #58  (0) 2024.09.26
Python - Algorithm #56  (0) 2024.09.24
Python - Algorithm #55  (0) 2024.09.23
Python - Algorithm #54  (0) 2024.09.20