TIL

23/10/18 TIL __ ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค lv1. ์ถ”์–ต์ ์ˆ˜

GABOJOK 2023. 10. 18. 17:49

 

 

๐ŸšŒ  ๋ฌธ์ œ

 

 

 

 

 

1๏ธโƒฃ   ์ฒซ๋ฒˆ์งธ ํ’€์ด ๊ณผ์ •

 

function solution(name, yearning, photo) {    
	let mapPhoto = photo.map((p)=>{
		let answer = [];
		p.map((e)=>{
			name.map((n)=>{
				return e ===n ? answer.push(e) : e
			})
		});
		return answer
	})
    
	let val = {};
	for(let i=0; i< name.length; i++){
		val[name[i]] = yearning[i]
	}
    
	let ansans = mapPhoto.map((a)=>{
		let newA = a.reduce((acc,acu)=>{
			return acc + val[acu]
		}, 0)
		return newA
	})
	return ansans;
    
}

let n = ["may", "kein", "kain", "radi"];
let y = [5, 10, 1, 3];
let p = [["may", "kein", "kain", "radi"],["may", "kein", "brin", "deny"], ["kon", "kain", "may", "coni"]];

solution(n, y, p)
//๊ฒฐ๊ณผ๊ฐ’ [19, 15, 6]

 

 

  1. name์˜ ์š”์†Œ์™€ ๊ฐ™์€์ง€ ๋น„๊ตํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—,  photo ์— ์žˆ๋Š” ์ž‘์€ ๋ฐฐ์—ด๋“ค์„ map์„ ์ด์šฉํ•ด ํ•˜๋‚˜ํ•˜๋‚˜ ๊บผ๋‚ด์ฃผ์—ˆ๋‹ค. 
  2. ์ž‘์€ ๋ฐฐ์—ด๋“ค์˜ ๊ฐ ์š”์†Œ๋“ค๋„ map์„ ์ด์šฉํ•ด์„œ ๊บผ๋‚ด์ฃผ์—ˆ๋‹ค. 
  3. ์ž‘์€ ๋ฐฐ์—ด์˜ ์š”์†Œ์š”์†Œ๊ฐ€, name์˜ ๊ฐ ์š”์†Œ๋“ค๊ณผ ๋™์ผํ•œ์ง€ ์•Œ๊ธฐ ์œ„ํ•ด map ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ ค์ฃผ์—ˆ๋‹ค.
  4. name์˜ ์š”์†Œ์™€, ์ž‘์€ ๋ฐฐ์—ด์˜ ์š”์†Œ๊ฐ€ ๋™์ผํ•œ ์• ๋“ค๋งŒ ๋‹ด๊ธฐ ์œ„ํ•ด์„œ ๋นˆ ๋ฐฐ์—ด answer๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ๋‹ค. 
  5. map ๋ฐ˜๋ณต๋ฌธ ์•ˆ์—์„œ๋Š” ์ž‘์€ ๋ฐฐ์—ด์˜ ์š”์†Œ์™€, name์˜ ์š”์†Œ๊ฐ€ ๋™์ผํ•œ ๊ฒฝ์šฐ, answer์— ํ•ด๋‹น ์š”์†Œ๋ฅผ ๋„ฃ์–ด์ฃผ์—ˆ๊ณ  ๋ฆฌํ„ดํ•ด์„œ mapPhoto์— ๋‹ด์•„์ฃผ์—ˆ๋‹ค.. 
  6. ์ด๋ฆ„์— ํ•ด๋‹นํ•˜๋Š” ์ˆซ์ž๋ฅผ ํ‚ค ๋ฒจ๋ฅ˜ ํ˜•ํƒœ๋กœ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด ๋นˆ obj๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ๋‹ค. 
  7.  name์˜ ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณตํ•˜๋ฉฐ, ์ด๋ฆ„์˜ i๋ฒˆ์งธ๋ฅผ ํ‚ค๋กœ, yearning์˜ i๋ฒˆ์งธ๋ฅผ  ๊ฐ’์œผ๋กœ ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค. 
  8.  ์ƒˆ๋กญ๊ฒŒ ์ถ”๋ฆฐ ๋ฐฐ์—ด mapPhoto์˜ ์š”์†Œ ํ•˜๋‚˜ํ•˜๋‚˜๋ฅผ ๋ฐ˜๋ณตํ•˜๋ฉฐ ์ƒˆ๋กญ๊ฒŒ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด ์ค„๊บผ๋ผ map์„ ์‚ฌ์šฉํ–ˆ๋‹ค. 
  9. ์š”์†Œ ํ•˜๋‚˜ํ•˜๋‚˜๋ฅผ reduce ๋ฐ˜๋ณต๋ฌธ์„ ํ™œ์šฉํ•ด ํ•ด๋‹นํ•˜๋Š” ์ˆซ์ž๋“ค์„ ๋ชจ๋‘ ๋”ํ•ด์ฃผ์—ˆ๋‹ค. 

 

 

 

๐Ÿšจ ๋ฌธ์ œ์ 

 

๐Ÿ‘‰ map์„ ์•„๋ฌด ์ƒ๊ฐ์—†์ด ๋ชจ๋“  ๊ณณ์— ์ป๋‹ค. 

map์ด๋ผ๋Š” ์• ๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ๋‚ด๋ณด๋‚ด์ฃผ๋Š” ์• ์ธ๋ฐ, 

๋‹จ์ˆœ ๋ฐ˜๋ณต๋ฌธ๋งŒ์ด ํ•„์š”ํ•œ ๊ณณ์—์„œ ์กฐ์ฐจ map์„ ์‚ฌ์šฉํ–ˆ๋‹ค. 

์ฒ˜์Œ์— answer ์ด๋ผ๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“ค์ง€ ์•Š๊ณ , ๋‹จ์ˆœํžˆ ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋กœ ๋ฆฌํ„ดํ–ˆ๋Š”๋ฐ, ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์™”๋‹ค. 

์•Œ๊ณ ๋ณด๋‹ˆ 3์ค‘๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ  ์žˆ์—ˆ๋˜๊ฒƒ....ใ…Ž

answer ์ด๋ผ๋Š” ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ , ๊ทธ๊ณณ์— ๋„ฃ์–ด์ฃผ๋ฉด์„œ, return p.map() ์ด ์•„๋‹Œ return answer์ด ๋˜์ž ๊ฐ’์ด ์ž˜ ๋‚˜์™”๋‹ค. 

p.map() ๋ถ€ํ„ฐ return answer ๊นŒ์ง€๋Š” ๊ตณ์ด map์„ ์‚ฌ์šฉํ•  ํ•„์š”๋„ ์—†์—ˆ๊ณ , ์‚ฌ์‹ค ๋” ์ข‹์€ ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ๋‹ค. 

 

 

 

 

 

 

2๏ธโƒฃ ๋‘๋ฒˆ์งธ ํ’€์ด ๊ณผ์ •

 

function solution(name, yearning, photo) {    
	let obj = {};
	name.forEach((e,i)=> obj[e] = yearning[i])
    
	let answer = photo.map((p)=>{
		let num = p.map((sp)=>{
			return obj[sp] ? obj[sp] : 0
		})
		let redu = num.reduce((acc, acu)=>{
			return acc+ acu; 
		},0);

	})
	return answer;
}

let n = ["may", "kein", "kain", "radi"];
let y = [5, 10, 1, 3];
let p = [["may", "kein", "kain", "radi"],["may", "kein", "brin", "deny"], ["kon", "kain", "may", "coni"]];

solution(n, y, p)
//๊ฒฐ๊ณผ๊ฐ’ [19, 15, 6]

 

๋‹ค๋ฅธ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ๋ณด๋‹ค๊ฐ€ ๋ฌด๋ฆŽ์„ ํƒ ์น˜๊ณ  ๋”ฐ๋ผ ํ’€์–ด๋ดค๋‹ค.

๋ฐ˜๋ณต๋ฌธ์„ ์ด 6๋ฒˆ์ด๋‚˜ ์‚ฌ์šฉํ•œ ๋‚˜์™€๋Š” ๋‹ค๋ฅด๊ฒŒ,,

4๋ฒˆ์œผ๋กœ ๋๋‚ฌ๋‹ค...?

์ด๋ง์€ ๋‚ด ์ฝ”๋“œ์—  ์˜๋ฏธ์—†๋Š” ๋ฐ˜๋ณต๋ฌธ์ด ๋งŽ์•˜๋‹ค๋Š” ๋ง์ด๋‹ค. ๐Ÿ˜ฟ๐Ÿ˜ฟ

 

 

์ฝ”๋“œ๋ฅผ ๋œฏ์–ด ๋ณด๋ฉด, 

 

  1. name์— ํ•ด๋‹นํ•˜๋Š” ์ˆซ์ž๋ฅผ ์ง์ง€์–ด ์ฃผ๊ธฐ ์œ„ํ•ด obj ๋ฅผ ๋งŒ๋“ค์–ด ์ค€๋‹ค. 
  2. name์„ forEach ํ•˜๋ฉด์„œ obj์—  ๊ฐ name๊ณผ ์ˆซ์ž๋ฅผ ํ‚ค ๋ฒจ๋ฅ˜๋กœ ๋„ฃ์–ด์ค€๋‹ค. 
  3. photo๋ผ๋Š” ํฐ ๋ฐฐ์—ด์„ map ํ•˜๋ฉด์„œ ๋ณ€์ˆ˜ answer์— ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ๋‚ด๋ณด๋‚ผ ์ค€๋น„๋ฅผ ํ•ด์ฃผ๊ณ 
  4. ๊ทธ ์•ˆ์— ์š”์†Œ์ธ p๋„ map ํ•˜๋ฉด์„œ ๋ณ€์ˆ˜ num์— ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ๋‚ด๋ณด๋‚ผ ์ค€๋น„๋ฅผ ํ•œ๋‹ค. . 
  5. photo์˜ ์ž‘์€ ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ๋“ค์ด obj ์— ํ‚ค๋กœ ์กด์žฌํ•œ๋‹ค๋ฉด,  obj์˜ ํ‚ค์— ๋Œ€ํ•œ ๊ฐ’. ์ฆ‰ name์— ํ•ด๋‹นํ•˜๋Š” ์ˆซ์ž๋ฅผ ๋ฆฌํ„ด
  6. ๊ทธ๊ฒŒ ์•„๋‹Œ ๊ฒฝ์šฐ ๋ฐฐ์—ด์˜ ์š”์†Œ๋กœ 0์„ ๋ฆฌํ„ดํ•œ๋‹ค. 
  7. num ์˜ ๊ฒฝ์šฐ [5, 10, 0, 0] ์ด๋Ÿฐ์‹์œผ๋กœ ๋‚˜์˜จ๋‹ค. 
  8. ์ƒˆ๋กญ๊ฒŒ ์ •๋ฆฌ๋œ ์ž‘์€ ๋ฐฐ์—ด๋“ค num์„  ๊ฐ€์ง€๊ณ  reduce๋ฅผ ์ด์šฉํ•ด ๊ฐ’์„ ๋ˆ„์‚ฐํ•˜๋ฉฐ ๋”ํ•ด์ค€๋‹ค. 
  9. ์ด๋ ‡๊ฒŒ ์ •๋ฆฌ๋œ redu ๋ฐฐ์—ด๋“ค์„ answer์— ๋‹ด์•„์„œ ๋ฆฌํ„ดํ•ด ์ค€๋‹ค. 

 

 

 

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

์‰ฝ์ง€ ์•Š๊ฒ ์ง€๋งŒ ํ•ญ์ƒ ๊ณ ๋ฏผํ•˜๋ฉด์„œ ์‚ฌ์šฉํ•ด์•ผ ๊ฒ ๋‹ค..๐Ÿ˜ƒ