diff options
-rw-r--r-- | find_pos_int_soln_given_eq.c | 59 | ||||
-rw-r--r-- | find_pos_int_soln_given_eq.py | 42 | ||||
-rw-r--r-- | keyboard_row.c | 50 | ||||
-rw-r--r-- | keyboard_row.py | 32 | ||||
-rw-r--r-- | sudoku_solver.c | 69 | ||||
-rw-r--r-- | sudoku_solver.py | 66 | ||||
-rw-r--r-- | valid_sudoku.c | 55 | ||||
-rw-r--r-- | valid_sudoku.py | 59 |
8 files changed, 432 insertions, 0 deletions
diff --git a/find_pos_int_soln_given_eq.c b/find_pos_int_soln_given_eq.c new file mode 100644 index 0000000..b17ecf6 --- /dev/null +++ b/find_pos_int_soln_given_eq.c @@ -0,0 +1,59 @@ +// 1237. Find Positive Integer Solution for a Given Equation +#include "leetcode.h" + +/* + * given a callable function 'f(x, y)' with a hidden formula and a value 'z', + * reverse engineer the formula and return all positive integer pairs 'x' and + * 'y' where 'f(x, y) == z'. you may return the pairs in any order. while the + * exact formula is hidden, the function is monotonically increasing. + */ + +int function_id = 0; + +int customFunction(int x, int y) { + switch (function_id) { + case 1: + return x + y; + case 2: + return x * y; + } +} + +int **findSolution(int (*customFunction)(int, int), int z, int *returnSize, + int **returnColumnSizes) { + int **ans = (int **)malloc(z * sizeof(int *)), k = 0; + int *cols = *returnColumnSizes = (int *)malloc(z * sizeof(int)); + for (int i = 1; i <= z; i++) + for (int j = z; j >= 1; j--) + if (customFunction(i, j) == z) { + (ans[k] = (int *)malloc(sizeof(int[cols[k] = 2])))[0] = i; + ans[k++][1] = j; + } + *returnSize = k; + return ans; +} + +int main() { + function_id = 1; + int rs1, *rcs1, **fs1 = findSolution(customFunction, 5, &rs1, &rcs1); + for (int i = 0; i < rs1; i++) { + for (int j = 0; j < rcs1[i]; j++) + printf("%d ", fs1[i][j]); // expect: [[1,4],[2,3],[3,2],[4,1]] + printf("\n"); + } + function_id = 2; + int rs2, *rcs2, **fs2 = findSolution(customFunction, 5, &rs2, &rcs2); + for (int i = 0; i < rs2; i++) { + for (int j = 0; j < rcs2[i]; j++) + printf("%d ", fs2[i][j]); // expect: [[1,5],[5,1]] + printf("\n"); + } + for (int i = 0; i < rs1; i++) + free(fs1[i]); + free(rcs1); + free(fs1); + for (int i = 0; i < rs2; i++) + free(fs2[i]); + free(rcs2); + free(fs2); +} diff --git a/find_pos_int_soln_given_eq.py b/find_pos_int_soln_given_eq.py new file mode 100644 index 0000000..5474513 --- /dev/null +++ b/find_pos_int_soln_given_eq.py @@ -0,0 +1,42 @@ +# 1237. Find Positive Integer Solution for a Given Equation + +""" +given a callable function 'f(x, y)' with a hidden formula and a value 'z', +reverse engineer the formula and return all positive integer pairs 'x' and +'y' where 'f(x, y) == z'. you may return the pairs in any order. while the +exact formula is hidden, the function is monotonically increasing. +""" + +function_id = 0 + + +class customfunction(object): + def f(self, x, y): + if function_id == 1: + return x + y + if function_id == 2: + return x * y + + +class Solution(object): + def findSolution(self, customfunction, z): + """ + :type num: int + :type z: int + :rtype: List[List[int]] + """ + ans, y = [], 1000 + for x in range(1, 1001): + while y > 1 and customfunction.f(x, y) > z: + y -= 1 + if customfunction.f(x, y) == z: + ans.append([x, y]) + return ans + + +if __name__ == "__main__": + obj = Solution() + function_id = 1 + print(obj.findSolution(customfunction, 5)) + function_id = 2 + print(obj.findSolution(customfunction, 5)) diff --git a/keyboard_row.c b/keyboard_row.c new file mode 100644 index 0000000..b01224e --- /dev/null +++ b/keyboard_row.c @@ -0,0 +1,50 @@ +// 500. Keyboard Row +#include "leetcode.h" + +/* + * given an array of strings 'words', return the words that can be typed using + * letters of the alphabet on only one row of american keyboard like the image + * below. note that the strings are case insensitive, both lowercased and + * uppercased of the same letter are treated as if they are at the same row. + */ + +char **findWords(char **words, int wordsSize, int *returnSize) { + char **ans = (char **)malloc(wordsSize * sizeof(char *)); + int idx = 0, lut[26] = {2, 3, 3, 2, 1, 2, 2, 2, 1, 2, 2, 2, 3, + 3, 1, 1, 1, 1, 2, 1, 1, 3, 1, 3, 1, 3}; + bool same_line = true; + for (int i = 0; i < wordsSize; i++) { + same_line = true; + for (int j = 0; j < strlen(words[i]); j++) { + if (lut[(words[i][j] - 'A') % 32] != lut[(words[i][j + 1] - 'A') % 32]) { + same_line = false; + break; + } + } + if (same_line) + ans[idx++] = words[i]; + } + *returnSize = idx; + return ans; +} + +int main() { + char *w1[] = {"Hello", "Alaska", "Dad", "Peace"}, *w2[] = {"omk"}, + *w3[] = {"adsdf", "sfd"}; + int rs1, rs2, rs3; + char **fw1 = findWords((char **)w1, ARRAY_SIZE(w1), &rs1); + char **fw2 = findWords((char **)w2, ARRAY_SIZE(w2), &rs2); + char **fw3 = findWords((char **)w3, ARRAY_SIZE(w3), &rs3); + for (int i = 0; i < rs1; i++) + printf("%s ", fw1[i]); // expect: Alaska Dad + printf("\n"); + for (int i = 0; i < rs2; i++) + printf("%s ", fw2[i]); // expect: omk + printf("\n"); + for (int i = 0; i < rs3; i++) + printf("%s ", fw3[i]); // expect: adsdf sfd + printf("\n"); + free(fw1); + free(fw2); + free(fw3); +} diff --git a/keyboard_row.py b/keyboard_row.py new file mode 100644 index 0000000..e4e92d8 --- /dev/null +++ b/keyboard_row.py @@ -0,0 +1,32 @@ +# 500. Keyboard Row + +""" +given an array of strings 'words', return the words that can be typed using +letters of the alphabet on only one row of american keyboard like the image +below. note that the strings are case insensitive, both lowercased and +uppercased of the same letter are treated as if they are at the same row. +""" + + +class Solution(object): + def findWords(self, words): + """ + :type words: List[str] + :rtype: List[str] + """ + l1 = set("qwertyuiop") + l2 = set("asdfghjkl") + l3 = set("zxcvbnm") + ans = [] + for i in words: + curr = set(i.lower()) + if curr <= l1 or curr <= l2 or curr <= l3: + ans.append(i) + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.findWords(words=["Hello", "Alaska", "Dad", "Peace"])) + print(obj.findWords(words=["omk"])) + print(obj.findWords(words=["adsdf", "sfd"])) diff --git a/sudoku_solver.c b/sudoku_solver.c new file mode 100644 index 0000000..88ccc01 --- /dev/null +++ b/sudoku_solver.c @@ -0,0 +1,69 @@ +// 37. Sudoku Solver +#include "leetcode.h" + +/* + * write a program to solve a sudoku puzzle by filling the empty cells. a sudoku + * solution must satisfy all of the following rules. each of the digits '1-9' + * must occur exactly once in each row, each of the digits '1-9', must occur + * exactly once in each column, and each of the digits '1-9' must occur exactly + * once in each of the 9 3x3 sub-boxes of the grid. the '.' character indicates + * empty cells. + */ + +bool solve(char **board, bool row[9][9], bool col[9][9], bool grid[9][9], int r, + int c) { + if (r == 9) + return true; + int next_r = c == 8 ? r + 1 : r; + int next_c = c == 8 ? 0 : c + 1; + if (board[r][c] != '.') + return solve(board, row, col, grid, next_r, next_c); + int grid_idx = (r / 3) * 3 + (c / 3); + for (int i = 0; i < 9; i++) { + if (!row[r][i] && !col[c][i] && !grid[grid_idx][i]) { + board[r][c] = '1' + i; + row[r][i] = col[c][i] = grid[grid_idx][i] = true; + if (solve(board, row, col, grid, next_r, next_c)) + return true; + board[r][c] = '.'; + row[r][i] = col[c][i] = grid[grid_idx][i] = false; + } + } + return false; +} + +void solveSudoku(char **board, int boardSize, int *boardColSize) { + if (boardSize != 9 || *boardColSize != 9) + return; + bool row[9][9] = {false}; + bool col[9][9] = {false}; + bool grid[9][9] = {false}; + for (int i = 0; i < 9; i++) + for (int j = 0; j < 9; j++) + if (board[i][j] != '.') { + int num = board[i][j] - '1'; + if (num < 0 || num >= 9) + return; + row[i][num] = col[j][num] = true; + grid[(i / 3) * 3 + (j / 3)][num] = true; + } + solve(board, row, col, grid, 0, 0); +} + +int main() { + char *board[] = {{"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"}}; + solveSudoku((char **)board, ARRAY_SIZE(board), ARRAY_SIZE(board[0])); + for (int i = 0; i < ARRAY_SIZE(board); i++) { + for (int j = 0; j < ARRAY_SIZE(board[0]); j++) + printf("%c ", board[i][j]); + printf("\n"); + } +} diff --git a/sudoku_solver.py b/sudoku_solver.py new file mode 100644 index 0000000..7a395e7 --- /dev/null +++ b/sudoku_solver.py @@ -0,0 +1,66 @@ +# 37. Sudoku Solver + +""" +write a program to solve a sudoku puzzle by filling the empty cells. a sudoku +solution must satisfy all of the following rules. each of the digits '1-9' +must occur exactly once in each row, each of the digits '1-9', must occur +exactly once in each column, and each of the digits '1-9' must occur exactly +once in each of the 9 3x3 sub-boxes of the grid. the '.' character indicates +empty cells. +""" + + +class Solution(object): + def solveSudoku(self, board): + """ + :type board: List[List[str]] + :rtype: None Do not return anything, modify board in-place instead. + """ + n = 9 + + def valid(row, col, idx): + row, col = int(row), int(col) + for i in range(9): + if board[i][col] == idx: + return False + if board[row][i] == idx: + return False + if board[3 * (row // 3) + i // 3][3 * (col // 3) + i % 3] == idx: + return False + return True + + def solve(row, col): + if row == n: + return True + if col == n: + return solve(row + 1, 0) + if board[row][col] == ".": + for i in range(1, 10): + if valid(row, col, str(i)): + board[row][col] = str(i) + if solve(row, col + 1): + return True + else: + board[row][col] = "." + return False + else: + return solve(row, col + 1) + + solve(0, 0) + + +if __name__ == "__main__": + obj = Solution() + board = [ + ["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"], + ] + obj.solveSudoku(board) + print(board) diff --git a/valid_sudoku.c b/valid_sudoku.c new file mode 100644 index 0000000..dd93f60 --- /dev/null +++ b/valid_sudoku.c @@ -0,0 +1,55 @@ +// 36. Valid Sudoku +#include "leetcode.h" + +/* + * determine if a 9 x 9 sudoku board is valid. only the filled cells need to be + * validated acording to the following rules. each row must contain the digits + * 1-9 without repetition, each column must contain the digits 1-9 without + * reptition, each of the nine 3 x 3 sub-boxes of the grid must contain the + * digits 1-9 without reptition. note, a sudoku board (partially filled) could + * be valid but it is not necessarily solvable. only the filled cells need to be + * validated according to the mentioned rules. + */ + +bool isValidSudoku(char **board, int boardSize, int *boardColSize) { + bool rows[9][9] = {false}; + bool cols[9][9] = {false}; + bool boxes[9][9] = {false}; + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 9; j++) { + if (board[i][j] != '.') { + int num = board[i][j] - '1'; + int box_idx = (i / 3) * 3 * (j / 3); + if (rows[i][num] || cols[j][num] || boxes[box_idx][num]) + return false; + rows[i][num] = cols[j][num] = boxes[box_idx][num] = true; + } + } + } + return true; +} + +int main() { + char *b1[] = {{"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"}}; + char *b2[] = {{"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"}}; + printf("%d\n", + isValidSudoku(b1, ARRAY_SIZE(b1), ARRAY_SIZE(b1[0]))); // expect: 1 + printf("%d\n", + isValidSudoku(b2, ARRAY_SIZE(b2), ARRAY_SIZE(b2[0]))); // expect: 0 +} diff --git a/valid_sudoku.py b/valid_sudoku.py new file mode 100644 index 0000000..8dd758a --- /dev/null +++ b/valid_sudoku.py @@ -0,0 +1,59 @@ +# 36. Valid Sudoku + +""" +determine if a 9 x 9 sudoku board is valid. only the filled cells need to be +validated acording to the following rules. each row must contain the digits +1-9 without repetition, each column must contain the digits 1-9 without +reptition, each of the nine 3 x 3 sub-boxes of the grid must contain the +digits 1-9 without reptition. note, a sudoku board (partially filled) could +be valid but it is not necessarily solvable. only the filled cells need to be +validated according to the mentioned rules. +""" + + +class Solution(object): + def isValidSudoku(self, board): + """ + :type board: List[List[str]] + :rtype: bool + """ + rows = [[False] * 9 for _ in range(9)] + cols = [[False] * 9 for _ in range(9)] + boxes = [[False] * 9 for _ in range(9)] + for i in range(9): + for j in range(9): + if board[i][j] != ".": + num = ord(board[i][j] - ord("1")) + box_idx = (i // 3) * 3 + (j // 3) + if rows[i][num] or cols[j][num] or boxes[box_idx][num]: + return False + rows[i][num] = cols[j][num] = boxes[box_idx][num] = True + return True + + +if __name__ == "__main__": + obj = Solution() + b1 = [ + ["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"], + ] + b2 = [ + ["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"], + ] + print(obj.isValidSudoku(b1)) + print(obj.isValidSudoku(b2)) |