본문 바로가기

전체 글267

[프로그래머스] 주식가격 파이썬 다른 사람의 풀이 from collections import deque def solution(prices): answer = [] prices = deque(prices) while prices: c = prices.popleft() count = 0 for i in prices: if c > i: count += 1 break count += 1 answer.append(count) return answer 문제가 약간 헷갈리게 되어있다. 테스트 케이스: [1, 2, 3, 2, 3, 1] 3번째 가격인 3같은 경우 3 -> 2로 될때 1초 동안 가격이 떨어지지 않았기 때문에 += 1을 해주고 break 해줘야 한다. 처음 풀었을 때는 이것도 1초뒤에 떨어졌기 때문에 += 1을 안해주는 거라고 생각 했.. 2021. 3. 3.
[프로그래머스] 스킬트리 파이썬 def solution(skill, skill_trees): flag = 0 for trees in skill_trees: skill_check = [] for a in trees: if a in skill: skill_check.append(a) for i, v in enumerate(skill_check): if v != skill[i]: flag += 1 break return len(skill_trees) - flag solution("CBD",["BACDE", "CBADF", "AECB", "BDA"]) 가장 생각이 안났던 부분은 check 리스트의 길이가 skill 리스트 보다 짧을 경우 어떻게 처리해야되는가 였는데 그냥 check_list 만 순회하면서 값을 비교하면 되니까 문제가 없다. -.. 2021. 3. 3.
[프로그래머스] 기능개발 파이썬 다른 사람의 풀이 geonlee.tistory.com/122 import math def solution(progresses, speeds): progresses = [math.ceil((100 - a) / b) for a, b in zip(progresses, speeds)] answer = [] front = 0 for idx in range(len(progresses)): if progresses[idx] > progresses[front]: answer.append(idx - front) front = idx answer.append(len(progresses) - front) return answer 완료일을 기준으로 문제풀이. 위의 해결방법으로 하면 시간 복잡도를 o(n)으로 만들 수 있다. .. 2021. 3. 3.
[프로그래머스] 프린터 파이썬 다른 사람의 풀이 def solution(priorities, location): answer = 0 from collections import deque d = deque([(v,i) for i,v in enumerate(priorities)]) while len(d): item = d.popleft() if d and max(d)[0] > item[0]: d.append(item) else: answer += 1 if item[1] == location: break return answer index와 value 두 가지가 동시에 필요할 때는 tuple를 활용하기 stack 문제의 경우 deque를 써서 양방향 queue를 사용하면 편하다 enumerate 함수를 사용해서 (index, value) .. 2021. 3. 3.
[프로그래머스] 멀쩡한 사각형 최대공약수 정답 출처: leedakyeong.tistory.com/entry/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4-%EB%A9%80%EC%A9%A1%ED%95%9C-%EC%82%AC%EA%B0%81%ED%98%95-in-python 2021. 3. 2.
[프로그래머스] 124 나라의 숫자 진법에 관한 문제인데 살짝 꼬아서 만든 문제 수를 표현할때 124만으로 표현한다고 하고 규칙은 3진법과 같음을 알 수 있다. 다른점은 3대신 4가 사용된다는 점이다. 1부터 9까지 3진법을 사용할 경우 1,2,3 11,12,13 21,22,23 이런식으로 표현이 되는데 여기서는 3대신 4를 써야하기 때문에 1,2,4 11,12,14 21,22,24 이런식으로 결과물이 출력 되어야 한다. 다른 사람의 풀이 def solution(n): num = ['1','2','4'] answer = "" while n > 0: n -= 1 # zero indexing answer = num[n % 3] + answer n //= 3 # 몫 값을 업데이트 하면서 자리수 증가 return answer 3대신 4를 리스트 .. 2021. 3. 2.
[Pytorch] Multi-label classification Loss Selection discuss.pytorch.org/t/is-there-an-example-for-multi-class-multilabel-classification-in-pytorch/53579/9 Is there an example for multi class multilabel classification in Pytorch? nn.CrossEntropyLoss uses the target to index the logits in your model’s output. Thus it is suitable for multi-class classification use cases (only one valid class in the target). nn.BCEWithLogitsLoss on the other hand tre.. 2021. 2. 22.
[딥러닝을 이용한 자연어 처리-조경현] Basic Machine Learning: Supervised Learning Probability 에서 가장 중요한 property 2가지 1. Non-negative: $p(X - e_{i}) \geqq 0$ (특정 event 가 일어날 확률은 0보다 크거나 같다) 2. Unit volume: $ \sum_{e \in \Omega} p(X=e) = 1 $ ( 전체 확률을 모두 더한 값은 1 ) 위에서 언급한 확률에서 중요한 2가지 특징을 사용해서 NN에서 예측값으로 사용하기 위해서 Supervised Learning에서 input이 directed acyclic graph를 통과하고 나온 결과가 답을 준다라고 한다. 이것을 input이 주어졌을 때 ouput이 $y'$에 대한 확률이 무었이냐 로 방향을 전환해서 NN을 사용해서 예측하기 위함. 여러 ouput이 존재했을 때 어떤.. 2021. 2. 16.
[Pytorch] imread and input shape pytorch dataset에서 이미지를 읽어오고 변환하고 저장하는 과정 3채널 이미지 VS. 1채널 이미지 3채널 이미지의 경우 open cv로 읽어오는데 open cv의 경우 pil과는 다르게 BGR로 이미지를 읽어오기 때문에 RGB로 변환하는 과정이 필요하다. (cv2.COLOR_BGR2RGB) 1채널 이미지의 경우 IMREAD_GRAYSCALE로 읽어오면 변환 끝 읽어온 후의 이미지 shape을 찍어보면, 3채널의 경우 height, width, channel 3 dim인 반면 1채널의 경우 height, width 만 존재하고 channel이 없는 걸로 표현된다 따라서 1채널 이미지의 경우 channel의 dim을 만들어 줘야하는데 여기서는 2가지 방법이 존재한다. 1. np.expand_dim.. 2021. 2. 12.