Skip to content

Commit

Permalink
Improvement
Browse files Browse the repository at this point in the history
Signed-off-by: begeekmyfriend <[email protected]>
  • Loading branch information
begeekmyfriend committed Oct 20, 2017
1 parent adf424d commit abcf598
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 48 deletions.
38 changes: 37 additions & 1 deletion 053_maximum_subarray/max_subarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,42 @@
#include <stdlib.h>
#include <limits.h>

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 */
Expand All @@ -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)
Expand Down
22 changes: 12 additions & 10 deletions 098_validate_binary_search_tree/valid_bst.c
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <limits.h>

struct TreeNode {
int val;
struct TreeNode *left;
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)
Expand Down
56 changes: 19 additions & 37 deletions 102_binary_tree_level_order_traversal/bst_bfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,48 +29,41 @@ 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;
new->prev = prev;
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;
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit abcf598

Please sign in to comment.