[leetCode/Javascript] 2418. Sort the People

2024. 2. 11. 17:50ยท ๊ฐœ๋ฐœ/๐Ÿ’ฌ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ
๋ชฉ์ฐจ
  1. ๋ฌธ์ œ
  2. ์ ‘๊ทผ๋ฐฉ๋ฒ•
  3. ์ฝ”๋“œ
  4. ๋‹ค๋ฅธ ์†”๋ฃจ์…˜
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

You are given an array of strings names, and an array heights that consists of distinct positive integers. Both arrays are of length n.

For each index i, names[i] and heights[i] denote the name and height of the ith person.

Return names sorted in descending order by the people's heights.

 

Example 1:

Input: names = ["Mary","John","Emma"], heights = [180,165,170]
Output: ["Mary","Emma","John"]
Explanation: Mary is the tallest, followed by Emma and John.

Example 2:

Input: names = ["Alice","Bob","Bob"], heights = [155,185,150]
Output: ["Bob","Alice","Bob"]
Explanation: The first Bob is the tallest, followed by Alice and the second Bob.

 

Constraints:

  • n == names.length == heights.length
  • 1 <= n <= 103
  • 1 <= names[i].length <= 20
  • 1 <= heights[i] <= 105
  • names[i] consists of lower and upper case English letters.
  • All the values of heights are distinct.

 

 

 

์ ‘๊ทผ๋ฐฉ๋ฒ•

์ด๋ฆ„ ๋ฐฐ์—ด๊ณผ ํ‚ค๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด์ด ์ฃผ์–ด์ง€๊ณ , ๊ฐ ์ธ๋ฑ์Šค ์ž๋ฆฌ์— ์š”์†Œ๋“ค์ด ํŽ˜์–ด์ด๋‹ค. 

ํ‚ค๋ฅผ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ์ด๋ฆ„์„ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋ผ๋Š”๊ฒŒ ๋ฌธ์ œ์˜ ์š”์ง€์ด๋‹ค. 

 

๊ฐ๊ฐ์˜ ๋ฐฐ์—ด์„ ๊ฐ์ฒด๋กœ ํ•ฉ์ณ์„œ ๊ทธ ๊ฐ์ฒด์— ํ‚ค value ๊ฐ’์„ sortํ•ด์„œ key๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋˜๊ฒ ๋‹ค๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค. 

object๋กœ ๋งŒ๋“ค๊ณ  sortํ•˜๊ธฐ ์œ„ํ•ด ๊ทธ๊ฑธ 2์ค‘ ๋ฐฐ์—ด๋กœ ๋‹ค์‹œ ๋งŒ๋“ค์–ด์•ผํ•จ. ...... ๋ณต์žกํ•œ๋ฐ..?... 

์ฒซ ์‹œ๋„๋Š” ์‹คํŒจ๋ฅผ ํ•˜๋Š”๋ฐ, key๊ฐ’์„ name์œผ๋กœ ํ•˜๊ณ  value ์ž๋ฆฌ์— height๋ฅผ ๋„ฃ์—ˆ๋Š”๋ฐ, ๋™๋ช…์ธ์ด ์žˆ์œผ๋ฉด ์ œ๋Œ€๋กœ ์ž๋ฃŒ๊ฐ€ ์•ˆ๋“ค์–ด๊ฐ€๋Š” ๊ฒƒ์ด๋‹ค!!

๊ทธ๋ž˜์„œ heights๋ฅผ key๋กœ ํ•ด์„œ ์ด๋ฆ„์„ value๋ฅผ ๋„ฃ๊ณ  ๋‹ค์‹œ sort๋ฅผ ํ–ˆ๋‹ค. 

 

 

 

 

์ฝ”๋“œ

var sortPeople = function(names, heights) {
    let ob = {}
    let result = []
    
    heights.map((h, i) => {
        ob[h] = names[i]
    })
    
   var sortable = [];
   for (let name in ob) {
      sortable.push([name, ob[name]]);
    }

  sortable
      .sort((a, b) => b[0] - a[0])
      .map((name) => {
          result.push(name[1])
  })
    
    return result

};

 

 

 

๋‹ค๋ฅธ ์†”๋ฃจ์…˜

let output = []
    
// let's concatenate the arrays to sort later
for (let i in heights) {
    output.push({name: names[i], height: heights[i]})
   }
    
 return output
             .sort((a,b) => b.height - a.height) // sort
             .map(i => i.name) // cut needed
728x90

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

[leetCode/Javascript] 2363. Merge Similar Items  (0) 2024.02.27
[leetCode/Javascript] 1572. Matrix Diagonal Sum  (0) 2024.02.13
[LeetCode/Javascript] 216. Combination Sum III  (0) 2024.02.05
[leetCode/javascript] 1071. Greatest Common Divisor of Strings  (0) 2024.02.02
[leetCode] 151. Reverse Words in a String - Javascript  (0) 2024.02.02
  1. ๋ฌธ์ œ
  2. ์ ‘๊ทผ๋ฐฉ๋ฒ•
  3. ์ฝ”๋“œ
  4. ๋‹ค๋ฅธ ์†”๋ฃจ์…˜
'๊ฐœ๋ฐœ/๐Ÿ’ฌ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [leetCode/Javascript] 2363. Merge Similar Items
  • [leetCode/Javascript] 1572. Matrix Diagonal Sum
  • [LeetCode/Javascript] 216. Combination Sum III
  • [leetCode/javascript] 1071. Greatest Common Divisor of Strings
๋ฐ(Ming) ๐Ÿˆโ€โฌ›
๋ฐ(Ming) ๐Ÿˆโ€โฌ›
๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป Frontend Developer
๋ฐ(Ming) ๐Ÿˆโ€โฌ›
Ming devlog
๋ฐ(Ming) ๐Ÿˆโ€โฌ›
์ „์ฒด
์˜ค๋Š˜
์–ด์ œ

๊ณต์ง€์‚ฌํ•ญ

  • About Ming ๐Ÿฐ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (261)
    • ๊ฐœ๋ฐœ (255)
      • ๐Ÿ“ TIL (58)
      • โŒ ์˜ค๋ฅ˜ ๋…ธํŠธ (23)
      • React (23)
      • Next.js (11)
      • React-Native (6)
      • JavaScript (10)
      • Flutter (19)
      • Database (1)
      • ๐Ÿ’ฌ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ (21)
      • ๐Ÿ“š English (9)
      • Etc... (25)
      • โ›ต ํ•ญํ•ด99 (49)
    • ๐Ÿ’ป Dev (6)
      • ๐Ÿ“ TIL (4)
      • ๐Ÿ’ฌ Algorithm (1)
      • Etc... (1)

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ
  • ์•Œ๊ณ ๋ฆฌ์ฆ˜
  • twid
  • ๊ฐœ๋ฐœ์ž์ผ๊ธฐ
  • ํ•ญํ•ด99
  • flutter
  • todaywhatidid
  • Firebase
  • ์ด์ฝ”๋…ธ๋ฏธ์ŠคํŠธ
  • ์˜์–ด๊ณต๋ถ€
  • ๊ฐœ๋ฐœ์žํšŒ๊ณ 
  • ์ฝ”ํ…Œ
  • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ
  • TIL
  • ๋ฆฌ์•กํŠธ๋„ค์ดํ‹ฐ๋ธŒ
  • ReactNative
  • nextjs
  • ์ฝ”ํ…Œ๊ณต๋ถ€
  • leetcode
  • ์ฝ”๋”ฉ๊ณต๋ถ€
  • ๋ถ€ํŠธ์บ ํ”„
  • Git
  • react-native
  • react
  • ์ฝ”๋”ฉํ…Œ์ŠคํŠธ
  • ์ŠคํŒŒ๋ฅดํƒ€์ฝ”๋”ฉํด๋Ÿฝ
  • ํ”Œ๋Ÿฌํ„ฐ
  • ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
  • github
  • ๋ฆฌ์•กํŠธ

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

250x250
hELLO ยท Designed By ์ •์ƒ์šฐ.v4.2.1
๋ฐ(Ming) ๐Ÿˆโ€โฌ›
[leetCode/Javascript] 2418. Sort the People
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”

๊ฐœ์ธ์ •๋ณด

  • ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ
  • ํฌ๋Ÿผ
  • ๋กœ๊ทธ์ธ

๋‹จ์ถ•ํ‚ค

๋‚ด ๋ธ”๋กœ๊ทธ

๋‚ด ๋ธ”๋กœ๊ทธ - ๊ด€๋ฆฌ์ž ํ™ˆ ์ „ํ™˜
Q
Q
์ƒˆ ๊ธ€ ์“ฐ๊ธฐ
W
W

๋ธ”๋กœ๊ทธ ๊ฒŒ์‹œ๊ธ€

๊ธ€ ์ˆ˜์ • (๊ถŒํ•œ ์žˆ๋Š” ๊ฒฝ์šฐ)
E
E
๋Œ“๊ธ€ ์˜์—ญ์œผ๋กœ ์ด๋™
C
C

๋ชจ๋“  ์˜์—ญ

์ด ํŽ˜์ด์ง€์˜ URL ๋ณต์‚ฌ
S
S
๋งจ ์œ„๋กœ ์ด๋™
T
T
ํ‹ฐ์Šคํ† ๋ฆฌ ํ™ˆ ์ด๋™
H
H
๋‹จ์ถ•ํ‚ค ์•ˆ๋‚ด
Shift + /
โ‡ง + /

* ๋‹จ์ถ•ํ‚ค๋Š” ํ•œ๊ธ€/์˜๋ฌธ ๋Œ€์†Œ๋ฌธ์ž๋กœ ์ด์šฉ ๊ฐ€๋Šฅํ•˜๋ฉฐ, ํ‹ฐ์Šคํ† ๋ฆฌ ๊ธฐ๋ณธ ๋„๋ฉ”์ธ์—์„œ๋งŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.