๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ”‘ ์•Œ๊ณ ๋ฆฌ์ฆ˜/๋ฐฑ์ค€ JS

[๋ฐฑ์ค€] node.js ์ •๋ ฌ / 1181 ๋‹จ์–ด์ •๋ ฌ

by Tamii 2021. 5. 2.
๋ฐ˜์‘ํ˜•

๐Ÿ“Œ1181 ๋‹จ์–ด ์ •๋ ฌ

www.acmicpc.net/problem/1181

 

1181๋ฒˆ: ๋‹จ์–ด ์ •๋ ฌ

์ฒซ์งธ ์ค„์— ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N ≤ 20,000) ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ง„ ๋‹จ์–ด๊ฐ€ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 50์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

www.acmicpc.net

์–ด์ œ ํ’€์—ˆ๋˜ ๋‹ค์ค‘ ์กฐ๊ฑด ์ •๋ ฌ์„ ์ด์šฉํ•˜๋Š”๊ฒŒ ๋– ์˜ฌ๋ผ ๋ฐ”๋กœ ๋„์ „!

 

๋‚ด ํ’€์ด

const fs = require("fs");

const input = fs.readFileSync("./test").toString().trim().split("\n");
input.shift();

sorted_lst = new Set(input);
sorted_lst = [...sorted_lst];

sorted_lst = sorted_lst.sort(function (a, b) {
  if (a.length > b.length) return 1;
  if (a.length < b.length) return -1;
  if (a > b) return 1;
  if (a < b) return -1;
  return 0;
});

sorted_lst.map((ele) => console.log(ele));

1) ๋ฐ›์€ ์ž…๋ ฅ๋ฐฐ์—ด์„ list -> set -> list ๊ณผ์ •์„ ๊ฑธ์ณ ์ค‘๋ณต ์ œ๊ฑฐ

2) ๊ธธ์ด๊ฐ€ ์งง์€ ์ˆœ -> ์‚ฌ์ „ ์ˆœ ์œผ๋กœ ๋‹ค์ค‘์ •๋ ฌ 

 

sorted_lst = sorted_lst.sort((a, b) => a.length - b.length || a - b);

์‚ฌ์‹ค ์ฒ˜์Œ์— ์ •๋ ฌํ•  ๋•Œ์—๋Š” ์ด๋ ‡๊ฒŒ ๋ฌถ์–ด ์ฃผ์—ˆ๋Š”๋ฐ ,

(์™ผ) ๋‹ต์•ˆ  (์šฐ) ์˜ค๋‹ต

์ด์ฒ˜๋Ÿผ ์‚ฌ์ „์ˆœ ์ •๋ ฌ์ด ๋จน์ง€ ์•Š์•˜๋‹ค. ์ด๊ฒŒ ๋จน๊ฒŒ ํ•˜๋ ค๋ฉด

sorted_lst = sorted_lst.sort(
  (a, b) => a.length - b.length || a.localeCompare(b)
);

์ด๋ ‡๊ฒŒ localeCompare๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ์‚ฌ์ „์ˆœ ์ •๋ ฌ๋„ ๊ฐ€๋Šฅํ•ด์กŒ๋‹ค.

 

 

๋‹ค๋ฅธ ํ’€์ด

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

input.shift();

console.log(
  [...new Set(input)]
    .sort((a, b) => a.length - b.length || a.localeCompare(b))
    .join("\n")
);

๊ฐ™์€ ๋กœ์ง์„ ์ผ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ  ์‹œ๊ฐ„์ฐจ์ด๊ฐ€ ์—„์ฒญ๋‚ฌ๋‹ค .

๋ญ๋•Œ๋ฌธ์ธ์ง€ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด ํ•˜๋‚˜์”ฉ ์ง€์›Œ๋ณด๋ฉฐ ํ™•์ธํ–ˆ๋”๋‹ˆ..

 

๋งˆ์ง€๋ง‰์— map์„ ๋Œ๋ฆฌ๋ฉฐ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ ๋•Œ๋ฌธ์ด์—ˆ๋‹ค.

๋‹จ์ง€ ์ถœ๋ ฅ์„ ์œ„ํ•ด ๋Œ๋ ธ๋Š”๋ฐ ์‹œ๊ฐ„์„ 7๋ฐฐ๋‚˜ ์žก์•„๋จน๋‹ค๋‹ˆ...

๋˜๊ฒŒ ๋†€๋ž๊ณ . ์•ž์œผ๋กœ ์ถœ๋ ฅ์€ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“ค์–ด์„œ ๋ถ™์ด๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ด์•ผ ๊ฒ ๋‹ค๊ณ  ๋‹ค์งํ–ˆ๋‹ค.

sorted_lst.map((ele) => console.log(ele));

console.log(sortes_list.join("\n")(;

 


 

๐Ÿ“Œ11650 ์ขŒํ‘œ์ •๋ ฌ

www.acmicpc.net/problem/11650

 

11650๋ฒˆ: ์ขŒํ‘œ ์ •๋ ฌํ•˜๊ธฐ

์ฒซ์งธ ์ค„์— ์ ์˜ ๊ฐœ์ˆ˜ N (1 ≤ N ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” i๋ฒˆ์ ์˜ ์œ„์น˜ xi์™€ yi๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (-100,000 ≤ xi, yi ≤ 100,000) ์ขŒํ‘œ๋Š” ํ•ญ์ƒ ์ •์ˆ˜์ด๊ณ , ์œ„์น˜๊ฐ€ ๊ฐ™์€ ๋‘ ์ ์€ ์—†๋‹ค.

www.acmicpc.net

 

๋‚ด ํ’€์ด (์‹œ๊ฐ„์ดˆ๊ณผ)

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

input.shift();
let point_lst = [];
input.map((ele, i) => {
  point_lst.push(ele.split(" "));
});

point_lst = point_lst.sort((a, b) => a[0] - b[0] || a[1] - b[1]);
point_lst.map((ele) => {
  console.log(ele[0], ele[1]);
});

map์„ ๋‘๋ฒˆ์ด๋‚˜ ๋Œ๋ ธ๋”๋‹ˆ ์—ญ์‹œ๋‚˜ ์‹œ๊ฐ„์ดˆ๊ณผ์— ๊ฑธ๋ ธ๋‹ค.

์—ฌ๊ธฐ์„œ ์‹œ๊ฐ„์„ ์ค„์ด๋ ค๋ฉด 

1) ์ฒ˜์Œ๋ถ€ํ„ฐ input์„ ์ด์ค‘๋ฐฐ์—ด๋กœ ๋ฐ›๋˜๊ฐ€

2) ์ถœ๋ ฅํ•  ๋•Œ join ํ•  ๋ฐฉ๋ฒ•์„ ์ฐพ๋˜๊ฐ€ 

 

 

 

 

๋‹ค๋ฅธ ํ’€์ด

const fs = require("fs");
let input = (fs.readFileSync("/dev/stdin") + "")
  .trim()
  .split("\n")
  .map((e) => e.split(" ").map(Number));
input.shift();

console.log(
  input
    .sort((a, b) => a[0] - b[0] || a[1] - b[1])
    .join("\n")
    .replace(/\,/g, " ")
);

join์œผ๋กœ ์ถœ๋ ฅํ•˜๋Š”  ๋ฐฉ๋ฒ•์„ ์ฐพ์•˜๋‹ค..

๋ฐ”๋กœ ์ •๊ทœํ‘œํ˜„์‹!@!!! 

 

/\,/g  ๋Š” ์ „์ฒด ๋ฌธ์ž์—์„œ ,๋ฅผ ์ฐพ์•„๋ผ ๋ผ๋Š”๋œป 

 

 

 

 

 

๐Ÿ’™์œ ์šฉํ•œ ์ •๊ทœํ‘œํ˜„์‹์€ ์™ธ์›Œ๋†”์•ผ๊ฒ ๋‹ค.

 

๋Œ“๊ธ€