昨天zTrix驾车带cbmixx和我进行了San Francisco Bay
Area一日游,游览了Google HQ、Facebook、Stanford University、Twitter
HQ和金门大桥,醒来疲惫不堪。到达San
Francisco机场,zTrix和cbmixx要回北京,而我要去台北,在登机口碰到ppwwyyxx。
int l = 0, h = a.size(); while (l < h-1) { int m = l+h >> 1; if (a[m-1] > a[m]) h = m; else l = m; } return l;
或者检查中间元素是否满足要求,可以提前退出循环。
1 2 3 4 5 6 7 8
int l = 0, h = a.size(); while (l < h-1) { int m = l+h >> 1; if (a[m-1] > a[m]) h = m; elseif (m+1 == h || a[m] > a[m+1]) l = h = m; else l = m+1; } return l;
// return true if a cycle is detected boolpointerReversal(ListNode *head){ if (! head) returnfalse; ListNode *x = head->next, *y, *z = head; while (x && x != head) { y = x->next; x->next = z; z = x; x = y; } return x == head; }
另有期望的算法,参见M. O. Rabin, J. O. Shallit, Randomized
Algorithms in Number Theory, Communications on Pure and Applied
Mathematics 39 (1986), no. S1, pp. S239–S256.
doi:10.1002/cpa.3160390713
#define ROF(i, a, b) for (int i = (b); --i >= (a); ) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, n) for (int i = 0; i < (n); i++)
class Solution { public: int maximalRectangle(vector<vector<char> > &a) { if (a.empty()) return 0; int m = a.size(), n = a[0].size(), ans = 0; vector<int> h(n), l(n), r(n, n-1); REP(i, m) { int ll = -1; REP(j, n) { h[j] = a[i][j] == '1' ? h[j]+1 : 0; if (a[i][j] == '0') ll = j; l[j] = h[j] ? max(h[j] == 1 ? 0 : l[j], ll+1) : j; } int rr = n; ROF(j, 0, n) { if (a[i][j] == '0') rr = j; r[j] = h[j] ? min(h[j] == 1 ? n-1 : r[j], rr-1) : j; ans = max(ans, (r[j]-l[j]+1)*h[j]); } } return ans; } };
#define ROF(i, a, b) for (int i = (b); --i >= (a); ) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, n) for (int i = 0; i < (n); i++)
class Solution { public: int maximalRectangle(vector<vector<char> > &a) { if (a.empty()) return 0; int m = a.size(), n = a[0].size(), ans = 0; vector<int> h(n), l(n), r(n, n-1); REP(i, m) { REP(j, n) { h[j] = a[i][j] == '1' ? h[j]+1 : 0; l[j] = j; while (l[j] && h[l[j]-1] >= h[j]) l[j] = l[l[j]-1]; } ROF(j, 0, n) { r[j] = j; while (r[j]+1 < n && h[j] <= h[r[j]+1]) r[j] = r[r[j]+1]; ans = max(ans, (r[j]-l[j]+1)*h[j]); } } return ans; } };
#define ROF(i, a, b) for (int i = (b); --i >= (a); ) #define FOR(i, a, b) for (int i = (a); i < (b); i++) #define REP(i, n) for (int i = 0; i < (n); i++) #define REP1(i, n) for (int i = 1; i <= (n); i++)
class Solution { public: int maximalRectangle(vector<vector<char> > &a) { if (a.empty()) return 0; int m = a.size(), n = a[0].size(), ans = 0; vector<int> h(n), p(n), b(m+1), s(n); REP(i, m) { REP(j, n) h[j] = a[i][j] == '1' ? h[j]+1 : 0; fill(b.begin(), b.end(), 0); REP(j, n) b[h[j]]++; REP1(j, m) b[j] += b[j-1]; REP(j, n) s[--b[h[j]]] = j; fill(p.begin(), p.end(), -1); ROF(j, 0, n) { int x = s[j], l = x, r = x; p[x] = x; if (x && p[x-1] != -1) { l = p[x-1]; p[l] = x; p[x] = l; } if (x+1 < n && p[x+1] != -1) { l = p[x]; r = p[x+1]; p[l] = r; p[r] = l; } ans = max(ans, (r-l+1)*h[x]); } } return ans; } };
copy($('td:has(.ac) ~ td:nth-child(3) a').map(function(_,e){ var id = $(e).parent().prev().text(); var h=e.href.replace(/htt.*problems/,'/leetcode');h=h.substr(0,h.length-1); var title=e.textContent, href=e.href, name=h.replace(/.*\//,''); return'|'+id+'|['+title+']('+href+')|['+name+'.cc]('+name+'.cc)|' }).toArray().join('\n'))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
require'mechanize' agent = Mechanize.new page = agent.get 'https://leetcode.com/accounts/login/' doc = page.form_with {|form| form['login'] = 'MaskRay' form['password'] = getpass }.submit.parser total = doc.css('td:nth-child(3)').size solved = doc.css('td:has(.ac)').size puts "You have solved #{solved}/#{total} problems." for a in doc.css('td:nth-child(3) a') id = a.parent.previous_element.text href = a['href'] name = href.sub(/\/problems\/(.*)\//, '\1') title = a.text puts "|#{id}|[#{title}](#{href})|[#{name}.cc](#{name}.cc)|" end