Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 33cf75e

Browse files
Merge pull request #40 from codewithhridoy/javascript
medium: 75. Sort Colors
2 parents 3f0e336 + 2f25a62 commit 33cf75e

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

‎JavaScript/leetcode/01-100/01/solution.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,38 @@
33
### Intuition
44

55
The problem requires finding two numbers in an array that sum up to a given target. A common approach to this problem is to use a hash table to store the indices of numbers as we iterate through the array. This way, we can quickly look up whether the complement of the current number exists in the hash table.
6+
67
### Approach
78

89
We'll iterate through the array once. For each number, we'll calculate its complement (the difference between the target and the current number). If the complement exists in the hash table, we've found the pair that sums up to the target. If not, we'll store the current number's index in the hash table.
10+
911
### Complexity
12+
1013
**Time complexity:** O(n), where n is the number of elements in the input array. We iterate through the array once.
1114

1215
**Space complexity:** O(n), where n is the number of elements in the input array. We use a hash table to store indices, which can have up to n entries in the worst case.
1316

17+
### Code
18+
19+
```javascript
20+
/**
21+
* @param {number[]} nums
22+
* @param {number} target
23+
* @return {number[]}
24+
*/
25+
const twoSum = function (nums, target) {
26+
const hash = {};
27+
28+
return nums.reduce((result, num, index) => {
29+
const complement = target - num;
30+
31+
if (complement in hash) {
32+
result.push(hash[complement], index);
33+
}
34+
35+
hash[num] = index;
36+
37+
return result;
38+
}, []);
39+
};
40+
```

‎JavaScript/leetcode/01-100/02/solution.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,39 @@ To solve this problem, we can iterate through the linked lists l1 and l2 simulta
1616
- Return the next node of the dummy head, which contains the start of the result linked list.
1717

1818
### Complexity
19+
1920
**Time complexity:** O(max(m, n)), where m and n are the lengths of the input linked lists l1 and l2, respectively. We traverse at most max(m, n) nodes in the longer list.
2021

2122
**Space complexity:** O(max(m, n)), the space required by the output linked list, which could be at most the length of the longer input list plus 1 for the carry.
2223

24+
### Code
25+
26+
```javascript
27+
/**
28+
* Definition for singly-linked list.
29+
* function ListNode(val, next) {
30+
* this.val = (val===undefined ? 0 : val)
31+
* this.next = (next===undefined ? null : next)
32+
* }
33+
*/
34+
/**
35+
* @param {ListNode} l1
36+
* @param {ListNode} l2
37+
* @return {ListNode}
38+
*/
39+
const addTwoNumbers = function (l1, l2) {
40+
const dummyHead = new ListNode(0);
41+
let [current, carry] = [dummyHead, 0];
42+
43+
while (l1 || l2 || carry) {
44+
const sum = (l1?.val ?? 0) + (l2?.val ?? 0) + carry;
45+
carry = Math.floor(sum / 10);
46+
current.next = new ListNode(sum % 10);
47+
current = current.next;
48+
l1 = l1?.next;
49+
l2 = l2?.next;
50+
}
51+
52+
return dummyHead.next;
53+
};
54+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {void} Do not return anything, modify nums in-place instead.
4+
*/
5+
function sortColors(nums) {
6+
let sorted = false;
7+
while (!sorted) {
8+
sorted = true;
9+
for (let i = 0; i < nums.length - 1; i++) {
10+
if (nums[i] > nums[i + 1]) {
11+
sorted = false;
12+
// Swap nums[i] and nums[i + 1]
13+
[nums[i], nums[i + 1]] = [nums[i + 1], nums[i]];
14+
}
15+
}
16+
}
17+
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# [75. Sort Colors](https://leetcode.com/problems/sort-colors)
2+
3+
---
4+
5+
title: "Intuition and Approach for Sorting Colors"
6+
summary: "Detailed intuition and approach to solving the problem of sorting colors with code implementation and complexity analysis."
7+
date: 2024年06月12日
8+
modifiedDate: 2024年06月12日
9+
tags: ["algorithm", "sorting", "complexity analysis"]
10+
slug: "intuition-approach-sorting-colors"
11+
12+
---
13+
14+
![image.png](https://assets.leetcode.com/users/images/e61d4204-745b-4833-9b39-0b2cf9aee959_1718152359.7529407.png)
15+
16+
# Intuition
17+
18+
To solve the problem of sorting colors, the first thought is to use a simple sorting algorithm. Since the problem can be viewed as sorting an array with a small set of distinct values (0, 1, and 2), we can utilize a sorting method that repeatedly compares and swaps adjacent elements if they are in the wrong order.
19+
20+
# Approach
21+
22+
The approach involves using the bubble sort algorithm, which repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. This process is repeated until the array is sorted. Although bubble sort is not the most efficient algorithm for large datasets, it is straightforward and works for this problem with a small set of values.
23+
24+
# Complexity
25+
26+
- Time complexity: $$O(n^2)$$
27+
- Space complexity: $$O(1)$$
28+
29+
# Code
30+
31+
```javascript
32+
/**
33+
* @param {number[]} nums
34+
* @return {void} Do not return anything, modify nums in-place instead.
35+
*/
36+
function sortColors(nums) {
37+
let sorted = false;
38+
while (!sorted) {
39+
sorted = true;
40+
for (let i = 0; i < nums.length - 1; i++) {
41+
if (nums[i] > nums[i + 1]) {
42+
sorted = false;
43+
// Swap nums[i] and nums[i + 1]
44+
[nums[i], nums[i + 1]] = [nums[i + 1], nums[i]];
45+
}
46+
}
47+
}
48+
}
49+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /