[python]itertools를 사용해서 고난이도 코딩테스트도 쉽게 풀자!
2020. 7. 18. 18:17
Programming/Python
Itertools APL, Haskell, SML에 영감을 받아 파이썬에 최적화되어 동작하도록 구성된 반복 연산자 패키지로 자체 또는 조합되어 효율적이고 빠르게 동작하며 operator 모듈과 효과적으로 동작함 코딩 테스트 풀이 시 굉장히 유용하게 사용되는 패키지 중 하나 generator와 같이 사용할 수 있다. (generator가 궁금하시다면 => "링크") * operator 모듈 : 파이썬의 내장 연산자에 해당하는 __add__ 등을 제공하는 패키지로 add, lt, ne 등 객체 비교, 논리 연산, 수학 연산, 시퀀스 등을 수행하는데, __func__ 가 아니라 단일 이름으로 제공되는 차이가 있음 I. 무한 반복 반복 연산자를 무한히 수행하기 때문에 while, try/except 구문 등과 ..
[Python/알고리즘] Minimum Product Sum(easy)
2020. 6. 28. 16:57
알고리즘 문제
0. 문제 자연수로 이루어진 동일한 길이의 배열 A, B에서 각각 한 개의 숫자를 뽑아 두 수를 곱합니다. 이러한 과정을 배열의 길이만큼 반복하며, 두 수를 곱한 값을 누적하여 더합니다. 이때 최종적으로 누적된 값이 최소가 되도록 만드는 것이 목표입니다. (단, 각 배열에서 k번째 숫자를 뽑았다면 다음에 k번째 숫자는 다시 뽑을 수 없습니다.) ex. ) A = [1, 4, 2] , B = [5, 4, 4] 라면 A에서 첫 번째 숫자인 1, B에서 두 번째 숫자인 5를 뽑아 곱하여 더합니다. (누적된 값 : 0 + 5(1x5) = 5) A에서 두번째 숫자인 4, B에서 세 번째 숫자인 4를 뽑아 곱하여 더합니다. (누적된 값 : 5 + 16(4x4) = 21) A에서 세번째 숫자인 2, B에서 첫 번째 ..

[Python/LeetCode]코딩테스트 준비를 위한 파이썬 알고리즘 문제 11. Container With Most Water 해설(Medium level)
2020. 1. 21. 12:23
알고리즘 문제
문제 11. Container With Most Water Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note: You may not slant the container and n is at least 2. Loo..

[Python/LeetCode]코딩테스트 준비를 위한 파이썬 알고리즘 문제 7.Reverse integer 해설(Easy level)
2020. 1. 19. 18:53
알고리즘 문제
문제 7. Reverse integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 Note:Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1]. For the purpose of this problem, assume that your function returns 0 when ..

[Python/LeetCode]코딩테스트 준비를 위한 파이썬 알고리즘 문제 5.Longest Palindromic Substring 참조해설(Medium level)
2020. 1. 19. 01:39
알고리즘 문제
문제 5. Longest palindromic substring Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example 1: Input: "babad" Output: "bab" Note: "aba" is also a valid answer. Example 2: Input: "cbbd" Output: "bb" 오늘 해설의 idea는 "Chomolungma" 라는 유저의 글을 참고하였습니다. def longestPalindrome(s): #문자열이 공백인 경우 그대로 출력 if len(s)==0: return s flag=1 start=0 #문..

[Python/LeetCode]코딩테스트 준비를 위한 파이썬 알고리즘 문제 4.Median of Two Sorted Arrays 해설(High level)
2020. 1. 18. 21:02
알고리즘 문제
문제 4. Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). You may assume nums1 and nums2 cannot be both empty. Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1, 2] nums2 = [3, 4] The median is (2 + 3)/2 = 2.5 두 개의 sorted ..

[Python/LeetCode]코딩테스트 준비를 위한 파이썬 알고리즘 문제 3.longest substring without repeating characters 해설(medium level)
2020. 1. 18. 18:10
알고리즘 문제
문제 3. longest substring without repeating characters Given a string, find the length of the longest substring without repeating characters. Example 1:Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2:Input: "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. Example 3:Input: "pwwkew" Output: 3 Explanation: The answer is "wke", with ..