Created
July 17, 2016 06:50
-
-
Save jianminchen/e4906acce9c763626453adb182ee3b03 to your computer and use it in GitHub Desktop.
DiffK - facebook code lab - study the solution provided by the lab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
bool diffPossible(vector<int> &num, int diff) { | |
if (num.size() < 2 || diff < 0) return false; | |
int j = 0, len = num.size(); | |
for (int i = 0; i < len - 1; i++) { | |
j = max(j, i + 1); | |
while (j < len && num[j] - num[i] < diff) j += 1; | |
if (j < len && num[j] - num[i] == diff) return true; | |
} | |
return false; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment