# ๋ฐฐ์—ด (Array)



  • C++์—์„œ ์‚ฌ์ด์ฆˆ ๊ตฌํ•˜๊ธฐ
int arr[] = { 1, 2, 3, 4, 5, 6, 7 }; 
int n = sizeof(arr) / sizeof(arr[0]); // 7


  1. # ๋ฐฐ์—ด ํšŒ์ „ ํ”„๋กœ๊ทธ๋žจ

img

์ „์ฒด ์ฝ”๋“œ๋Š” ๊ฐ ํ•˜์ดํผ๋งํฌ๋ฅผ ๋ˆŒ๋Ÿฌ์ฃผ์‹œ๋ฉด ์ด๋™๋ฉ๋‹ˆ๋‹ค.


  • ๊ธฐ๋ณธ์ ์ธ ํšŒ์ „ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„ (opens new window)

    temp๋ฅผ ํ™œ์šฉํ•ด์„œ ์ฒซ๋ฒˆ์งธ ์ธ๋ฑ์Šค ๊ฐ’์„ ์ €์žฅ ํ›„ arr[0]~arr[n-1]์„ ๊ฐ๊ฐ arr[1]~arr[n]์˜ ๊ฐ’์„ ์ฃผ๊ณ , arr[n]์— temp๋ฅผ ๋„ฃ์–ด์ค€๋‹ค.

    void leftRotatebyOne(int arr[], int n){
        int temp = arr[0], i;
        for(i = 0; i < n-1; i++){
           arr[i] = arr[i+1];
        }
        arr[i] = temp;
    }
    

    ์ด ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•ด ์›ํ•˜๋Š” ํšŒ์ „ ์ˆ˜ ๋งŒํผ for๋ฌธ์„ ๋Œ๋ ค ๊ตฌํ˜„์ด ๊ฐ€๋Šฅ


  • ์ €๊ธ€๋ง ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„ (opens new window)

    ArrayRotation

    ์ตœ๋Œ€๊ณต์•ฝ์ˆ˜ gcd๋ฅผ ์ด์šฉํ•ด ์ง‘ํ•ฉ์„ ๋‚˜๋ˆ„์–ด ์—ฌ๋Ÿฌ ์š”์†Œ๋ฅผ ํ•œ๊บผ๋ฒˆ์— ์ด๋™์‹œํ‚ค๋Š” ๊ฒƒ

    ์œ„ ๊ทธ๋ฆผ์ฒ˜๋Ÿผ ๋ฐฐ์—ด์ด ์•„๋ž˜์™€ ๊ฐ™๋‹ค๋ฉด

    arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

    1,2,3์„ ๋’ค๋กœ ์˜ฎ๊ธธ ๋•Œ, ์ธ๋ฑ์Šค๋ฅผ 3๊ฐœ์”ฉ ๋ฌถ๊ณ  ํšŒ์ „์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.

    a) arr [] -> { 4 2 3 7 5 6 10 8 9 1 11 12}

    b) arr [] -> {4 5 3 7 8 6 10 11 9 1 2 12}

    c) arr [] -> {4 5 6 7 8 9 10 11 12 1 2 3 }


  • ์—ญ์ „ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„ (opens new window)

    ํšŒ์ „์‹œํ‚ค๋Š” ์ˆ˜์— ๋Œ€ํ•ด ๊ตฌ๊ฐ„์„ ๋‚˜๋ˆ„์–ด reverse๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•

    d = 2์ด๋ฉด

    1,2 / 3,4,5,6,7๋กœ ๊ตฌ๊ฐ„์„ ๋‚˜๋ˆˆ๋‹ค.

    ์ฒซ๋ฒˆ์งธ ๊ตฌ๊ฐ„ reverse -> 2,1

    ๋‘๋ฒˆ์งธ ๊ตฌ๊ฐ„ reverse -> 7,6,5,4,3

    ํ•ฉ์น˜๊ธฐ -> 2,1,7,6,5,4,3

    ํ•ฉ์นœ ๋ฐฐ์—ด์„ reverse -> 3,4,5,6,7,1,2

    • swap์„ ํ†ตํ•œ reverse
    void reverseArr(int arr[], int start, int end){
        while (start < end){
            int temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
    
            start++;
            end--;
        }
    }
    

    โ€‹

    • ๊ตฌ๊ฐ„์„ d๋กœ ๋‚˜๋ˆ„์—ˆ์„ ๋•Œ ์—ญ์ „ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„
    void rotateLeft(int arr[], int d, int n){
    	reverseArr(arr, 0, d-1);
    	reverseArr(arr, d, n-1);
    	reverseArr(arr, 0, n-1);
    }
    


  1. # ๋ฐฐ์—ด์˜ ํŠน์ • ์ตœ๋Œ€ ํ•ฉ ๊ตฌํ•˜๊ธฐ


์˜ˆ์‹œ) arr[i]๊ฐ€ ์žˆ์„ ๋•Œ, i*arr[i]์˜ Sum์ด ๊ฐ€์žฅ ํด ๋•Œ ๊ทธ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ธฐ

(ํšŒ์ „ํ•˜๋ฉด์„œ ์ตœ๋Œ€๊ฐ’์„ ์ฐพ์•„์•ผํ•œ๋‹ค.)

Input: arr[] = {1, 20, 2, 10}
Output: 72

2๋ฒˆ ํšŒ์ „ํ–ˆ์„ ๋•Œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ตœ๋Œ€๊ฐ’์ด ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.
{2, 10, 1, 20}
20*3 + 1*2 + 10*1 + 2*0 = 72

Input: arr[] = {10, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Output: 330

9๋ฒˆ ํšŒ์ „ํ–ˆ์„ ๋•Œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ตœ๋Œ€๊ฐ’์ด ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
0*1 + 1*2 + 2*3 ... 9*10 = 330

# ์ ‘๊ทผ ๋ฐฉ๋ฒ•

arr[i]์˜ ์ „์ฒด ํ•ฉ๊ณผ i*arr[i]์˜ ์ „์ฒด ํ•ฉ์„ ์ €์žฅํ•  ๋ณ€์ˆ˜ ์„ ์–ธ

์ตœ์ข… ๊ฐ€์žฅ ํฐ sum ๊ฐ’์„ ์ €์žฅํ•  ๋ณ€์ˆ˜ ์„ ์–ธ

๋ฐฐ์—ด์„ ํšŒ์ „์‹œํ‚ค๋ฉด์„œ i*arr[i]์˜ ํ•ฉ์˜ ๊ฐ’์„ ์ €์žฅํ•˜๊ณ , ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ์ €์žฅํ•ด์„œ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.


# ํ•ด๊ฒฐ๋ฒ•

ํšŒ์ „ ์—†์ด i*arr[i]์˜ sum์„ ์ €์žฅํ•œ ๊ฐ’
R0 = 0*arr[0] + 1*arr[1] +...+ (n-1)*arr[n-1]


1๋ฒˆ ํšŒ์ „ํ•˜๊ณ  i*arr[i]์˜ sum์„ ์ €์žฅํ•œ ๊ฐ’
R1 = 0*arr[n-1] + 1*arr[0] +...+ (n-1)*arr[n-2]

์ด ๋‘๊ฐœ๋ฅผ ๋นผ๋ฉด?
R1 - R0 = arr[0] + arr[1] + ... + arr[n-2] - (n-1)*arr[n-1]

2๋ฒˆ ํšŒ์ „ํ•˜๊ณ  i*arr[i]์˜ sum์„ ์ €์žฅํ•œ ๊ฐ’
R2 = 0*arr[n-2] + 1*arr[n-1] +...+ (n?1)*arr[n-3]

1๋ฒˆ ํšŒ์ „ํ•œ ๊ฐ’๊ณผ ๋นผ๋ฉด?
R2 - R1 = arr[0] + arr[1] + ... + arr[n-3] - (n-1)*arr[n-2] + arr[n-1]


์—ฌ๊ธฐ์„œ ๊ทœ์น™์„ ์ฐพ์„ ์ˆ˜ ์žˆ์Œ.

Rj - Rj-1 = arrSum - n * arr[n-j]

์ด๋ฅผ ํ™œ์šฉํ•ด์„œ ๋ช‡๋ฒˆ ํšŒ์ „ํ–ˆ์„ ๋•Œ ์ตœ๋Œ€๊ฐ’์ด ๋‚˜์˜ค๋Š” ์ง€ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ตฌํ˜„ ์†Œ์Šค ์ฝ”๋“œ ๋งํฌ (opens new window)



  1. # ํŠน์ • ๋ฐฐ์—ด์„ arr[i] = i๋กœ ์žฌ๋ฐฐ์—ด ํ•˜๊ธฐ

์˜ˆ์‹œ) ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์—์„œ arr[i] = i์ด ๊ฐ€๋Šฅํ•œ ๊ฒƒ๋งŒ ์žฌ๋ฐฐ์—ด ์‹œํ‚ค๊ธฐ

Input : arr = {-1, -1, 6, 1, 9, 3, 2, -1, 4, -1}
Output : [-1, 1, 2, 3, 4, -1, 6, -1, -1, 9]

Input : arr = {19, 7, 0, 3, 18, 15, 12, 6, 1, 8,
              11, 10, 9, 5, 13, 16, 2, 14, 17, 4}
Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
         11, 12, 13, 14, 15, 16, 17, 18, 19]

arr[i] = i๊ฐ€ ์—†์œผ๋ฉด -1๋กœ ์ฑ„์šด๋‹ค.

# ์ ‘๊ทผ ๋ฐฉ๋ฒ•

arr[i]๊ฐ€ -1์ด ์•„๋‹ˆ๊ณ , arr[i]์ด i๊ฐ€ ์•„๋‹ ๋•Œ๊ฐ€ ์šฐ์„  ์กฐ๊ฑด

ํ•ด๋‹น arr[i] ๊ฐ’์„ ์ €์žฅ(x)ํ•ด๋‘๊ณ , ์ด ๊ฐ’์ด x์ผ ๋•Œ arr[x]๋ฅผ ํƒ์ƒ‰

arr[x] ๊ฐ’์„ ์ €์žฅ(y)ํ•ด๋‘๊ณ , arr[x]๊ฐ€ -1์ด ์•„๋‹ˆ๋ฉด์„œ arr[x]๊ฐ€ x๊ฐ€ ์•„๋‹Œ ๋™์•ˆ์„ ํƒ์ƒ‰

arr[x]๋ฅผ x๊ฐ’์œผ๋กœ ์ €์žฅํ•ด์ฃผ๊ณ , ๊ธฐ์กด์˜ x๋ฅผ y๋กœ ์ˆ˜์ •

int fix(int A[], int len){
    
    for(int i = 0; i < len; i++) {
        
        
        if (A[i] != -1 && A[i] != i){ // A[i]๊ฐ€ -1์ด ์•„๋‹ˆ๊ณ , i๋„ ์•„๋‹ ๋•Œ
            
            int x = A[i]; // ํ•ด๋‹น ๊ฐ’์„ x์— ์ €์žฅ
            
            while(A[x] != -1 && A[x] != x){ // A[x]๊ฐ€ -1์ด ์•„๋‹ˆ๊ณ , x๋„ ์•„๋‹ ๋•Œ
                
                int y = A[x]; // ํ•ด๋‹น ๊ฐ’์„ y์— ์ €์žฅ
                A[x] = x; 
                
                x = y;
            }
            
            A[x] = x;
            
            if (A[i] != i){
                A[i] = -1;
            }
        }
    }
    
}

๊ตฌํ˜„ ์†Œ์Šค ์ฝ”๋“œ (opens new window)



์ตœ์ข… ์ˆ˜์ • : 12/17/2022, 7:23:59 AM