diff --git a/053_maximum_subarray/max_subarray.c b/053_maximum_subarray/max_subarray.c index 0b36dbc..fe4b3ba 100644 --- a/053_maximum_subarray/max_subarray.c +++ b/053_maximum_subarray/max_subarray.c @@ -2,9 +2,42 @@ #include #include +static int recursive(int *nums, int lo, int hi) +{ + if (lo == hi) { + return nums[lo]; + } + + int ce = (hi - lo) / 2; + int left_max = recursive(nums, lo, lo + ce); + int right_max = recursive(nums, hi - ce, hi); + + int i; + int left_border = 0, left_border_max = INT_MIN; + for (i = ce; i >= lo; i--) { + left_border += nums[i]; + if (left_border > left_border_max) { + left_border_max = left_border; + } + } + + int right_border = 0, right_border_max = INT_MIN; + for (i = ce + 1; i <= hi; i++) { + right_border += nums[i]; + if (right_border > right_border_max) { + right_border_max = right_border; + } + } + + int sum = left_border_max + right_border_max; + int max = left_max > right_max ? left_max : right_max; + return sum > max ? sum : max; +} + static int maxSubArray(int* nums, int numsSize) { - int i, max = INT_MIN, len = 0; +#if 1 + int i, len = 0, max = INT_MIN; for (i = 0; i < numsSize; i++) { len += nums[i]; /* Calculate maximum each time in loop */ @@ -14,6 +47,9 @@ static int maxSubArray(int* nums, int numsSize) } } return max; +#else + return recursive(nums, 0, numsSize - 1); +#endif } int main(int argc, char **argv) diff --git a/098_validate_binary_search_tree/valid_bst.c b/098_validate_binary_search_tree/valid_bst.c index f112d5b..3b90ad7 100644 --- a/098_validate_binary_search_tree/valid_bst.c +++ b/098_validate_binary_search_tree/valid_bst.c @@ -1,6 +1,7 @@ #include #include #include +#include struct TreeNode { int val; @@ -8,32 +9,33 @@ struct TreeNode { struct TreeNode *right; }; -static int last_val = -1; - -static bool traverse(struct TreeNode* node) +static bool traverse(struct TreeNode* node, int *last_val) { bool ret = true; if (ret && node->left != NULL) { - ret = traverse(node->left); + ret = traverse(node->left, last_val); } - if (last_val != -1 && node->val <= last_val) { + if (node->val <= *last_val) { return false; } - last_val = node->val; + *last_val = node->val; if (ret && node->right != NULL) { - ret = traverse(node->right); + ret = traverse(node->right, last_val); } return ret; } static bool isValidBST(struct TreeNode* root) { - if (root == NULL) return true; - last_val = -1; - return traverse(root); + if (root == NULL) { + return true; + } else { + int last_val = -1; + return traverse(root, &last_val); + } } int main(int argc, char **argv) diff --git a/102_binary_tree_level_order_traversal/bst_bfs.c b/102_binary_tree_level_order_traversal/bst_bfs.c index ba1664a..af2f81c 100644 --- a/102_binary_tree_level_order_traversal/bst_bfs.c +++ b/102_binary_tree_level_order_traversal/bst_bfs.c @@ -29,20 +29,17 @@ struct list_head { struct list_head *next, *prev; }; -static inline void -INIT_LIST_HEAD(struct list_head *list) +static inline void INIT_LIST_HEAD(struct list_head *list) { list->next = list->prev = list; } -static inline int -list_empty(const struct list_head *head) +static inline int list_empty(const struct list_head *head) { return (head->next == head); } -static inline void -__list_add(struct list_head *new, struct list_head *prev, struct list_head *next) +static inline void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next) { next->prev = new; new->next = next; @@ -50,27 +47,23 @@ __list_add(struct list_head *new, struct list_head *prev, struct list_head *next prev->next = new; } -static inline void -list_add(struct list_head *_new, struct list_head *head) +static inline void list_add(struct list_head *_new, struct list_head *head) { __list_add(_new, head, head->next); } -static inline void -list_add_tail(struct list_head *_new, struct list_head *head) +static inline void list_add_tail(struct list_head *_new, struct list_head *head) { __list_add(_new, head->prev, head); } -static inline void -__list_del(struct list_head *entry) +static inline void __list_del(struct list_head *entry) { entry->next->prev = entry->prev; entry->prev->next = entry->next; } -static inline void -list_del(struct list_head *entry) +static inline void list_del(struct list_head *entry) { __list_del(entry); entry->next = entry->prev = NULL; @@ -135,41 +128,30 @@ static int** levelOrder(struct TreeNode* root, int** columnSizes, int* returnSiz } struct list_head free_list; - struct list_head bfs_queue0; - struct list_head bfs_queue1; + struct list_head q0; + struct list_head q1; INIT_LIST_HEAD(&free_list); - INIT_LIST_HEAD(&bfs_queue0); - INIT_LIST_HEAD(&bfs_queue1); + INIT_LIST_HEAD(&q0); + INIT_LIST_HEAD(&q1); int **results = malloc(BST_MAX_LEVEL * sizeof(int *)); *columnSizes = malloc(BST_MAX_LEVEL * sizeof(int)); memset(*columnSizes, 0, BST_MAX_LEVEL * sizeof(int)); int level = 0; - struct bfs_node *new; - if (root->left != NULL) { - new = node_new(&free_list, root->left); - list_add_tail(&new->link, &bfs_queue0); - } - - if (root->right != NULL) { - new = node_new(&free_list, root->right); - list_add_tail(&new->link, &bfs_queue0); - } - - results[level] = malloc(sizeof(int)); - results[level][0] = root->val; - (*columnSizes)[level] = 1; + struct bfs_node *new = node_new(&free_list, root); + list_add_tail(&new->link, &q0); - while (!list_empty(&bfs_queue0) || !list_empty(&bfs_queue1)) { - if (++level & 1) { - queue(&bfs_queue0, &bfs_queue1, &free_list, results, *columnSizes, level); + while (!list_empty(&q0) || !list_empty(&q1)) { + if (level & 0x1) { + queue(&q1, &q0, &free_list, results, *columnSizes, level); } else { - queue(&bfs_queue1, &bfs_queue0, &free_list, results, *columnSizes, level); + queue(&q0, &q1, &free_list, results, *columnSizes, level); } + level++; } - *returnSize = level + 1; + *returnSize = level; return results; }