DEV Community

loading...

[BAEKJOON Online Judge + Node.js ] 1181: Word sort

Brandon Ha
Goona be a king of developer and investor. Follow me @iDevBrandon
・1 min read

This is my attempt. Although i thought i got it right, but got returned "Memory limit exceeded" :(

// This question is asking me to do 
// input words based on the length of each element 

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

let temp = [];
const num = Number(input[0]); // 13
let uniqueValues;


// make a 'temp' array 
for(let i=1; i<= num; i++) {
  temp.push(input[i]);
   uniqueValues = new Set(temp)
}

let sortedArr = Array.from(uniqueValues)



for(let i=0; i< sortedArr.length; i++) {
  sortedArr.sort((a,b) => a.length-b.length || a.localeCompare(b))
}

console.log(sortedArr.join('\n'))


// iterate through temp 
// for(let i=0; i<temp.length; i++) {

//   temp.sort(function(a,b) {
//      a.length - b.length
//   })

// }


Enter fullscreen mode Exit fullscreen mode

Discussion (0)