Skip to content

Added solutions for July 16th meetup. #200

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 3 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
24 changes: 24 additions & 0 deletions challenges/UglyNumber/solutions/UglyNumberSolutionsGuide.md
Original file line number Diff line number Diff line change
@@ -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)

25 changes: 25 additions & 0 deletions challenges/grumpyBookstoreOwner/solutions/GrumpySolutionsGuide.md
Original file line number Diff line number Diff line change
@@ -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.
37 changes: 37 additions & 0 deletions challenges/wordSearch/solutions/wordSearch.cpp
Original file line number Diff line number Diff line change
@@ -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<vector<char>>& 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<vector<char>>& board, string word) {
int M,N,i,j,sLen = word.size();
if( (M=board.size()) && (N=board[0].size()) && sLen)
{
for(i=0; i<M; ++i)
for(j=0; j<N; ++j)
if(dfs(board, i, j, word, 0, M, N, sLen)) return true;
}
return false;
}
};
29 changes: 29 additions & 0 deletions challenges/wordSearch/solutions/wordSearch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Please check link for explanation of code below
// https://leetcode.com/problems/word-search/discuss/27658/Accepted-very-short-Java-solution.-No-additional-space.

// Additional solutions
// Straightforward - https://leetcode.com/problems/word-search/discuss/27811/My-Java-solution
// DFS approach - https://leetcode.com/problems/word-search/discuss/27765/Java-DFS-solution-beats-97.64

public boolean exist(char[][] board, String word) {
char[] w = word.toCharArray();
for (int y=0; y<board.length; y++) {
for (int x=0; x<board[y].length; x++) {
if (exist(board, y, x, w, 0)) return true;
}
}
return false;
}

private boolean exist(char[][] board, int y, int x, char[] word, int i) {
if (i == word.length) return true;
if (y<0 || x<0 || y == board.length || x == board[y].length) return false;
if (board[y][x] != word[i]) return false;
board[y][x] ^= 256;
boolean exist = exist(board, y, x+1, word, i+1)
|| exist(board, y, x-1, word, i+1)
|| exist(board, y+1, x, word, i+1)
|| exist(board, y-1, x, word, i+1);
board[y][x] ^= 256;
return exist;
}
63 changes: 63 additions & 0 deletions challenges/wordSearch/solutions/wordSearch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Please check link for explanation of code below
// https://leetcode.com/problems/word-search/discuss/121396/Javascript-solution

// Additional solutions
// Backtracking Approach - https://leetcode.com/problems/word-search/discuss/219666/Javascript-Using-Backtracking
// Clean Solution - https://leetcode.com/problems/word-search/discuss/133078/Clean-JavaScript-solution
// Well Commented - https://leetcode.com/problems/word-search/discuss/27767/Javascript-solution-well-commented!

var exist = function(board, word) {
// Initial validation check
if (board.length === 0) return false;
if (word.length === 0) return false;

// Flag to track if it was found
let found = false;
// Set to track the cells already visited
let visited = new Set();

/* Recursive method to check the entries */
function find(prefix, row, col) {
const char = board[row][col];
// If it already found quit to prevent unnecessary work
if (found) return;
// If the current sequence doesn't match with the beginning of the word quit
if (prefix !== word.substr(0, prefix.length)) return;
// If this cell has already being visited quit
if (visited.has(`${row}-${col}`)) return;

// Adds to the current sequence and checks if it is the word we are looking for
const current = prefix + char;
if (current === word) {
found = true;
return;
}

// Flag the current cell so its not used twice
visited.add(`${row}-${col}`);

// Searches in all the directions
const existsUp = (row > 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;
};
30 changes: 30 additions & 0 deletions challenges/wordSearch/solutions/wordSearch.py
Original file line number Diff line number Diff line change
@@ -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