Skip to content
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

Khandice Schuhmann #2

Open
wants to merge 4 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
111 changes: 111 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,114 @@ npm test
```

Feel free to add additional tests and point out if you think they should be in the final project submission.


# Hash Table Practice

In this exercise you will complete two coding problems using Hash Tables.

## Learning Goals

By the end of this exercise you should be able to:

- Use a hash table to solve a coding problem
- Identify how a hash table can produce a more attractive runtime over alternative solutions

## Grouped Anagrams

Given an array of strings, group anagrams together.

### Example

```
Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
```

Note:

- All inputs will be in lowercase.
- The order of your output does not matter

## Most Frequent K Elements

Given a non-empty array of integers, return the *k* most frequent elements.

## Example 1

```
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
```

## Example 2

```
Input: nums = [1], k = 1
Output: [1]
```

## Note

You may assume k is always valid, 1 ≤ k ≤ number of unique elements.

You should be able to equal or beat O(n log n), where n is the array's size.


## Optional: Valid Sudoku

Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

1. Each row must contain the digits 1-9 without repetition.
1. Each column must contain the digits 1-9 without repetition.
1. Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.

![sudoku image](images/250px-Sudoku-by-L2G-20050714.svg.png)

Above is a valid Sudoku grid.

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.

**Example 1:**

```
Input:
[
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
Output: true
```


**Example 2:**

```
Input:
[
["8","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being
modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
```
44 changes: 34 additions & 10 deletions lib/exercises.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,57 @@
// This method will return an array of arrays.
// Each subarray will have strings which are anagrams of each other
// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is O(n) time complexity, where n is the number of words. Yes, querying and updating anagramDict is O(1), but we have to do it n times.

// Space Complexity: O(n)
function groupedAnagrams(strings) {
throw new Error("Method hasn't been implemented yet!");
anagramDict = {};
anagramsArray = [];

for (let word of strings) {
let temp = word.split("").sort().join("");
if (anagramDict.hasOwnProperty(temp)) {
anagramsArray[anagramDict[temp]].push(word);
} else {
anagramDict[temp] = anagramsArray.length;
anagramsArray[anagramDict[temp]] = [word];
}
}

return anagramsArray;
}

// This method will return the k most common elements
// in the case of a tie it will select the first occuring element.
// Time Complexity: ?
// Space Complexity: ?
// Time Complexity: O(n)
// Space Complexity: O(n)
function topKFrequentElements(list, k) {
throw new Error("Method hasn't been implemented yet!");
}
let frequency = {};

for (let num of list) {
if (num in frequency) {
frequency[num] += 1;
} else {
frequency[num] = 1;
}
}

const freqArray = Object.entries(frequency).sort((a, b) => b[1] - a[1]);
const sortArray = freqArray.map((element) => parseInt(element[0]));
return sortArray.slice(0, k);
}

// This method will return true if the table is still
// a valid sudoku table.
// Each element can either be a ".", or a digit 1-9
// The same digit cannot appear twice or more in the same
// The same digit cannot appear twice or more in the same
// row, column or 3x3 subgrid
// Time Complexity: ?
// Space Complexity: ?
function validSudoku(table) {
throw new Error("Method hasn't been implemented yet!");
throw new Error("Method hasn't been implemented yet!");
}

module.exports = {
groupedAnagrams,
topKFrequentElements,
validSudoku
validSudoku,
};
Loading