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..

[프로그래머스] 피보나치수 (파이썬)

메모이제이션 없이 재귀로 돌렸더니 시간초과 나서 for문으로 처리했다 def solution(n): answer=[0,1] for i in range(2,n+1): answer.append((answer[i-1]+answer[i-2])%1234567) # print(answer) return answer[n] 이런 방법도 있다 def fibonacci(num): a,b = 0,1 for i in range(num): a,b = b,a+b return a

[프로그래머스] 가장 큰 정사각형 찾기 (파이썬, dp)

def solution(board): for i in range(1,len(board)): for j in range(1,len(board[0])): if board[i][j]==1: board[i][j]=min([board[i-1][j-1],board[i][j-1],board[i-1][j]])+1 # 이런식으로 쭉 늘여놓을 수 있는 듯 return max([item for row in board for item in row])**2

[프로그래머스] 행렬의 곱셈 (파이썬)

하나를 행열 위치를 바꾼 행렬로 만들어서 각 행 별로 원소를 곱해서 더해줌 def solution(arr1, arr2): r2=list(map(list,zip(*arr2))) answer = [[sum(a*b) for a,b in zip(row,col) for col in r2] for row in arr1] return answer

[프로그래머스] 프렌즈 4블록 (파이썬)

이미 터트린거 어떻게 지우나 했는데 이런 방법이 있어서 참고했다 똑똑한 사람이 많군 velog.io/@tjdud0123/%ED%94%84%EB%A0%8C%EC%A6%88-4%EB%B8%94%EB%A1%9D-2018-%EC%B9%B4%EC%B9%B4%EC%98%A4-%EA%B3%B5%EC%B1%84-python b=list(map(list,zip(*board)) 이렇게 하면 세로 줄에 해당하는 아이템을 행으로 해서 새로운 배열로 리턴해준다 지워진 만큼 앞에 '_'을 붙여서 남은 것 끼리 오른쪽으로 모아준다 def pop_num(b, m, n): pop_set = set() # n,m 방향 주의 for i in range(1, n): for j in range(1, m): if b[i][j] == b[i -..

[프로그래머스] level3. 베스트앨범 (파이썬, javascript)

1. 파이썬 def solution(genres, plays): answer = [] dic={} for g,p in zip(genres,plays): if g not in dic: dic[g]=p else: dic[g]+=p # 곡 내림차순 정렬 sdic=sorted(dic.items(),key=lambda x:x[1],reverse=True) for s in sdic: temp=[] for i,p in enumerate(plays): if genres[i]==s[0]: temp.append((p,i)) cnt=0 temp=sorted(temp, key=lambda x:(-x[0],x[1])) for t in temp: num,idx=t answer.append(idx) cnt+=1 if cnt==2:..

[프로그래머스] 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

[프로그래머스] 영어 끝말잇기 (파이썬)

dictionary 사용 def solution(n, words): answer = [] dic={} prev=words[0] dic[prev]=0 for i,w in enumerate(words): if i==0: continue if w[0]!=prev[-1] or w in dic: return [i%n+1,i//n+1] else: prev=w dic[w]=i return [0,0]

[프로그래머스] 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