Skip to content

update solutions for some homework #796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions Week_04/id_26/Leetcode_169_26.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package divideandconquer;

import java.util.HashMap;
import java.util.Map;

// Source : https://leetcode.com/problems/majority-element/
// Id : 169
// Author : Fanlu Hai
// Date : 2018-05-24
// Other :
// Tips : boyer moore voting algorithm;
// take full advantage of the situation to get the best solution.

public class MajorityElement {
// straight forward way is to store appearance of number in a hashmap
// 16.69% 16 ms 99.07% 39.1 MB
public int majorityElementSlow(int[] nums) {
// System.out.println(nums.length / 2 + 1);
Map<Integer, Integer> counter = new HashMap<>();

if (nums.length == 1)
return nums[0];

for (int num : nums) {
if (counter.containsKey(num)) {
if (counter.get(num) == nums.length / 2)
return num;
else
counter.put(num, counter.get(num) + 1);
} else
counter.put(num, 1);
}

return -1;
}

// boyer moore voting algorithm
// 100.00% 1ms 50.94% 41.5 MB
public int majorityElement(int[] nums) {
int count = 0;
int major = nums[0];

for (int num : nums) {
if (num == major)
count++;
else
count--;
if (count == -1) {
major = nums[0];
count = 0;
}
}
return major;
}

public static void main(String[] args) {
int[] array = {1, 2, 1};
MajorityElement m = new MajorityElement();
System.out.println(m.majorityElement(array));
}

}
103 changes: 103 additions & 0 deletions Week_04/id_26/Leetcode_211_26.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.fanlu.leetcode.trietree;

// Source : https://leetcode.com/problems/add-and-search-word-data-structure-design/
// Id : 211
// Author : Fanlu Hai
// Date : 2018-05-12
// Other :
// Tips :

public class WordDictionary {
TrieNode root;

/**
* Initialize your data structure here.
*/
public WordDictionary() {
root = new TrieNode();
}

/**
* Adds a word into the data structure.
*/
public void addWord(String word) {
TrieNode node = root;
for (char c : word.toCharArray()) {
int index = c - 'a';
if (node.children[index] == null) {
node.children[index] = new TrieNode();
}
node = node.children[index];
}
node.isEndOfAWord = true;
}

/**
* Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter.
*/
// 100.00% 76.32% (54.9)
public boolean search(String word) {
return search(root, word, 0);
}

// compare each node's children with the char at the given index of the word incrementally
public boolean search(TrieNode node, String word, int index) {
// if index is already longer than max index (length-1)
// then check if the node is the end of an work, there's no need to check its children
if (index == word.length()) {
return node.isEndOfAWord;
}

char ch = word.charAt(index);
index++;

if (ch == '.') {
for (TrieNode child : node.children) {
if (child != null) {
// if this child works then return true
if (search(child, word, index)) {
return true;
}
}
}
// no child's path works.
return false;
} else {
if (node.children[ch - 'a'] != null) {
return search(node.children[ch - 'a'], word, index);
} else {
return false;
}
}
}

class TrieNode {
public TrieNode[] children = new TrieNode[26];
public boolean isEndOfAWord = false;

}

public static void main(String[] args) {
WordDictionary obj = new WordDictionary();
// obj.addWord("at");
obj.addWord("and");
// obj.addWord("an");
obj.addWord("add");
// obj.addWord("a");
System.out.println(obj.search(".ad"));
System.out.println(obj.search("a.d"));
System.out.println(obj.search("b."));
System.out.println(obj.search("...."));
System.out.println(obj.search(".."));
System.out.println(obj.search("a.d."));
System.out.println(obj.search(".ad"));
}
}


/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary obj = new WordDictionary();
* obj.addWord(word);
* boolean param_2 = obj.search(word);
*/
106 changes: 106 additions & 0 deletions Week_04/id_26/Leetcode_241_26.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package divideandconquer;
// Source : https://leetcode.com/problems/different-ways-to-add-parentheses/
// Id : 241
// Author : Fanlu Hai
// Date : 2018-05-24
// Other :
// Tips : "improvements" can make program run slower

import java.util.*;

public class DifferentWaysToAddParentheses {

//32.84% 3 ms 34.61% 39.6 MB
public List<Integer> diffWaysToComputeSlow(String input) {
List<Integer> result = new LinkedList<>();

for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) == '+' || input.charAt(i) == '-' || input.charAt(i) == '*') {
String part1 = input.substring(0, i);
String part2 = input.substring(i + 1);

List<Integer> resultPart1 = new LinkedList<>();
List<Integer> resultPart2 = new LinkedList<>();
resultPart1 = diffWaysToComputeSlow(part1);
resultPart2 = diffWaysToComputeSlow(part2);

// when either on is empty, nothing will be added to result
for (int res1 : resultPart1) {
for (int re2 : resultPart2) {
switch (input.charAt(i)) {
case '+':
result.add(res1 + re2);
break;
case '-':
result.add(res1 - re2);
break;
case '*':
result.add(res1 * re2);
break;
}
}
}
}
}
// if nothing is added to result list, it means the string is a number
if (result.isEmpty())
result.add(Integer.valueOf(input));
return result;
}

// use map to store intermediate results to speed up recursion
Map<String, List<Integer>> resultCache = new HashMap<>();


//1 ms 100% 34.2 MB 100%
public List<Integer> diffWaysToCompute(String input) {

if (resultCache.containsKey(input))
return resultCache.get(input);

List<Integer> result = new LinkedList<>();

for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) == '+' || input.charAt(i) == '-' || input.charAt(i) == '*') {
String part1 = input.substring(0, i);
String part2 = input.substring(i + 1);

List<Integer> resultPart1 = new LinkedList<>();
List<Integer> resultPart2 = new LinkedList<>();
resultPart1 = diffWaysToCompute(part1);
resultPart2 = diffWaysToCompute(part2);

// when either on is empty, nothing will be added to result
for (int res1 : resultPart1) {
for (int re2 : resultPart2) {
//using swith here is not very efficient
// 78.94% 2 ms 100.00% 34.2 MB
// switch (input.charAt(i)) {
// case '+':
// result.add(res1 + re2);
// break;
// case '-':
// result.add(res1 - re2);
// break;
// case '*':
// result.add(res1 * re2);
// break;
// }
if (input.charAt(i) == '+')
result.add(res1 + re2);
else if (input.charAt(i) == '-')
result.add(res1 - re2);
else
result.add(res1 * re2);

}
}
}
}
// if nothing is added to result list, it means the string is a number
if (result.isEmpty())
result.add(Integer.valueOf(input));
resultCache.put(input, result);
return result;
}
}
Loading