4:31
keep coding don't stop
[Frequency Order]
-
Two Sum
class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer,Integer> hm = new HashMap<>(); for(int i = 0; i < nums.length; i++){ int exp = target - nums[i]; if(hm.containsKey(exp) && hm.get(exp) != i){ return new int[] {hm.get(exp), i}; } else hm.put(nums[i], i); } throw new IllegalArgumentException(); } }
-
Add Two Numbers
public ListNode addTwoNumbers(ListNode l1, ListNode l2) { if(l1 == null || l2 == null) return l1 == null ? l2 : l1; int carry = 0; ListNode dummy = new ListNode(-1), curr = dummy; while(l1 != null || l2 != null) { int v1 = (l1 == null) ? 0 : l1.val; int v2 = (l2 == null) ? 0 : l2.val; int res = v1 + v2 + carry; curr.next = new ListNode(res % 10); curr = curr.next; if(res > 9) carry = 1; else carry = 0; l1 = (l1 == null) ? l1 : l1.next; l2 = (l2 == null) ? l2 : l2.next; } if(carry == 1){ curr.next = new ListNode(1); } return dummy.next; } }
-
Trapping Rain Water
class Solution { public int trap(int[] height) { int n = height.length, i = 0, j = n - 1, ans = 0, lmax = 0, rmax = 0; while(i < j){ if(height[i] <= height[j]){ if(height[i] > lmax) lmax = height[i++]; else ans += lmax - height[i++]; } else{ if(height[j] > rmax) rmax = height[j--]; else ans += rmax - height[j--]; } }
return ans;} }
-
LRU Cache
class LRUCache {
int limit = 0;
Map<Integer, Integer> cache;
public LRUCache(int capacity) {
cache = new LinkedHashMap<>();
limit = capacity;
}
//
public int get(int key) {
if(!cache.containsKey(key)) return -1;
int val = cache.get(key);
cache.remove(key, val);
cache.put(key, val);
return val;
}
//
public void put(int key, int value) {
if(cache.containsKey(key)) cache.remove(key);
cache.put(key,value);
if(cache.size() > limit){
for(int i : cache.keySet()){
cache.remove(i);
break;
}
}
}
}
9:48
keep coding don't stop #thuglife
-
Longest Palindromic Substring
class Solution { public String longestPalindrome(String s) { if(s == null || s.length() == 0) return ""; int start = 0, end = 0; for(int i = 0; i < s.length(); i++){ int len1 = expandAroundCenter(s, i, i); int len2 = expandAroundCenter(s, i, i + 1); int max = Math.max(len1, len2); if(max > end - start + 1){ start = i - (max - 1) / 2; end = i + max / 2; } } return s.substring(start, end + 1); } public int expandAroundCenter(String s, int start, int end){ int l = start, r = end; while(l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)){ l--; r++; } return r - l - 1; } }
Other
OA2 Review:
-
merge two sorted linked list
class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2){ if(l1 == null || l2 == null) return (l1 == null) ? l2 : l1; ListNode dummy = new ListNode(-1), curr = dummy; while(l1 != null || l2 != null){ int v1 = (l1 == null) ? Integer.MAX_VALUE : l1.val; int v2 = (l2 == null) ? Integer.MAX_VALUE : l2.val; if(v1 <= v2){ curr.next = new ListNode(v1); curr = curr.next; l1 = l1.next; } else{ curr.next = new ListNode(v2); curr = curr.next; l2 = l2.next; } } return dummy.next; } }
-
Path With Maximum Minimum Value //there is (at least) one test case can be approved //tight schedule, I will go with the "wrong" version for now
class Solution {
int max, min, row, col;
public int maximumMinimumPath(int[][] grid) {
row = grid.length;
col = grid[0].length;
max = Integer.MIN_VALUE;
min = Integer.MAX_VALUE;
dfs(grid, 0, 0, min);
return max;
}
public void dfs(int[][] grid, int x, int y, int curmin){
if(x >= row || y >= col) return;
if(x == row - 1 && y == col - 1){
curmin = Math.min(curmin, grid[x][y]);
max = Math.max(curmin, max);
}
curmin = Math.min(curmin, grid[x][y]);
dfs(grid, x + 1, y, curmin);
dfs(grid, x, y + 1, curmin);
}
}
- K Closest Points to Origin
class Solution {
public int[][] kClosest(int[][] points, int K){
int[] distances = new int[points.length];
int i = 0;
for(int[] point : points){
distances[i++] = d(point);
}
Arrays.sort(distances);
int[][] ans = new int[K][2];
i = 0;
for(int j = 0; j < points.length; j++){
if(d(points[j]) <= distances[K - 1]) ans[i++] = points[j];
}
return ans;
}
public int d(int[] point){
return point[0] * point[0] + point[1] * point[1];
}
}
MergeSort:
public static void mergeSort(int[] arr, int n){
//n is the size of the arr
if(n < 2) return;
int mid = n / 2;
int[] l = new int[mid];
int[] r = new int[n - mid];
for(int i = 0; i < mid; i++) l[i] = arr[i];
for(int i = mid; i < n; i++) r[i - mid] = arr[i];
mergeSort(l, mid);
mergeSort(r, n - mid);
merge(arr, l, r, mid, n - mid);
}
public static void merge(int[] arr, int[] l, int[] r, int left, int right){
int i = 0, j = 0, k = 0;
while(i < left && j < right){
if(l[i] <= r[j]) arr[k++] = l[i++];
else arr[k++] = r[j++];
}
while(i < left) arr[k++] = l[i++];
while(j < right) arr[k++] = r[j++];
}