From 56cf893da538656339c94ecd5101aabc18dc357c Mon Sep 17 00:00:00 2001 From: jho378 Date: Wed, 26 Oct 2022 13:26:39 +0900 Subject: [PATCH 1/2] binary-search --- binary-search/12015/jinho.js | 37 ++++++++++++++++++++++++++++++++++++ binary-search/1300/jinho.js | 30 +++++++++++++++++++++++++++++ binary-search/1920/jinho.js | 26 +++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 binary-search/12015/jinho.js create mode 100644 binary-search/1300/jinho.js create mode 100644 binary-search/1920/jinho.js diff --git a/binary-search/12015/jinho.js b/binary-search/12015/jinho.js new file mode 100644 index 0000000..863706e --- /dev/null +++ b/binary-search/12015/jinho.js @@ -0,0 +1,37 @@ +const fs = require('fs'); +const input = fs.readFileSync('a.txt').toString().trim().split('\n'); + +const N = parseInt(input[0]); +const data = input[1].split(' ').map(e => parseInt(e)); + +const answer = [data[0]]; + +const binarySearch = ( arr, elem ) => { + let left = 0; + let right = arr.length - 1; + let idx; + while( left <= right ){ + let mid = Math.floor((left + right) / 2); + if ( arr[mid] === elem ){ + return -1; + } + else if ( arr[mid] < elem ) { + left = mid + 1; + } + else if ( arr[mid] > elem ) { + right = mid - 1; + } + } + arr[left] = elem; +} + +for (let i=1; i answer[answer.length -1] ){ + answer.push(data[i]); + } + else if ( data[i] < answer[answer.length - 1] ){ + binarySearch(answer, data[i]); + } +} + +console.log(answer.length) \ No newline at end of file diff --git a/binary-search/1300/jinho.js b/binary-search/1300/jinho.js new file mode 100644 index 0000000..499ef35 --- /dev/null +++ b/binary-search/1300/jinho.js @@ -0,0 +1,30 @@ +const fs = require('fs'); +const input = fs.readFileSync('a.txt').toString().trim().split('\n'); + +const N = parseInt(input[0]); +const M = parseInt(input[1]); + +let left = 1; +let right = M; + +const countLessOrEqual = (x) => { + let cnt = 0; + for (let i=1; i count ){ + left = mid + 1; + } else { + right = mid; + } +} + +console.log(left); \ No newline at end of file diff --git a/binary-search/1920/jinho.js b/binary-search/1920/jinho.js new file mode 100644 index 0000000..e95a314 --- /dev/null +++ b/binary-search/1920/jinho.js @@ -0,0 +1,26 @@ +const fs = require('fs'); +const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n'); + +const M = parseInt(input[0]); +const N = parseInt(input[2]); + +const data = input[1].split(' '); +const comparison = input[3].split(' '); + +const obj = {}; +const answer = []; +for(let i=0; i Date: Wed, 26 Oct 2022 13:50:47 +0900 Subject: [PATCH 2/2] Binary search --- binary-search/19637/jinho.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 binary-search/19637/jinho.js diff --git a/binary-search/19637/jinho.js b/binary-search/19637/jinho.js new file mode 100644 index 0000000..b103807 --- /dev/null +++ b/binary-search/19637/jinho.js @@ -0,0 +1,33 @@ +const fs = require('fs'); +const input = fs.readFileSync('a.txt').toString().trim().split('\n'); + +const N = Number(input[0].split(' ')[0]); +const M = Number(input[0].split(' ')[1]); + +const ranks = []; +const scores = []; +for (let i=1; i { + let left = 0; + let right = N; + + while ( left < right ){ + let mid = Math.floor((left + right)/2); + if (x > scores[mid]){ + left = mid + 1; + } else { + right = mid ; + } + } + return ranks[left]; +} + +for (let i=N+1; i