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

Python - Algorithm #30

hkl22 2024. 8. 1. 10:14

Python Algorithm

LeetCode

2114. Maximum Number of Words Found in Sentences

  • A sentence is a list of words that are separated by a single space with no leading or trailing spaces.
  • You are given an array of strings [sentences], where each sentences[i] represents a single sentence.
  • Return the maximum number of words that appear in a single sentence.
  • Constraints
    • 1 sentences.length 100
    • 1 sentences[i].length 100
    • sentences[i] consists only of lowercase English letters and ' ' only.
    • sentences[i] does not have leading or trailing spaces.
    • All the words in sentences[i] are separated by a single space.
class Solution:
    def mostWordsFound(self, sentences: List[str]) -> int:
        # 문자열 split, len, max
        answer = 0
        for sentence in sentences:
            words = sentence.split(" ")
            # words의 길이와 answer를 비교해서 더 큰 값을 answer로 지정
            answer = max(len(words), answer)
        return answer
class Solution:
    def mostWordsFound(self, sentences: List[str]) -> int:
        return max([len(sentence.split(" ")) for sentence in sentences])
class Solution:
    def mostWordsFound(self, sentences: List[str]) -> int:
        answer = []
        for s in sentences:
            answer.append(len(s.split(" ")))
        return max(answer)

2027. Minimum Moves to Convert String

  • You are given a string s consisting of n characters which are either 'X' or 'O'.
  • A move is defined as selecting three consecutive characters of s and converting them to 'O'. Note that if a move is applied to the character 'O', it will stay the same.
  • Return the minimum number of moves required so that all the characters of s are converted to 'O'.
  • Constraints
    • 3 s.length 1000
    • s[i] is either 'X' or 'O'.
class Solution:
    def minimumMoves(self, s: str) -> int:
        answer = 0
        c_list = [c for c in s]
        for i, c in enumerate(c_list):
            if c == "X":
                # 현재 위치의 문자 변경
                c_list[i] = "O"
                # 다음 위치의 문자 변경
                # 다다음 위치의 문자 변경
                # 이때, 다음 혹은 다다음 위치가 범위를 벗어날 경우 예외 처리
                answer += 1
                if i + 1 >= len(c_list):
                    continue
                c_list[i+1] = "O"
                if i + 2 >= len(c_list):
                    continue
                c_list[i+2] = "O"
        return answer
class Solution:
    def minimumMoves(self, s: str) -> int:
        answer = 0
        i = 0
        while i < len(s):
            if s[i] == "X":
                answer += 1
                i += 3
            else:
                i += 1
        return answer
class Solution:
    def minimumMoves(self, s: str) -> int:
        s = list(s)
        answer = 0
        for i in range(len(s)-2):
            if s[i] == "O" and i+3 < len(s):
                continue
            cnt = 0
            for j in range(3):
                if s[i+j] == "X":
                    s[i+j] = "O"
                    cnt += 1
            if cnt != 0:
                answer += 1
            if "X" not in s:
                return answer

https://leetcode.com/problems/maximum-number-of-words-found-in-sentences/

https://leetcode.com/problems/minimum-moves-to-convert-string/

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

Python - Algorithm #32  (0) 2024.08.05
Python - Algorithm #31  (0) 2024.08.02
Python - Algorithm #29  (0) 2024.07.31
Python - Algorithm #28  (0) 2024.07.30
Python - Algorithm #27  (0) 2024.07.29