Skip to content

Commit a2ad48f

Browse files
committed
premium leetcode
1 parent c9c0b7f commit a2ad48f

File tree

3 files changed

+233
-0
lines changed

3 files changed

+233
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
3+
4+
Given a file and assume that you can only read the file using a given method read4, implement a method to read n characters.
5+
Method read4:
6+
The API read4 reads four consecutive characters from file, then writes those characters into the buffer array buf4.
7+
The return value is the number of actual characters read.
8+
Note that read4() has its own file pointer, much like FILE *fp in C.
9+
Definition of read4:
10+
Parameter: char[] buf4
11+
Returns: int
12+
13+
buf4[] is a destination, not a source. The results from read4 will be copied to buf4[].
14+
Below is a high-level example of how read4 works:
15+
File file("abcde"); // File is "abcde", initially file pointer (fp) points to 'a'
16+
char[] buf4 = new char[4]; // Create buffer with enough space to store characters
17+
read4(buf4); // read4 returns 4. Now buf4 = "abcd", fp points to 'e'
18+
read4(buf4); // read4 returns 1. Now buf4 = "e", fp points to end of file
19+
read4(buf4); // read4 returns 0. Now buf4 = "", fp points to end of file
20+
Method read:
21+
By using the read4 method, implement the method read that reads n characters from file and store it in the buffer array buf. Consider that you cannot manipulate file directly.
22+
The return value is the number of actual characters read.
23+
Definition of read:
24+
Parameters: char[] buf, int n
25+
Returns: int
26+
27+
buf[] is a destination, not a source. You will need to write the results to buf[].
28+
Note:
29+
Consider that you cannot manipulate the file directly. The file is only accessible for read4 but not for read.
30+
The read function will only be called once for each test case.
31+
You may assume the destination buffer array, buf, is guaranteed to have enough space for storing ncharacters.
32+
Example 1:
33+
Input: file = "abc", n = 4
34+
Output: 3
35+
Explanation: After calling your read method, buf should contain "abc". We read a total of 3 characters from the file, so return 3.
36+
Note that "abc" is the file's content, not buf. buf is the destination buffer that you will have to write the results to.
37+
Example 2:
38+
Input: file = "abcde", n = 5
39+
Output: 5
40+
Explanation: After calling your read method, buf should contain "abcde". We read a total of 5 characters from the file, so return 5.
41+
Example 3:
42+
Input: file = "abcdABCD1234", n = 12
43+
Output: 12
44+
Explanation: After calling your read method, buf should contain "abcdABCD1234". We read a total of 12 characters from the file, so return 12.
45+
Example 4:
46+
Input: file = "leetcode", n = 5
47+
Output: 5
48+
Explanation: After calling your read method, buf should contain "leetc". We read a total of 5 characters from the file, so return 5.
49+
Constraints:
50+
1 <= file.length <= 500
51+
file consist of English letters and digits.
52+
1 <= n <= 1000
53+
54+
*/
55+
56+
/*
57+
58+
59+
APPROACH 1
60+
61+
62+
As we know, we have to implement the ‘READ’ method using the ‘READ4’ method.
63+
64+
So first, we call the ‘READ4’ method and pass a ‘TEMP_BUFFER’ array/list of 4 sizes.
65+
And this method returns the number of characters reads from the ‘FILE’ and all these characters are
66+
stored in this ‘TEMP_BUFFER’ array/list. When we read the ‘N’ characters from the ‘FILE’ we return the number of actual
67+
characters that are read from ‘FILE’.
68+
69+
70+
71+
72+
As we know, the ‘READ’ method is called any number of times. So we have to store the remaining
73+
characters of the ‘TEMP_BUFFER’ array/list also. So that's why we declare this ‘TEMP_BUFFER’
74+
array/list as a global variable.
75+
76+
77+
78+
Here is the algorithm:
79+
80+
81+
82+
We declare an array/list ‘TEMP_BUFFER’ of size 4 in which we store the current 4 characters of the ‘FILE’.
83+
We declare two variables ‘TEMP_POINTER’ and ‘TEMP_COUNT’ where ‘TEMP_POINTER’ points to the location of the
84+
current character in ‘TEMP_BUFFER’ and ‘TEMP_COUNT’ stores how many characters are present in ‘TEMP_BUFFER’.
85+
We declare a variable ‘COUNTER’ which represents the number of characters read from the ‘FILE’.
86+
We run a loop while ‘COUNTER’ less than ‘N’:
87+
If ‘TEMP_POINTER’ == 0:
88+
‘TEMP_COUNT’ = ‘READ4[‘TEMP_BUFFER’].
89+
If ‘TEMP_COUNT’ == 0:
90+
Break.
91+
We run a loop while ‘COUNTER’ less than ‘N’ and ‘TEMP_POINTER’ less than ‘TEMP_COUNT’:
92+
BUFFER[POINTER] = ‘TEMP_BUFFER[‘TEMP_POINTER’]’
93+
POINTER.
94+
‘TEMP_POINTER’.
95+
If ‘TEMP_POINTER’ equal to ‘TEMP_COUNT’:
96+
‘TEMP_POINTER’ = 0.
97+
Return ‘COUNTER’.
98+
99+
100+
*/
101+
102+
abstract class Reader4 {
103+
int read4(List<int> buf4);
104+
}
105+
106+
class Solution extends Reader4 {
107+
int read(List<int> buf, int n) {
108+
List<int> buf4 = [4];
109+
int i4 = 0; // buf4's index
110+
int n4 = 0; // buf4's size
111+
int i = 0; // buf's index
112+
while (i < n) {
113+
if (i4 == n4) {
114+
// all characters in buf4 are consumed
115+
i4 = 0; // reset buf4's index
116+
n4 = read4(buf4); // read 4 (or less) chars from file to buf4
117+
if (n4 == 0) // reach the EOF
118+
return i;
119+
}
120+
buf[i++] = buf4[i4++];
121+
}
122+
123+
return i;
124+
}
125+
126+
@override
127+
int read4(List<int> buf4) {
128+
throw UnimplementedError();
129+
}
130+
}
131+
132+
class Solution2 extends Reader4 {
133+
int read(List<int> buf, int n) {
134+
int copyCharIndex = 0;
135+
int charRead = 4;
136+
List<int> buf4 = List.filled(4, 0);
137+
while (copyCharIndex < n && charRead == 4) {
138+
charRead = read4(buf4);
139+
for (var i = 0; i < charRead; i++) {
140+
if (copyCharIndex == n) {
141+
return n;
142+
}
143+
buf[copyCharIndex++] = buf4[i];
144+
}
145+
}
146+
return copyCharIndex;
147+
}
148+
149+
@override
150+
int read4(List<int> buf4) {
151+
throw UnimplementedError();
152+
}
153+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package main
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 🔥 DART || Read N Characters Given Read4 Solution || with Explanation
2+
3+
## Reader4 Class
4+
5+
### Or
6+
7+
```dart
8+
// if you don't wanna override
9+
class Reader4 {
10+
int read4(List<int> buf4){
11+
return 0;
12+
}
13+
}
14+
```
15+
16+
```dart
17+
abstract class Reader4 {
18+
int read4(List<int> buf4);
19+
}
20+
```
21+
22+
```dart
23+
class Solution extends Reader4 {
24+
int read(List<int> buf, int n) {
25+
int copyCharIndex = 0;
26+
int charRead = 4;
27+
List<int> buf4 = List.filled(4, 0);
28+
while (copyCharIndex < n && charRead == 4) {
29+
// charRead = Reader4().read4(buf4)
30+
charRead = read4(buf4);
31+
for (var i = 0; i < charRead; i++) {
32+
if (copyCharIndex == n) {
33+
return n;
34+
}
35+
buf[copyCharIndex++] = buf4[i];
36+
}
37+
}
38+
return copyCharIndex;
39+
}
40+
41+
42+
// this override is because of abstract class
43+
@override
44+
int read4(List<int> buf4) {
45+
throw UnimplementedError();
46+
}
47+
}
48+
```
49+
50+
## Solution - 2
51+
52+
```dart
53+
class Solution extends Reader4 {
54+
int read(List<int> buf, int n) {
55+
List<int> buf4 = [4];
56+
int i4 = 0; // buf4's index
57+
int n4 = 0; // buf4's size
58+
int i = 0; // buf's index
59+
while (i < n) {
60+
if (i4 == n4) {
61+
// all characters in buf4 are consumed
62+
i4 = 0; // reset buf4's index
63+
// charRead = Reader4().read4(buf4)
64+
n4 = read4(buf4); // read 4 (or less) chars from file to buf4
65+
if (n4 == 0) // reach the EOF
66+
return i;
67+
}
68+
buf[i++] = buf4[i4++];
69+
}
70+
71+
return i;
72+
}
73+
74+
@override
75+
int read4(List<int> buf4) {
76+
throw UnimplementedError();
77+
}
78+
}
79+
```

0 commit comments

Comments
 (0)