Skip to content

Commit 8e4946a

Browse files
committed
Refactor code structure for improved readability and maintainability
1 parent 16c92a1 commit 8e4946a

27 files changed

+666
-83
lines changed

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.13

docs/cp31/1000.md

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# 1000 Rated problems
2+
3+
1. [Binary String Transformation Problem](#binary-string-transformation-problem)
4+
5+
6+
## Binary String Transformation Problem
7+
Problem link on [codeforces](https://codeforces.com/problemset/problem/1913/B)
8+
9+
### Problem Statement
10+
11+
Given a binary string `s`, transform it into a "good" string `t` where every position differs from the original string (`t[i] ≠ s[i]`).
12+
13+
**Available operations:**
14+
15+
- **Delete** a character (cost: 1 coin)
16+
- **Swap** any two characters (cost: 0 coins)
17+
18+
**Goal:** Find the minimum cost to create a good string.
19+
20+
### Solution Approach
21+
22+
#### Key Insight
23+
24+
Since swaps are free, we can rearrange characters at no cost. The optimal strategy is to:
25+
26+
1. **Swap characters** to maximize positions where `t[i] ≠ s[i]`
27+
2. **Delete excess characters** that cannot be repositioned
28+
29+
#### Algorithm Logic
30+
31+
The solution uses a **greedy matching approach**:
32+
33+
```
34+
For each position i in the original string:
35+
- If s[i] = '1', try to place a '0' at position i
36+
- If s[i] = '0', try to place a '1' at position i
37+
- Track remaining available 0s and 1s
38+
```
39+
40+
#### Special Cases
41+
42+
1. **Single character** → Must delete it (cost: 1)
43+
2. **Equal 0s and 1s** → Perfect swap exists (cost: 0)
44+
3. **Only 0s or only 1s** → Must delete all (cost: n)
45+
46+
#### Core Implementation
47+
48+
```cpp
49+
int zeros = count('0');
50+
int ones = count('1');
51+
52+
// Greedy assignment
53+
for each character c in s:
54+
if c == '1':
55+
use a '0' → zeros--
56+
else:
57+
use a '1' → ones--
58+
59+
if ran out of needed characters:
60+
remaining positions must be deleted
61+
break
62+
```
63+
64+
#### Code
65+
```cpp
66+
void solve() {
67+
string s; cin >> s;
68+
if (s.size() == 1) {
69+
std::cout << 1 << "\n"; return;
70+
}
71+
72+
int zeros = 0;
73+
int ones = 0;
74+
for (auto c : s) {
75+
if (c == '0') {
76+
zeros++;
77+
} else {
78+
ones++;
79+
}
80+
}
81+
82+
if (zeros == ones) {
83+
std::cout << 0 << "\n"; return;
84+
}
85+
86+
if (zeros == 0 or ones == 0) {
87+
std::cout << s.size() << "\n"; return;
88+
}
89+
90+
int t = 0;
91+
for (int i = 0; i < s.size(); i++) {
92+
char c = s[i];
93+
if (c == '1') {
94+
t++; zeros--;
95+
if (zeros < 0) {
96+
t--;
97+
std::cout << s.size() - t << "\n"; return;
98+
}
99+
} else if (c == '0') {
100+
t++; ones--;
101+
if (ones < 0) {
102+
t--;
103+
std::cout << s.size() - t << "\n"; return;
104+
}
105+
}
106+
}
107+
}
108+
```
109+
110+
#### Complexity
111+
112+
- **Time:** O(n) - only pass twice through the string
113+
- **Space:** O(1) - only counters needed
114+
115+
### Example Walkthrough
116+
117+
**Input:** `s = "1100"`
118+
119+
- Initial: zeros = 2, ones = 2
120+
- Position 0: `s[0] = '1'` → use `'0'` (zeros = 1) ✓
121+
- Position 1: `s[1] = '1'` → use `'0'` (zeros = 0) ✓
122+
- Position 2: `s[2] = '0'` → use `'1'` (ones = 1) ✓
123+
- Position 3: `s[3] = '0'` → use `'1'` (ones = 0) ✓
124+
125+
**Result:** `t = "0011"`, cost = **0** (perfect swap)
126+
127+
128+
129+
**Input:** `s = "111000"`
130+
131+
- Initial: zeros = 3, ones = 3
132+
- Positions 0-2: consume all 3 zeros
133+
- Positions 3-5: consume all 3 ones
134+
135+
**Result:** cost = **0**
136+
137+
138+
139+
**Input:** `s = "11100"`
140+
141+
- Initial: zeros = 2, ones = 3
142+
- Position 0-1: use 2 zeros (zeros = 0)
143+
- Position 2: need zero but none left → **stop**
144+
- Successfully placed: 2 characters
145+
- Must delete: 5 - 2 = **3 characters**
146+
147+
**Result:** cost = **3**

docs/cp31/cp31.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# CP 31 sheet
2+
This sheet is curated by Priyansh31dec. Can be found [here](https://www.tle-eliminators.com/cp-sheet). This part of the website contains solutions I submitted for the problems in the popular CP31 sheet.
3+
4+
CP 31 sheet is divided into ratings $\in (800, 900, 1000, 1100, \dots 1900)$. For each rating we have a total of 31 tasks to solve. I recommend starting from $800$ rated problems itself.
5+
6+
## Rated Problems
7+
- [1000 rated problems](./1000.md)

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ This material categorizes coding interview problems into a set of **12 patterns*
4545
9. [Heaps overview](https://algorithms.theroyakash.com/heaps/heap-pq/) and [heap problems](https://algorithms.theroyakash.com/heaps/problems/) and [2 heaps problems](https://algorithms.theroyakash.com/2heaps/problems/).
4646
10. [Greedy algorithms](https://algorithms.theroyakash.com/greedy/problems/)
4747
11. [Dynamic Programming](https://algorithms.theroyakash.com/dp/problems/)
48-
12. Bit Manipulation (With C/C++) (coming soon)
48+
12. [CP 31 Sheet](/cp31/cp31)
4949

5050
## Problem Counter
5151
The following table is meant to represent the amount of problems has a full analysis in the website. Maybe incorrectly represented upto 1-2 problems.

docs/io.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
# :pencil: Standard input output usage
22

3-
## Taking data in and writing data out to a file
4-
In some of the problems in SDE 1 and SDE 2 online coding rounds, you have to take care of the program inputs.
3+
## Taking fast input output
4+
In some of the problems in SDE 1 and SDE 2 online coding rounds, you have to take care of the program inputs. Use this following two lines of code to increase your i/o speed in competitive programming contests.
5+
6+
Brief explanation:
7+
8+
- If you include `ios::sync_with_stdio(false)`, then mixing C (`scanf`, `printf`) and C++ (`cin`, `cout`) style I/O may produce unexpected results. The upside is that both `cin / cout` become faster.
9+
- Including `cin.tie(nullptr)` will reduce the runtime if you are interleaving `cin` and `cout` (as is the case in the task at hand).
10+
11+
```cpp
12+
int main() {
13+
ios::sync_with_stdio(false);
14+
cin.tie(nullptr);
515

16+
/* code */
17+
18+
return 0;
19+
}
20+
```
21+
22+
## Taking data in and writing data out to a file
623
Here is an example of how we can write all inputs to a text file and then run our algorithms.
724

8-
You must add 2 files called "input.txt" and "output.txt" in the same directory add the function in the code, then in the `main()` function just call the function `fileIO()` to have the input taken from input.txt and the output written to output.txt.
25+
You must add 2 files called `input.txt` and `output.txt` in the same directory add the function in the code, then in the `main()` function just call the function `fileIO()` to have the input taken from `input.txt` and the output written to output.txt.
926

1027
```cpp
1128
void fileIO() {
@@ -23,7 +40,7 @@ int main() {
2340
```
2441

2542
### Input
26-
Now in the input.txt file, you design as the inputs are given in the following case the input was
43+
Now in the `input.txt` file, you design as the inputs are given in the following case the input was
2744

2845
```
2946
3
-40.2 KB
Binary file not shown.

docs/stylesheets/Regards.ttf

-226 KB
Binary file not shown.
-41.6 KB
Binary file not shown.
-40.4 KB
Binary file not shown.

docs/stylesheets/SegoeUI.ttf

-33.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)