1386. 安排电影院座位
先sort一下,就能将reservedSeats 数组按照行和列排序,设begin = 0, end = 11
则遍历每个reservedSeats时,将有cnt = reservedSeats[i][1] - begin - 1个空位
如果reservedSeats[i][0] == reservedSeats[i + 1][0],则说明在同一行,令begin = reservedSeats[i][0] 即可
否则,begin = 0,并计算出剩余位置有多少空位 即 cnt = end - reservedSeats[i][1] - 1
每次计算出空位时需判断是否在不合适的位置,就10个位置,哪个不合适,自己看一下就行,看代码也能看出来
class Solution { public: int maxNumberOfFamilies(int n, vector<vector<int>>& reservedSeats) { int len = reservedSeats.size(); sort(reservedSeats.begin(), reservedSeats.end()); int row = 1; int ret = 0, begin = 0, end = 11; for(int i = 0; i < len - 1; i++) { int cnt = reservedSeats[i][1] - begin - 1; if(cnt >= 4) { if(begin == 0 || begin == 4 || begin == 2 || begin == 6) cnt--; if(cnt >= 4) ret += cnt / 4; } if(reservedSeats[i + 1][0] == reservedSeats[i][0]) begin = reservedSeats[i][1]; else { row++; begin = 0; cnt = end - reservedSeats[i][1] - 1; if(cnt >= 4) { if(reservedSeats[i][1] == 0 || reservedSeats[i][1] == 4 || reservedSeats[i][1] == 2 || reservedSeats[i][1] == 6) cnt--; if(cnt >= 4) ret += cnt / 4; } } } int cnt = reservedSeats[len - 1][1] - begin - 1; if(cnt >= 4) { if(begin == 0 || begin == 4 || begin == 2 || begin == 6) cnt--; if(cnt >= 4) ret += cnt / 4; } cnt = end - reservedSeats[len - 1][1] - 1; if(cnt >= 4) { if(reservedSeats[len - 1][1] == 0 || reservedSeats[len - 1][1] == 4 || reservedSeats[len - 1][1] == 2 || reservedSeats[len - 1][1] == 6) cnt--; if(cnt >= 4) ret += cnt / 4; } ret += (n - row) * 2; return ret; } };
自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。