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

Python - Algorithm #14

hkl22 2024. 7. 2. 10:07

Python Algorithm

LeetCode

2315. Count Asterisks

  • You are given a string s, where every two consecutive vertical bars '|' are grouped into a pair. In other words, the 1st and 2nd '|' make a pair, the 3rd and 4th '|' make a pair, and so forth.
  • Return the number of '*' in s, excluding the '*' between each pair of '|'.
  • Note that each '|' will belong to exactly one pair.
  • Constraints
    • 1 s.length 1000
    • s consists of lowercase English letters, vertical bars '|', and asterisks '*'.
    • s contains an even number of vertical bars '|'.
class Solution:
    def countAsterisks(self, s: str) -> int:
        # split, count, for, if
        answer = 0
        word_list = s.split("|")
        for i in range(0, len(word_list), 2):
            # word_list[i] 안에 포함되어 있는 *의 개수 세어서
            # answer에다가 추가하기
            answer += word_list[i].count("*")
        return answer
class Solution:
    def countAsterisks(self, s: str) -> int:
        answer = 0
        l = s.split("|")
        for i in range(0, len(l)+1, 2):
            answer += l[i].count("*")
        return answer

1608. Special Array With X Elements Greater Than or Equal X

  • You are given an array [nums] of non-negative integers. [nums] is considered special if there exists a number x such that there are exactly x numbers in [nums] that are greater than or equal to x.
  • Notice that x does not have to be an element in [nums].
  • Return x if the array is special, otherwise, return -1. It can be proven that if [nums] is special, the value for x is unique.
  • Constraints
    • 1 nums.length 100
    • 0 nums[i] 1000
class Solution:
    def specialArray(self, nums: List[int]) -> int:
        nums = sorted(nums, reverse=True)
        answer = -1
        # [4, 4, 3, 0, 0]
        # 0, 1, 2, 3, 4, 5
        # 만약 0일 때 special이라면, 0보다 큰 수가 0개 있어야 한다. X
        # 만약 1일 때 special이라면, 1보다 같거나 큰 수가 1개 있어야 한다. X
        # 만약 2일 때 special이라면, 2보다 같거나 큰 수가 2개 있어야 한다. X
        # 만약 3일 때 special이라면, 3보다 같거나 큰 수가 3개 있어야 한다. O
        # i 번째 element가 x보다 같거나 크고
        # i+1번째 element가 x보다 작으면 special!
        # 이 때, i+1 번째 element가 존재하지 않는 경우 예외 처리
        for i in range(len(nums)):
            x = i + 1
            if i == len(nums) - 1:
                if nums[i] >= x:
                    answer = x
            elif nums[i] >= x and nums[i+1] < x:
                answer = x
                break
        return answer
class Solution:
    def specialArray(self, nums: List[int]) -> int:
        nums.sort()
        n = len(nums)
        for i in range(1, n+1):
            cnt = 0
            for j in nums:
                if j >= i:
                    cnt += 1
            if cnt == i:
                return cnt
        return -1

https://leetcode.com/problems/count-asterisks/

https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x/

 

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

Python - Algorithm #16  (0) 2024.07.04
Python - Algorithm #15  (0) 2024.07.03
Python - Algorithm #13  (0) 2024.07.01
Python - Algorithm #12  (0) 2024.06.28
Python - Algorithm #11  (0) 2024.06.27