Publish:

ํƒœ๊ทธ: , , , , ,

์นดํ…Œ๊ณ ๋ฆฌ:

img_3.png

๋ฌธ์ œ

๋ฌธ์ œ ๋งํฌ

img_3.png

ํ’€์ด

์ฒซ๋ฒˆ์งธ ํ’€์ด (๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ)

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
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;

class Solution {
    public int[] solution(int n, long left, long right) {
        
        // 1. 2์ฐจ์› ๋ฐฐ์—ด์— ๊ฐ’ ์„ธํŒ…
        int[][] arr = new int[n][n];
        for (int i = 1; i <= n; i++) {
          for (int j = 1; j <= n; j++) {
            arr[i - 1][j - 1] = Math.max(j, i);
          }
        }

        // 2. 2์ฐจ์› ๋ฐฐ์—ด -> 1์ฐจ์› ๋ฐฐ์—ด
        int[] tmp = new int[arr.length * arr[0].length];
        for (int i = 0; i < n; i++) {
          for (int j = 0; j < n; j++) {
            tmp[arr[i].length * i + j] = arr[i][j];
          }
        }

        // 3. ์ธ๋ฑ์Šค ๋ฒ”์œ„๋งŒ ๊ฐ€์ ธ์˜ค๊ธฐ
        int[] result = Arrays.copyOfRange(tmp, (int) left, (int) right + 1);


        return result;
    
    }
}

์ฒ˜์Œ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋Š” ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๋กœ ํ†ต๊ณผํ•˜์ง€ ๋ชปํ–ˆ๋‹ค. ๊ฐ„๋žตํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์„ค๋ช…ํ•˜์ž๋ฉด,

1๋ฒˆ ์ฃผ์„์—์„œ, ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด n = 1 ~ n ์˜ ๋ชจ๋“  ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค. ์ด๋•Œ, ํ–‰๊ณผ ์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ๋น„๊ตํ•ด ๋‘˜ ์ค‘ ํฐ ๊ฐ’์„ ๋„ฃ๋Š”๋‹ค. arr ๋ฐฐ์—ด์—๋Š” ์•„๋ž˜ ์ฒ˜๋Ÿผ ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

1
2
3
4
5
// n = 3
[[1, 2, 3], [2, 2, 3], [3, 3, 3]]

// n = 4
[[1, 2, 3, 4], [2, 2, 3, 4], [3, 3, 3, 4], [4, 4, 4, 4]]

2๋ฒˆ ์ฃผ์„์—์„œ, ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ 2์ฐจ์› ๋ฐฐ์—ด์„ 1์ฐจ์› ๋ฐฐ์—ด๋กœ ํ•ฉ์นœ๋‹ค. 1์ฐจ์› ๋ฐฐ์—ด์˜ ๊ธธ์ด๋Š” 2์ฐจ์› ๋ฐฐ์—ด์˜ ํ–‰์˜ ๊ธธ์ด * ๊ฐ ์—ด์˜ ๊ธธ์ด ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ’ ์ธ๋ฑ์Šค์— ๋“ค์–ด๊ฐ€์•ผ ํ•  ๊ฐ’์„ ์‚ดํŽด๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ทœ์น™์ด ์žˆ๋‹ค.

1
2
3
4
5
6
7
8
9
10
11
12
13
// n ์ด 4์ผ๋•Œ
tmp[4] = arr[1][0]
tmp[5] = arr[1][1]
tmp[6] = arr[1][2]
tmp[7] = arr[1][3]
  ...

// 4 = n * 1 + 0
// 5 = n * 1 + 1
// 6 = n * 1 + 2
// 7 = n * 1 + 3
  ...
// x = n * i + j

3๋ฒˆ ์ฃผ์„์—์„œ, ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” left ๋ถ€ํ„ฐ right ๋ฒ”์œ„๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด Arrays.copyOfRange() ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ์ด ์ฝ”๋“œ๋Š” ์‹คํ–‰ํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค. ๋ฌธ์ œ์—์„œ n ์˜ ๋ฒ”์œ„๊ฐ€ 10^7 ์ด๊ธฐ ๋•Œ๋ฌธ์— 1๋ฒˆ ์ฃผ์„์—์„œ 10^7 * 10^7 ์ด ๋˜์–ด ๋ฌด๋ ค 100์กฐ๋ฒˆ ๊ณ„์‚ฐํ•ด์•ผ ํ•œ๋‹ค. ์ด๋ฅผ ์œ„์™€ ๊ฐ™์ด O(n^2) ๋กœ ํ’€๊ฒŒ๋˜๋ฉด ๋Œ€๋žต 100๋งŒ์ดˆ๊ฐ€ ๊ฑธ๋ฆฐ๋‹คโ€ฆ (1์ดˆ์— 10^8 ๋ฒˆ ์—ฐ์‚ฐํ•œ๋‹ค๊ณ  ๊ฐ€์ •.)

๋‘๋ฒˆ์งธ ํ’€์ด

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;

class Solution {
    public int[] solution(int n, long left, long right) {
        int len = (int) (right - left + 1);
        int[] result = new int[len];

        for (int i = 0; i < len; i++) {
          long pos = left + i;
          int row = (int) (pos / n);
          int col = (int) (pos % n);
          result[i] = Math.max(row, col) + 1;
        }


        return result;
    
    }
}

์ผ๋‹จ ์ฒซ๋ฒˆ์งธ ํ’€์ด์—์„œ ์ด์ค‘ for ๋ฌธ์„ ์—†์• ์•ผ ํ•œ๋‹ค. ๊ฒฐ๊ตญ 2์ฐจ์› ๋ฐฐ์—ด์— ๊ฐ’์„ ์„ธํŒ…ํ•˜๋Š” ๊ณผ์ •์„ ๊ฑด๋„ˆ๋›ฐ๊ณ  ๋ฐ”๋กœ ๊ฒฐ๊ณผ ๋ฐฐ์—ด์„ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค.

1
2
3
4
5
6
7
8
9
// n = 4, left = 7 ์ผ๋•Œ
result[7] = 4     // (7 + 0) / 4 = 1...3
result[8] = 3     // (7 + 1) / 4 = 2...0
result[9] = 3     // (7 + 2) / 4 = 2...1
result[10] = 3    // (7 + 3) / 4 = 2...2
result[11] = 4    // (7 + 4) / 4 = 2...3
  ...

// result[x] = (/ ์—ฐ์‚ฐ) or (% ์—ฐ์‚ฐ) ๊ฒฐ๊ณผ ์ค‘ ํฐ ๊ฐ’ + 1

์‚ฌ์‹ค ๋ฌธ์ œ์—์„œ n ์˜ ๋ฒ”์œ„ ์กฐ๊ฑด๋งŒ ํ™•์ธํ•ด๋„ ์–ด๋–ป๊ฒŒ ํ’€์–ด์•ผ ๋ ์ง€ ๊ฐ์„ ์žก์„ ์ˆ˜ ์žˆ์—ˆ์„ ํ…๋ฐ, ์‹œ๊ฐ„๋ณต์žก๋„ ๊ณ„์‚ฐ์„ ํ•˜๋Š” ๊ฒƒ์ด ์ต์ˆ™ํ•˜์ง€ ์•Š์•„์„œ ํ•ญ์ƒ ์ด๋ ‡๊ฒŒ ํ‹€๋ฆฌ๊ณ  ๋‚˜์„œ ์ˆ˜์ •ํ•˜๊ณค ํ•˜๋Š”๋ฐ.. ์‹œ๊ฐ„๋ณต์žก๋„์˜ ์ข…๋ฅ˜๋‚˜ ๋Œ€๋žต์ ์ธ ๊ณ„์‚ฐ ๋ฐฉ๋ฒ• ๋“ฑ์„ ์ข€ ๋” ํ•™์Šตํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค.

๋ฐฉ๋ฌธํ•ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๋Œ“๊ธ€,์ง€์ ,ํ”ผ๋“œ๋ฐฑ ์–ธ์ œ๋‚˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜Š

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ