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

Python - Algorithm #11

hkl22 2024. 6. 27. 10:06

Python Algorithm

LeetCode

2903. Find Indices With Index and Value Difference I

  • You are given a 0-indexed integer array [nums] having length n, an integer indexDifference, and an integer valueDifference.
  • Your task is to find two indices i and j, both in the range [0, n - 1], that satisfy the following conditions:
    • abs(i - j) ≥ indexDifference, and abs(nums[i] - nums[j]) valueDifference
  • Return an integer array answer, where answer = [i, j] if there are two such indices, and answer = [-1, -1] otherwise. If there are multiple choices for the two indices, return any of them.
  • Note:
    • i and j may be equal.
  • Constraints:
    • 1 n == nums.length 100
    • 0 nums[i] 50
    • 0 indexDifference 100
    • 0 valueDifference 50
class Solution:
    def findIndices(self, nums: List[int], indexDifference: int, valueDifference: int) -> List[int]:
        # 2중 for문을 이용해서 풀어볼 것
        for i in range(len(nums)):
            for j in range(len(nums)):
                # abs(i - j) >= indexDifference
                # abs(nums[i] - nums[j]) >= valueDifference
                # 두 가지 조건을 만족시키면 answer에 i, j append, break
                if abs(i - j) >= indexDifference and abs(nums[i] - nums[j]) >= valueDifference:
                    return [i, j]
        return [-1, -1]
class Solution:
    def findIndices(self, nums: List[int], indexDifference: int, valueDifference: int) -> List[int]:
        for i in range(len(nums)):
            for j in range(len(nums)):
                if abs(i - j) >= indexDifference and abs(nums[i] - nums[j]) >= valueDifference:
                    return [i, j]
        return [-1, -1]

2000. Reverse Prefix of Word

  • Given a 0-indexed string [word] and a character ch, reverse the segment of [word] that starts at index 0 and ends at the index of the first occurrence of ch (inclusive). If the character ch does not exist in word, do nothing.
  • For example, if word = "abcdefd" and ch = "d", then you should reverse the segment that starts at 0 and ends at 3 (inclusive). The resulting string will be "dcbaefd".
  • Return the resulting string.
  • Constraints:
    • 1 word.length 250
    • word consists of lowercase English letters.
    • ch is a lowercase English letter.
class Solution:
    def reversePrefix(self, word: str, ch: str) -> str:
        loc = word.find(ch)
        return word[:loc+1][::-1] + word[loc+1:]
class Solution:
    def reversePrefix(self, word: str, ch: str) -> str:
        n = word.find(ch)
        if n != -1:
            return word[n::-1] + word[n+1:]
        else:
            return word

https://leetcode.com/problems/find-indices-with-index-and-value-difference-i/

https://leetcode.com/problems/reverse-prefix-of-word/

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

Python - Algorithm #13  (0) 2024.07.01
Python - Algorithm #12  (0) 2024.06.28
SQL - Algorithm #10  (0) 2024.06.24
SQL - Algorithm #9  (0) 2024.06.21
SQL - Algorithm #8  (0) 2024.06.20