Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Find the row with max ones
//Title of this code #include <iostream> #include <set> using namespace std; const int N = 5; const int M = 5; int searchRow(int t[][M]) { set<int>* rows = new set<int>(); set<int>* tmpRows = new set<int>(); for (int i = 0; i < N; ++i) rows->insert(i); int min = 0; int max = M - 1; bool everFound = false; while (min <= max) { int mid = ((max - min) / 2) + min; for (auto it = rows->begin(); it != rows->end(); ++it) { if (t[*it][mid] == 1) { everFound = true; tmpRows->insert(*it); } } if (tmpRows->empty()) { if (min == max ) { if (everFound) return *(rows->begin()); else return -1; } min = mid + 1; swap(rows, tmpRows); } else if (tmpRows->size() > 1) { max = mid - 1; } else if (tmpRows->size() == 1) { return *(tmpRows->begin()); } rows->clear(); swap(rows, tmpRows); } return *(rows->begin()); } int main() { int t[N][M] = { {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 1}, {0, 0, 0, 1, 1}, {0, 0, 0, 0, 0} }; cout << searchRow(t); }
run
|
edit
|
history
|
help
0
Przesylka
Problem: binary
cynb
CV
Listas enlazadas - k-esimo elemento
Test 1(2021)
DBeach Resort 4R8J-8P (State of Rio Grande.do Norte Brazil)
LIS
code
subset sum=k(dp)