9.23 快手笔试
1、用两个栈来回倒腾、同时记录对应的次数 100%;
#include <bits/stdc++.h> using namespace std; class FreqStack{ public: unordered_map<int, int> cnts; stack<int> s; }; int main(){ FreqStack fs; int num; while(cin >> num){ if(num == 0){ if(fs.s.size() == 0){ cout << -1 << endl; continue; } stack<int> s_2; int max_cnts = 0; for(auto it: fs.cnts){ max_cnts = max(max_cnts, it.second); } while(!fs.s.empty()){ if(fs.cnts[fs.s.top()] == max_cnts){ cout << fs.s.top() << endl; fs.cnts[fs.s.top()] -= 1; fs.s.pop(); break; } s_2.push(fs.s.top()); fs.s.pop(); } while(!s_2.empty()){ fs.s.push(s_2.top()); s_2.pop(); } }else{ fs.s.push(num); fs.cnts[num] += 1; } } return 0; }2、力扣原题,不说了 100%;
3、起手就是一个暴力回溯骗分;20%;头疼交了
#include <bits/stdc++.h> using namespace std; vector<vector<long>> path(1, vector<long>(2)); vector<vector<long>> nums; long ret = LONG_MAX; void callback(int idx){ if(idx == path.size()){ long cur_num = 1; long cur_ret = 0; for(int i=0; i< path.size(); i++){ cur_ret = max(cur_num/path[i][1], cur_ret); cur_num *= path[i][0]; } ret = min(cur_ret, ret); return; } for(int i=idx; i< path.size(); i++){ callback(i+1); swap(path[i], path[idx]); callback(i+1); swap(path[i], path[idx]); } } int main(){ long n; cin >> n; cin >> path[0][0] >> path[0][1]; for(int i=0; i< n; i++){ vector<long> num_i(2); cin >> num_i[0] >> num_i[1]; path.push_back(num_i); } vector<bool> used(n, false); callback(1); cout << ret; return 0; }值得一提的是,网上有原题....;