내 맘대로 알고리즘

KiwiJuiceEasy, InterestingParty, Cryptography


문제 :


class : KiwiJuiceEasy

URL : https://community.topcoder.com/stat?c=problem_statement&pm=11019


0. 내가 생각한 코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class KiwiJuiceEasy {
    /**
     * @author gamjatwigim
     * @date 2018.12.23
     * @url: https://gamjatwigim.tistory.com/
     * @algorithm : https://www.topcoder.com/stat?c=problem_statement&pm=11019 */
    static public void main(String[] args) {
        
    }
    
    public int[] thePouring(int capacities[], int bottles[], int fromId[], int fromTo[]) {
        for(int i=0; i<fromId.length; i++) {
            int tempTotalBottle = bottles[fromTo[i]] + bottles[fromId[i]];//이전 병에서 받았을 때,
            //일단 음료를 줌
            bottles[fromTo[i]] += bottles[fromId[i]]; //fromId로부터 받음
            bottles[fromId[i]] -= bottles[fromId[i]]; //fromId의 값을 온전히 다 뺌
            if(tempTotalBottle > capacities[fromTo[i]])//담을 수 있는 양보다 많을 경우
            {
                int restDrink = tempTotalBottle - capacities[fromTo[i]]; //나머지 음료
                bottles[fromTo[i]] = capacities[fromTo[i]];//넣을 음료를 가득 채워버림
                bottles[fromId[i]] = restDrink; //나머지 음료를 병에 넣음
            }
        }
        return bottles; 
    }
}
 
cs


1 코드를 생각하게 된 이유


- 우선 음료수를 따른다.

- 음료수가 맥스라면, 덜어낸다.


라는 생각으로 코드는 작성되었고, 그에 따라서 음료수가 다 따라진 값을 tempTotalBottle이라고 하고,,

이게 capacities[]의 양보다 클 경우에만 남은 음료라고 하여 restDrink라는 변수를 만들어서, 이전 병에 넣어주고

담아야할 병은 capacities[]와 동일시 시켰다.


현실적으로 코드를 작성했다고 생각은 했지만, 좋은 예시를 봤을 때, 좋지 않은 코드라는 것을 알 수 있었다.


가장 좋은 코드는 Math.min을 이용해서 코드를 간결화 시키는 것이 있다는 것을 알았다.


  

문제 :


Class: InterestingParty

URL : https://community.topcoder.com/stat?c=problem_statement&pm=11019


0. 내가 생각한 코드


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.HashMap;
 
public class InterestingParty {
    /**
     * @author gamjatwigim
     * @date 2018.12.23
     * @url: https://gamjatwigim.tistory.com/
     * @algorithm : https://community.topcoder.com/stat?c=problem_statement&pm=11312&rd=14423 */
    private int max = 0//결과 값
    public int bestInvitation(String[] first, String[] second) {
        HashMap<String, Integer> hashMap = new HashMap();//hashMap을 통해서 key,value로 접근
        for(int i=0; i<first.length;i++) {
            hashMap.put(first[i], 0);//초기화
            hashMap.put(second[i], 0);//초기화
        }
        for(int i=0; i<first.length; i++) {
            hashMap.put(first[i], hashMap.get(first[i])+1); //접근하게 되면 값을 증가
            hashMap.put(second[i], hashMap.get(second[i])+1);//접근하게 되면 값을 증가
        }
        hashMap.values().forEach(value->{
            max = Math.max(max, value); //max와 value중 큰 값을 value에 넣음
        });
        return max;
    }
}
cs


1. 코드를 생각하게 된 이유:


a배열과 b배열의 콘텐츠는 key값이 될 수 있고, 그 key값이 나온만큼 hashMap[key]++, 를 한다면 된다고 생각을 했다.



문제 :


Class : Cryptography

class :  https://community.topcoder.com/stat?c=problem_statement&pm=10814


0. 내가 생각한 코드 :


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class Cryptography {
    /**
     * @author gamjatwigim
     * @date 2018.12.23
     * @url: https://gamjatwigim.tistory.com/
     * @algorithm : https://community.topcoder.com/stat?c=problem_statement&pm=10814 */
    
    //해결 방법: 가장 작은 수에서 +1을 하면 가장 큰 비율로 증가한다.
    public long encrypt(int[] passwords) {
        int min = 99999999//가장 작은 값
        int min_index = -1//가장 작은 인덱스
        long result = 1//결과값
        //for문을 통해서 가장 작은 값과 index를 구한다. 
        for(int i=0;i<passwords.length;i++) {
            if(passwords[i] == Math.min(min, passwords[i])) {
                min = passwords[i];
                min_index = i;
            }
        }
        
        //가장 작은 값일 경우에만 1을 더해서 곱한다.
        for(int i=0; i<passwords.length;i++) {
            if(i==min_index)
                result *= (passwords[i]+1);
            else
                result *= passwords[i];
        }
        
        //결과값
        return result;
    }
}
 
cs


1. 생각한 이유 :


가장 작은 값에서 +1 을 했을 경우에 가장 큰 값으로 값이 곱해진다.