๐Ÿ“• Algorithm/Solved

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ๋ชจ์˜๊ณ ์‚ฌ (Javascript)

ํ•œ์ฝ”๋”ฉ 2021. 1. 10. 20:41
728x90
728x90

๋ฌธ์ œ ์„ค๋ช…

์ˆ˜ํฌ์ž๋Š” ์ˆ˜ํ•™์„ ํฌ๊ธฐํ•œ ์‚ฌ๋žŒ์˜ ์ค€๋ง์ž…๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž ์‚ผ์ธ๋ฐฉ์€ ๋ชจ์˜๊ณ ์‚ฌ์— ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ์ „๋ถ€ ์ฐ์œผ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž๋Š” 1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฐ์Šต๋‹ˆ๋‹ค.

1๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ...
2๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ...
3๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ...

1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€์˜ ์ •๋‹ต์ด ์ˆœ์„œ๋Œ€๋กœ ๋“ค์€ ๋ฐฐ์—ด answers๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ€์žฅ ๋งŽ์€ ๋ฌธ์ œ๋ฅผ ๋งžํžŒ ์‚ฌ๋žŒ์ด ๋ˆ„๊ตฌ์ธ์ง€ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”.

 

์ œํ•œ ์กฐ๊ฑด

  • ์‹œํ—˜์€ ์ตœ๋Œ€ 10,000 ๋ฌธ์ œ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.
  • ๋ฌธ์ œ์˜ ์ •๋‹ต์€ 1, 2, 3, 4, 5์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.
  • ๊ฐ€์žฅ ๋†’์€ ์ ์ˆ˜๋ฅผ ๋ฐ›์€ ์‚ฌ๋žŒ์ด ์—ฌ๋Ÿฟ์ผ ๊ฒฝ์šฐ, returnํ•˜๋Š” ๊ฐ’์„ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ด์ฃผ์„ธ์š”.

์ž…์ถœ๋ ฅ ์˜ˆ

[1,2,3,4,5] [1]
[1,3,2,4,2] [1,2,3]

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

  • ์ˆ˜ํฌ์ž 1์€ ๋ชจ๋“  ๋ฌธ์ œ๋ฅผ ๋งžํ˜”์Šต๋‹ˆ๋‹ค.
  • ์ˆ˜ํฌ์ž 2๋Š” ๋ชจ๋“  ๋ฌธ์ œ๋ฅผ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค.
  • ์ˆ˜ํฌ์ž 3์€ ๋ชจ๋“  ๋ฌธ์ œ๋ฅผ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ๊ฐ€์žฅ ๋ฌธ์ œ๋ฅผ ๋งŽ์ด ๋งžํžŒ ์‚ฌ๋žŒ์€ ์ˆ˜ํฌ์ž 1์ž…๋‹ˆ๋‹ค.

 

์ž…์ถœ๋ ฅ ์˜ˆ #2

  • ๋ชจ๋“  ์‚ฌ๋žŒ์ด 2๋ฌธ์ œ์”ฉ์„ ๋งž์ท„์Šต๋‹ˆ๋‹ค.
function solution(answers) {
    var answer = [];
    
    // ์ˆ˜ํฌ์ž๋“ค์˜ ๋ฐ˜๋ณต๋˜๋Š” ๊ทœ์น™์˜ ์ˆ˜๋ฅผ ๋ฐฐ์—ด๋กœ ์ €์žฅ
    let p1 = [1,2,3,4,5];
    let p2 = [2,1,2,3,2,4,2,5];
    let p3 = [3,3,1,1,2,2,4,4,5,5];
    
    // filter๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ answers๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ์ˆ˜ํฌ์ž๋“ค์˜ ๋ฒˆํ˜ธ์™€ ๋น„๊ต(์™„์ „ํƒ์ƒ‰)
    let s1 = answers.filter( (a, i) => a === p1[i%5] ).length;
    let s2 = answers.filter( (a, i) => a === p2[i%8] ).length;
    let s3 = answers.filter( (a, i) => a === p3[i%10] ).length;
    
    // ์„ธ ์‚ฌ๋žŒ์˜ ์ ์ˆ˜ ์ค‘ ์ตœ๋Œ€๊ฐ’ ๊ตฌํ•˜๊ธฐ
    let max = Math.max(s1, s2, s3);
    
    // ์ตœ๋Œ€๊ฐ’๊ณผ ์ผ์น˜ํ•œ ์ ์ˆ˜๋ฅผ ๊ฐ€์ง€๋ฉด answer์— ์ €์žฅ
    if(s1 === max) answer.push(1);
    if(s2 === max) answer.push(2);
    if(s3 === max) answer.push(3);
    
    return answer;
}

 

Javascript filter (๋ฐฐ์—ด ์ฒ˜๋ฆฌ)

filter

- ์ฝœ๋ฐฑํ•จ์ˆ˜์— ์ง€์ •๋œ ์กฐ๊ฑด์— ๋งž๋Š” ์š”์†Œ๋ฅผ ์ƒˆ๋กญ๊ฒŒ ๋ฐ˜ํ™˜

- ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ ์ธ์ž (์ˆœ์„œ๋Œ€๋กœ) : ๊ฐ’, ์ธ๋ฑ์Šค, ์› ๋ฐฐ์—ด

- Array.prototype.filter

 

filter ์‚ฌ์šฉ ํŒจํ„ด 1

let data = [
    {name: '1', age: 15},
    {name: '2', age: 20},
    {name: '3', age: 25},
];

let result = data.filter( x => {
    return x.age >= 20;
});

console.log(result);

data์˜ ์š”์†Œ ์ค‘์— age๋ผ๋Š” value๊ฐ’์ด 20 ์ด์ƒ์ธ ์š”์†Œ๋งŒ result ๋ฐฐ์—ด์— ๋„ฃ๋Š” ์˜ˆ์ œ์ด๋‹ค.

result๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜์˜จ๋‹ค.

 

0: {name:'2', age:'20'}

1: {name:'3', age:'25'}

length: 2

 

์œ„์™€ ๊ฐ™์ด ๋ฐ˜ํ™˜๋˜๋ฉด์„œ length๋ฅผ ์ด์šฉํ•˜๋ฉด data ๋ฐฐ์—ด์— ์กฐ๊ฑด์— ๋งž๋Š” ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

filter ์‚ฌ์šฉ ํŒจํ„ด 2

let data = [
    {name: '1', age: 15},
    {name: '2', age: 20},
    {name: '3', age: 25},
];

let result = data.filter( (x, idx, array) => {
    return idx >= 2 && x.age >= 20;
});

console.log(result);

data์˜ ์š”์†Œ ์ค‘์— age๊ฐ€ 20์ด์ƒ์ด๋ฉด์„œ, data์˜ ์š”์†Œ ์ธ๋ฑ์Šค๊ฐ€ 2์ด์ƒ์ธ ์š”์†Œ๋งŒ result์— ๋„ฃ๋Š” ์˜ˆ์ œ์ด๋‹ค.

 

์•ž์„  ์˜ˆ์ œ์™€ ๋‹ฌ๋ฆฌ filter์˜ ์ธ์ž๊ฐ’์„ (๊ฐ’, ์ธ๋ฑ์Šค, ๋ฐฐ์—ด) ํ˜•ํƒœ๋กœ ํ•˜๋ฉด ๊ฐ’๊ณผ ์ธ๋ฑ์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ๋” ์ •๊ตํ•œ ํ•„ํ„ฐ๋ง์ด ๊ฐ€๋Šฅํ•˜๊ณ , array๋ฅผ ํ†ตํ•ด ์› ๋ฐฐ์—ด๊ณผ ๋น„๊ตํ•˜๋Š” ์ž‘์—… ๋˜ํ•œ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜จ๋‹ค.

 

0: {name:'3', age:'25'}

length: 1

 

 

์ฐธ๊ณ  : niceman.tistory.com/77

728x90
๋ฐ˜์‘ํ˜•