post-thumbnail

LeetCode - Insert Interval

Description > Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Example 2: 겹치지 않는 정수 범위 배열이 범위의 시작을 기준으로 정렬된 상태로 주어졌을 때 새롭게 주어진 범위를 원래 배열에 삽입하는 문제. 만약 오버랩 되는 부분이 있으면 기존 범위를 merge한다. 풀이 첫번째 시도 정렬되어 있다는 점에서 착안해서 이분탐색을 생각해보았다.(결과적으론 뻘짓) 먼저 최종적으로 삽입될 범위에 대한 배열과 병합되어야 할 배열 요소들의 시작과 끝을 담을 변수를 선언해두었다. 그리고 범위의 시작과 끝에 대해 이분탐색

2020년 9월 13일
·
0개의 댓글
·
post-thumbnail

[알고리즘] LeetCode - 33. Search in Rotated Sorted Array

Description > Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., [0, 1, 2, 4, 5, 6, 7] might become [4, 5, 6, 7, 0, 1, 2].) You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplicate exists in the array. Your algorithm's runtime complexity must be in the order of O(logn). > 번역) 오름차순으로 정렬된 배열이 특정 지점에서 회전되어 있다고 가정하자. 회전하는 축은 알려져있지 않다.(예를들어 배열 `[0, 1, 2, 4, 5, 6, 7

2020년 8월 11일
·
0개의 댓글
·
post-thumbnail

[알고리즘] LeetCode - 42. Trapping Rain Water

Description > Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. (번역) 높이를 나타내는 음이 아닌 정수로 구성된 배열이 주어졌을 때 비가 온 후 담기게 될 물의 양을 구하라. 각 bar의 폭은 1이다. Example: 풀이 뭔가 stack을 써야할 것 같은 느낌적인 느낌이 든다. Stack에 elevation들을 쌓다가 직전 elavation보다 높은 elavation을 만났을 때 기존 스택에 쌓인 bar들을 찾아가며 계산을 하면

2020년 8월 10일
·
0개의 댓글
·
post-thumbnail

[알고리즘] LeetCode - 48. Rotate Image

Description > Given an n by n 2D matrix representing an image. Rotate the image by 90 degrees(clockwise). (번역) 이미지를 나타내는 n x n 매트릭스가 주어졌을 때, 이미지를 90도(시계방향) 회전한 결과를 리턴하라. Note > You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation. (번역) 별도의 2D 매트릭스를 생성하지 않고 in-place하게 Input으로 주어진 매트릭스를 수정하라. Example 풀어보자 In-place?? 사실 그냥 회전시키는 건 그렇게 어렵지 않다. 각각의 row를 새로운

2020년 8월 7일
·
0개의 댓글
·

[알고리즘] LeetCode - 32. Longest Valid Parentheses

Description > Given a string containing just the characters "(" and ")", find the length of the longest valid(well-formed) parentheses substring. Example 1 Example 2 풀이 Brute force로 풀면 시간 복잡도가 O(n^3) 이므로 좀 더 효율적인 방법을 찾아보자. 1. Stack 활용하기 먼저 stack에 초기값으로 -1을 넣어준다. Input string을 순회하면서 "("을 만나면 stack에 해당 index를 넣어준다. ")"인 경우 stack의 element 하나를 제거한다. 그 다음: stack에 element가 없는 경우: 현재 index를 stack에 넣어준다. stack에 element가 있는 경우: 현재 index와 stack의 top

2020년 8월 6일
·
0개의 댓글
·
post-thumbnail

[알고리즘] LeetCode - Next Permutation

Description > - Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replacement must be in-place and use only constant extra memory. > - Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column. 1,2,3 → 1,3,2 3,2,1 → 1,2,3 `1,1,5 → 1,5,

2020년 8월 4일
·
0개의 댓글
·
post-thumbnail

[알고리즘] LeetCode - 3Sum

😳 영 좋지 않은 제목 3sum 물론 저 제목을 보고 이상한 걸 떠올리면 인성에 문제가 있다고 봐야... Description > Given an array nums of n integers, are there elements, a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. n개의 정수로 이루어진 배열을 입력받아 세 요소의 합이 0이 되는 부분 집합을 반환하라. Note > The solution set must not contain duplicate triplets. 부분집합은 unique 해야함.(중복되는거 있으면 안됨) Example 도전! 중복되는 케이스를 피하려면 어떻게 해야하면 좋을까. 우선 잘 모르겠지만 걍 닥치고 정렬부터 해봄 두번째로 든 생각은 오름차순으로 정

2020년 7월 28일
·
0개의 댓글
·
post-thumbnail

[알고리즘] LeetCode - String to Integer(atoi)

Description string을 integer로 반환하는 atoi를 구현하라. 해당 함수는 첫번째로, 공백이 아닌 문자가 나올 때 까지 최대한 많은 공백을 제거한다. 그런 다음 첫번째 문자가 + 또는 -인 경우 부호를 택한다. 그런다음 등장하는 숫자 모두를 integer로 변환한다. 이후에도 문자가 나올 수 있으나 integer변환 이후에 등장하는 문자는 변환에 아무런 영향을 주지 않으므로 무시한다. 만약 공백 제거 이후 첫번째 문자가 숫자가 아닌 경우 또는 공백 제거 이후 문자열이 존재하지 않는 경우에는 변환이 일어나지 않는다. 이런 경우 0을 반환한다. Note 공백은 모두 space charactor(' ')로 이루어져있다. 32비트 정수만을 저장할 수 있다고 가정한다. [-2^31, 2^31 - 1] 풀이 우선 단계를 아래와 같이 나누어보았다. 먼저 string의 왼쪽 공백을 모두 제거한다. 2.

2020년 7월 21일
·
0개의 댓글
·
post-thumbnail

[알고리즘] LeetCode - Two Sum

리트코드 문제풀이 - Two Sum > Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. > Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. > nums 인수로 주어진 정수 배열에서 두 수를 추출하여 더했을 때 target값이 되는 두 배열요소의 index를 배열 형태로 리턴하시오. 같은 요소를 두번 고르면 안됨. 정답은 반드시 하나 뿐임. 처

2020년 7월 10일
·
0개의 댓글
·