Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
water drop/ water land
// Water Drop/ Water Land #include <vector> #include <iostream> using namespace std; class Solution { public: vector<int> pour(vector<int> land, int pos, int k) { vector<int> water = land; while(k--){ int best = pos; // try left for(int i = pos-1; i>=0; i--){ if(water[i] > water[best]) break; if(water[i] < water[best]) best = i; } if(best != pos){ water[best]++; continue; // if left part finished, continue to next drop } // try right for(int i = pos+1; i<water.size(); i++) { if(water[i] > water[best]) break; if(water[i] < water[best]) best = i; } water[best]++; } return water; } void print(vector<int>& land, vector<int>& water) { if(land.size()!= water.size()) return; int maxi = 0; for(int x: water) maxi = max(maxi, x); for(int h = maxi; h >=0; h--) { string line = ""; for(int i=0; i<land.size(); i++){ if(land[i]>= h) line.push_back('O'); else if(water[i]>=h) line.push_back('w'); else line.push_back(' '); } cout << line << endl; } } }; int main() { vector<int> land = {2, 1, 2, 4}; int pos = 0; int k = 4; Solution s; vector<int> water = s.pour(land, pos, k); s.print(land, land); cout << endl; s.print(land, water); return 0; }
run
|
edit
|
history
|
help
0
matrix calculator
ADAKOHL - hpclearn
constructing object on first use as return value of (pointer to) object-returning function
pac update
adjacent_difference-30-Seconds-Of-CPP
bitmap with pairs
pi with cmath
pointer to template function
dia
remove_copy_if-30-Seconds-of-C++