Данный отчёт сгенерирован 10.02.2023 18:21:31 UTC.
HTML версия (этот сайт) сгенерирован 10.02.2023 18:21:38 UTC.
Коммит: [bfeb65b9] add automatic zip creation 10.02.2023 18:21:31
HTML версия (этот сайт) сгенерирован 10.02.2023 18:21:38 UTC.
Коммит: [bfeb65b9] add automatic zip creation 10.02.2023 18:21:31
Задача: Ключ у квадратному масиві
10.02.2023 18:21:31
Описание: Є двовимірний масив розміром n x n. Значення в кожному рядку та в кожному стовпці
упорядковані за зростанням. Визначити, чи є у масиві заданий ключ k.
Для цього оголосіть функцію search(a, k), яка повертає true, якщо ключ є false, якщо ключа немає.
Спробуйте зробити це алгоритмом із часовою складністю O(n).
10.02.2023 18:21:31
Решений: 24
10.02.2023 18:21:31
Ключ у квадратному масиві
10.02.2023 18:21:31
function search (a, k) {
let counter = 0;
for (let i = 0; i < a.length; i++) {
for (let j = 0; j < a[i].length; j++) {
let element = a[i][j];
if (element === k) {
counter++;;
}
}
}
if (counter >= 1) {
return true;
}
else {
return false;
}
}
function search(a, k)
{
for(i=0; i< a.length; i++)
{
if(a[i].includes(k))
{
return true
}
}
return false
}
function search(a, k) {
for(let s of a) {
let l = 0, r = s.length;
if(s[l] > k && s[r - 1] < k)
continue;
while(r - l > 1) {
let m = Math.floor((l + r)/2);
if(s[m] > k) r = m;
if(s[m] < k) l = m;
if(s[m] == k) return true;
}
if(s[l] == k)
return true;
}
return false;
}
function search(a, k){
function binarySearch (key, arr) {
let end = arr.length - 1;
let start = 0;
while(start <= end){
let mid = Math.floor((end - start) / 2 + start)
if(key == arr[mid])
return mid;
if(key > arr[mid])
start = ++mid;
else
end = --mid;
}
return -1;
}
let res;
for(let i = 0; i < a.length; i++){
if(a[i][0] <= k && a[i][a.length - 1] >= k){
if( res = binarySearch(k, a[i]) !== -1)
return true;
}
}
return false;
}
function search(a, k){
let n=a.length
if(k>a[n-1][n-1]||k< a[0][0]){
return false
}
for(let i=0;i< n;i++){
if(k<=a[n-1][i]&&k>=a[0][i]){
for(let j=0;j< n;j++){
if(k===a[j][i]){
return true
}
}
}
}
return false
}
function search(a, k) {
for(i=0;i< a.length;i++){
for(j=0;j< a[i].length;j++){
if(a[i][j]==k)
return true
}
}
return false;
}
function search(a, k){
let arr2 = [];
for(let i = 0; i < a.length; i++){
arr2 = arr2.concat(a[i]);
}
return arr2.includes(k);
}
function search(a, k,b=false){
for(x = 0 ; x< a.length ; x++){
for(y = 0 ;y< a[x].length;y++){
if(k==a[x][y]){
b=true
}
}
}
return b
}
function search(a, k)
{
for (let index = 0; index < a.length; index++) {
for (let i = 0; i < a.length; i++) {
if (a[index][i]==k) {
return true;
}
}
}
return false;
}
function search(a, k){
for(i=0;i< a.length;i++){
for(j=0;j< a[i].length;j++){
if(a[i][j]==k)
return true
}
}
return false
}
function search(a, k){
for(let i = 0; i < a.length; i++){
if(a[i].includes(k)){
return true;
}
}
return false;
}
function search(a, k){
for (let arr of a) if (arr.includes(k)) return true;
return false;
}
function search(a, k){
for(let i = 0; i < a.length; i++){
if(a[i].includes(k)){
return true;
}
}
return false;
}
function search(a, k) {
let row = 0;
let col = a.length - 1;
while (row < a.length && col >= 0) {
if (a[row][col] === k) {
return true;
} else if (a[row][col] < k) {
row++;
} else {
col--;
}
}
return false;
}
function search(a, k) {
a = a.flat();
if (a.indexOf(k) === -1) return false;
else return true;
}
function search(a, k){
for(let i=0; i for(let j=0; j if(a[i][j] == k){
return true;
}
}
}
return false;
}
function search(a, k){
for(let i of a){
let start = 0;
let end = i.length - 1;
function g(start, end) {
if (start > end) return false;
let mid = Math.floor((start + end) / 2);
if (i[mid] === k) return true;
if (i[mid] > k) return g(start, mid - 1);
else return g(mid + 1, end);
}
if(g(start, end)) return true;
}
return false;
}
function search(a, k){
let emptyArr =[]
a.forEach(item=>{
emptyArr = emptyArr.concat([...item])
})
return emptyArr.includes(k)
}
function search(arr, target){
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
if (arr[i][j] == target) {
return true;
}
}
}
return false;
}
function search(a, k){
for(let i = 0; i < a.length; i++){
let el = a[i];
if(el.indexOf(k) != -1){
return true
}
}
return false
}
function search(a, k) {
let i = 0, j = a.length - 1;
while (i < a.length && j >= 0) {
if (a[i][j] == k) {
return true;
}
if (a[i][j] > k)
j--;
else
i++;
}
return false;
}
function search(a, k) {
let row = 0;
let column = 0;
while (row < a.length && column < a.length) {
const cur = a[row][column];
if (k == cur) return true;
if (k < cur) {
row++;
column = 0;
continue;
}
if (column < a.length - 1) column++;
else {
column = 0;
row++;
}
}
return false;
}
function search(arr, key){
let row = 0
let column = arr.length - 1
while (row < arr.length && column >= 0){
if (arr[row][column] === key) return true
if (arr[row][column] > key) column--
else row++
}
return false
}
function search(a, k)
{
for (let index = 0; index < a.length; index++) {
for (let i = 0; i < a.length; i++) {
if (a[index][i]==k) {
return true;
}
}
}
return false;
}
let counter = 0;
for (let i = 0; i < a.length; i++) {
for (let j = 0; j < a[i].length; j++) {
let element = a[i][j];
if (element === k) {
counter++;;
}
}
}
if (counter >= 1) {
return true;
}
else {
return false;
}
}
{
for(i=0; i< a.length; i++)
{
if(a[i].includes(k))
{
return true
}
}
return false
}
for(let s of a) {
let l = 0, r = s.length;
if(s[l] > k && s[r - 1] < k)
continue;
while(r - l > 1) {
let m = Math.floor((l + r)/2);
if(s[m] > k) r = m;
if(s[m] < k) l = m;
if(s[m] == k) return true;
}
if(s[l] == k)
return true;
}
return false;
}
function binarySearch (key, arr) {
let end = arr.length - 1;
let start = 0;
while(start <= end){
let mid = Math.floor((end - start) / 2 + start)
if(key == arr[mid])
return mid;
if(key > arr[mid])
start = ++mid;
else
end = --mid;
}
return -1;
}
let res;
for(let i = 0; i < a.length; i++){
if(a[i][0] <= k && a[i][a.length - 1] >= k){
if( res = binarySearch(k, a[i]) !== -1)
return true;
}
}
return false;
}
let n=a.length
if(k>a[n-1][n-1]||k< a[0][0]){
return false
}
for(let i=0;i< n;i++){
if(k<=a[n-1][i]&&k>=a[0][i]){
for(let j=0;j< n;j++){
if(k===a[j][i]){
return true
}
}
}
}
return false
}
for(i=0;i< a.length;i++){
for(j=0;j< a[i].length;j++){
if(a[i][j]==k)
return true
}
}
return false;
}
let arr2 = [];
for(let i = 0; i < a.length; i++){
arr2 = arr2.concat(a[i]);
}
return arr2.includes(k);
}
for(x = 0 ; x< a.length ; x++){
for(y = 0 ;y< a[x].length;y++){
if(k==a[x][y]){
b=true
}
}
}
return b
}
{
for (let index = 0; index < a.length; index++) {
for (let i = 0; i < a.length; i++) {
if (a[index][i]==k) {
return true;
}
}
}
return false;
}
for(i=0;i< a.length;i++){
for(j=0;j< a[i].length;j++){
if(a[i][j]==k)
return true
}
}
return false
}
for(let i = 0; i < a.length; i++){
if(a[i].includes(k)){
return true;
}
}
return false;
}
for (let arr of a) if (arr.includes(k)) return true;
return false;
}
for(let i = 0; i < a.length; i++){
if(a[i].includes(k)){
return true;
}
}
return false;
}
let row = 0;
let col = a.length - 1;
while (row < a.length && col >= 0) {
if (a[row][col] === k) {
return true;
} else if (a[row][col] < k) {
row++;
} else {
col--;
}
}
return false;
}
a = a.flat();
if (a.indexOf(k) === -1) return false;
else return true;
}
for(let i=0; i
return true;
}
}
}
return false;
}
for(let i of a){
let start = 0;
let end = i.length - 1;
function g(start, end) {
if (start > end) return false;
let mid = Math.floor((start + end) / 2);
if (i[mid] === k) return true;
if (i[mid] > k) return g(start, mid - 1);
else return g(mid + 1, end);
}
if(g(start, end)) return true;
}
return false;
}
let emptyArr =[]
a.forEach(item=>{
emptyArr = emptyArr.concat([...item])
})
return emptyArr.includes(k)
}
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
if (arr[i][j] == target) {
return true;
}
}
}
return false;
}
for(let i = 0; i < a.length; i++){
let el = a[i];
if(el.indexOf(k) != -1){
return true
}
}
return false
}
let i = 0, j = a.length - 1;
while (i < a.length && j >= 0) {
if (a[i][j] == k) {
return true;
}
if (a[i][j] > k)
j--;
else
i++;
}
return false;
}
let row = 0;
let column = 0;
while (row < a.length && column < a.length) {
const cur = a[row][column];
if (k == cur) return true;
if (k < cur) {
row++;
column = 0;
continue;
}
if (column < a.length - 1) column++;
else {
column = 0;
row++;
}
}
return false;
}
let row = 0
let column = arr.length - 1
while (row < arr.length && column >= 0){
if (arr[row][column] === key) return true
if (arr[row][column] > key) column--
else row++
}
return false
}
{
for (let index = 0; index < a.length; index++) {
for (let i = 0; i < a.length; i++) {
if (a[index][i]==k) {
return true;
}
}
}
return false;
}