Skip to content

Commit

Permalink
version 5
Browse files Browse the repository at this point in the history
  • Loading branch information
jackqiang123 authored and Lian Lu committed Jun 9, 2016
1 parent 1c5f644 commit a962070
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 118 deletions.
38 changes: 22 additions & 16 deletions lc146.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,51 @@ public void set(int key, int val){
map.get(key).val = val;
moveToHead(map.get(key));
}
else{
else{// create a new node
DoubleLinkedListNode cur = new DoubleLinkedListNode(key, val);
map.put(key, cur);
if (head == null){
if (head == null){// if the current list is empty, put it there by marking head
head = cur; tail = cur;
}
else {
cur.next = head;
else { // if not, put it at the head.
head.prev = cur;
cur.next = head;
head = cur;
}
}
}

public void moveToHead(DoubleLinkedListNode node){
if (head == node) return;
public void moveToHead(DoubleLinkedListNode node){// a newly visit node
if (head == node) return; // if this node is already head do nothing.
if (tail == node) { // there are at least two nodes, while node is tail
DoubleLinkedListNode prevNode = node.prev;
tail = prevNode;
prevNode.next = null;
node.prev = null;
head.prev = node;
node.next = head;
head = node;
return;
}
// if node is not head or tail
DoubleLinkedListNode prevNode = node.prev;
DoubleLinkedListNode nextNode = node.next;
prevNode.next = nextNode;
if (node == tail){
tail = prevNode;
}
else{
nextNode.prev = prevNode;
}
node.prev = null;
nextNode.prev = prevNode;
head.prev = node;
node.next = head;
head = node;
}

public void removetail(){
if (tail == null) return;
if (head == tail){
map.remove(tail.val);
map.remove(tail.key);
head = null;
tail = null;
}
else{
map.remove(tail.val);
else{// tail is differnet from head
map.remove(tail.key);
DoubleLinkedListNode last = tail.prev;
last.next = null;
tail = last;
Expand Down
4 changes: 3 additions & 1 deletion lc164.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// fit in the 32-bit signed integer range.
public class Solution {
public int maximumGap(int[] nums) {
if (nums.length <= 1) return 0;
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int i : nums)
Expand All @@ -16,8 +17,9 @@ public int maximumGap(int[] nums) {
min = Math.min(min,i);
}
int gap = max - min;
if (gap == 0) return 0;
int bucketSize = gap/nums.length;
if (bucketSize == 0) return 0;
if (bucketSize == 0) bucketSize = 1;
int numofBucket = gap/bucketSize + 1;
int maxArray[] = new int[numofBucket];
Arrays.fill(maxArray, Integer.MIN_VALUE);
Expand Down
13 changes: 9 additions & 4 deletions lc188.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ public class Solution {
public int maxProfit(int K, int[] prices) {
// add speical case handle
// when K >= prices.length/2
if (K >= prices.length/2){
int sum = 0;
for (int i = 1; i < prices.length; i++)
sum += Math.max(0, prices[i] - prices[i-1]);
return sum;
}
int[][]dp = new int[K+1][prices.length+1];
//do transcation k at day i
for (int i = 0; i <= K; i++){
int temp = - prices[0];
for (int j = 0; j <= prices.length; j++)
{
if (i == 0 || j == 0) dp[i][j] = 0;
else {
for (int l = 1; l <= j; l++)
{
dp[i][j] = Math.max(dp[i][j], dp[i-1][l] + prices[j-1] - prices[l-1]);
}
dp[i][j] = Math.max(dp[i][j-1], prices[j-1] + temp);
if (j < prices.length) temp = Math.max(temp, dp[i-1][j-1] - prices[j-1]);
}
}
}
Expand Down
14 changes: 13 additions & 1 deletion lc204.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@

public class Solution {
public int countPrimes(int n) {

if (n == 0) return 0;
n--;
if (n == 1) return 0;
boolean []num = new boolean[n+1];
int count = 0;
for (int i = 2; i <= n; i++){
if (num[i]) continue;
count++;
for (int j = 1; j <= n/i; j++){
num[j*i] = true;
}
}
return count;
}
}
54 changes: 54 additions & 0 deletions lc207.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// There are a total of n courses you have to take, labeled from 0 to n - 1.
//
// Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]
//
// Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
//
// For example:
//
// 2, [[1,0]]
//
// There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible.
//
// 2, [[1,0],[0,1]]
//
// There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
//
// Note:
// The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
public class Solution {
//for exercise purpose, we do a loop check for question, and do topologica sort for II version
boolean[]visit;
boolean[]onstack;
public boolean canFinish(int numCourses, int[][] prerequisites) {
visit = new boolean[numCourses];
onstack = new boolean[numCourses];
Map<Integer, List<Integer>> adjlist = new HashMap();
for (int[]preq : prerequisites){
int src = preq[1];
int dst = preq[0];
if (adjlist.get(src) == null) adjlist.put(src, new ArrayList<Integer>());
adjlist.get(src).add(dst);
}
for (int i = 0; i < numCourses; i++){
if (hasCycle(i, adjlist))
return false;
}
return true;
}

private boolean hasCycle(int i, Map<Integer, List<Integer>> adjlist){
if (onstack[i]) return true;
if (visit[i]) return false;
visit[i] = true;
onstack[i] = true;
List<Integer> nb = adjlist.get(i);
if (nb != null) {
for (int n : nb){
if (hasCycle(n, adjlist)) return true;
}
}
onstack[i] = false;
return false;
}
}
36 changes: 19 additions & 17 deletions lc214.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,27 @@
// Given "abcd", return "dcbabcd".
public class Solution {
public String shortestPalindrome(String s) {
StringBuilder sb;
int len = s.length();
for (int i = 0; i < len; i++){
sb = new StringBuilder(s);
for (int j = 0; j <= i; j++){
sb.insert(len, s.charAt(j));
if (s.length() == 0) return s;
String ss = s + " " + (new StringBuilder(s).reverse()) + "#";
int[]next = getNextArray(ss);
int lengthofSP = next[next.length - 1];
return (new StringBuilder(s.substring(lengthofSP)).reverse()) + s;
}
private int[]getNextArray(String s){
int []next = new int[s.length()];
int j = 0;
int k = -1;
next[0] = -1;
while(j < s.length() - 1){
if (k == -1 || s.charAt(j) == s.charAt(k)){
next[++j] = ++k;
}
else {
k = next[k];
}
if (isPalindrome(sb))
return sb.toString();
}
return null;
return next;
}

private boolean isPalindrome(StringBuilder s){
int i = 0; int j = s.length() - 1;
while(i < j){
if (s.charAt(i) != s.charAt(j)) return false;
i++; j--;
}
return true;
}

}
12 changes: 6 additions & 6 deletions lc300.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ public int lengthOfLIS(int[] nums) {
return max;
}

// making an monotoically increasing seqeu


public int lengthOfLISNlogN(int[] nums) {
//dp can lead to a O(n^2) algorithm
int []dp = new int[nums.length];
// dp is storing the smallest number to have consectuive seq length i +1
int len = 0;
for (int x : nums){
int i = Arrays.binarySearch(nums, 0, len, x);
int i = Arrays.binarySearch(dp, 0, len, x);
if (i < 0) i = -(i+1);
dp[i] = x;
if (i == len) len++;
dp[i] = x; // note that x is the largest index elements, we can either replace previous existing x, or insert x into it
//x is the just larger than dp[i]
if (i == len) len++; // if i is larger than all elemetns, then increase the range
}
return len;
}
Expand Down
3 changes: 2 additions & 1 deletion lc32.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public int longestValidParentheses(String s) {
int res = s.length() - 1 - last;
while(!stack.isEmpty()){
int cur = stack.pop();
res = Math.max(res, last - cur - 1);
if (last - cur - 1 > res)
res = last - cur - 1;
last = cur;
}
return Math.max(res, last);
Expand Down
Loading

0 comments on commit a962070

Please sign in to comment.