본문 바로가기
반응형

전체 글147

[브루트포스/재귀함수] N개 알파벳 중에 R개를 나열하는 경우 # N개 알파벳 중에 R개를 나열하는 경우 def permutation (cur, n, r, result): if cur >= r : print(result) return else: for i in range(n): alpha = chr(ord('a') + i) if not check[cur]: result[cur] = alpha check[cur] = True permutation(cur+1,n,r,result) # check[cur] = False result[cur] = 0 if __name__ == '__main__': n,r = map(int,input().split()) check = [False]*r result = [0]*r permutation(0,n,r,result) r중 for문 수행.. 2021. 4. 5.
Leet Code 121. Best Time to Buy and Sell Stock leetcode.com/problems/best-time-to-buy-and-sell-stock/ 시간초과 (브루트포스) class Solution: def maxProfit(self, prices: List[int]) -> int: profit = 0 for i in range(len(prices)): for j in range(i+1, len(prices)): if prices[j] - prices[i] > profit: profit = prices[j] - prices[i] return profit 시간복잡도 : O(N^2) 1 = prices[j] : i = j # 박힌돌 위치 이동 #박힌돌보다 굴러들어온 돌이 더 클 때 elif prices[j] - prices[i] > profit : prof.. 2021. 4. 4.
[DP] Leet code 118. Pascal's Triangle leetcode.com/problems/pascals-triangle/ 내가 푼 코드 class Solution: def generate(self, numRows: int) -> List[List[int]]: result = [[1]] if numRows >= 2: result.append([1,1]) if numRows >= 3: for k in range(3, numRows+1): temp = [1] * k for i in range(1, k-2+1): temp[i] = result[k-2][i-1] + result[k-2][i] result.append(temp) return result 솔루션 코드 (코드를 좀 더 일반화) class Solution: def generate(self, num_ro.. 2021. 4. 4.
[이진트리/DFS] Leet code 104 - Maximum Depth of Binary Tree 1. 스택 활용하는 방법 # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def maxDepth(self, root: TreeNode) -> int: if root is not None: s = [(root, 1)] else: s = [] maxValue = 0 while s: cur, cnt = s.pop() if cur.left is None and cur.right is None: if cnt > maxValue: maxValue.. 2021. 4. 3.
Hadoop 클러스터 구축 과정 1. Zookeeper 설치 1) 패키지 설치 zookeeper, zookeeper-server 2) 설정파일 배포 - zoo.cfg (myid 설정) 2. Hadoop 설치 1) 네임노드 - 패키지 설치 (hadoop, hadoop-hdfs-namenode) - 네임노드용 디렉토리 생성(dfs_namenode_name_dir) 2) YARN Resource manager - 패키지 설치(hadoop, hadoop-yarn-resourcemanager) 3) Journal Node - 패키지 설치(hadoop-hdfs-journalnode) - 저널노드용 디렉토리 생성 (dfs_journalnode_edits_dir) 4) ZKFC - 패키지 설치(hadoop-hdfs-zkfc) 5) Hadoop 설치 .. 2021. 4. 1.
[BFS / 이진트리] LeetCode 101. Symmetric Tree 이진트리가 데칼코마니 같은 형식인지 확인하는 문제 내 첫 아이디어는 BFS로 왼쪽트리 따로 탐색(왼쪽->오른쪽방향), 오른쪽트리 따로 탐색(오른쪽->왼쪽방향) BFS로 방향 고려해서 탐색한다는 아이디어 잘 떠올렸따. 각각 탐색한 결과를 left, right 배열에 담아서 최종적으로 left, right 배열이 같은지 다른지 확인한다. # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def isSymmetric(self, root: Tr.. 2021. 3. 23.
[HBase] 데이터 Read/Write 과정 (memstore, WAL, HFile) HDFS는 데이터를 읽을 때 랜덤 엑세스가 아니고 Full scan을 한다. 그리고 update가 불가하며 append만 가능하다. HBase는 HDFS에 있는 데이터를 랜덤 엑세스 하게 해주고 데이터를 update 할 수 있게 해준다. 구성 Master HBase 설정파일과 Region Server들의 정보를 관리한다. Zookeeper Master의 Active를 선정한다. Client는 META테이블(hbase:meta)을 갖고 있는 Region Server의 정보를 Zookeeper를 통해 얻는다. znode: /hbase/meta-region-server Master와 Region Server 모두 Zookeeper와 세션을 맺는다. Region Server 하나 이상의 region들을 관리한다.. 2021. 3. 23.
[재귀호출] LeetCode 70. Climbing Stairs 1과 2의 합으로 n을 만드는 경우의 수를 구한다. 1. 재귀호출 (브루트포스. 모든 경우의 수를 다 구한다) 예를 들어 1과 2의 합으로 4를 만드는 경우의 수를 봐보자. 1+1+1+1, 1+1+1+2, 1+1+2, 1+2+1, 1+2+2, ... 이런식으로 모든 경우의 수를 다 해보는 것이다. class Solution: def __init__(self): self.cnt = 0 def do(self, result,n): if result == n: self.cnt += 1 return if result > n: return for i in [1,2]: self.do(result+i,n) def climbStairs(self, n: int) -> int: self.do(0,n) return self... 2021. 3. 20.
[리스트] Leet Code 26. Remove Duplicates from Sorted Array 리스트에서 중복값을 제외하고 중복값 제외한 숫자들의 개수를 구한다. class Solution: def removeDuplicates(self, nums: List[int]) -> int: i = 0 while i pop(i) O(N) ([1,1,1,1,1] 인 경우) 공간복잡도 O(1) : 변수 i 공간 1개만 필요 솔루션 보고 개선한 코드 class Solution: def removeDuplicates(self, nums: List[int]) -> int: if len(nums)==0 :.. 2021. 3. 20.
반응형