Loading...

[프로그래머스] 프린터 (javascript)

2021. 03. 31 업데이트된 풀이 그냥 인덱스 위치를 담은 배열을 새로 만든다음에 비교해주면 된다. 기존 배열을 변형시킬 필요 없다. function solution(priorities, location) { var answer = 0; const q=Array(priorities.length).fill(0).map((_,i)=>i); while(q.length>0){ let max=Math.max(...priorities); let x=q.shift(); if(priorities[x]b-a); const sequence=[]; let idx=0; while(priorities){ // console.log(idx) if(priorities[idx]!==sorted_prior[0]){ idx=(id..

[프로그래머스] 기능 개발 (javascript)

계속 틀리다가 다른 코드를 참고하고 틀린 부분을 알아냈다 남은 일수가 5, 1, 1, 1, 2 인 경우 stack에 [5,1,1,1] 이고 현재 남은 일수가 2일 경우 나는 stack의 마지막 1과 비교했는데 그게 아니라 기준은 5이므로 (먼저 끝나는 일) 가장 앞의 원소와 비교해야 했다. 예제를 못찾으면 잘못된 부분을 못찾으니 ㅠㅠ 열심히 여러 문제 풀어봐야지 function solution(progresses, speeds) { var answer = []; let stack=[]; let days=[]; for(let i=0;i

[프로그래머스] 위장 (python, 해시)

조합의 경우의 수라고 볼 수 있는 것 같다 각 종류의 옷을 안입는 경우까지 포함해서 종류별 옷의 개수+1 해서 모두 곱해준 다음 모든 의상을 입지않는 경우 1을 빼준다 (적어도 하나는 입어야한다) from collections import defaultdict def solution(clothes): answer = 1 dic=defaultdict(int) for a,b in clothes: dic[b]+=1 for v in dic.values(): answer*=(v+1) return answer-1

[프로그래머스] 전화번호 목록 (python, 해시)

정렬하면 길이가 더 짧고 더 작은 수가 앞으로 온다 따라서 앞의 문자열 길이 만큼 뒤에 오는 문자열을 잘라서 앞의 문자열과 같은지 보면 된다 def solution(book): book.sort() for i in range(1,len(book)): a,b=book[i-1],book[i] if b[:len(a)]==a: # print(b[:len(a)],a) return False return True

[프로그래머스] 완주하지 못한 선수 (Python, javascript)

1. 파이썬 풀이 동명이인이 있을 수 있다 Counter로 빼주면 알아서 0은 사라지는 모양이다 남는 1명의 이름을 list로 반환해주면 된다 from collections import Counter def solution(participant, completion): answer=Counter(participant)-Counter(completion) return list(answer)[0] 2. 자바스크립트 풀이 completion[i]가 undefined가 될 때 participant[i]를 리턴한다 function solution(participant, completion) { participant.sort(); completion.sort(); for(let i=0;i

[프로그래머스] 기지국 설치 (파이썬)

예시 1 2 3 4 5 6 7 8 9 10 11 cur (설치할 기지국 위치는 1에서 시작) w가 1인 경우 다음 기지국 설치 위치를 cur+2*w+1 하는 이유는 cur는 1을 기준으로 해도 사실상 2에 설치해서 1(기준 위치)~2(실제 설치 위치)~3을 커버하는 거라고 보면 된다 즉 기준위치는 cur인 1이지만 실제 설치 위치는 2인 것이고 cur+2*w+1이 다음 새로 설치 가능한지 보는 이유이다. cur(기준 위치)가 stations[idx]-w (기존 기지국이 닿는 범위)보다 작아야 기존 기지국 위치와 겹치지 않는다. 겹친 다면 기지국이 닿는 오른쪽 전파범위 (stations[idx]+w)+1인 위치를 다음 기준 위치(cur)로 잡는다. def solution(n, stations, w): an..

[프로그래머스] 쿼드 압축 후 개수 세기

for 문이나 재귀로 돌면서 길이를 반으로 줄여가며 전수 검사한다 def solution(arr): answer = [0,0] n=len(arr) def go(x,y,l): cur=arr[x][y] for i in range(x,x+l): for j in range(y,y+l): if arr[i][j]!=cur: # 다르면 길이 짧게한다 ll=l//2 go(x,y+ll,ll) # 다음 시작점 go(x+ll,y,ll) go(x,y,ll) go(x+ll,y+ll,ll) return answer[cur]+=1 # 해당 범위 내 수가 다 같으면 해당 원소의 개수를 더해주면 된다 go(0,0,n) return answer

[프로그래머스] 이진 변환 반복하기 (python)

1의 개수만큼 계속 문자열이 '1'이 될 때까지 이진 변환을 해준다. n은 이진 변환한 횟수, c는 삭제한 0의 개수이다. 0의 개수는 계속 더해주며 1의 개수만큼 이진변환을 해준다. def solution(s): n=0 c=0 while s!='1': c+=s.count('0') s=str(bin(len(s)-s.count('0'))[2:]) n+=1 return [n,c]

[프로그래머스] 내적 (파이썬 ,python, javascript)

1. 파이썬 풀이 각 위치의 수를 곱한 뒤 더하기 def solution(a, b): answer = 0 for x,y in zip(a,b): answer+=x*y return answer 2. 자바스크립트 풀이 function solution(a, b) { return a.reduce((acc,cur,idx)=>{ return acc+cur*b[idx]; },0) }

[프로그래머스] 3진법 뒤집기

현재 숫자를 3진법으로 만든 뒤 뒤집어서 다시 10진법으로 변환한다 def solution(n): answer = 0 convert='' while n: a,b=divmod(n,3)//몫, 나머지 convert=str(b)+convert // 새로운 나머지수가 더 먼저 와야된다 (나눠보면서 이해) n=a // 새로운 몫 return int(convert[::-1],3) //3진법을 10진법으로 변환하는 방법