본문 바로가기

python20

[Python-백준 24479] DFS - 알고리즘 수업 - 깊이 우선 탐색 1 알고리즘 수업 - 깊이 우선 탐색 1 DFS 알고리즘 문제 풀이 시간 초과 건으로 인해서 계속 오류가 나는데 이유를 모르겠어서 헤매다가 해결했습니다. 문제 자체는 DFS 기본적인 내용인데 오류가 나니까 원인을 계속 찾았는데, 알고보니 입출력 간 발생 하는 문제였습니다. Python3 같은 경우, 입출력 간 시간이 많이 소요되기 때문에 input = sys.stdin.readline 코드를 통해서 입력 시간을 줄여주는 작업이 필요합니다. 입출력 관련한 문제는 백준 새싹 문제에 있으니 한번 풀어보도록 하여 익히도록 합니다. https://www.acmicpc.net/problem/15552 15552번: 빠른 A+B 첫 줄에 테스트케이스의 개수 T가 주어진다. T는 최대 1,000,000이다. 다음 T줄에는.. 2022. 11. 16.
[Python] 고위함수(higher-order function) map(), filter(), lambda() / namedtuple 사용하기 map() 함수 map() 함수는 두 번째 인수의 연속된 요소에 첫 번째 인수(함수)를 적용한 결과를 가지는 반복 가능형 객체를 반복합니다. list(map(lambda x:x, range(1, 5))) # -> [1, 2, 3, 4] map()함수의 첫 번째 인자인 lambda 함수에 두 번째 인자인 range(1, 5) 적용한 결과로 [1, 2, 3, 4]를 반환합니다. map는 lazy evaluation으로서, 실제로 계산이 필요할때 연산을 하므로 메모리 절약에 도움이 됩니다. 그래서 list로 감싸줄 때 실제 연산을 하여, [1, 2, 3, 4]를 반환하게 됩니다. 다른 예제를 통해서 map함수를 알아보겠습니다. def factorial(n): return 1 if n> [10, 12, 18, .. 2022. 6. 24.
[Python-Leetcode] 6. Zigzag Conversion (difficulty : Medium- ☆☆) 문제 설명 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSIIGYIR" Write the code that will take a string and make this conversion given a number of rows: string convert(string s, int numRows); Example 1: Input: s.. 2022. 5. 9.
[Python-Leetcode] 509. Fibonacci Number (difficulty : Easy - ☆) 문제 설명 The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, F(0) = 0, F(1) = 1 F(n) = F(n - 1) + F(n - 2), for n > 1. Given n, calculate F(n). Example 1: Input: n = 2 Output: 1 Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1. Example 2: Input: n = 3 Output: 2 Explanation.. 2022. 5. 6.
[Python-Leetcode] 13. Roman to Integer (difficulty : Easy - ☆) 문제 설명 Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II. Roman numerals are usually written largest to smallest from left to right. .. 2022. 5. 5.
[Python-Leetcode] 1. Two Sum (difficulty : Easy - ☆) 문제설명 Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. Example 1: Input: nums = [2,7,11,15], target = 9 Output: [0,1] Explanation: Because nums[0] + nums[1] == 9, we return [0, 1.. 2022. 5. 1.