forked from kdn251/interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
164 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target. | ||
|
||
// Note: | ||
// Given target value is a floating point. | ||
// You are guaranteed to have only one unique value in the BST that is closest to the target. | ||
|
||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode(int x) { val = x; } | ||
* } | ||
*/ | ||
public class Solution { | ||
public int closestValue(TreeNode root, double target) { | ||
|
||
int value = root.val; | ||
TreeNode child = root.val < target ? root.right : root.left; | ||
|
||
if(child == null) return value; | ||
|
||
int childValue = closestValue(child, target); | ||
|
||
return Math.abs(value - target) < Math.abs(childValue - target) ? value : childValue; | ||
|
||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad. | ||
|
||
// Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad. | ||
|
||
// You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API. | ||
|
||
/* The isBadVersion API is defined in the parent class VersionControl. | ||
boolean isBadVersion(int version); */ | ||
|
||
public class Solution extends VersionControl { | ||
|
||
public int firstBadVersion(int n) { | ||
|
||
int start = 1; | ||
int end = n; | ||
|
||
while(start < end) { | ||
|
||
int mid = start + (end - start) / 2; | ||
if(!isBadVersion(mid)) start = mid + 1; | ||
else end = mid; | ||
|
||
} | ||
|
||
return start; | ||
|
||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// We are playing the Guess Game. The game is as follows: | ||
|
||
// I pick a number from 1 to n. You have to guess which number I picked. | ||
|
||
// Every time you guess wrong, I'll tell you whether the number is higher or lower. | ||
|
||
// You call a pre-defined API guess(int num) which returns 3 possible results (-1, 1, or 0): | ||
|
||
// -1 : My number is lower | ||
// 1 : My number is higher | ||
// 0 : Congrats! You got it! | ||
// Example: | ||
// n = 10, I pick 6. | ||
|
||
// Return 6. | ||
|
||
/* The guess API is defined in the parent class GuessGame. | ||
@param num, your guess | ||
@return -1 if my number is lower, 1 if my number is higher, otherwise return 0 | ||
int guess(int num); */ | ||
|
||
public class Solution extends GuessGame { | ||
|
||
public int guessNumber(int n) { | ||
|
||
return binarySearch(1, n); | ||
|
||
} | ||
|
||
private int binarySearch(int start, int end) { | ||
|
||
if(start > end) return -1; | ||
|
||
if(guess(start) == 0) return start; | ||
if(guess(end) == 0) return end; | ||
|
||
int mid = start + (end - start) / 2; | ||
|
||
if(guess(mid) == 0) return mid; | ||
|
||
else if(guess(mid) == 1) return binarySearch(mid + 1, end); | ||
else return binarySearch(start, mid - 1); | ||
|
||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Implement pow(x, n). | ||
|
||
public class Solution { | ||
|
||
public double myPow(double x, int n) { | ||
|
||
if(n == 0) { | ||
|
||
return 1; | ||
|
||
} | ||
|
||
if(Double.isInfinite(x)) { | ||
|
||
return 0; | ||
|
||
} | ||
|
||
if(n < 0) { | ||
|
||
n = -n; | ||
x = 1 / x; | ||
|
||
} | ||
|
||
return n % 2 == 0 ? myPow(x * x, n / 2) : x * myPow(x * x, n / 2); | ||
|
||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Implement int sqrt(int x). | ||
|
||
// Compute and return the square root of x. | ||
|
||
public class Solution { | ||
|
||
public int mySqrt(int x) { | ||
|
||
if(x == 0) return 0; | ||
|
||
int left = 1; | ||
int right = x; | ||
|
||
while(left <= right) { | ||
|
||
int mid = left + (right - left) / 2; | ||
|
||
if(mid == x / mid) return mid; | ||
else if(mid > x / mid) right = mid - 1; | ||
else if(mid < x / mid) left = mid + 1; | ||
|
||
} | ||
|
||
return right; | ||
|
||
} | ||
|
||
} |