STUDY/Algorithm

[프로그래머스] LEVEL2 조이스틱,python3, 탐욕법(greedy)

sinawi95 2019. 11. 4. 11:48
728x90

1차시도

def solution(name):
    answer = 0
    init="A"*len(name)
    for i in range(len(name)):# ascii: A=65 Z=90
        if ord(name[i]) == 65:
            pass
        else:
            tmp=ord(name[i]) - 65
            #print(tmp)
            if tmp>13:
                answer+=26-tmp
            else:
                answer+=tmp
    return answer


2차시도

def solution(name):
    answer = 0
    len_name=len(name)
    init="A"*len_name
    chk=[]
    for i in range(len_name):# ascii: A=65 Z=90
        if ord(name[i]) == 65:
            chk.append(1)
            pass
        else:
            tmp=ord(name[i]) - 65
            chk.append(2)
            #print(tmp)
            if tmp>13:
                answer+=26-tmp
            else:
                answer+=tmp
    #print(chk)
    ind=0
    ind_tmp=0
    while True:
        #print(chk)
        try:
            ind_tmp = chk.index(2)
        except:
            break
        diff_ind=ind_tmp-ind
        if diff_ind>(len_name//2):#차이가 길이의 반보다 큰 경우
            answer+=len_name-diff_ind
        else:   # 차이가 길이의 반보다 작거나 같은 경우
            answer+=diff_ind
        chk[ind]=0
        ind=ind_tmp
    return answer

 

"BAAAABB" 는 5가 나와야하는데 6이 나온다.

index를 구하는거는 제일 처음 B를 구하는거여서 2+1이되나보다


def solution(name):
    answer = 0
    len_name=len(name)
    init="A"*len_name
    chk=[]
    ## Number of movements
    for i in range(len_name):
        if ord(name[i]) == 65:
            pass
        else:
            tmp=ord(name[i]) - 65
            chk.append(i)
            if tmp>13:
                answer+=26-tmp
            else:
                answer+=tmp
    ind=0
    
    ## Greedy algorithm
    while chk!=[]:  
        tmp1=abs(chk[0]-ind)      
        tmp2=len_name+ind-chk[-1]  
        #print("tmp1:{}\ttmp2:{}".format(tmp1,tmp2))
        if tmp1>tmp2:               
            ind=chk.pop()  
            answer+=tmp2
        else:               
            ind=chk.pop(0)
            answer+=tmp1
        #print("ind:{}\tchk:{}".format(ind,chk))
    return answer

 


def solution(name):
    answer = 0
    len_name=len(name)
    init="A"*len_name
    chk=[]
    ## Number of movements
    for i in range(len_name):
        if ord(name[i]) == 65:
            pass
        else:
            tmp=ord(name[i]) - 65
            chk.append(i)
            if tmp>13:
                answer+=26-tmp
            else:
                answer+=tmp
    ind=0

    ## Greedy algorithm
    while chk!=[]:  
        tmp1=min(abs(chk[0]-ind),len_name-abs(chk[0]-ind))
        tmp2=min(abs(chk[-1]-ind),len_name-abs(chk[-1]-ind))
        min_chk=min(tmp1,tmp2)
        #print("tmp1:{}\ttmp2:{}".format(tmp1,tmp2))
        if tmp1>tmp2:               
            ind=chk.pop()  
            answer+=tmp2
        else :               
            ind=chk.pop(0)
            answer+=tmp1
        #print("ind:{}\tchk:{}".format(ind,chk))
    return answer