본문 바로가기

자료구조,알고리즘

(51)
LeetCode-167 Two SUM II - Input Array Is Sorted 문제 오름차순으로 정렬된 int형 배열이 주어졌을 때, 배열의 두 요소를 더한 값이 target인 요소들의 인덱스를 구하는 문제입니다. 주어진 자료형이 배열이고, 한쌍의 값을 충족시키는 경우를 찾는 문제이기 때문에 Two Pointers를 적용해 볼 수 있습니다. 풀이 첫번째 풀이입니다. 배열의 맨 첫번째 요소를 startIdx, 그 다음 요소를 endIdx로 설정합니다. 1. endIdx로 startIdx의 바로 다음요소부터 배열의 가장 오른쪽까지 조회합니다. 2. 이 과정에서 두 요소의 합이 target과 같다면 각각 인덱스를 반환합니다. 3. 이 과정에서 두 요소의 합이 target보다 작다면 계속해서 endIdx를 +1 해가며 endIdx가 배열의 끝까지 갈때까지 조회합니다. 4. endIdx가 ..
LeetCode-125 Valid Palindrome 파이썬 풀이 Valid Palindrome - LeetCode Can you solve this real interview question? Valid Palindrome - 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 cha leetcode.com 문제 주어진 문자열에서 알파벳과 숫자가 아닌 특수 문자를 모두 제거하고, 모든 알파벳을 소문자로 변경합니다. 이렇게 변경된 문자열을 뒤집었쓸 때 와 뒤집지 않았을 때가 동일하면 t..
LeetCode-122 Best time to sell stock2 파이썬 풀이 Best Time to Buy and Sell Stock II - LeetCodeCan you solve this real interview question? Best Time to Buy and Sell Stock II - You are given an integer array prices where prices[i] is the price of a given stock on the ith day. On each day, you may decide to buy and/or sell the stock. You can only holdleetcode.com문제앞서 작성하였던 문제와 유사한 문제입니다. 배열에 그날그날의 주식 값이 나열되어있고 가장 이익을 크게 낼 수 있는 값을 계산하여 반환하면 됩니다. 이..
LeetCode-55 Jump Game 88 파이썬 풀이 문제배열의 맨 처음 인덱스 부터 시작하여 현재 위치한 인덱스와 해당 인덱스의 값을 더한 합이 최대로 점프할수 있는 양이라고 했을때, 배열의 마지막까지 도달할 수 있는지를 판단하는 문제입니다. 배열 한칸 이동시마다 1씩 소모 됩니다.풀이배열을 하나씩 순회하면서 해당 요소의 인덱스와 값을 더한 합이 현재 저장되어있는 count보다 크다면 해당 합을 저장합니다. 이때 i가 count에 저장되어 있는 값 보다 크다면 i까지 이동할 수 없기때문에 false를 반환합니다. 반대로 count가 배열의 마지막 인덱스보다 커진다면 배열의 끝까지 도달할 수 있기때문에 true를 반환합니다. class Solution: def canJump(self, nums: List[int]) -> bool: count = nums[0..
LeetCode-88 Merge sort array Merge Sorted Array - LeetCodeCan you solve this real interview question? Merge Sorted Array - You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively. Merge nums1 anleetcode.com 처음 문제를 보았을 때 nums2의 각 요소들을 키 값으로 하고 nums1을 배열로 하여 이진 탐색을 통해 해결할 수 있다고 생각했습니다. 애초에 주어진 배열이 오름차순으로 정렬되..
LeetCode-151 Reverse Words in a String 파이썬 풀 Reverse Words in a String - LeetCode Can you solve this real interview question? Reverse Words in a String - Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space. Return a strin leetcode.com 문제 문장을 단어단위로 뒤집는 문제입니다. 예를 들어 the sky is blue는 blue is sky the로 변경하여 반환하면 됩니다. 풀이 split을 사용..
LeetCode-121 Best Time to Buy and Sell Stock 파이썬 문제 풀이 Best Time to Buy and Sell Stock - LeetCode Can you solve this real interview question? Best Time to Buy and Sell Stock - You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosin leetcode.com 문제 매일 달라지는 주식의 값이 배열에 저장되어 있습니다. 배열의 0부터 하루 지날떄마다 다음 순서로 넘어간다고 했을 때 한번 사고, 한번 팔수 있었을 ..
LeetCode-189 Rotate Array 파이썬 문제 풀이 Rotate Array - LeetCode Can you solve this real interview question? Rotate Array - Given an integer array nums, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 step leetcode.com 문제 int형 배열 nums 안의 요소들을 k 만큼 오른쪽으로 회전 시키는 문제입니다. 예를들어 [ 1, 2, 3, 4 ] 배열을 두번 회전 하면 1,2가 순서대로 배열 뒤쪽으로 배치되어 [ ..