729. My Calendar I
You are implementing a program to use as your calendar. We can add a new event if adding the event will not cause a double booking.
A double booking happens when two events have some non-empty intersection (i.e., some moment is common to both events.).
The event can be represented as a pair of integers start and end that represents a booking on the half-open interval [start, end), the range of real numbers x such that start <= x < end.
Implement the MyCalendar
class:
MyCalendar()
Initializes the calendar object.
boolean book(int start, int end)
Returns true if the event can be added to the calendar successfully without causing a double booking. Otherwise, return false and do not add the event to the calendar.
Example 1:
Input
["MyCalendar", "book", "book", "book"]
[[], [10, 20], [15, 25], [20, 30]]
Output
[null, true, false, true]
Solution:
class MyCalendar {
List<Integer> sorted;
public MyCalendar() {
sorted = new LinkedList<>();
}
public boolean book(int start, int end) {
if (sorted.size() == 0){
sorted.add(start);
sorted.add(end);
return true;
}
int res = getIndex(start, end);
if (res != -1){
sorted.add(res, end);
sorted.add(res, start);
}
return res != -1;
}
private int getIndex(int start, int end){
int n = sorted.size();
int ind1 = searchL(start, 0, n);
int ind2 = searchR(end, 0, n);
if (ind1 == ind2 && ind1 % 2 == 0){
return ind1;
}
return -1;
}
private int searchL(int val, int left, int right){
if (left >= right){
return left;
}
int mid = (left + right) >> 1;
int res = 0;
if (sorted.get(mid) > val) res = searchL(val, left, mid);
else res = searchL(val, mid + 1, right);
return res;
}
private int searchR(int val, int left, int right){
if (left >= right){
return left;
}
int mid = (left + right) >> 1;
int res = 0;
if (sorted.get(mid) >= val) res = searchR(val, left, mid);
else res = searchR(val, mid + 1, right);
return res;
}
}
70. Climbing Stairs
You are climbing a staircase. It takes n steps to reach the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Solution.
class Solution {
public int climbStairs(int n) {
int num1 = 1, num2 = 2, num3 = 0;
if (n == 1) return 1;
for (int i = 3; i <= n; i++){
num3 = num1 + num2;
num1 = num2;
num2 = num3;
}
return num2;
}
}
72. Edit Distance
Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2.
You have the following three operations permitted on a word:
- Insert a character
- Delete a character
- Replace a character
Example 1:
Input: word1 = "horse", word2 = "ros"
Output: 3
Explanation:
horse -> rorse (replace 'h' with 'r')
rorse -> rose (remove 'r')
rose -> ros (remove 'e')
Solution.
class Solution {
public int minDistance(String word1, String word2) {
int m = word1.length();
int n = word2.length();
int[][] dp = new int[m][n];
for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++){
dp[i][j] = -1;
}
}
int res = recur(word1, word2, m-1, n-1, dp);
return res;
}
public int recur(String word1, String word2, int m, int n, int[][] dp){
if (m < 0){
return n + 1;
}
if (n < 0){
return m + 1;
}
if (dp[m][n] != -1){
return dp[m][n];
}
if (word1.charAt(m) == word2.charAt(n)){
return recur(word1, word2, m-1, n-1, dp);
}else{
int res = Math.min(recur(word1, word2, m-1, n, dp), recur(word1, word2, m, n-1, dp));
dp[m][n] = 1 + Math.min(res, recur(word1, word2, m-1, n-1, dp));
}
return dp[m][n];
}
}
Explanation.
Think:
Make 2 pointers point to the last index of word1
and word2
. if word1[pt1] == word2[pt2]
, then we ignore. Otherwise, we assume it to do three kinds of operations. Furthermore, we get the minimum of them. The code will be like this:
if (word1[pt1] == word2[pt2])
pt1--;
pt2--;
else
result = min(Insert Operation, Remove Operation, Replace Operation);
Then we discuss the base case.
pt1 < 0
: comes to the situation below, then the result is pt2 + 1
straightly.
word1: ""
word2:"abcd"
pt2 < 0
: same to the pt1 < 0
, the result comes to pt1 + 1
.
Finally we can get the code like this.