본문 바로가기

Algorithm

(130)
[연결리스트] 15 역순 연결 리스트(Reverse Linked List) https://leetcode.com/problems/reverse-linked-list/ Reverse Linked List - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 연결 리스트를 뒤집는 문제입니다. 반복 구조를 통해 연결리스트를 뒤집으면 다음과 같습니다. # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = va..
[연결리스트] 14 두 정렬 리스트의 병합(Merge Two Sorted Lists) https://leetcode.com/problems/merge-two-sorted-lists/ Merge Two Sorted Lists - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 정렬되어 있는 두 연결 리스트를 합치는 문제입니다. 이때, 각 리스트가 정렬된 상태라는 점이 중요합니다. 병합 정렬의 마지막 조합과 동일한 방식으로 재귀를 통해 코드를 작성하면 다음과 같습니다. # Definition for singly-linked list. # class L..
[연결리스트] 13 팰린드롬 연결 리스트(Palindrome Linked List) https://leetcode.com/problems/palindrome-linked-list/ Palindrome Linked List - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 연결 리스트가 팰린드롬 구조인지 판별하는 문제입니다. 연결 리스트에서 값을 뽑아 리스트에 넣어준 후, 슬라이싱을 통해 원본 리스트가 뒤집은 리스트와 같은지 비교했습니다. # Definition for singly-linked list. # class ListNode: # def..
8/3 스터디 정리중 reverse(): 리스트반환 reversed(): reversed객체반환(형변환필요) ''.join(reversed(str)) 문자열뒤집기 문자열 뒤집기 str[::-1] max함수: key를 기준으로 최대반환 예외 부분들 잘 처리하기 dp[i][j] 인덱스 i부터 j까지 팰린드롬인지 True .index에서 해당값없으면 에러난다 find는 이터레이터 반환, find_if도 있다 .end()는 마지막에서 하나 더 뒤 .begin()는 맨첫번째 set에 리스트 여러개가 안먹는다 벡터에 erase로 지울수 있다 vector는 erase하나만 ::iterator 붙이는건 저거에 대한 이터레이터 쓴다는뜻 array 자료형 list 자료형 vector 자료형 capacity와 size차이 erase와 remov..
[배열] 12 주식을 사고팔기 가장 좋은 시점(Best Time to Buy and Sell Stock) https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ Best Time to Buy and Sell Stock - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 한 번의 거래로 낼 수 있는 최대 이익을 산출하는 문제입니다. 이 문제는 N이 최대 105이므로 브루트포스를 통한 O(N2)으로는 풀 수 없습니다. 처음 풀이한 코드는 다음과 같습니다. 가격의 최저점을 갱신해나가고, 최저점보다 가격이 높다면 시세..
[배열] 11 자신을 제외한 배열의 곱(Product of Array Except Self) https://leetcode.com/problems/product-of-array-except-self/ Product of Array Except Self - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 배열을 입력받아 output[i]가 자신을 제외한 나머지 모든 요소의 곱셈 결과가 되도록 출력하는 문제입니다. 이때 나눗셈을 하지 않고 O(N)에 풀이해야 되는 점이 핵심입니다. 처음에 머리에 든 생각은 미리 전체 곱셈 값을 구해놓고 각 항목별로 자기 자신..
[배열] 10 배열 파티션 I(Array Partition I) https://leetcode.com/problems/array-partition-i/ Array Partition I - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com n개의 페이를 이용한 min(a,b)의 합으로 만들 수 있는 가장 큰 수를 출력하는 문제입니다. 두 페어씩 묶어서 최대한 크게 만들어야 하는데, 예시를 살펴보면 모두 정렬후에 앞에서부터 2개씩 묶습니다. 따라서 짝수index(0,2,4,...)인 값들의 합이 문제의 답입니다.(정렬된 상태에서 짝..
[배열] 09 세 수의 합(3Sum) https://leetcode.com/problems/3sum/ 3Sum - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 배열을 입력받아 합으로 0을 만들 수 있는 3개의 엘리먼트를 출력하는 문제입니다. 배열 길이의 최댓값이 3000이므로 브루트포스를 통한 O(N3)으로는 시간초과가 납니다. 따라서 배열을 정렬한 이후에 투 포인터를 이용해서 간격을 좁혀나가며 sum을 계산해 나가야 합니다. sum이 0보다 작다면 left를 우측으로, 0보다 크다면 right를..