Skip to content

Commit

Permalink
feat: solve problem 152. Maximum Product Subarray in C (#1837)
Browse files Browse the repository at this point in the history
* test(problem-152): add support to test in C

* feat(problem-152/c): solve by using Kadane's algorithm
  • Loading branch information
threeal committed Sep 4, 2024
1 parent b549a52 commit 0600194
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ LeetSpace serves as a dedicated workspace and archive for my [LeetCode](https://
| [145. Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/) | Easy | [C++](./old-problems/0145/solution.cpp) |
| [150. Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | Medium | [C](./old-problems/0150/c/solution.c) [C++](./old-problems/0150/solution.cpp) |
| [151. Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | Medium | [C](./old-problems/0151/c/solution.c) [C++](./old-problems/0151/solution.cpp) |
| [152. Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) | Medium | [C++](./old-problems/0152/solution.cpp) |
| [152. Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) | Medium | [C](./old-problems/0152/c/solution.c) [C++](./old-problems/0152/solution.cpp) |
| [165. Compare Version Numbers](https://leetcode.com/problems/compare-version-numbers/) | Medium | [C](./old-problems/0165/c/solution.c) [C++](./old-problems/0165/solution.cpp) |
| [169. Majority Element](https://leetcode.com/problems/majority-element/) | Easy | [C](./old-problems/0169/c/solution.c) [C++](./old-problems/0169/solution.cpp) |
| [191. Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/) | Easy | [C](./old-problems/0191/c/solution.c) [C++](./old-problems/0191/solution.cpp) |
Expand Down
3 changes: 3 additions & 0 deletions old-problems/0152/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
add_c_solution(c_solution c/interface.cpp c/solution.c)

get_dir_name(id)
add_problem_test(test-${id} test.yaml)
target_link_libraries(test-${id} PRIVATE ${c_solution})
9 changes: 9 additions & 0 deletions old-problems/0152/c/interface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <vector>

extern "C" {
int maxProduct(int* nums, int numsSize);
}

int solution_c(std::vector<int> nums) {
return maxProduct(nums.data(), nums.size());
}
18 changes: 18 additions & 0 deletions old-problems/0152/c/solution.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <limits.h>

int maxProduct(int* nums, int numsSize) {
int max = INT_MIN;

int left = 1, right = 1;
for (int l = 0, r = numsSize - 1; r >= 0; ++l, --r) {
if (left == 0) left = 1;
left *= nums[l];
if (left > max) max = left;

if (right == 0) right = 1;
right *= nums[r];
if (right > max) max = right;
}

return max;
}
1 change: 1 addition & 0 deletions old-problems/0152/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ types:
output: int

solutions:
c:
cpp:
function: maxProduct

Expand Down

0 comments on commit 0600194

Please sign in to comment.