본문 바로가기

분류 전체보기

(73)
Two Pointers 문제 타 블로그의 글을 기억하기 위해 정리 & 저장해놓음. Two Pointers 알고리즘 ? 리스트에 순차적으로 접근해야 할 때 두 개의 점의 위치를 기록하면서 처리하는 알고리즘이다.위 알고리즘으로 풀 수 있는 대표적인 문제는 아래와 같다.특정한 합을 가지는 부분 연속 수열 찾기정렬되어 있는 두 리스트의 합집합 구하기  출처1. https://freedeveloper.tistory.com/393 [이것이 코딩 테스트다 with Python] 39강 투 포인터https://www.youtube.com/watch?v=ttLRltNDiCo&list=PLVsNizTWUw7H9_of5YCB0FmsSc-K44y81&index=39 투 포인터 (Two Pointers) 투 포인터 알고리즘은 리스트에 순차적으로 접근해야 ..
Hash map 문제 타 블로그의 글을 기억하기 위해 정리 & 저장해놓음. 해쉬 알고리즘 문제들은 파이썬의 딕셔너리를 사용하는 문제이다.파이썬의 딕셔너리가 Hash Table 과 같은 구조이다.해쉬 구조란? Key 와 Value 쌍으로 이루어진 데이터 구조를 뜻한다.보통 배열로 미리 Hash Table 크기 만큼 생성해서 사용한다.해쉬 테이블의 장 / 단점장점 :데이터 저장 / 검색 속도가 빠르다.중복 확인이 쉽다.단점 :저장 공간이 조금 더 많이 필요하다.충돌 해결 알고리즘이 필요하다.  출처1. https://davinci-ai.tistory.com/19 파이썬으로 구현하는 자료구조 요약 정리 - 해쉬 테이블(Hash Table)Writer: Harim Kang 해당 내용은 코딩 테스트 및 기술 면접을 대비하기 위해서 자..
[Top Interview 150 - Two Pointers] 167. Two Sum II - Input Array Is Sorted [Q] [Medium]Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1  모르겠어서 찾아보고 풀음하나 고정해놓고 하나씩 더해보려고 헀는데, 왼쪽 / 오른쪽 하나씩 줄여나가는 방법이 더 좋다는 생각이 들었다.l 과 r 의 + / - 는 달라지면 안된다. -> sorted 되어 있기 때문에 # Answerclass Solution: def twoSu..
[Top Interview 150 - Two Pointers] 392. Is Subsequence [Q] [Easy]Given two strings s and t, return true if s is a subsequence of t, or false otherwise.A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not). 이런 문제가 취약점인가보다 좀 헷갈려서 찾아보고 풀었다. # Answ..
[Top Interview 150 - Two Pointers] 125. Valid Palindrome [Q] [Easy]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] 만 알면 쉬운 문제! 까먹고 있어서 어렵게 돌아가다가 ..
[Top Interview 150 - Array / String] 189. Rotate Array [Q] [Medium]Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.Constraints:1  일단 rotate 문제니까, deque.rotate(k) 쓰면 된다고 생각했다.그래서 작성했는데 먹히지 않음.이번엔 아래와 같이 작성했는데 또 안됨! 왜!조건들 때문이었다.. 그래서 if 문 추가하고 k가 n 보다 클 수 있어서 k%=n 추가하고 제출# Wrong Answerclass Solution: def rotate(self, nums: List[int], k: int) -> None: """ Do not return anything, modify num..
[Top Interview 150 - Array / String] 169. Majority Element [Q] [Easy]Given an array nums of size n, return the majority element.The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array. 일단 in-place 문제가 아니면 좀 더 편하게 풀 수 있는 것 같다.라고 생각했는데 제출하니까 틀렸다고 한다.아무리봐도 틀린 부분이 없는 것 같아 colab 에서 돌려봤더니 맞게 답이 나오는데 어디서 연산이 잘못 되었을까..n 이 $len(nums)$ 라고 했는데 내가 다른 변수로 써서 그런가 해서 m 으로 바꿔보기도 하고, f..
[Top Interview 150 - Array / String] 80. Remove Duplicates from Sorted Array II [Q] [Medium]Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice.The relative order of the elements should be kept the same.Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are..