LeetCode Problems/Easy

๐Ÿ˜Š LeetCode Easy Collections I - 20 Problems

metamong 2024. 8. 5.

0001. Two Sum / 0268. Missing Number

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        dct = dict()
        for i in range(len(nums)):
            dct[nums[i]] = i
        for i in range(len(nums)):
            if (target-nums[i]) in dct:
                if i!=dct[target-nums[i]]:
                    return [i,dct[target-nums[i]]]
class Solution:
    def missingNumber(self, nums: List[int]) -> int:
        nums.sort()
        for i in range(len(nums)):
            if i==0:
                if nums[i] != 0:
                    return 0
            else:
                if nums[i] != (nums[i-1]+1):
                    return (nums[i-1]+1)
        return len(nums)

 

๐Ÿงก 0001) [2, 7, 11, 15]๋ฅผ list๋กœ ๋Œ๋ฆด ๋•Œ target์ด 9์ผ ๊ฒฝ์šฐ 2 ๋‚˜ ์ž์‹ ์„ ์ œ์™ธํ•œ 7์ด list์— ์กด์žฌํ•˜๋Š” ์ง€๋ฅผ hash map O(1)๋กœ ๊ตฌํ˜„ํ•ด ์ƒ๋Œ€์ ์œผ๋กœ ๋น ๋ฅธ ์‹œ๊ฐ„ ๋‚ด์— ์ •๋‹ต ์ฝ”๋“œ ๊ตฌํ˜„ ๊ฐ€๋Šฅ

๐Ÿงก 0268) ๋จผ์ € sort()๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ ๋’ค, ๋งจ ์•ž์ด 0์ด ์•„๋‹ˆ๋ฉด ๋ฐ”๋กœ 0 ์ถœ๋ ฅ. index 1 ์ด์ƒ์ผ ๊ฒฝ์šฐ ๋ฐ”๋กœ ์•ž์˜ index์™€์˜ ์ฐจ์ด๊ฐ€ 1์ด ์•„๋‹ˆ๋ฉด ๋ฐ”๋กœ ์•ž์˜ index + 1์„ ์ถœ๋ ฅ. ๋งจ ๋งˆ์ง€๋ง‰๊นŒ์ง€ ๋ชจ๋‘ ์ •์ƒ์ ์œผ๋กœ +1์ด ๋‹ค ๋˜์—ˆ๋‹ค๋ฉด ๋งจ ๋งˆ์ง€๋ง‰ ์ˆซ์ž ์ถœ๋ ฅ


0020. Valid Parentheses / 0232. Implement Queue Using Stacks

class Solution:
    def isValid(self, s: str) -> bool:
        stack=[]
        for x in s:
            if stack:
                if stack[-1] == '(' and x == ')':
                    stack.pop()
                elif stack[-1] == '[' and x == ']':
                    stack.pop()
                elif stack[-1] == '{' and x == '}':
                    stack.pop()
                else:
                    stack.append(x)
            else:
                stack.append(x)
        if stack:
            return False
        else:
            return True
from collections import deque

class MyQueue:

    def __init__(self):
        self.stack_A, self.stack_B = [], []
        
    def push(self, x: int) -> None:
        self.stack_B.append(x)

    def pop(self) -> int:
        if self.stack_A:
            return self.stack_A.pop()
        else: #stack_B
            while self.stack_B:
                self.stack_A.append(self.stack_B.pop())
            return self.stack_A.pop()

    def peek(self) -> int:
        if self.stack_A:
            return self.stack_A[-1]
        else: #stack_B
            while self.stack_B:
                self.stack_A.append(self.stack_B.pop())
            return self.stack_A[-1]

    def empty(self) -> bool:
        if not self.stack_A and not self.stack_B:
            return True
        else:
            return False


# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()

 

๐Ÿงก 0020) stack์„ ํ™œ์šฉํ•ด validํ•œ ์ง€ ์•Œ์•„๋ณด๋Š” ๋ฌธ์ œ. stack์˜ pop()๊ณผ [-1] indexing์œผ๋กœ O(1) ๋น ๋ฅธ ์—ฐ์‚ฐ์„ ํ™œ์šฉํ•ด ๊ด„ํ˜ธ์˜ ์Œ์ด ์ ์ ˆํžˆ ๋งค์นญ๋˜์—ˆ๋Š”์ง€ ์‰ฝ๊ฒŒ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿงก 0232) Queue๋Š” ๋‘ ๊ฐœ์˜ stack์„ ํ™œ์šฉํ•ด ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์„ ์ฐธ์กฐ.

: stack ๋‘ ๊ฐœ๋ฅผ queue๋กœ ์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋งŒ, stack_A ์•ž ๋ถ€๋ถ„ stack์„ reverseํ•œ ๊ฒฐ๊ณผ์™€ stack_B๋ฅผ ๊ทธ๋Œ€๋กœ ๋ถ™์ธ ๊ฒฐ๊ณผ๊ฐ€ queue. queue์— append()ํ•œ ๊ฑด stack_B์— append() ์—ฐ์‚ฐ๊ณผ ๋™์ผ. queue ๋งจ ์™ผ์ชฝ peek๊ณผ pop์€ stack_A๋ฅผ stack_A[-1] indexing๊ณผ pop() ์—ฐ์‚ฐ๊ณผ ๋™์ผ.


0217. Contains Duplicate / 0013. Roman to Integer

class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:
        if len(set(nums)) != len(nums):
            return True
        else:
            return False
class Solution:
    def romanToInt(self, s: str) -> int:
        dct=dict()
        dct['I']=1
        dct['V']=5
        dct['X']=10
        dct['L']=50
        dct['C']=100
        dct['D']=500
        dct['M']=1000
        ans=0
        for x in range(len(s)):
            if x>=1:
                if s[x-1] == 'C' and s[x] in ['M','D']:
                    ans+=(dct[s[x]]-200)
                    continue
                elif s[x-1] == 'I' and s[x] in ['V','X']:
                    ans+=(dct[s[x]]-2)
                    continue
                elif s[x-1] == 'X' and s[x] in ['C','L']:
                    ans+=(dct[s[x]]-20)
                    continue
            ans+=dct[s[x]]
        return ans

 

๐Ÿงก 0217) duplicate ํ—ˆ์šฉ ์—ฌ๋ถ€๋Š” set ์ž๋ฃŒ๊ตฌ์กฐ ํ™œ์šฉ

๐Ÿงก 0013) I, C, X ์˜ˆ์™ธ ์ผ€์ด์Šค๋งŒ ๊ฐ๊ฐ ์•ž์— ์žˆ๋Š” ๋ฌธ์ž์— ๋”ฐ๋ผ dict[]์˜ ๊ฐ’์„ ์ผ์ • ์ˆ˜ ๋งŒํผ ๋นผ์ฃผ๊ณ , ๋‚˜๋จธ์ง€์˜ ๊ฒฝ์šฐ๋Š” ์ผ์ • ์ˆ˜๋ฅผ dictionary์˜ value๋กœ ๋”ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.


0009. Palindrome Number / 0409. Longest Palindrome

class Solution:
    def isPalindrome(self, x: int) -> bool:
        x=''.join(str(x))
        if x==x[::-1]:
            return True
        else:
            return False
from collections import Counter
class Solution:
    def longestPalindrome(self, s: str) -> int:
        ans=0
        odd_exists = False
        freq = Counter(s)
        for x in freq:
            if freq[x]%2==0:
                ans+=freq[x]
            else:
                odd_exists = True
                ans+=(freq[x]-1)
        if odd_exists:
            ans+=1
        return ans

 

๐Ÿงก 0009) ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด Palindrome์œผ๋กœ ์ขŒ์šฐ ๋’ค์ง‘์œผ๋ฉด ๋

๐Ÿงก 0409) Counter ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์˜ frequency dictionary ์ƒ์„ฑ. ์ตœ๋Œ€ํ•œ ๊ธด ๊ธธ์ด์˜ palindrome์„ ๋งŒ๋“ค๋ ค๋ฉด ์ฃผ์–ด์ง„ frequency dictionary์—์„œ ์ง์ˆ˜ ๊ฐœ์ˆ˜์˜ frequency๋Š” ๋ฐ”๋กœ add up. ํ™€์ˆ˜ ๊ฐœ์ˆ˜์˜ frequency๋Š” -1 ํ•˜๊ณ  add up. ๋งŒ์•ฝ ํ™€์ˆ˜ ๊ฐœ์ˆ˜์˜ ๋ฌธ์ž๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด, ์ค‘์•™์— 1๊ฐœ ๋ฌธ์ž๋ฅผ ๋†“์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ +1.


0383. Ransom Note / 0242. Valid Anagram

from collections import Counter
class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        ransomNote_Counter = Counter(ransomNote)
        magazine_Counter = Counter(magazine)
        magazine_Counter_keys = magazine_Counter.keys()
        for letter in ransomNote_Counter:
            if letter not in magazine_Counter_keys:
                return False
            else:
                if ransomNote_Counter[letter] > magazine_Counter[letter]:
                    return False
        return True
from collections import Counter
class Solution:
    def isAnagram(self, s: str, t: str) -> bool:
        s_freq = Counter(s)
        t_freq = Counter(t)
        s_freq_keys = s_freq.keys()
        t_freq_keys = t_freq.keys()
        if s_freq_keys != t_freq_keys:
            return False
        for s_freq_key in s_freq_keys:
            if s_freq[s_freq_key] != t_freq[s_freq_key]:
                return False
        return True

 

๐Ÿงก 0383) ransomNote์˜ freq dictionary์™€  magazine์˜ freq dictionary๋ฅผ ๋ชจ๋‘ ๊ตฌํ•œ๋‹ค. ์ดํ›„ ransoeNote_Counter์˜ key๊ฐ€ magazine_Counter์— ์กด์žฌํ•˜์ง€ ์•Š๊ฑฐ๋‚˜, ์กด์žฌํ•˜๋”๋ผ๋„ ransomNote_Counter์˜ key์— ํ•ด๋‹นํ•˜๋Š” value๊ฐ€ magazine_Counter์˜ key์— ํ•ด๋‹นํ•˜๋Š” value๋ณด๋‹ค ํฌ๋‹ค๋ฉด magazine์œผ๋กœ ๋” ์ด์ƒ ๋งŒ๋“ค ์ˆ˜ ์—†์œผ๋ฏ€๋กœ False. ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ๋Š” magazine์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ True

๐Ÿงก 0242) anagram์˜ ์—ฌ๋ถ€๋Š” ๋‘ ๋ฌธ์ž์—ด์˜ Counter ๊ฒฐ๊ณผ๋กœ ๋งŒ๋“ค์–ด์ง„ frequency dictionary ์ผ์น˜ ์—ฌ๋ถ€๋กœ ์•Œ ์ˆ˜ ์žˆ๋‹ค. keys๊ฐ€ ์„œ๋กœ ์ผ์น˜ํ•˜์ง€ ์•Š์œผ๋ฉด ๋‹น์—ฐํžˆ False. ์„œ๋กœ ์ผ์น˜ํ•˜๋”๋ผ๋„ ๋นˆ๋„์ˆ˜๊ฐ€ ๋‹ค๋ฅด๋ฉด ๋งŒ๋“ค ์ˆ˜ ์—†์œผ๋ฏ€๋กœ(ex ab์™€ aabb) ๋นˆ๋„์ˆ˜๊ฐ€ ํ•˜๋‚˜๋ผ๋„ ๋‹ค๋ฅด๋‹ค๋ฉด ๋ฐ”๋กœ False ๋ฆฌํ„ด. ๋ชจ๋“  for๋ฌธ์„ ๋‹ค ๊ฑฐ์ณค๋‹ค๋Š” ๊ฑด True์ด๋ฏ€๋กœ True ๋ฆฌํ„ด


0136. Single Number / 0069. Sqrt(x)

from collections import Counter
class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        freq = Counter(nums)
        for x in freq:
            if freq[x] == 1:
                return x
class Solution:
    def mySqrt(self, x: int) -> int:
        if x == 0: return 0
        elif x == 1: return 1
        start,end=1,x
        while start<=end:
            mid=(start+end)//2
            if x<mid*mid:
                end=mid-1
            elif x==(mid*mid):
                return mid
            else:
                ans=mid
                start=mid+1
        return ans

 

๐Ÿงก 0136) ๋นˆ๋„์ˆ˜ 1์ธ ๊ฒฝ์šฐ Counter๋กœ ๋Œ๋ฆฌ๋ฉด ๋จ.

๐Ÿงก 0069) x์˜ sqrt(x)๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ. ์ง์ ‘ O(n)์œผ๋กœ brute-force๋กœ ๋Œ๋ ค๋„ ๋˜๋‚˜, O(logn)์œผ๋กœ binary search ํ’€์ด๋„ ๊ฐ€๋Šฅ. start์™€ end๋ฅผ ๊ฐ๊ฐ 1๊ณผ x๋กœ ์žก๊ณ  ์ค‘๊ฐ„ mid๋ฅผ ์ œ๊ณฑํ•œ ๊ฒฐ๊ณผ๊ฐ€ x๋ณด๋‹ค ํฐ ์ง€, ๊ฐ™์€ ์ง€, ์ž‘์€ ์ง€์— ๋”ฐ๋ผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฌธ์ œ. 


0374. Guess Number Higher or Lower / 0035. Search Insert Position

# The guess API is already defined for you.
# @param num, your guess
# @return -1 if num is higher than the picked number
#          1 if num is lower than the picked number
#          otherwise return 0
# def guess(num: int) -> int:

class Solution:
    def guessNumber(self, n: int) -> int:
        start,end=1,n
        while start<=end:
            mid=(start+end)//2
            if guess(mid) == 0:
                return mid
            elif guess(mid) == 1:
                start=mid+1
            else:
                end=mid-1
class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        start,end=0,len(nums)-1
        ans=len(nums)
        while start<=end:
            mid=(start+end)//2
            if nums[mid] == target:
                return mid
            elif nums[mid] > target:
                ans=mid
                end=mid-1
            else:
                start=mid+1
        return ans

 

๐Ÿงก 0374) predefined ๋˜์–ด ์žˆ๋Š” guess() ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•ด 1๋ถ€ํ„ฐ n๊นŒ์ง€์˜ ์ˆ˜ ์ค‘ ์ž„์˜๋กœ ๊ณ ๋ฅธ ์ˆ˜๋ณด๋‹ค up, same, down์ธ์ง€์— ๋”ฐ๋ผ ํƒ์ƒ‰ ๋ฒ”์œ„ ์ ˆ๋ฐ˜์œผ๋กœ ์ค„์—ฌ๊ฐ€๋ฉฐ ํ™•์ธ. ํƒ์ƒ‰ ๋ฒ”์œ„ ์ ˆ๋ฐ˜์œผ๋กœ ์ค„์—ฌ๊ฐ€๋ฉฐ(์ด๋ถ„ํƒ์ƒ‰) target ํƒ์ƒ‰ํ•˜๋ฏ€๋กœ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(logn)

๐Ÿงก 0035) ์ฃผ์–ด์ง„ ์˜ค๋ฆ„์ฐจ์ˆœ ๋ฐฐ์—ด์—์„œ target์ด ์–ด๋”” ์œ„์น˜์— ๋“ค์–ด๊ฐˆ ์ง€ index๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ. start์™€ end๋ฅผ index 0๊ณผ index len(nums)-1๋กœ ์„ค์ •ํ•˜๊ณ  ๊ทธ ์ ˆ๋ฐ˜ index์— ํ•ด๋‹นํ•˜๋Š” ์›์†Œ๋ณด๋‹ค target์ด ํฐ์ง€/์ž‘์€์ง€/๊ฐ™์€์ง€์— ๋”ฐ๋ผ binary search ์ง„ํ–‰. ์ด ๋•Œ, target์ด ์ „์ฒด list์˜ ์ตœ๋Œ“๊ฐ’(๋งจ ์˜ค๋ฅธ์ชฝ ๊ฐ’)๋ณด๋‹ค ํฌ๋‹ค๋ฉด default๋กœ ๊ธธ์ด๊ฐ€ ์ถœ๋ ฅ๋˜๊ฒŒ๋”, ans = len(nums)๋กœ ์„ค์ •.


0278. First Bad Version / 0121. Best Time to Buy and Sell Stock

# The isBadVersion API is already defined for you.
# def isBadVersion(version: int) -> bool:

class Solution:
    def firstBadVersion(self, n: int) -> int:
        if n == 1: return 1
        start,end=1,n
        while start<=end:
            mid=(start+end)//2
            if isBadVersion(mid):
                ans=mid
                end=mid-1
            else:
                start=mid+1
        return ans
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        ans = 0
        cur_min = prices[0]
        for price in prices[1:]:
            ans = max(ans,price-cur_min)
            if price<cur_min:
                cur_min=price
                
                
        return ans

 

๐Ÿงก 0278) start์™€ end๋ฅผ ๊ฐ๊ฐ 1๊ณผ n์œผ๋กœ ๋‘๊ณ , mid์˜ isBadVersion() ๊ฒฐ๊ณผ์— ๋”ฐ๋ผ update. ๊ฐ€์žฅ ์ฒซ๋ฒˆ์งธ BadVersion์˜ ๋ฒˆํ˜ธ๋ฅผ ๊ตฌํ•˜๋ฏ€๋กœ isBadVersion์ด ๋งž์„ ๋•Œ ans = mid ์„ค์ •

๐Ÿงก 0121) DP ๋ฌธ์ œ๋กœ, prices list๋ฅผ for๋ฌธ์œผ๋กœ ๋Œ๋ฆฌ๋ฉด์„œ ์•ž์˜ list์˜ ์ตœ์†Ÿ๊ฐ’ update. ํ˜„์žฌ ์›์†Œ์˜ ์œ„์น˜ index๊ฐ€ i๋ผ๋ฉด index 0 ~ (i-1)๊นŒ์ง€ ํ•ด๋‹นํ•˜๋Š” ์›์†Œ ์ค‘์— ์ตœ์†Ÿ๊ฐ’์ด prices[i]๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ans update(์ด ๋•Œ ans๋Š” ์ตœ๋Œ“๊ฐ’ ์œ ์ง€). ์•ž๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋ˆ„์ ํ•ด์„œ ๊ฒฐ๊ณผ๋ฅผ updateํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ฏ€๋กœ bottom-up DP


0070. Climbing Stairs / 0733. Flood Fill

class Solution:
    def climbStairs(self, n: int) -> int:
        dp=[0]*(n+1)
        if n == 1:
            return 1
        else:
            dp[1],dp[2]=1,2
            for x in range(3,n+1):
                dp[x] = dp[x-2]+dp[x-1]
            return dp[n]
from collections import deque

def BFS(graph, visited, start,color):
    m,n=len(graph),len(graph[0])
    start_color = graph[start[0]][start[1]]
    graph[start[0]][start[1]] = color
    queue = deque([start])
    visited[start[0]][start[1]] = True
    dx,dy=[-1,1,0,0],[0,0,-1,1]
    while queue:
        v=queue.popleft()
        for i in range(4):
            nx,ny=v[0]+dx[i],v[1]+dy[i]
            if 0<=nx<m and 0<=ny<n:
                if not visited[nx][ny] and graph[nx][ny] == start_color:
                    visited[nx][ny] = True
                    graph[nx][ny] = color
                    queue.append((nx,ny))

class Solution:
    def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
        visited=[[False]*len(image[0]) for _ in range(len(image))]
        BFS(image,visited,(sr,sc),color)
        return image

 

๐Ÿงก 0070) bottom-up DP ์œ ํ˜•์œผ๋กœ, n๊ฐœ์˜ step์„ ๊ฑด๋„ˆ๋Š”๋ฐ 1๊ฐœ ๋˜๋Š” 2๊ฐœ์˜ step์„ ์‚ฌ์šฉํ•ด ๊ฑด๋„ˆ๋Š” ๊ฐ€์ง“์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ์œ ํ˜•. dp[n]์„ n๊ฐœ์˜ ๊ณ„๋‹จ์œผ๋กœ ๊ฑด๋„ˆ๋Š” ๊ฐ€์ง“์ˆ˜๋ผ๋ฉด, ์ ํ™”์‹์€ dp[n] = dp[n-1] + dp[n-2]๋กœ ์‰ฝ๊ฒŒ ๋‚ด๋ฆด์ˆ˜ ์žˆ๋‹ค(n≥3) 

๐Ÿงก 0733) flood-fill ์œ ํ˜•์œผ๋กœ, ์‹œ์ž‘์ ๋ถ€ํ„ฐ ๋™์ผํ•œ ๋ถ€๋ถ„๋งŒ ์ฃผ์–ด์ง„ color๋กœ ์น ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. dx,dy๋กœ ๋ฐฉํ–ฅ๋ฒกํ„ฐ๋งŒ๋“ค์–ด์„œ simulation ๋Œ๋ฆฌ๋ฉด์„œ BFS ํ˜•์‹์œผ๋กœ '์กฐ๊ฑด์— ๋งž๋Š”' ์ธ์ ‘๋…ธ๋“œ๋งŒ ๋™์ผ color๋กœ ์ฑ„์›Œ๋‚˜๊ฐ€๋ฉด ๋œ๋‹ค.


0463. Island Perimeter / 0844. Backspace String Compare

from collections import deque

def BFS(graph, visited, start):
    perimeter = 0
    queue = deque([start])
    visited[start[0]][start[1]] = True
    dx,dy=[-1,1,0,0],[0,0,-1,1]
    while queue:
        vx,vy = queue.popleft()
        near_islands=4
        for i in range(4):
            nx,ny=vx+dx[i],vy+dy[i]
            if 0<=nx<len(graph) and 0<=ny<len(graph[0]):
                if graph[nx][ny] == 1:
                    near_islands-=1
                    if not visited[nx][ny]:
                        visited[nx][ny] = True
                        queue.append((nx,ny))
        perimeter+=near_islands
    return perimeter


class Solution:
    def islandPerimeter(self, grid: List[List[int]]) -> int:
        row,col=len(grid),len(grid[0])
        visited=[[False]*col for _ in range(row)]
        for i in range(row):
            for j in range(col):
                if grid[i][j] == 1:
                    return BFS(grid,visited,(i,j))
def get_stack(string):
        stack = []
        for s in string:
            if s == '#':
                if stack: stack.pop()
            else:
                stack.append(s)
        return stack

class Solution:

    def backspaceCompare(self, s: str, t: str) -> bool:
        after_s = get_stack(s)
        after_t = get_stack(t)
        if after_s == after_t:
            return True
        else:
            return False

 

๐Ÿงก 0463) ์—ฐ๊ฒฐ์˜์—ญ์˜ ๋‘˜๋ ˆ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ. ๊ฐ€๋Šฅ near_islands = 4๋ผ ์„ค์ •ํ•œ ๋’ค, ์ฃผ์œ„ ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ณณ ์žˆ์„ ๋•Œ๋งˆ๋‹ค -1. 4๋ฐฉํ–ฅ ๊ฐ๊ฐ ํ™•์ธ ๋’ค update๋œ near_islands ๊ฐ’์„ perimeter์— ๊ณ„์† add up.

๐Ÿงก 844) ์Šคํƒ ๋ฌธ์ œ. #์ด ์žˆ๋‹ค๋ฉด stack์— ๋ฌด์—‡์ด ์žˆ์„ ๊ฒฝ์šฐ ๋ฐ”๋กœ pop(). backspace๋กœ ๋ชจ๋‘ ์ง€์šฐ๊ณ  ๋‚œ ํ›„ ๊ฐ™์€์ง€ ๋‹ค๋ฅธ ์ง€ ์„œ๋กœ ๋น„๊ตํ•˜๋ฉด ๋


'LeetCode Problems > Easy' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

๐Ÿ˜ LeetCode Easy Collections II - 14 Problems  (1) 2024.09.02

๋Œ“๊ธ€