Created
March 3, 2021 07:33
-
-
Save songjucho/081580456238fce106e37def85c0524c to your computer and use it in GitHub Desktop.
프로그래머스 순위 검색 풀이
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def filterStr(str): | |
str = str.replace("and ", "").split(" ") | |
number = int(str[-1]) | |
return str[:-1], number | |
def solution(info, query): | |
dic = {"cpp": | |
{"backend": | |
{"junior": | |
{"pizza":[], "chicken":[], "-":[]}, | |
"senior": | |
{"pizza":[], "chicken":[], "-":[]}, | |
"-": | |
{"pizza":[], "chicken":[], "-":[]}, | |
}, | |
"frontend": | |
{"junior": | |
{"pizza":[], "chicken":[], "-":[]}, | |
"senior": | |
{"pizza":[], "chicken":[], "-":[]}, | |
"-": | |
{"pizza":[], "chicken":[], "-":[]}, | |
}, | |
"-": | |
{"junior": | |
{"pizza":[], "chicken":[], "-":[]}, | |
"senior": | |
{"pizza":[], "chicken":[], "-":[]}, | |
"-": | |
{"pizza":[], "chicken":[], "-":[]}, | |
} | |
}, | |
"java": | |
{"backend": | |
{"junior": | |
{"pizza":[], "chicken":[], "-":[]}, | |
"senior": | |
{"pizza":[], "chicken":[], "-":[]}, | |
"-": | |
{"pizza":[], "chicken":[], "-":[]}, | |
}, | |
"frontend": | |
{"junior": | |
{"pizza":[], "chicken":[], "-":[]}, | |
"senior": | |
{"pizza":[], "chicken":[], "-":[]}, | |
"-": | |
{"pizza":[], "chicken":[], "-":[]}, | |
}, | |
"-": | |
{"junior": | |
{"pizza":[], "chicken":[], "-":[]}, | |
"senior": | |
{"pizza":[], "chicken":[], "-":[]}, | |
"-": | |
{"pizza":[], "chicken":[], "-":[]}, | |
} | |
}, | |
"python": | |
{"backend": | |
{"junior": | |
{"pizza":[], "chicken":[], "-":[]}, | |
"senior": | |
{"pizza":[], "chicken":[], "-":[]}, | |
"-": | |
{"pizza":[], "chicken":[], "-":[]}, | |
}, | |
"frontend": | |
{"junior": | |
{"pizza":[], "chicken":[], "-":[]}, | |
"senior": | |
{"pizza":[], "chicken":[], "-":[]}, | |
"-": | |
{"pizza":[], "chicken":[], "-":[]}, | |
}, | |
"-": | |
{"junior": | |
{"pizza":[], "chicken":[], "-":[]}, | |
"senior": | |
{"pizza":[], "chicken":[], "-":[]}, | |
"-": | |
{"pizza":[], "chicken":[], "-":[]}, | |
} | |
}, | |
"-": | |
{"backend": | |
{"junior": | |
{"pizza":[], "chicken":[], "-":[]}, | |
"senior": | |
{"pizza":[], "chicken":[], "-":[]}, | |
"-": | |
{"pizza":[], "chicken":[], "-":[]}, | |
}, | |
"frontend": | |
{"junior": | |
{"pizza":[], "chicken":[], "-":[]}, | |
"senior": | |
{"pizza":[], "chicken":[], "-":[]}, | |
"-": | |
{"pizza":[], "chicken":[], "-":[]}, | |
}, | |
"-": | |
{"junior": | |
{"pizza":[], "chicken":[], "-":[]}, | |
"senior": | |
{"pizza":[], "chicken":[], "-":[]}, | |
"-": | |
{"pizza":[], "chicken":[], "-":[]}, | |
} | |
}, | |
} | |
answer = [] | |
for k in info: | |
spl = k.split(" ") | |
idx = upperBoundSearch(dic[spl[0]][spl[1]][spl[2]][spl[3]], int(spl[-1])) | |
dic[spl[0]][spl[1]][spl[2]][spl[3]].insert(idx, int(spl[-1])) | |
idx = upperBoundSearch(dic["-"][spl[1]][spl[2]][spl[3]], int(spl[-1])) | |
dic["-"][spl[1]][spl[2]][spl[3]].insert(idx, int(spl[-1])) | |
idx = upperBoundSearch(dic[spl[0]]["-"][spl[2]][spl[3]], int(spl[-1])) | |
dic[spl[0]]["-"][spl[2]][spl[3]].insert(idx, int(spl[-1])) | |
idx = upperBoundSearch(dic[spl[0]][spl[1]]["-"][spl[3]], int(spl[-1])) | |
dic[spl[0]][spl[1]]["-"][spl[3]].insert(idx, int(spl[-1])) | |
idx = upperBoundSearch(dic[spl[0]][spl[1]][spl[2]]["-"], int(spl[-1])) | |
dic[spl[0]][spl[1]][spl[2]]["-"].insert(idx, int(spl[-1])) | |
idx = upperBoundSearch(dic["-"]["-"][spl[2]][spl[3]], int(spl[-1])) | |
dic["-"]["-"][spl[2]][spl[3]].insert(idx, int(spl[-1])) | |
idx = upperBoundSearch(dic["-"][spl[1]]["-"][spl[3]], int(spl[-1])) | |
dic["-"][spl[1]]["-"][spl[3]].insert(idx, int(spl[-1])) | |
idx = upperBoundSearch(dic["-"][spl[1]][spl[2]]["-"], int(spl[-1])) | |
dic["-"][spl[1]][spl[2]]["-"].insert(idx, int(spl[-1])) | |
idx = upperBoundSearch(dic[spl[0]]["-"]["-"][spl[3]], int(spl[-1])) | |
dic[spl[0]]["-"]["-"][spl[3]].insert(idx, int(spl[-1])) | |
idx = upperBoundSearch(dic[spl[0]]["-"][spl[2]]["-"], int(spl[-1])) | |
dic[spl[0]]["-"][spl[2]]["-"].insert(idx, int(spl[-1])) | |
idx = upperBoundSearch(dic[spl[0]][spl[1]]["-"]["-"], int(spl[-1])) | |
dic[spl[0]][spl[1]]["-"]["-"].insert(idx, int(spl[-1])) | |
idx = upperBoundSearch(dic["-"]["-"]["-"][spl[3]], int(spl[-1])) | |
dic["-"]["-"]["-"][spl[3]].insert(idx, int(spl[-1])) | |
idx = upperBoundSearch(dic["-"]["-"][spl[2]]["-"], int(spl[-1])) | |
dic["-"]["-"][spl[2]]["-"].insert(idx, int(spl[-1])) | |
idx = upperBoundSearch(dic["-"][spl[1]]["-"]["-"], int(spl[-1])) | |
dic["-"][spl[1]]["-"]["-"].insert(idx, int(spl[-1])) | |
idx = upperBoundSearch(dic[spl[0]]["-"]["-"]["-"], int(spl[-1])) | |
dic[spl[0]]["-"]["-"]["-"].insert(idx, int(spl[-1])) | |
idx = upperBoundSearch(dic["-"]["-"]["-"]["-"], int(spl[-1])) | |
dic["-"]["-"]["-"]["-"].insert(idx, int(spl[-1])) | |
for i in query: | |
ans = 0 | |
q, num = filterStr(i) | |
scores = dic[q[0]][q[1]][q[2]][q[3]] | |
answer.append(len(scores) - lowerboundSearch(scores, num)) | |
return answer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def lowerboundSearch(arr, target): | |
if len(arr) == 0 or (len(arr) >= 1 and target > arr[-1]): | |
return len(arr) | |
left, right = 0, len(arr) - 1 | |
while (left < right): | |
mid = (left + right) // 2 | |
if(arr[mid] < target): | |
left = mid + 1 | |
else: | |
right = mid | |
return right | |
def parseQuery(s): | |
string = s.replace("and ", "").split(" ") | |
number = int(string[-1]) | |
return ''.join(string[:-1]), number | |
def parseInfo(info): | |
arr = info.split(' ') | |
keys = [] | |
for i in (arr[0], '-'): | |
for j in (arr[1], '-'): | |
for k in (arr[2], '-'): | |
for l in (arr[3], '-'): | |
keys.append(i+j+k+l) | |
return keys, int(arr[-1]) | |
def solution(info, query): | |
answer = [] | |
dic = {} | |
for l in ('cpp', 'java', 'python', '-'): | |
for i in ('backend', 'frontend', '-'): | |
for s in ('senior', 'junior', '-'): | |
for f in ('pizza', 'chicken', '-'): | |
dic[l+i+s+f] = [] | |
for l in info: | |
keys, score = parseInfo(l) | |
for key in keys: | |
dic[key].append(score) | |
for k in dic.keys(): | |
dic[k].sort() | |
for i in query: | |
q, num = parseQuery(i) | |
scores = dic[q] | |
answer.append(len(scores) - lowerboundSearch(scores, num)) | |
return answer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment