Skip to content

Commit b2fe2ea

Browse files
committed
Added tasks 148-239
1 parent 17d1e76 commit b2fe2ea

File tree

21 files changed

+1866
-0
lines changed

21 files changed

+1866
-0
lines changed

Diff for: README.md

+65
Large diffs are not rendered by default.

Diff for: src/main/c/g0101_0200/s0148_sort_list/readme.md

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-C/LeetCode-in-C?label=Stars&style=flat-square)](https://github.com/LeetCode-in-C/LeetCode-in-C)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-C/LeetCode-in-C?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-C/LeetCode-in-C/fork)
3+
4+
## 148\. Sort List
5+
6+
Medium
7+
8+
Given the `head` of a linked list, return _the list after sorting it in **ascending order**_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg)
13+
14+
**Input:** head = [4,2,1,3]
15+
16+
**Output:** [1,2,3,4]
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2020/09/14/sort_list_2.jpg)
21+
22+
**Input:** head = [-1,5,3,4,0]
23+
24+
**Output:** [-1,0,3,4,5]
25+
26+
**Example 3:**
27+
28+
**Input:** head = []
29+
30+
**Output:** []
31+
32+
**Constraints:**
33+
34+
* The number of nodes in the list is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.
35+
* <code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code>
36+
37+
**Follow up:** Can you sort the linked list in `O(n logn)` time and `O(1)` memory (i.e. constant space)?
38+
39+
## Solution
40+
41+
```c
42+
#include <stdio.h>
43+
#include <stdlib.h>
44+
45+
/**
46+
* Definition for singly-linked list.
47+
* struct ListNode {
48+
* int val;
49+
* struct ListNode *next;
50+
* };
51+
*/
52+
// Helper function to create a new ListNode
53+
struct ListNode* createNode(int val) {
54+
struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
55+
node->val = val;
56+
node->next = NULL;
57+
return node;
58+
}
59+
60+
// Helper function to merge two sorted linked lists
61+
struct ListNode* merge(struct ListNode* l1, struct ListNode* l2) {
62+
struct ListNode dummy;
63+
struct ListNode* tail = &dummy;
64+
dummy.next = NULL;
65+
66+
while (l1 != NULL && l2 != NULL) {
67+
if (l1->val <= l2->val) {
68+
tail->next = l1;
69+
l1 = l1->next;
70+
} else {
71+
tail->next = l2;
72+
l2 = l2->next;
73+
}
74+
tail = tail->next;
75+
}
76+
77+
if (l1 != NULL) tail->next = l1;
78+
if (l2 != NULL) tail->next = l2;
79+
80+
return dummy.next;
81+
}
82+
83+
// Function to sort the linked list using merge sort
84+
struct ListNode* sortList(struct ListNode* head) {
85+
if (head == NULL || head->next == NULL) {
86+
return head;
87+
}
88+
89+
// Split the list into two halves
90+
struct ListNode *slow = head, *fast = head, *pre = NULL;
91+
while (fast != NULL && fast->next != NULL) {
92+
pre = slow;
93+
slow = slow->next;
94+
fast = fast->next->next;
95+
}
96+
pre->next = NULL; // Break the list into two halves
97+
98+
// Recursively sort both halves
99+
struct ListNode* left = sortList(head);
100+
struct ListNode* right = sortList(slow);
101+
102+
// Merge the sorted halves
103+
return merge(left, right);
104+
}
105+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-C/LeetCode-in-C?label=Stars&style=flat-square)](https://github.com/LeetCode-in-C/LeetCode-in-C)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-C/LeetCode-in-C?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-C/LeetCode-in-C/fork)
3+
4+
## 152\. Maximum Product Subarray
5+
6+
Medium
7+
8+
Given an integer array `nums`, find a contiguous non-empty subarray within the array that has the largest product, and return _the product_.
9+
10+
The test cases are generated so that the answer will fit in a **32-bit** integer.
11+
12+
A **subarray** is a contiguous subsequence of the array.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [2,3,-2,4]
17+
18+
**Output:** 6
19+
20+
**Explanation:** [2,3] has the largest product 6.
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [-2,0,-1]
25+
26+
**Output:** 0
27+
28+
**Explanation:** The result cannot be 2, because [-2,-1] is not a subarray.
29+
30+
**Constraints:**
31+
32+
* <code>1 <= nums.length <= 2 * 10<sup>4</sup></code>
33+
* `-10 <= nums[i] <= 10`
34+
* The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer.
35+
36+
## Solution
37+
38+
```c
39+
#include <stdio.h>
40+
#include <limits.h>
41+
42+
// Function to get the maximum of two integers
43+
int max(int a, int b) {
44+
return (a > b) ? a : b;
45+
}
46+
47+
// Function to get the minimum of two integers
48+
int min(int a, int b) {
49+
return (a < b) ? a : b;
50+
}
51+
52+
// Function to find the maximum product subarray
53+
int maxProduct(int* nums, int numsSize) {
54+
if (numsSize == 0) return 0;
55+
56+
int currentMaxProd = nums[0];
57+
int currentMinProd = nums[0];
58+
int overAllMaxProd = nums[0];
59+
60+
for (int i = 1; i < numsSize; i++) {
61+
if (nums[i] < 0) {
62+
// Swap currentMaxProd and currentMinProd
63+
int temp = currentMaxProd;
64+
currentMaxProd = currentMinProd;
65+
currentMinProd = temp;
66+
}
67+
68+
// Update the current maximum and minimum products
69+
currentMaxProd = max(nums[i], nums[i] * currentMaxProd);
70+
currentMinProd = min(nums[i], nums[i] * currentMinProd);
71+
72+
// Update the overall maximum product
73+
overAllMaxProd = max(overAllMaxProd, currentMaxProd);
74+
}
75+
76+
return overAllMaxProd;
77+
}
78+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-C/LeetCode-in-C?label=Stars&style=flat-square)](https://github.com/LeetCode-in-C/LeetCode-in-C)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-C/LeetCode-in-C?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-C/LeetCode-in-C/fork)
3+
4+
## 153\. Find Minimum in Rotated Sorted Array
5+
6+
Medium
7+
8+
Suppose an array of length `n` sorted in ascending order is **rotated** between `1` and `n` times. For example, the array `nums = [0,1,2,4,5,6,7]` might become:
9+
10+
* `[4,5,6,7,0,1,2]` if it was rotated `4` times.
11+
* `[0,1,2,4,5,6,7]` if it was rotated `7` times.
12+
13+
Notice that **rotating** an array `[a[0], a[1], a[2], ..., a[n-1]]` 1 time results in the array `[a[n-1], a[0], a[1], a[2], ..., a[n-2]]`.
14+
15+
Given the sorted rotated array `nums` of **unique** elements, return _the minimum element of this array_.
16+
17+
You must write an algorithm that runs in `O(log n) time.`
18+
19+
**Example 1:**
20+
21+
**Input:** nums = [3,4,5,1,2]
22+
23+
**Output:** 1
24+
25+
**Explanation:** The original array was [1,2,3,4,5] rotated 3 times.
26+
27+
**Example 2:**
28+
29+
**Input:** nums = [4,5,6,7,0,1,2]
30+
31+
**Output:** 0
32+
33+
**Explanation:** The original array was [0,1,2,4,5,6,7] and it was rotated 4 times.
34+
35+
**Example 3:**
36+
37+
**Input:** nums = [11,13,15,17]
38+
39+
**Output:** 11
40+
41+
**Explanation:** The original array was [11,13,15,17] and it was rotated 4 times.
42+
43+
**Constraints:**
44+
45+
* `n == nums.length`
46+
* `1 <= n <= 5000`
47+
* `-5000 <= nums[i] <= 5000`
48+
* All the integers of `nums` are **unique**.
49+
* `nums` is sorted and rotated between `1` and `n` times.
50+
51+
## Solution
52+
53+
```c
54+
#include <stdio.h>
55+
56+
// Helper function to find the minimum element in a rotated sorted array
57+
int findMinUtil(int* nums, int l, int r) {
58+
if (l == r) {
59+
return nums[l];
60+
}
61+
62+
int mid = (l + r) / 2;
63+
64+
// If array is not rotated or only two elements left
65+
if (mid == l && nums[mid] < nums[r]) {
66+
return nums[l];
67+
}
68+
69+
// Check if the middle element is the minimum
70+
if (mid - 1 >= 0 && nums[mid - 1] > nums[mid]) {
71+
return nums[mid];
72+
}
73+
74+
// Decide which half to search
75+
if (nums[mid] < nums[l]) {
76+
return findMinUtil(nums, l, mid - 1);
77+
} else if (nums[mid] > nums[r]) {
78+
return findMinUtil(nums, mid + 1, r);
79+
}
80+
81+
// If there’s ambiguity, search the left half
82+
return findMinUtil(nums, l, mid - 1);
83+
}
84+
85+
// Function to initialize the search for minimum in the rotated sorted array
86+
int findMin(int* nums, int numsSize) {
87+
int l = 0;
88+
int r = numsSize - 1;
89+
return findMinUtil(nums, l, r);
90+
}
91+
```

0 commit comments

Comments
 (0)