⚙️ Problem Solving

Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.Implement the MinStack class:- MinStack() initializes the stack object.- void push(int val) pushes the element val onto the stack.- void pop() removes the element on the top of the stack.- int top() gets the top element of the stack.- int getMin() retrieves the minimum element in the stack.You must i..
Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:Each row must contain the digits 1-9 without repetition.Each column must contain the digits 1-9 without repetition.Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.Note:A Sudoku board (partially filled) could be valid but is not necess..
Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.You must write an algorithm that runs in O(n) time.longest consecutive sequence를 찾는 문제! O(n) 안으로 풀어야 한다.주의할 점은 constraints에 nums의 length가 0부터 가능하다는 것  Hash Set을 이용한 풀이class Solution: def longestConsecutive(self, nums: List[int]) -> int: numSet = set(nums) longest = 0 f..
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.Given a string s, return true if it is a palindrome, or false otherwise.유효한 palindrome(회문) 검사하는 메서드 구현하기! 풀이1: Reverse Stringclass Solution: def isPalindrome(self, s:..
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.An input string is valid if:Open brackets must be closed by the same type of brackets.Open brackets must be closed in the correct order.Every close bracket has a corresponding open bracket of the same type.괄호가 올바르게 짝지어졌는지 확인하는 문제! 풀이1: Stack 이용class Solution: def isValid(sel..
Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.You must write an algorithm that runs in O(n) time and without using the division operation.division 연산 없이 O(n)의 시간 복잡도로 계산하라고 제약 조건이 주어진 문제였다! 풀이class Solution: def produ..
Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.빈도수가 높은 순서대로, 상위 k개의 elements를 출력하는 문제다. 풀이 1class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: count = defaultdict(int) for i in nums: count[i] += 1 result = sorted(count, key=count.get, reverse=Tr..
anagram은 일단 sort 후 비교를 떠올리자! 풀이1: 리스트 활용class Solution: def groupAnagrams(self, strs: List[str]) -> List[List[str]]: result = [] visited = [False] * len(strs) for i in range(len(strs)): if visited[i]: continue tmp = [] tmp.append(strs[i]) visited[i] = True for j in range(len(strs)): if so..
내 풀이: 브루트포스class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: result = set() for i in range(len(nums)): for j in range(len(nums)): if nums[i] + nums[j] == target and i != j: result.add(i) result.add(j) result = list(result) return sorted(result)set에다가 추가하고 리스트로 바꾸고 sor..
풀이1: 리스트 사용class Solution: def isAnagram(self, s: str, t: str) -> bool: if len(s) != len(t): return False letters = [] for i in s: letters.append(i) for i in t: if i in letters: letters.remove(i) else: return False return True리스트를 사용하여 단순하게 구현한 풀이 풀이2: sorted 후 ..