Skip to content

Commit d74e12d

Browse files
committed
[Function add]
1. Add leetcode solutions with tag binary search.
1 parent 5d27798 commit d74e12d

4 files changed

+169
-2
lines changed

leetcode/34. Find First and Last Position of Element in Sorted Array.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,27 @@ class Solution {
7575
return binarySearch(nums, target, mid + 1, high);
7676
}
7777
}
78-
```
78+
```
79+
80+
###Third Time
81+
* Method 1: Binary Search
82+
```Java
83+
class Solution {
84+
public int[] searchRange(int[] nums, int target) {
85+
if(nums == null || nums.length == 0) return new int[]{-1, -1};
86+
int index = bs(nums, 0, nums.length - 1, target);
87+
if(index == -1) return new int[]{-1, -1};
88+
int left = index, right = index;
89+
while(--left >= 0 && nums[left] == target){}
90+
while(++right < nums.length && nums[right] == target){}
91+
return new int[]{left + 1, right - 1};
92+
}
93+
private int bs(int[] nums, int low, int high, int target){
94+
if(low > high) return -1;
95+
int mid = low + (high - low) / 2;
96+
if(nums[mid] == target) return mid;
97+
else if(nums[mid] < target) return bs(nums, mid + 1, high, target);
98+
else return bs(nums, low, mid - 1, target);
99+
}
100+
}
101+
```

leetcode/35. Search Insert Position.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,26 @@ class Solution {
5757
return nums.length;
5858
}
5959
}
60-
```
60+
```
61+
62+
### Third Time
63+
* Method 1: binary search AC 100%
64+
```Java
65+
class Solution {
66+
public int searchInsert(int[] nums, int target) {
67+
int low = 0, high = nums.length - 1;
68+
return bs(nums, low, high, target);
69+
}
70+
private int bs(int[] nums, int low, int high, int target){
71+
int mid = low + (high - low) / 2;
72+
if(target > nums[nums.length - 1]) return nums.length;
73+
if(mid == low || mid == high){
74+
if(target > nums[low]) return high;
75+
else return low;
76+
}
77+
if(nums[mid] == target) return mid;
78+
else if(nums[mid] > target) return bs(nums, low, mid, target);
79+
else return bs(nums, mid, high, target);
80+
}
81+
}
82+
```

leetcode/704. Binary Search.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## 704. Binary Search
2+
3+
### Question:
4+
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a function to search target in nums. If target exists, then return its index, otherwise return -1.
5+
6+
```
7+
Example 1:
8+
9+
Input: nums = [-1,0,3,5,9,12], target = 9
10+
Output: 4
11+
Explanation: 9 exists in nums and its index is 4
12+
13+
Example 2:
14+
15+
Input: nums = [-1,0,3,5,9,12], target = 2
16+
Output: -1
17+
Explanation: 2 does not exist in nums so return -1
18+
```
19+
20+
Note:
21+
1. You may assume that all elements in nums are unique.
22+
2. n will be in the range [1, 10000].
23+
3. The value of each element in nums will be in the range [-9999, 9999].
24+
25+
26+
### Solution:
27+
* Method 1: binary search
28+
```Java
29+
class Solution {
30+
public int search(int[] nums, int target) {
31+
if(nums == null || nums.length == 0) return -1;
32+
return bs(nums, 0, nums.length - 1, target);
33+
}
34+
private int bs(int[] nums, int low, int high, int target){
35+
if(low > high) return -1;
36+
int mid = low + (high - low) / 2;
37+
if(nums[mid] == target) return mid;
38+
else if(nums[mid] > target) return bs(nums, low, mid - 1, target);
39+
else return bs(nums, mid + 1, high, target);
40+
}
41+
}
42+
```
43+
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
## 981. Time Based Key-Value Store
2+
3+
### Question:
4+
Create a timebased key-value store class TimeMap, that supports two operations.
5+
6+
1. set(string key, string value, int timestamp)
7+
8+
Stores the key and value, along with the given timestamp.
9+
10+
2. get(string key, int timestamp)
11+
12+
Returns a value such that set(key, value, timestamp_prev) was called previously, with timestamp_prev <= timestamp.
13+
If there are multiple such values, it returns the one with the largest timestamp_prev.
14+
If there are no values, it returns the empty string ("").
15+
16+
17+
···
18+
Example 1:
19+
20+
Input: inputs = ["TimeMap","set","get","get","set","get","get"], inputs = [[],["foo","bar",1],["foo",1],["foo",3],["foo","bar2",4],["foo",4],["foo",5]]
21+
Output: [null,null,"bar","bar",null,"bar2","bar2"]
22+
Explanation:
23+
TimeMap kv;
24+
kv.set("foo", "bar", 1); // store the key "foo" and value "bar" along with timestamp = 1
25+
kv.get("foo", 1); // output "bar"
26+
kv.get("foo", 3); // output "bar" since there is no value corresponding to foo at timestamp 3 and timestamp 2, then the only value is at timestamp 1 ie "bar"
27+
kv.set("foo", "bar2", 4);
28+
kv.get("foo", 4); // output "bar2"
29+
kv.get("foo", 5); //output "bar2"
30+
31+
Example 2:
32+
33+
Input: inputs = ["TimeMap","set","set","get","get","get","get","get"], inputs = [[],["love","high",10],["love","low",20],["love",5],["love",10],["love",15],["love",20],["love",25]]
34+
Output: [null,null,null,"","high","high","low","low"]
35+
···
36+
37+
Note:
38+
1. All key/value strings are lowercase.
39+
2. All key/value strings have length in the range [1, 100]
40+
3. The timestamps for all TimeMap.set operations are strictly increasing.
41+
4. 1 <= timestamp <= 10^7
42+
5. TimeMap.set and TimeMap.get functions will be called a total of 120000 times (combined) per test case.
43+
44+
45+
46+
47+
### Solution:
48+
* Method 1: TreeMap
49+
```Java
50+
class TimeMap {
51+
private Map<String, TreeMap<Integer, String>> map;
52+
/** Initialize your data structure here. */
53+
public TimeMap() {
54+
map = new HashMap<>();
55+
}
56+
public void set(String key, String value, int timestamp) {
57+
TreeMap<Integer, String> treeMap = map.containsKey(key) ? map.get(key): new TreeMap<>();
58+
treeMap.put(timestamp, value);
59+
map.put(key, treeMap);
60+
}
61+
public String get(String key, int timestamp) {
62+
if(!map.containsKey(key)) return "";
63+
else{
64+
TreeMap treeMap = map.get(key);
65+
Map.Entry<Integer, String> entry = treeMap.floorEntry(timestamp);
66+
if(entry == null) return "";
67+
else return entry.getValue();
68+
}
69+
}
70+
}
71+
/**
72+
* Your TimeMap object will be instantiated and called as such:
73+
* TimeMap obj = new TimeMap();
74+
* obj.set(key,value,timestamp);
75+
* String param_2 = obj.get(key,timestamp);
76+
*/
77+
```
78+
79+

0 commit comments

Comments
 (0)