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 f24708f

Browse files
updating all READMEs
1 parent dbaab48 commit f24708f

File tree

11 files changed

+858
-258
lines changed

11 files changed

+858
-258
lines changed

‎.dev/logs.json

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,8 +1993,10 @@
19931993
}
19941994
},
19951995
"443": {
1996-
"len": 0,
1997-
"sols": {}
1996+
"len": 2,
1997+
"sols": {
1998+
"string_compression.rs": "5fb302855760a7a4d0ea4295ed9a857d55bc05720be8684b9b6fc3d354b4dc92"
1999+
}
19982000
},
19992001
"444": {
20002002
"len": 0,
@@ -3922,8 +3924,10 @@
39223924
"sols": {}
39233925
},
39243926
"912": {
3925-
"len": 0,
3926-
"sols": {}
3927+
"len": 3,
3928+
"sols": {
3929+
"sort_an_array.go": "6c1d42703394ec5fdc2c84b9104e3d421203cbe7e8e2f1a50735a8de6a62fa0a"
3930+
}
39273931
},
39283932
"913": {
39293933
"len": 0,
@@ -4716,8 +4720,10 @@
47164720
"sols": {}
47174721
},
47184722
"1108": {
4719-
"len": 0,
4720-
"sols": {}
4723+
"len": 2,
4724+
"sols": {
4725+
"defanging_an_ip_address.rs": "ce3c86c9d7374f52d4e425f6d3b59a5f00044500e044ea321a747df3248716c8"
4726+
}
47214727
},
47224728
"1109": {
47234729
"len": 0,
@@ -7982,8 +7988,10 @@
79827988
"sols": {}
79837989
},
79847990
"1920": {
7985-
"len": 0,
7986-
"sols": {}
7991+
"len": 2,
7992+
"sols": {
7993+
"build_array_from_permutation.rs": "b9c95f880619e12a98dcec4b79ce9da4a529e86c877a25220d13f30b45d41944"
7994+
}
79877995
},
79887996
"1921": {
79897997
"len": 0,
@@ -8018,8 +8026,10 @@
80188026
"sols": {}
80198027
},
80208028
"1929": {
8021-
"len": 0,
8022-
"sols": {}
8029+
"len": 2,
8030+
"sols": {
8031+
"concatenation_of_array.rs": "6cf40b85b961b1afc615c836527ab8e7930882df779261c3cd4b78e61f125bcc"
8032+
}
80238033
},
80248034
"1930": {
80258035
"len": 0,
@@ -10186,8 +10196,10 @@
1018610196
"sols": {}
1018710197
},
1018810198
"2469": {
10189-
"len": 0,
10190-
"sols": {}
10199+
"len": 2,
10200+
"sols": {
10201+
"convert_the_temperature.rs": "ef8d6a2f6e262d0f555e505b1849edfbe01c662c4b1f4983ff2e8847919321c5"
10202+
}
1019110203
},
1019210204
"2470": {
1019310205
"len": 0,

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
[**_Rejection Sampling_**][rejection sampling]   
9696
[**_Biconnected Component_**][biconnected component]
9797

98-
### **Total Problems Solved: _150_**
98+
### **Total Problems Solved: _156_**
9999

100100
---
101101

‎TOPICWISE.md

Lines changed: 109 additions & 91 deletions
Large diffs are not rendered by default.

‎docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ It might take some time to reflect changes from the repository.
103103
[**_Rejection Sampling_**][rejection sampling]   
104104
[**_Biconnected Component_**][biconnected component]
105105

106-
### **Total Problems Solved: _150_**
106+
### **Total Problems Solved: _156_**
107107

108108
## Contributors
109109

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# 443. String Compression [![share]](https://leetcode.com/problems/string-compression/)
2+
3+
![][medium]
4+
5+
## Problem Statement
6+
7+
<p>Given an array of characters <code>chars</code>, compress it using the following algorithm:</p>
8+
<p>Begin with an empty string <code>s</code>. For each group of <strong>consecutive repeating characters</strong> in <code>chars</code>:</p>
9+
<ul>
10+
<li>If the group's length is <code>1</code>, append the character to <code>s</code>.</li>
11+
<li>Otherwise, append the character followed by the group's length.</li>
12+
</ul>
13+
<p>The compressed string <code>s</code> <strong>should not be returned separately</strong>, but instead, be stored <strong>in the input character array <code>chars</code></strong>. Note that group lengths that are <code>10</code> or longer will be split into multiple characters in <code>chars</code>.</p>
14+
<p>After you are done <strong>modifying the input array,</strong> return <em>the new length of the array</em>.</p>
15+
<p>You must write an algorithm that uses only constant extra space.</p>
16+
<p> </p>
17+
<p><strong class="example">Example 1:</strong></p>
18+
19+
```
20+
Input: chars = ["a","a","b","b","c","c","c"]
21+
Output: Return 6, and the first 6 characters of the input array should be: ["a","2","b","2","c","3"]
22+
Explanation: The groups are "aa", "bb", and "ccc". This compresses to "a2b2c3".
23+
```
24+
25+
<p><strong class="example">Example 2:</strong></p>
26+
27+
```
28+
Input: chars = ["a"]
29+
Output: Return 1, and the first character of the input array should be: ["a"]
30+
Explanation: The only group is "a", which remains uncompressed since it's a single character.
31+
```
32+
33+
<p><strong class="example">Example 3:</strong></p>
34+
35+
````
36+
Input: chars = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
37+
Output: Return 4, and the first 4 characters of the input array should be: ["a","b","1","2"].
38+
Explanation: The groups are "a" and "bbbbbbbbbbbb". This compresses to "ab12".```
39+
40+
<p> </p>
41+
<p><strong>Constraints:</strong></p>
42+
<ul>
43+
<li><code>1 &lt;= chars.length &lt;= 2000</code></li>
44+
<li><code>chars[i]</code> is a lowercase English letter, uppercase English letter, digit, or symbol.</li>
45+
</ul>
46+
47+
48+
<details>
49+
<summary>
50+
51+
#### _Click to open Hints_
52+
53+
</summary>
54+
55+
- How do you know if you are at the end of a consecutive group of characters?
56+
57+
</details>
58+
59+
## Solutions
60+
61+
### [_Rust_](string_compression.rs)
62+
63+
```rs [Rust]
64+
impl Solution {
65+
pub fn compress(chars: &mut Vec<char>) -> i32 {
66+
let (mut i, n) = (0, chars.len());
67+
let mut new_len = 0;
68+
69+
while i < n {
70+
let mut j = i;
71+
72+
// increment j until we find a different character or reach the end
73+
while j < n && chars[j] == chars[i] {
74+
j += 1;
75+
}
76+
77+
// place the character at the new position
78+
// e.g. if we have aabbccc, we place a at the start of the array
79+
chars[new_len] = chars[i];
80+
new_len += 1;
81+
82+
// if the length of the group of characters is greater than 1
83+
// i.e. suppose if new_len is 12, we need to place 12 as characters [..., '1','2', ...]
84+
if j - i > 1 {
85+
for c in (j - i).to_string().chars() {
86+
chars[new_len] = c;
87+
new_len += 1;
88+
}
89+
}
90+
91+
// place i at same position as j,
92+
// i.e. the start of the next group of characters
93+
i = j;
94+
}
95+
96+
new_len as i32
97+
}
98+
}
99+
100+
````
101+
102+
### [_..._]()
103+
104+
```
105+
106+
```
107+
108+
<!----------------------------------{ link }--------------------------------->
109+
110+
[share]: https://graph.org/file/3ea5234dda646b71c574a.png
111+
[easy]: https://img.shields.io/badge/Difficulty-Easy-bright.svg
112+
[medium]: https://img.shields.io/badge/Difficulty-Medium-yellow.svg
113+
[hard]: https://img.shields.io/badge/Difficulty-Hard-red.svg
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# 912. Sort an Array [![share]](https://leetcode.com/problems/sort-an-array/)
2+
3+
![][medium]
4+
5+
## Problem Statement
6+
7+
<p>Given an array of integers <code>nums</code>, sort the array in ascending order and return it.</p>
8+
<p>You must solve the problem <strong>without using any built-in</strong> functions in <code>O(nlog(n))</code> time complexity and with the smallest space complexity possible.</p>
9+
<p> </p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
```
13+
Input: nums = [5,2,3,1]
14+
Output: [1,2,3,5]
15+
Explanation: After sorting the array, the positions of some numbers are not changed (for example, 2 and 3), while the positions of other numbers are changed (for example, 1 and 5).
16+
```
17+
18+
<p><strong class="example">Example 2:</strong></p>
19+
20+
```
21+
Input: nums = [5,1,1,2,0,0]
22+
Output: [0,0,1,1,2,5]
23+
Explanation: Note that the values of nums are not necessairly unique.
24+
```
25+
26+
<p> </p>
27+
<p><strong>Constraints:</strong></p>
28+
<ul>
29+
<li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>4</sup></code></li>
30+
<li><code>-5 * 10<sup>4</sup> &lt;= nums[i] &lt;= 5 * 10<sup>4</sup></code></li>
31+
</ul>
32+
33+
## Solutions
34+
35+
### [_Go_](sort_an_array.go)
36+
37+
```go [Go]
38+
package main
39+
40+
func sortArray(nums []int) []int {
41+
quickSort(nums, 0, len(nums)-1)
42+
43+
return nums
44+
}
45+
46+
func quickSort(nums []int, start, end int) {
47+
if start >= end {
48+
return
49+
}
50+
51+
pivot := partition(nums, start, end)
52+
quickSort(nums, start, pivot-1)
53+
quickSort(nums, pivot+1, end)
54+
}
55+
56+
func partition(arr []int, start int, end int) int {
57+
pivot := arr[end]
58+
i := start
59+
60+
for j := start; j < end; j++ {
61+
if arr[j] < pivot {
62+
arr[i], arr[j] = arr[j], arr[i]
63+
i++
64+
}
65+
}
66+
67+
arr[i], arr[end] = arr[end], arr[i]
68+
69+
return i
70+
}
71+
72+
```
73+
74+
### [_Rust_](sort_an_array.rs)
75+
76+
```rs [Rust]
77+
impl Solution {
78+
pub fn sort_array(nums: Vec<i32>) -> Vec<i32> {
79+
let len = nums.len() as i32;
80+
let mut nums = nums;
81+
82+
Self::quick_sort(&mut nums, 0, len - 1);
83+
84+
nums
85+
}
86+
87+
fn quick_sort(nums: &mut Vec<i32>, start: i32, end: i32) {
88+
if start >= end {
89+
return;
90+
}
91+
92+
let pivot = Self::partition(nums, start, end);
93+
Self::quick_sort(nums, start, pivot - 1);
94+
Self::quick_sort(nums, pivot + 1, end);
95+
}
96+
97+
fn partition(nums: &mut Vec<i32>, start: i32, end: i32) -> i32 {
98+
let pivot_index = Self::choose_pivot(nums, start as usize, end as usize);
99+
let pivot = nums[pivot_index as usize];
100+
nums.swap(pivot_index as usize, end as usize);
101+
102+
let mut i = start;
103+
104+
for j in start..end {
105+
if nums[j as usize] < pivot {
106+
nums.swap(i as usize, j as usize);
107+
i += 1;
108+
}
109+
}
110+
111+
nums.swap(i as usize, end as usize);
112+
113+
i
114+
}
115+
116+
// this is to optimize the pivot selection
117+
fn choose_pivot(nums: &mut Vec<i32>, start: usize, end: usize) -> usize {
118+
let mid = start + (end - start) / 2;
119+
if nums[start] <= nums[mid] && nums[mid] <= nums[end] {
120+
mid
121+
} else if nums[start] <= nums[end] && nums[end] <= nums[mid] {
122+
end
123+
} else {
124+
start
125+
}
126+
}
127+
}
128+
129+
```
130+
131+
### [_..._]()
132+
133+
```
134+
135+
```
136+
137+
<!----------------------------------{ link }--------------------------------->
138+
139+
[share]: https://graph.org/file/3ea5234dda646b71c574a.png
140+
[easy]: https://img.shields.io/badge/Difficulty-Easy-bright.svg
141+
[medium]: https://img.shields.io/badge/Difficulty-Medium-yellow.svg
142+
[hard]: https://img.shields.io/badge/Difficulty-Hard-red.svg

0 commit comments

Comments
(0)

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