1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

指定要素の検索 (query)

1
Posted at

今回はpaizaの「指定要素の検索(query)」に挑戦!

簡単な問題だけど、基礎は大事だから一応解いていく!


ちなみに「クエリ」は日本語で “問い合わせ” を意味し、IT分野では主にデータベースに対して、データの検索や要求をする命令文を指すらしい。



問題概要

  • 長さ N の重複のない数列 A

  • Q 個の整数 K_1 … K_Q

各 K_i が A に含まれてるか調べて、あれば「YES」、なければ「NO」と出力せよ。


入力例:

5 5 // N Q
1
2
3
4
5
1
3
5
7
9

出力例:

YES
YES
YES
NO
NO






✅ OKコード例:

const rl = require('readline').createInterface({ input: process.stdin });
const lines = [];

rl.on('line', line => lines.push(line));

rl.on('close', () => {
  const [N, Q] = lines[0].split(' ').map(Number);
  const arrA = lines.slice(1, N+1).map(Number);
  const K = lines.slice(N+1).map(Number);


  K.forEach(k => {
    console.log(arrA.includes(k) ? 'YES' : 'NO');
  });
});




✅ OK例 ②:Set

  const [N, Q] = lines[0].split(' ').map(Number);
  const arrA = lines.slice(1, N+1).map(Number);
  const K = lines.slice(N+1).map(Number);

  const setA = new Set(arrA);

  K.forEach(k => {
    console.log(setA.has(k) ? 'YES' : 'NO');
  });






🗒️ メモ

  • .includes() → 配列にその要素があるか確認する便利メソッド

  • Set → 重複なしの集合データ構造。 .has() で一発検索できる




僕の失敗談(´;ω;`)と解決法🐈

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?