์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Searching(์ •๋ ฌ, ์ด๋ถ„๊ฒ€์ƒ‰๊ณผ ๊ฒฐ์ •์•Œ๊ณ ๋ฆฌ์ฆ˜) : ์žฅ๋‚œ ๊พธ๋Ÿฌ๊ธฐ

2023. 11. 8. 14:33ใ†์ธํ”„๋Ÿฐ/์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ์ž…๋ฌธ : ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋Œ€๋น„

728x90

 

https://hyejin.tistory.com/1244

 

์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Searching(์ •๋ ฌ, ์ด๋ถ„๊ฒ€์ƒ‰๊ณผ ๊ฒฐ์ •์•Œ๊ณ ๋ฆฌ์ฆ˜) : ์ค‘๋ณต ํ™•

https://hyejin.tistory.com/1243 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Searching(์ •๋ ฌ, ์ด๋ถ„๊ฒ€์ƒ‰๊ณผ ๊ฒฐ์ •์•Œ๊ณ ๋ฆฌ์ฆ˜) : Least Recent https://hyejin.tistory.com/1242 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Sea

hyejin.tistory.com

-> ์ด์ „ ๋ฌธ์ œ ํ’€์ด 

 

 

6. ์žฅ๋‚œ ๊พธ๋Ÿฌ๊ธฐ 

์„ค๋ช…

์ƒˆ ํ•™๊ธฐ๊ฐ€ ์‹œ์ž‘๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ฒ ์ˆ˜๋Š” ์ƒˆ ์ง๊ฟ์„ ๋งŒ๋‚˜ ๋„ˆ๋ฌด ์‹ ์ด ๋‚ฌ์Šต๋‹ˆ๋‹ค.

์ฒ ์ˆ˜๋„ค ๋ฐ˜์—๋Š” N๋ช…์˜ ํ•™์ƒ๋“ค์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์„ ์ƒ๋‹˜์€ ๋ฐ˜ ํ•™์ƒ๋“ค์—๊ฒŒ ๋ฐ˜ ๋ฒˆํ˜ธ๋ฅผ ์ •ํ•ด ์ฃผ๊ธฐ ์œ„ํ•ด ์šด๋™์žฅ์— ๋ฐ˜ ํ•™์ƒ๋“ค์„ ํ‚ค๊ฐ€ ๊ฐ€์žฅ ์ž‘์€ ํ•™์ƒ๋ถ€ํ„ฐ ์ผ๋ ฌ๋กœ ํ‚ค์ˆœ์œผ๋กœ ์„ธ์› ์Šต๋‹ˆ๋‹ค.

 

์ œ์ผ ์•ž์— ๊ฐ€์žฅ ์ž‘์€ ํ•™์ƒ๋ถ€ํ„ฐ ๋ฐ˜ ๋ฒˆํ˜ธ๋ฅผ 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. ์ฒ ์ˆ˜๋Š” ์ง๊ฟ๋ณด๋‹ค ํ‚ค๊ฐ€ ํฝ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์ฒ ์ˆ˜๊ฐ€ ์•ž ๋ฒˆํ˜ธ๋ฅผ ๋ฐ›๊ณ  ์‹ถ์–ด ์ง๊ฟ๊ณผ ์ž๋ฆฌ๋ฅผ ๋ฐ”๊ฟจ์Šต๋‹ˆ๋‹ค.

์„ ์ƒ๋‹˜์€ ์ด ์‚ฌ์‹ค์„ ๋ชจ๋ฅด๊ณ  ํ•™์ƒ๋“ค์—๊ฒŒ ์„œ์žˆ๋Š” ์ˆœ์„œ๋Œ€๋กœ ๋ฒˆํ˜ธ๋ฅผ ๋ถ€์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค.

์ฒ ์ˆ˜์™€ ์ง๊ฟ์ด ์ž๋ฆฌ๋ฅผ ๋ฐ”๊พผ ๋ฐ˜ ํ•™์ƒ๋“ค์˜ ์ผ๋ ฌ๋กœ ์„œ์žˆ๋Š” ํ‚ค ์ •๋ณด๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ์ฒ ์ˆ˜๊ฐ€ ๋ฐ›์€ ๋ฒˆํ˜ธ์™€ ์ฒ ์ˆ˜ ์ง๊ฟ์ด ๋ฐ›์€ ๋ฒˆํ˜ธ๋ฅผ

์ฐจ๋ก€๋กœ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

์ž…๋ ฅ

์ฒซ ๋ฒˆ์งธ ์ค„์— ์ž์—ฐ์ˆ˜ N(5<=N<=100)์ด ์ฃผ์–ด์ง„๋‹ค.

๋‘ ๋ฒˆ์งธ ์ค„์— ์ œ์ผ ์•ž์—๋ถ€ํ„ฐ ์ผ๋ ฌ๋กœ ์„œ์žˆ๋Š” ํ•™์ƒ๋“ค์˜ ํ‚ค๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

ํ‚ค(๋†’์ด) ๊ฐ’ H๋Š” (120<=H<=180)์˜ ์ž์—ฐ์ˆ˜ ์ž…๋‹ˆ๋‹ค.

 

์ถœ๋ ฅ

์ฒซ ๋ฒˆ์งธ ์ค„์— ์ฒ ์ˆ˜์˜ ๋ฐ˜ ๋ฒˆํ˜ธ์™€ ์ง๊ฟ์˜ ๋ฐ˜ ๋ฒˆํ˜ธ๋ฅผ ์ฐจ๋ก€๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ์‹œ ์ž…๋ ฅ 1 

9
120 125 152 130 135 135 143 127 160

 

์˜ˆ์‹œ ์ถœ๋ ฅ 1

3 8

 

ํžŒํŠธ

์ถœ๋ ฅํ•ด์„ค : ํ‚ค ์ •๋ณด 152๊ฐ€ ์ฒ ์ˆ˜์ด๊ณ , 127์ด ์ฒ ์ˆ˜ ์ง๊ฟ์ž…๋‹ˆ๋‹ค.

 

 

๋ฌธ์ œ ํ’€์ด 1

public ArrayList<Integer> solution(int n, int[] arr)
{
   ArrayList<Integer> answer = new ArrayList<>();
   int[] copyArrs = Arrays.copyOf(arr, arr.length);
   Arrays.sort(arr);
   
   for (int i = 0; i < n; i++)
   {
      if (arr[i] != copyArrs[i]) {
         answer.add(i+1);
      }
   }
   
   return answer;
}

 

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป : ์ด ๋ฌธ์ œ๋ฅผ ์ •๋ ฌํ•ด์„œ ํ‘ธ๋Š” ๋ฐฉ๋ฒ• ๋ง๊ณ  ์ฒ˜์Œ์—๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์–ด๋ณด๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ํ‚ค๊ฐ€ ๊ฐ™์€ ํ•™์ƒ์ด ์—ฌ๋Ÿฌ๋ช… ๋‚˜์˜ค๋Š” ์˜ˆ์‹œ์—์„œ ์ž๊พธ ์˜ค๋‹ต์ด ๋‚˜์™€์„œ.. ๊ฒฐ๊ตญ ์ •๋ ฌ๋กœ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์•„์„œ ์œ„์— ํ’€์ด ๋ฐฉ์‹์œผ๋กœ ์ •๋‹ต์ด ๋‚˜์™”๋‹ค. 

 

๋จผ์ € Arrays.copyOf ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ arr๋ฅผ arr.length ๋งŒํผ ๋ณต์‚ฌ๋ฅผ ํ•ด์„œ copyArrs ๋กœ ์ฃผ์–ด์ง„ arr ๋ฐฐ์—ด๊ณผ ๋˜‘๊ฐ™์€ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์ฃผ๊ณ , 

๊ทธ ๋‹ค์Œ Arrays.sort ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ copyArrs ๋ฐฐ์—ด์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์คฌ๋‹ค.

 

๊ทธ๋‹ค์Œ for๋ฌธ์„ ๋Œ๋ฉด์„œ copyArrs์™€ arr์ด ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ’์ด ์กด์žฌํ•  ๋•Œ, ๊ฑฐ๊ธฐ๊ฐ€ ์ฒ ์ˆ˜๊ฐ€ ํ‚ค๊ฐ€ ์ž‘์€ ํ•™์ƒ๊ณผ ์ž๋ฆฌ๋ฅผ ๋ฐ”๊พผ ๊ฒƒ์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— Arraylist์— ํ•ด๋‹น ํ•™์ƒ์˜ i + 1 ์œ„์น˜๋ฅผ add ํ•ด์คฌ๋‹ค. 

 

 

๋ฌธ์ œ ํ’€์ด 2

public ArrayList<Integer> solution2(int n, int[] arr) {
   ArrayList<Integer> answer = new ArrayList<>();
   int[] tmp = arr.clone(); // ๊นŠ์€ ๋ณต์‚ฌ
   Arrays.sort(tmp);

   for (int i = 0; i < n; i++) {
      if (arr[i] != tmp[i]) {
         answer.add(i + 1);
      }
   }
   return answer;
}

 

๐Ÿ‘พ : ๊ฐ•์‚ฌ๋‹˜๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ •๋ ฌ์„ ์ด์šฉํ•ด์„œ ํ‘ธ์…จ๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ์ •๋ ฌ๋กœ ํ’€๋ฉด ๊ต‰์žฅํžˆ ๋น ๋ฅด๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

์ฃผ์–ด์ง„ ๋ฐฐ์—ด arr๋ฅผ ๊นŠ์€ ๋ณต์‚ฌ clone() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ tmp ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ , 

for๋ฌธ์„ ์ด์šฉํ•ด์„œ arr[i] ์™€ tmp[i]์˜ ๊ฐ’์ด ๋‹ค๋ฅด๋ฉด ์ž๋ฆฌ๋ฅผ ๋ฐ”๊ฟจ๋‹ค๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ๊ทธ ํ•™์ƒ๋“ค์˜ i ์œ„์น˜ + 1 ์„ answer ์— ๋‹ด์•„์คฌ๋‹ค. 

i+ 1 ์„ ๋‹ด๋Š” ์ด์œ ๋Š” ๋ฐฐ์—ด์€ 0 ๋ถ€ํ„ฐ ์‹œ์ž‘์ด์ง€๋งŒ, ํ•™์ƒ๋“ค์˜ ์œ„์น˜๋Š” 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

728x90

'์ธํ”„๋Ÿฐ > ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ์ž…๋ฌธ : ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋Œ€๋น„' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Searching(์ •๋ ฌ, ์ด๋ถ„๊ฒ€์ƒ‰๊ณผ ๊ฒฐ์ •์•Œ๊ณ ๋ฆฌ์ฆ˜) : ์ด๋ถ„ ๊ฒ€์ƒ‰  (0) 2023.11.09
์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Searching(์ •๋ ฌ, ์ด๋ถ„๊ฒ€์ƒ‰๊ณผ ๊ฒฐ์ •์•Œ๊ณ ๋ฆฌ์ฆ˜) : ์ขŒํ‘œ ์ •๋ ฌ  (1) 2023.11.08
์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Searching(์ •๋ ฌ, ์ด๋ถ„๊ฒ€์ƒ‰๊ณผ ๊ฒฐ์ •์•Œ๊ณ ๋ฆฌ์ฆ˜) : ์ค‘๋ณต ํ™•์ธ  (0) 2023.11.07
์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Searching(์ •๋ ฌ, ์ด๋ถ„๊ฒ€์ƒ‰๊ณผ ๊ฒฐ์ •์•Œ๊ณ ๋ฆฌ์ฆ˜) : Least Recently Used  (0) 2023.11.06
์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ch06. Sorting and Searching(์ •๋ ฌ, ์ด๋ถ„๊ฒ€์ƒ‰๊ณผ ๊ฒฐ์ •์•Œ๊ณ ๋ฆฌ์ฆ˜) : ์‚ฝ์ž… ์ •๋ ฌ  (0) 2023.11.06