Skip to content

[pull] master from begeekmyfriend:master #4

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

Merged
merged 2 commits into from
Oct 24, 2023
Merged
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
8 changes: 7 additions & 1 deletion 0015_three_sum/three_sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static void two_sum(int *nums, int low, int high, int target, int **results, int
** Return an array of arrays of size *returnSize.
** Note: The returned array must be malloced, assume caller calls free().
**/
int** threeSum(int* nums, int numsSize, int* returnSize)
int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes)
{
if (numsSize < 3) {
return NULL;
Expand All @@ -47,6 +47,12 @@ int** threeSum(int* nums, int numsSize, int* returnSize)
two_sum(nums, i + 1, numsSize - 1, -nums[i], results, returnSize);
}
}

*returnColumnSizes = malloc(*returnSize * sizeof(int *));
for (i = 0; i < *returnSize; i++) {
(*returnColumnSizes)[i] = 3;
}

return results;
}

Expand Down
19 changes: 12 additions & 7 deletions 0018_four_sum/four_sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ static int compare(const void *a, const void *b)
return *(int *) a - *(int *) b;
}

static void k_sum(int *nums, int low, int high, int target, int total, int k,
int *stack, int len, int **results, int *count, int *col_sizes)
static void k_sum(int *nums, int low, int high, long target, int total, int k,
int *stack, int len, int **results, int *count)
{
int i;
if (k == 2) {
Expand All @@ -24,7 +24,6 @@ static void k_sum(int *nums, int low, int high, int target, int total, int k,
stack[len++] = nums[high];
results[*count] = malloc(total * sizeof(int));
memcpy(results[*count], stack, total * sizeof(int));
col_sizes[*count] = total;
(*count)++;
len -= 2;
while (++low < high && nums[low] == nums[low - 1]) {}
Expand All @@ -36,7 +35,8 @@ static void k_sum(int *nums, int low, int high, int target, int total, int k,
for (i = low; i <= high - k + 1; i++) {
if (i > low && nums[i] == nums[i - 1]) continue;
stack[len] = nums[i];
k_sum(nums, i + 1, high, target - nums[i], 4, k - 1, stack, len + 1, results, count, col_sizes);
k_sum(nums, i + 1, high, target - nums[i], 4, k - 1, stack,
len + 1, results, count);
}
}
}
Expand All @@ -49,15 +49,20 @@ static void k_sum(int *nums, int low, int high, int target, int total, int k,
int** fourSum(int* nums, int numsSize, int target, int* returnSize, int** returnColumnSizes)
{
*returnSize = 0;
int i, j, capacity = 50000;
int i, capacity = 50000;
int **results = malloc(capacity * sizeof(int *));
*returnColumnSizes = malloc(capacity * sizeof(int));

if (numsSize >= 4) {
qsort(nums, numsSize, sizeof(*nums), compare);
int *stack = malloc(4 * sizeof(int));
k_sum(nums, 0, numsSize - 1, target, 4, 4, stack, 0, results, returnSize, *returnColumnSizes);
k_sum(nums, 0, numsSize - 1, target, 4, 4, stack, 0, results, returnSize);
}

*returnColumnSizes = malloc(capacity * sizeof(int));
for (i = 0; i < *returnSize; i++) {
(*returnColumnSizes)[i] = 4;
}

return results;
}

Expand Down
8 changes: 2 additions & 6 deletions 0031_next_permutation/next_permutation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@ using namespace std;
class Solution {
public:
void nextPermutation(vector<int>& nums) {
if (nums.size() < 2) {
return;
}

int i = nums.size() - 2;
while (i >= 0 && nums[i] >= nums[i + 1]) {
i--;
}

if (i >= 0) {
int j = nums.size() - 1;
while (j >= 0 && nums[j] >= nums[i]) {
while (j >= 0 && nums[i] >= nums[j]) {
j--;
}
swap(nums.begin() + i, nums.begin() + j);
swap(nums[i], nums[j]);
}

reverse(nums.begin() + i + 1, nums.end());
Expand Down
27 changes: 27 additions & 0 deletions 0042_trapping_rain_water/trap_water.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
#include <stdio.h>
#include <stdlib.h>

#if 0
static int trap(int* height, int heightSize)
{
int i, res = 0;
int *lmax = malloc(heightSize * sizeof(int));
int *rmax = malloc(heightSize * sizeof(int));

lmax[0] = height[0];
rmax[heightSize - 1] = height[heightSize - 1];

for (i = 1; i < heightSize; i++) {
lmax[i] = height[i] > lmax[i - 1] ? height[i] : lmax[i - 1] ;
}

for (i = heightSize - 2; i >= 0; i--) {
rmax[i] = height[i] > rmax[i + 1] ? height[i] : rmax[i + 1] ;
}

for (i = 1; i < heightSize - 1; i++) {
res += (lmax[i] < rmax[i] ? lmax[i] : rmax[i] ) - height[i];
}

return res;
}
#endif

static int trap(int* height, int heightSize)
{
Expand All @@ -12,13 +37,15 @@ static int trap(int* height, int heightSize)
int r = heightSize - 1, rmax = 0;
while (l < r) {
if (height[l] < height[r]) {
/* Only lmax is needed for lmax < rmax here */
if (height[l] > lmax) {
lmax = height[l];
} else {
res += lmax - height[l];
}
l++;
} else {
/* Only rmax is needed for rmax < lmax here */
if (height[r] > rmax) {
rmax = height[r];
} else {
Expand Down
2 changes: 2 additions & 0 deletions 0042_trapping_rain_water/trap_water.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ class Solution {
int right = height.size() - 1, right_max = 0;
while (left < right) {
if (height[left] < height[right]) {
/* Only lmax is needed for lmax < rmax here */
if (height[left] > left_max) {
left_max = height[left];
} else {
res += left_max - height[left];
}
left++;
} else {
/* Only rmax is needed for rmax < lmax here */
if (height[right] > right_max) {
right_max = height[right];
} else {
Expand Down
24 changes: 12 additions & 12 deletions 0084_largest_rectangle_in_histogram/rect_in_histogram.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@

static int largestRectangleArea(int* heights, int heightsSize)
{
int *indexes = malloc(heightsSize * sizeof(int));
int *left = malloc(heightsSize * sizeof(int));
int *right = malloc(heightsSize * sizeof(int));
int *idx_stk = malloc(heightsSize * sizeof(int));
int *lmax = malloc(heightsSize * sizeof(int));
int *rmax = malloc(heightsSize * sizeof(int));

int i, pos = 0;
for (i = 0; i < heightsSize; i++) {
/* monotonous increasing stack */
while (pos > 0 && heights[indexes[pos - 1]] >= heights[i]) {
/* keep monotonous increasing stack */
while (pos > 0 && heights[i] < heights[idx_stk[pos - 1]]) {
pos--;
}
left[i] = pos == 0 ? -1 : indexes[pos - 1];
indexes[pos++] = i;
lmax[i] = pos == 0 ? -1 : idx_stk[pos - 1];
idx_stk[pos++] = i;
}

pos = 0;
for (i = heightsSize - 1; i >= 0; i--) {
/* monotonous increasing stack */
while (pos > 0 && heights[indexes[pos - 1]] >= heights[i]) {
/* keep monotonous increasing stack */
while (pos > 0 && heights[i] < heights[idx_stk[pos - 1]]) {
pos--;
}
right[i] = pos == 0 ? heightsSize : indexes[pos - 1];
indexes[pos++] = i;
rmax[i] = pos == 0 ? heightsSize : idx_stk[pos - 1];
idx_stk[pos++] = i;
}

int max_area = 0;
for (i = 0; i < heightsSize; i++) {
int area = heights[i] * (right[i] - left[i] - 1);
int area = heights[i] * (rmax[i] - lmax[i] - 1);
max_area = area > max_area ? area : max_area;
}

Expand Down
40 changes: 21 additions & 19 deletions 0085_maximal_rectangle/maximal_rectangle.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,49 @@ static inline int max(int a, int b)

static int area_calc(int *heights, int size)
{
int *indexes = malloc(size * sizeof(int));
int *lhist = malloc(size * sizeof(int));
int *rhist = malloc(size * sizeof(int));
int *idx_stk = malloc(size * sizeof(int));
int *lmax = malloc(size * sizeof(int));
int *rmax = malloc(size * sizeof(int));

int i, pos = 0;
for (i = 0; i < size; i++) {
/* squeeze to keep monotonous increasing histograms */
while (pos > 0 && heights[indexes[pos - 1]] >= heights[i]) {
/* keep monotonous increasing maxograms */
while (pos > 0 && heights[i] < heights[idx_stk[pos - 1]]) {
pos--;
}
lhist[i] = pos == 0 ? -1 : indexes[pos - 1];
indexes[pos++] = i;
lmax[i] = pos == 0 ? -1 : idx_stk[pos - 1];
idx_stk[pos++] = i;
}

pos = 0;
for (i = size - 1; i >= 0; i--) {
/* squeeze to keep monotonous increasing histograms */
while (pos > 0 && heights[indexes[pos - 1]] >= heights[i]) {
/* keep monotonous increasing maxograms */
while (pos > 0 && heights[i] < heights[idx_stk[pos - 1]]) {
pos--;
}
rhist[i] = pos == 0 ? size : indexes[pos - 1];
indexes[pos++] = i;
rmax[i] = pos == 0 ? size : idx_stk[pos - 1];
idx_stk[pos++] = i;
}

int max_area = 0;
for (i = 0; i < size; i++) {
int area = heights[i] * (rhist[i] - lhist[i] - 1);
int area = heights[i] * (rmax[i] - lmax[i] - 1);
max_area = max(area, max_area);
}

return max_area;
}

static int maximalRectangle(char** matrix, int matrixRowSize, int matrixColSize)
static int maximalRectangle(char** matrix, int matrixSize, int* matrixColSize)
{
int i, j, max_area = 0;
int *heights = malloc(matrixColSize * sizeof(int));
memset(heights, 0, matrixColSize * sizeof(int));
for (i = 0; i < matrixRowSize; i++) {
for (j = 0; j < matrixColSize; j++) {
int *heights = malloc(matrixColSize[0] * sizeof(int));
memset(heights, 0, matrixColSize[0] * sizeof(int));
for (i = 0; i < matrixSize; i++) {
for (j = 0; j < matrixColSize[i]; j++) {
heights[j] = matrix[i][j] == '1' ? heights[j] + 1 : 0;
}
max_area = max(max_area, area_calc(heights, matrixColSize));
max_area = max(max_area, area_calc(heights, matrixColSize[i]));
}
return max_area;
}
Expand All @@ -68,9 +68,11 @@ int main(int argc, char **argv)
int i, j;
int row_size = argc - 1;
int col_size = strlen(argv[1]);
int *cols = malloc(row_size * sizeof(int));
for (i = 0; i < row_size; i++) {
cols[i] = strlen(argv[1]);
printf("%s\n", argv[i + 1]);
}
printf("%d\n", maximalRectangle(argv + 1, argc - 1, strlen(argv[1])));
printf("%d\n", maximalRectangle(argv + 1, argc - 1, cols));
return 0;
}
8 changes: 4 additions & 4 deletions 0167_two_sum_ii/two_sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ static int* twoSum(int* numbers, int numbersSize, int target, int* returnSize)
{
int i = 0, j = numbersSize - 1;
while (i < j) {
int diff = target - numbers[i] - numbers[j];
if (diff > 0) {
int sum = numbers[i] + numbers[j];
if (sum < target) {
i++;
} else if (diff < 0) {
} else if (sum > target) {
j--;
} else {
*returnSize = 2;
int *indexes = malloc(*returnSize * sizeof(int));
int *indexes = malloc(2 * sizeof(int));
indexes[0] = i + 1;
indexes[1] = j + 1;
return indexes;
Expand Down
6 changes: 3 additions & 3 deletions 0167_two_sum_ii/two_sum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class Solution {
int i = 0;
int j = numbers.size() - 1;
while (i < j) {
int diff = target - numbers[i] - numbers[j];
if (diff > 0) {
int sum = numbers[i] + numbers[j];
if (sum < target) {
i++;
} else if (diff < 0) {
} else if (sum > target) {
j--;
} else {
res.push_back(i + 1);
Expand Down