From da02c6fbb4265c4cc91ee9d41c85bc03ad0905b8 Mon Sep 17 00:00:00 2001 From: Kimberly Ha Date: Tue, 16 Jul 2019 15:35:11 -0400 Subject: [PATCH 1/3] Added solutions guide for Ugly Number. --- .../solutions/UglyNumberSolutionsGuide.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 challenges/UglyNumber/solutions/UglyNumberSolutionsGuide.md diff --git a/challenges/UglyNumber/solutions/UglyNumberSolutionsGuide.md b/challenges/UglyNumber/solutions/UglyNumberSolutionsGuide.md new file mode 100644 index 0000000..f335935 --- /dev/null +++ b/challenges/UglyNumber/solutions/UglyNumberSolutionsGuide.md @@ -0,0 +1,24 @@ +# Ugly Number Solutions Guide + +### C/C++ +* [Using 2, 3, and 5 as divisors](https://leetcode.com/problems/ugly-number/discuss/69214/2-4-lines-every-language) +* [Simple but no explanation](https://leetcode.com/problems/ugly-number/discuss/69353/Simple-C%2B%2B-solution) + +### C# +* [Using 2, 3, and 5 as divisors](https://leetcode.com/problems/ugly-number/discuss/69214/2-4-lines-every-language) + +### Java +* [Using 2, 3, and 5 as divisors](https://leetcode.com/problems/ugly-number/discuss/69332/Simple-java-solution-with-explanation) +* [Simple but no explanation](https://leetcode.com/problems/ugly-number/discuss/69225/My-2ms-java-solution) +* [Using divisors explained](https://leetcode.com/problems/ugly-number/discuss/69308/Java-solution-greatest-divide-by-2-3-5) + +### JavaScript +* [Using 2, 3, and 5 as divisors](https://leetcode.com/problems/ugly-number/discuss/69214/2-4-lines-every-language) + +### Python +* [Using 2, 3, and 5 as divisors](https://leetcode.com/problems/ugly-number/discuss/69214/2-4-lines-every-language) +* [Dense one line implementation by @StefanPochmann](https://leetcode.com/problems/ugly-number/discuss/69232/Python:-1-line-solution/71216) + +### Ruby +* [Using 2, 3, and 5 as divisors](https://leetcode.com/problems/ugly-number/discuss/69214/2-4-lines-every-language) + From 758db6504679155b7cc64fe01b35015d5cf9d662 Mon Sep 17 00:00:00 2001 From: Kimberly Ha Date: Tue, 16 Jul 2019 15:35:37 -0400 Subject: [PATCH 2/3] Added individual solutions for Word Search. --- .../wordSearch/solutions/wordSearch.cpp | 37 +++++++++++ .../wordSearch/solutions/wordSearch.java | 29 +++++++++ challenges/wordSearch/solutions/wordSearch.js | 63 +++++++++++++++++++ challenges/wordSearch/solutions/wordSearch.py | 30 +++++++++ 4 files changed, 159 insertions(+) create mode 100644 challenges/wordSearch/solutions/wordSearch.cpp create mode 100644 challenges/wordSearch/solutions/wordSearch.java create mode 100644 challenges/wordSearch/solutions/wordSearch.js create mode 100644 challenges/wordSearch/solutions/wordSearch.py diff --git a/challenges/wordSearch/solutions/wordSearch.cpp b/challenges/wordSearch/solutions/wordSearch.cpp new file mode 100644 index 0000000..3f9a10a --- /dev/null +++ b/challenges/wordSearch/solutions/wordSearch.cpp @@ -0,0 +1,37 @@ +// Please check link for explanation of code below +// https://leetcode.com/problems/word-search/discuss/27739/My-DFS-%2B-Backtracking-C%2B%2B-solution-(16ms) + +// Additional solutions +// https://leetcode.com/problems/word-search/discuss/27675/My-19ms-accepted-C%2B%2B-code +// https://leetcode.com/problems/word-search/discuss/27835/C%2B%2B-dfs-solution. + +// Typical dfs + backtracking question. It compare board[row][col] with word[start], if they match, change board[row][col] to '*' to mark it as visited. Then move to the next one (i.e. word[start+1]) and compare it to the current neighbors ( doing it by recursion) +class Solution { +private: + bool dfs(vector>& board, int row, int col, const string &word, int start, int M, int N, int sLen) + { + char curC; + bool res = false; + if( (curC = board[row][col]) != word[start]) return false; + if(start==sLen-1) return true; + board[row][col] = '*'; + if(row>0) res = dfs(board, row-1, col, word, start+1, M, N, sLen); + if(!res && row < M-1) res = dfs(board, row+1, col, word, start+1, M, N, sLen); + if(!res && col > 0) res = dfs(board, row, col-1, word, start+1, M, N, sLen); + if(!res && col < N-1) res = dfs(board, row, col+1, word, start+1, M, N, sLen); + board[row][col] = curC; + return res; + } + +public: + bool exist(vector>& board, string word) { + int M,N,i,j,sLen = word.size(); + if( (M=board.size()) && (N=board[0].size()) && sLen) + { + for(i=0; i 0) && find(current, row - 1, col); + const existsDown = (row < board.length - 1) && find(current, row + 1, col); + const existsLeft = (col > 0) && find(current, row, col - 1); + const existsRight = (col < board[0].length - 1) && find(current, row, col + 1); + + // Unflag the current cell so its not marked in the next interation + visited.delete(`${row}-${col}`); + } + + // Swipe the matrix to look for the first letter of the word + const firstLetter = word.charAt(0); + for (let row = 0; row < board.length; row += 1) { + for (let col = 0; col < board[0].length; col += 1) { + const letter = board[row][col]; + // In case it has found calls the recursive method to look for the remaining + if (letter === firstLetter) find('', row, col); + // In case it has found abort and return true + if (found) return true; + } + } + + // In case it has not found + return false; +}; \ No newline at end of file diff --git a/challenges/wordSearch/solutions/wordSearch.py b/challenges/wordSearch/solutions/wordSearch.py new file mode 100644 index 0000000..a886e9d --- /dev/null +++ b/challenges/wordSearch/solutions/wordSearch.py @@ -0,0 +1,30 @@ +# Please check link for explanation of code below +# https://leetcode.com/problems/word-search/discuss/27660/Python-dfs-solution-with-comments. + +# Additional solutions +# Another DFS - https://leetcode.com/problems/word-search/discuss/27820/Python-DFS-solution +# Easy to understand - https://leetcode.com/problems/word-search/discuss/122236/Easy-Python-Solution +# Backtracking - https://leetcode.com/problems/word-search/discuss/27911/Accepted-Python-backtracking-solution + +def exist(self, board, word): + if not board: + return False + for i in xrange(len(board)): + for j in xrange(len(board[0])): + if self.dfs(board, i, j, word): + return True + return False + +# check whether can find word, start at (i,j) position +def dfs(self, board, i, j, word): + if len(word) == 0: # all the characters are checked + return True + if i<0 or i>=len(board) or j<0 or j>=len(board[0]) or word[0]!=board[i][j]: + return False + tmp = board[i][j] # first character is found, check the remaining part + board[i][j] = "#" # avoid visit agian + # check whether can find "word" along one direction + res = self.dfs(board, i+1, j, word[1:]) or self.dfs(board, i-1, j, word[1:]) \ + or self.dfs(board, i, j+1, word[1:]) or self.dfs(board, i, j-1, word[1:]) + board[i][j] = tmp + return res From b41fdee812f5f8c59abf9b59f37817624aa98f5c Mon Sep 17 00:00:00 2001 From: Kimberly Ha Date: Tue, 16 Jul 2019 15:35:58 -0400 Subject: [PATCH 3/3] Added solutions guide for Grumpy BookStore Owner. --- .../solutions/GrumpySolutionsGuide.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 challenges/grumpyBookstoreOwner/solutions/GrumpySolutionsGuide.md diff --git a/challenges/grumpyBookstoreOwner/solutions/GrumpySolutionsGuide.md b/challenges/grumpyBookstoreOwner/solutions/GrumpySolutionsGuide.md new file mode 100644 index 0000000..a1cf3fd --- /dev/null +++ b/challenges/grumpyBookstoreOwner/solutions/GrumpySolutionsGuide.md @@ -0,0 +1,25 @@ +# Grumpy Bookstore Owner Solutions Guide + +### C/C++ +* [Sliding window technique](https://leetcode.com/problems/grumpy-bookstore-owner/discuss/299237/C%2B%2B-Sliding-Window) +* [Linear time](https://leetcode.com/problems/grumpy-bookstore-owner/discuss/299198/C%2B%2B-Linear-Time-(Easy-to-Understand)) +* [Dynamic programming](https://mikecoder.github.io/2019/05/27/GrumpyBookstoreOwner/) + +### C# +* [No explanation, but readable code](https://leetcode.com/problems/grumpy-bookstore-owner/discuss/299469/Sliding-Window-Simple-Solution) + +### Java +* [Sliding window technique](https://leetcode.com/problems/grumpy-bookstore-owner/discuss/299230/Java-Sliding-window.) +* [One pass, sliding window technique](https://leetcode.com/problems/grumpy-bookstore-owner/discuss/299492/Java-one-pass-with-comments-sliding-window) + + +### JavaScript +* [Two pass, sliding window technique](https://leetcode.com/problems/grumpy-bookstore-owner/discuss/314969/JS-Two-Pass-Sliding-Window-O(n)-Time-and-O(1)-Space) +* [98% faster and 100% less memory](https://leetcode.com/problems/grumpy-bookstore-owner/discuss/333527/JavaScript-Solution-(98-faster-and-100-less-memory)) + +### Python +* [Detailed explanation](https://leetcode.com/problems/grumpy-bookstore-owner/discuss/299284/Python-with-explanation.-Rolling-sum.) +* [Sliding window technique](https://leetcode.com/problems/grumpy-bookstore-owner/discuss/317326/python-sliding-window-mask-calculating-delta-of-total.) + +### Ruby +* No solution found on Leetcode. I recommend looking at [cpp solution](https://leetcode.com/problems/grumpy-bookstore-owner/discuss/299237/C%2B%2B-Sliding-Window) or [java solution](https://leetcode.com/problems/grumpy-bookstore-owner/discuss/299230/Java-Sliding-window.) to understand how to solve.