Loading...

[프로그래머스] 튜플 (문자열, 정렬, python, javascript)

1. python 풀이 def solution(s): answer = [] temp=[] for i in s[2:-2].split('},{'): temp.append(i.split(',')) temp.sort(key=len) for t in temp: for item in t: if int(item) not in answer: answer.append(int(item)) return answer 2. javascript 풀이 function solution(s) { var answer = []; s=s.split("").map(ch=>{ if(ch==="{" || ch==="}"){ return ""; } return ch; }).join("").split(","); const dict={}; for(l..

[프로그래머스] 1차 캐시 (파이썬, javascript)

deque은 remove나 insert함수도 있다 1. 파이썬 def solution(cacheSize, cities): answer = 0 cache=[] for c in cities: c=c.lower() if c not in cache: if cache and len(cache)>=cacheSize: cache.pop(0) if cacheSize>0: cache.append(c) answer+=5 else: cache.remove(c) cache.append(c) answer+=1 return answer 2. 자바스크립트 function solution(cacheSize, cities) { const cache=[]; var answer = 0; for(let i=0;icacheSize)cache..

[프로그래머스] 땅따먹기 (파이썬, javascript )

dp 문제 열이 연속으로 같지 않게 최대값 구하는 문제 1. 파이썬 def solution(land): for i in range(1,len(land)): for j in range(4): land[i][j]+=max(land[i-1][:j]+land[i-1][j+1:]) return max(land[len(land)-1]) 2. javascript function solution(land) { var answer = 0; let n=land.length; let dp=Array(n).fill(0).map(()=>Array(4).fill(0)); for(let j=0;j

[프로그래머스] h-index (파이썬, javascript)

1. javascript 가장 큰 인용 가능 수 =논문 개수, 최소=0 나는 완전탐색으로 했다 function solution(citations) { var answer = 0; for(let i=citations.length;i>=0;i-=1){ const arr=citations.filter(x=>x>=i); if(arr.length>=i && citations.length-i0: cnt=0 for c in citations: if c>=idx: cnt+=1 if cnt>=idx and len(citations)-cnt

[프로그래머스] 짝지어 제거하기 (파이썬, javascript)

o(n) 시간 안에 해결해야 되는 문제들을 잘 못 푸는데 이런 방법이 있다는 것을 알았다 여기저기 쓰일 것 같으니 잘 봐놔야지 1. 파이썬 풀이 def solution(s): answer = [] for i in range(len(s)): if len(answer)==0: answer.append(s[i]) elif answer[-1]==s[i]: answer.pop() else: answer.append(s[i]) if len(answer): return 0 else: return 1 2. javascript function solution(s) { var answer = 0; const stack=[]; for(let i=0;i

프로그래머스 입국심사 (파이썬, javascript, 이분탐색)

l, r은 시간 기준이며 모든 사람이 다 처리될 수 있는 최소 시간을 구해준다 mid(기준 시간)을 각 줄에서 처리할 수 있는 시간으로 나눠주면 전체 처리된 인원 수가 나오게 된다 1. 파이썬 def solution(n, times): l=min(times)*n//len(times) r=max(times)*n//len(times) answer=r while l=n: if answer>mid: answer=mid r=mid-1 else: l=mid+1 return answer 2. javascript 사람 수가 n보다 큰 경우도 시간을 갱신하는 조건이다. 가장 오래는 걸리는 시간을 right으로 해서 전체 시간을 각 시간으로 나눠준 것으로 사람 수를 계산한다. function solution(n, time..