본문 바로가기

TIL/Algorithm

(16)
[프로그래머스] 모의고사(easy) def solution(answers): answer = [] num_p1 = [1,2,3,4,5] num_p2 = [2,1,2,3,2,4,2,5] num_p3 = [3,3,1,1,2,2,4,4,5,5] n = len(answers) score = [0,0,0] for i in range(n): if answers[i] == num_p1[i%5]: score[0] += 1 if answers[i] == num_p2[i%8]: score[1] += 1 if answers[i] == num_p3[i%10]: score[2] += 1 max_score = max(score) for index, i in enumerate(score): if i == max_score: answer.append(index+1) ..
[Algorithm] Graph Number from collections import defaultdict def bfs(g, start): global cnt qu = [] # 기억 장소 1: 앞으로 처리해야 할 꼭짓점을 큐에 저장 done = set() # 기억 장소 2: 이미 큐에 추가한 꼭짓점들을 집합에 기록(중복 방지) for j in g: if j not in done: qu.append(j) # 시작점을 큐에 넣고 시작 done.add(j) # 집합에도 추가 cnt += 1 while qu: # 큐에 처리할 꼭짓점이 남아 있으면 p = qu.pop(0) # 큐에서 처리 대상을 꺼내어 print(p) # 꼭짓점 이름을 출력하고 for x in g[p]: # 대상 꼭짓점에 연결된 꼭짓점들 중에 if x not in done: # 아직 큐..
[Algorithm] Hackerrank - 2D Array - DS 텍스트를 복사해서 넣으니 수식이 복사가 안되는군ㅠ 문제: 코드: #!/bin/python3 import math import os import random import re import sys # Complete the hourglassSum function below. def hourglassSum(arr): sum_ = -9999 for y in range(len(arr)-2): for x in range(len(arr[0])-2): temp = arr[y][x] + arr[y][x+1] + arr[y][x+2] +\ + arr[y+1][x+1] +\ arr[y+2][x] + arr[y+2][x+1] + arr[y+2][x+2] if temp > sum_: sum_ = temp return sum_ ..
[Algorithm] Hackerrank - Arrays: Left Rotation 문제: A left rotation operation on an array shifts each of the array's elements 1 unit to the left. For example, if 2 left rotations are performed on array [1,2,3,4,5], then the array would become [3,4,5,1,2]. Given an array a of n integers and a number, d, perform d left rotations on the array. Return the updated array to be printed as a single line of space-separated integers. Function Descripti..
[Algorithm] Hackerrank - Counting Valleys 문제: Gary is an avid hiker. He tracks his hikes meticulously, paying close attention to small details like topography. During his last hike he took exactly steps. For every step he took, he noted if it was an uphill, , or a downhill, step. Gary's hikes start and end at sea level and each step up or down represents a unit change in altitude. We define the following terms: A mountain is a sequence ..
[Algorithm] Hackerrank - Repeated String 문제: Lilah has a string, s, of lowercase English letters that she repeated infinitely many times. Given an integer, n, find and print the number of letter a's in the first letters of Lilah's infinite string. For example, if the string s = 'abcac' and n = 10, the substring we consider is abcacabcac, the first 10 characters of her infinite string. There are 4 occurrences of 'a' in the substring. Fu..
[Algorithm] Hackerrank - Sock Merchant 문제: John works at a clothing store. He has a large pile of socks that he must pair by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are. For example, there are socks with colors . There is one pair of color and one of color . There are three odd socks left, one of each color. The number of pairs is . F..
[Algorithm] Hackerrank - Jumping on the Clouds 알고리즘(코딩 시험이 걱정인건 함정)이 너무 약해서... 공부좀 해야지... 문제 Emma is playing a new mobile game that starts with consecutively numbered clouds. Some of the clouds are thunderheads and others are cumulus. She can jump on any cumulus cloud having a number that is equal to the number of the current cloud plus or . She must avoid the thunderheads. Determine the minimum number of jumps it will take Emma to jump from..