전체 글 (27) 썸네일형 리스트형 Frequency Queries 문제 https://www.hackerrank.com/challenges/frequency-queries/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=dictionaries-hashmaps 제출 코드 def freqQuery(queries): res=[] d1 = Counter() d2 = Counter() for i in range(q): s, v = queries[i] if s==1: d1[v]+=1 d2[d1[v]]+=1 elif s==2: d1[v] = max(0, d1[v]-1) if d1[v]>0: d2[d1[v]]+=1 d2[d1[v]+1]-=1 elif s==3: if d2[.. Two Strings 문제 https://www.hackerrank.com/challenges/two-strings/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=dictionaries-hashmaps 두 문자열이 공통으로 사용하는 알파벳이 있으면 YES, 없으면 NO를 출력 제출한 코드 def twoStrings(s1, s2): set1=set(s1) set2=set(s2) if set1 & set2: return "YES" else: return "NO" Jumping on the Clouds 문제 https://www.hackerrank.com/challenges/jumping-on-the-clouds/problem?h_l=interview&playlist_slugs%5B%5D%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D%5B%5D=warmup thunderheads를 피해서 점프하기 입력 리스트가 주어졌을 때 1, 2만큼 이동가능하고 0만 밟아서 최소 점프로 배열의 끝에 도착해야 함 입력 예 7 0 0 1 0 0 1 0 출력 예 4 제출한 코드 def jumpingOnClouds(c): idx,cnt = 0,0 while idxlen(c)-1: cnt+=1 return cnt if c[idx+2]==1: idx+=1 else: idx+=2 .. New Year Chaos 문제 https://www.hackerrank.com/challenges/new-year-chaos/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=arrays 뇌물을 주고받아 순서가 변경된 que가 주어졌을 때 몇 번 이동해야하는지를 출력. 단 아래 조건에 어긋난 que일 경우 'Too Chaotic'을 출력 조건 바로 앞에 있는 사람한테 뇌물을 주고 앞으로 갈 수 있음 한 사람 당 2명까지 뇌물을 줄 수 있음. 즉 최대 2번 앞으로 이동가능 입력 예 2 (입력 세트의 개수) 5 (큐의 길이) 2 1 5 3 4 5 2 5 1 3 4 출력 예 3 Too chaotic 첫번째 큐(2 1 5 3 .. Arrays: Left Rotation 문제 https://www.hackerrank.com/challenges/ctci-array-left-rotation/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=arrays 제출 코드 def rotLeft(a, d): return a[d:]+a[:d] 너무 쉬워서 생략 Cats and a Mouse 문제 https://www.hackerrank.com/challenges/cats-and-a-mouse/problem?h_r%5B%5D=next-challenge&h_r%5B%5D=next-challenge&h_v%5B%5D=zen&h_v%5B%5D=zen&isFullScreen=false 고양이 2마리랑 쥐 1마리가 1차원 축 위에 있을때, 쥐를 잡는 고양이를 출력하기. 단, 고양이는 동일한 속도로 움직이고 고양이 두마리가 만나면 쥐는 도망칠수 있음. 그때는 쥐를 출력 입력 예 2 (입력 케이스 수) 1 2 3 (Cat A, Cat B, Mouse C의 좌표) 1 3 2 출력 예 Cat B Mouse C 제출 코드 def catAndMouse(x, y, z): a_ = abs(x-z) b_ = abs(.. Repeated String 문제 https://www.hackerrank.com/challenges/repeated-string/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=warmup n번째까지 문자열 s를 반복했을 때 a의 개수 출력 제출한 코드 def repeatedString(s, n): x = n//len(s) # 개의 s + @ dcnt = Counter(s) reminder = n%len(s) remcnt = Counter(s[:reminder]) if reminder == 0: return dcnt['a']*x else: return dcnt['a']*x+remcnt['a'] Counting Valleys 문제 https://www.hackerrank.com/challenges/counting-valleys/problem 현재 높이에서 내려간 뒤 다시 원래 높이까지 올라갔을 경우를 카운팅 제출한 코드 def countingValleys(n, s): isValley = False v, res= 0, 0 for i in s: if i == "D": v-=1 elif i =="U": v+=1 if v < 0: isValley = True if isValley and v ==0: res+=1 isValley = False return res 머신러닝 공부해야하는데 잼따.. 이전 1 2 3 4 다음