whatisthis?

๋ฐฑ์ค€ 10818 javascript (node.js) ํ’€์ด ๋ณธ๋ฌธ

ALGORITHM/BOJ (Node.js)

๋ฐฑ์ค€ 10818 javascript (node.js) ํ’€์ด

thisisyjin 2022. 1. 4. 10:14

1์ฐจ์› ๋ฐฐ์—ด - (1)

 

 


 

๐Ÿ’ก๋ฌธ์ œ

N๊ฐœ์˜ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด๋•Œ, ์ตœ์†Ÿ๊ฐ’๊ณผ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. 

๐Ÿ“์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ •์ˆ˜์˜ ๊ฐœ์ˆ˜ N (1 ≤ N ≤ 1,000,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” N๊ฐœ์˜ ์ •์ˆ˜๋ฅผ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ฃผ์–ด์ง„๋‹ค. ๋ชจ๋“  ์ •์ˆ˜๋Š” -1,000,000๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 1,000,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ •์ˆ˜์ด๋‹ค.

๐Ÿ“ˆ์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์ฃผ์–ด์ง„ ์ •์ˆ˜ N๊ฐœ์˜ ์ตœ์†Ÿ๊ฐ’๊ณผ ์ตœ๋Œ“๊ฐ’์„ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด ์ถœ๋ ฅํ•œ๋‹ค.

 

์˜ˆ์ œ ์ž…๋ ฅ

5
20 10 35 30 7

 

์˜ˆ์ œ ์ถœ๋ ฅ

7 35

 

 

 


< ์ฝ”๋“œ >

let input = require('fs').readFileSync('/dev/stdin').toString().split('\n');

let T = Number(input[0]);
let nums = input[1].split(' ').map(x => Number(x));

let max = nums[0];
let min = nums[0];

for(i=1;i<T;i++){
   if(nums[i] > max) {
      max = nums[i];
   } 
   if(nums[i] < min) {
      min = nums[i];
   }
}
console.log(min + ' ' + max);

<Key Point>

 

  • input[0]์„ T๋ผ๊ณ  ์„ค์ •ํ•œ๋‹ค.
  • ๋‘๋ฒˆ์งธ ๊ฐ’๋ถ€ํ„ฐ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด์„œ max์™€ min๊ฐ’์„ ์ฒซ๋ฒˆ์งธ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.
  • ์ˆซ์ž ๋ฐฐ์—ด์˜ 2๋ฒˆ์งธ ์•„์ดํ…œ๋ถ€ํ„ฐ T-1๋ฒˆ์งธ ์•„์ดํ…œ๊นŒ์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค. (i=1๋ถ€ํ„ฐ i<T์ด๋ฏ€๋กœ) 

 

 

+) ๐Ÿ’ก ๋‹ค๋ฅธ ํ’€์ด

let input = require("fs").readFileSync("/dev/stdin").toString().split('\n');

let T = Number(input[0]);
let nums = input[1].split(" ").map(x => Number(x));

let max = -1000000;
let min = 1000000;

for(let i = 0; i < T; i++) {
    if(nums[i] < min)
        min = nums[i];
    if (nums[i] > max)
        max = nums[i];
}

console.log(`${min} ${max}`);

๋ฌธ์ œ์—์„œ -1,000,000๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 1,000,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ •์ˆ˜๋ผ๊ณ  ๋ช…์‹œํ–ˆ์œผ๋ฏ€๋กœ

max = -1,000,000

min = 1,000,000 ์œผ๋กœ ์„ค์ •ํ•œ ํ›„์— i = 0, ์ฆ‰ ์ฒซ๋ฒˆ์งธ ๊ฐ’๋ถ€ํ„ฐ ๋น„๊ตํ•ด๋„ ๋œ๋‹ค!

 

>> ๊ฐ’์ด ๋„ˆ๋ฌด ์ปค์ง€๋ฉด ์˜ค๋ž˜ ๊ฑธ๋ฆด ์ˆ˜๋„ ์žˆ๊ฒ ๋‹ค ใ…ก ์‹ถ์—ˆ๋Š”๋ฐ ์‹คํ–‰์‹œ๊ฐ„์€ ๋น„์Šทํ•˜๋‹ค!

 

 

 

โŒ ์ฒ˜์Œ ํ‹€๋ ธ๋˜ ์ฝ”๋“œ ๐Ÿ”ป

 

let input = require('fs').readFileSync('/dev/stdin').toString().split('\n');

let nums = input[1].split(' ').map(x => Number(x));

let max = 1;
let min = nums[0];

for(i=0;i<nums.length;i++){
   if(nums[i] > max) {
      max = nums[i];
   } else if(nums[i] < min) {
      min = nums[i];
      }
}
console.log(min + ' ' + max);

๋ฐฐ์—ด์˜ ๊ธธ์ด์ธ nums.length๊ฐ€ ์•„๋‹ˆ๋ผ

์ž…๋ ฅ ๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ํ•ญ๋ชฉ์ธ input[0]๋ฅผ T๋กœ ์„ค์ •ํ•˜๋ฉด ํŽธํ•˜๋‹ค.

 

๋˜ํ•œ i=0 ๋ถ€ํ„ฐ i<T๊ฐ€ ์•„๋‹ˆ๋ผ

i=1 ๋ถ€ํ„ฐ i<T๋กœ ํ•ด์•ผํ•œ๋‹ค.

>> ๋‘๋ฒˆ์งธ ๊ฐ’๋ถ€ํ„ฐ ๋น„๊ตํ•˜๋ฉด ๋จ. (์ตœ์†Œ๊ฐ’์„ ์ฒซ๋ฒˆ์งธ ๊ฐ’์œผ๋กœ ํ•œ ํ›„)

 

 

 


 

์‰ฌ์šด ์ดํ•ด๋ฅผ ์œ„ํ•œ ์˜ˆ์‹œ๋ฅผ ๋“ค์–ด๋ณด์ž.

 

์ž…๋ ฅ์ด

5
20 10 35 30 7

์™€ ๊ฐ™๋‹ค๋ฉด,

 

input[0], ์ฆ‰ T=5๊ฐ€ ๋œ๋‹ค.

input[1]์„ split(' ')ํ•ด์„œ ์ €์žฅํ•œ nums ๋ฐฐ์—ด์€ [20, 10, 35, 30, 7]์ด ๋œ๋‹ค.

 

max = nums[0]

min = nums[0] ์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.

 

์ฒซ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ์ตœ๋Œ€๊ฐ’์ด์ž ์ตœ์†Œ๊ฐ’์œผ๋กœ ์„ค์ •ํ•œ ํ›„์— ์ˆœ์„œ๋Œ€๋กœ ํฌ๊ธฐ๋ฅผ ๋น„๊ตํ•ด์„œ

๋งŒ์•ฝ max๋ณด๋‹ค ํฌ๋‹ค๋ฉด ๊ทธ ๊ฐ’์„ max๋กœ ๊ฐฑ์‹ ํ•˜๊ณ 

min๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ๊ทธ ๊ฐ’์„ min์œผ๋กœ ๊ฐฑ์‹ ํ•œ๋‹ค.

** ์—ฌ๊ธฐ์„œ ๊ทธ ๊ฐ’์ด๋ž€? nums[i]