Movatterモバイル変換


[0]ホーム

URL:


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

Commit986f35a

Browse files
authored
Sri Hari: Batch-3/Neetcode-ALL/Added articles (neetcode-gh#3751)
* Batch-3/Neetcode-ALL/Added* Batch-3/Neetcode-All/Added
1 parentdd8c1d1 commit986f35a

15 files changed

+3903
-2
lines changed

‎articles/can-place-flowers.md

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
##1. Iteration - I
2+
3+
::tabs-start
4+
5+
```python
6+
classSolution:
7+
defcanPlaceFlowers(self,flowerbed: List[int],n:int) ->bool:
8+
f= [0]+ flowerbed+ [0]
9+
10+
for iinrange(1,len(f)-1):
11+
if f[i-1]==0and f[i]==0and f[i+1]==0:
12+
f[i]=1
13+
n-=1
14+
15+
return n<=0
16+
```
17+
18+
```java
19+
publicclassSolution {
20+
publicbooleancanPlaceFlowers(int[]flowerbed,intn) {
21+
int[] f=newint[flowerbed.length+2];
22+
for (int i=0; i< flowerbed.length; i++) {
23+
f[i+1]= flowerbed[i];
24+
}
25+
26+
for (int i=1; i< f.length-1; i++) {
27+
if (f[i-1]==0&& f[i]==0&& f[i+1]==0) {
28+
f[i]=1;
29+
n--;
30+
}
31+
}
32+
return n<=0;
33+
}
34+
}
35+
```
36+
37+
```cpp
38+
classSolution {
39+
public:
40+
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
41+
vector<int> f(flowerbed.size() + 2, 0);
42+
for (int i = 0; i < flowerbed.size(); i++) {
43+
f[i + 1] = flowerbed[i];
44+
}
45+
46+
for (int i = 1; i < f.size() - 1; i++) {
47+
if (f[i - 1] == 0 && f[i] == 0 && f[i + 1] == 0) {
48+
f[i] = 1;
49+
n--;
50+
}
51+
}
52+
return n <= 0;
53+
}
54+
};
55+
```
56+
57+
```javascript
58+
class Solution {
59+
/**
60+
* @param {number[]} flowerbed
61+
* @param {number} n
62+
* @return {boolean}
63+
*/
64+
canPlaceFlowers(flowerbed, n) {
65+
const f = [0, ...flowerbed, 0];
66+
67+
for (let i = 1; i < f.length - 1; i++) {
68+
if (f[i - 1] === 0 && f[i] === 0 && f[i + 1] === 0) {
69+
f[i] = 1;
70+
n--;
71+
}
72+
}
73+
return n <= 0;
74+
}
75+
}
76+
```
77+
78+
::tabs-end
79+
80+
###Time & Space Complexity
81+
82+
* Time complexity: $O(n)$
83+
* Space complexity: $O(n)$
84+
85+
---
86+
87+
##2. Iteration - II
88+
89+
::tabs-start
90+
91+
```python
92+
classSolution:
93+
defcanPlaceFlowers(self,flowerbed: List[int],n:int) ->bool:
94+
empty=0if flowerbed[0]else1
95+
96+
for fin flowerbed:
97+
if f:
98+
n-=int((empty-1)/2)
99+
empty=0
100+
else:
101+
empty+=1
102+
103+
n-= empty//2
104+
return n<=0
105+
```
106+
107+
```java
108+
publicclassSolution {
109+
publicbooleancanPlaceFlowers(int[]flowerbed,intn) {
110+
int empty= flowerbed[0]==0?1:0;
111+
112+
for (int f: flowerbed) {
113+
if (f==1) {
114+
n-= (empty-1)/2;
115+
empty=0;
116+
}else {
117+
empty++;
118+
}
119+
}
120+
121+
n-= empty/2;
122+
return n<=0;
123+
}
124+
}
125+
```
126+
127+
```cpp
128+
classSolution {
129+
public:
130+
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
131+
int empty = flowerbed[0] == 0 ? 1 : 0;
132+
133+
for (int f : flowerbed) {
134+
if (f == 1) {
135+
n -= (empty - 1) / 2;
136+
empty = 0;
137+
} else {
138+
empty++;
139+
}
140+
}
141+
142+
n -= empty / 2;
143+
return n <= 0;
144+
}
145+
};
146+
```
147+
148+
```javascript
149+
class Solution {
150+
/**
151+
* @param {number[]} flowerbed
152+
* @param {number} n
153+
* @return {boolean}
154+
*/
155+
canPlaceFlowers(flowerbed, n) {
156+
let empty = flowerbed[0] === 0 ? 1 : 0;
157+
158+
for (let f of flowerbed) {
159+
if (f === 1) {
160+
n -= Math.floor(Math.max(0, empty - 1) / 2);
161+
empty = 0;
162+
} else {
163+
empty++;
164+
}
165+
}
166+
167+
n -= Math.floor(empty / 2);
168+
return n <= 0;
169+
}
170+
}
171+
```
172+
173+
::tabs-end
174+
175+
###Time & Space Complexity
176+
177+
* Time complexity: $O(n)$
178+
* Space complexity: $O(1)$

‎articles/concatenation-of-array.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
##1. Iteration (Two Pass)
2+
3+
::tabs-start
4+
5+
```python
6+
classSolution:
7+
defgetConcatenation(self,nums: List[int]) -> List[int]:
8+
ans= []
9+
for iinrange(2):
10+
for numin nums:
11+
ans.append(num)
12+
return ans
13+
```
14+
15+
```java
16+
publicclassSolution {
17+
publicint[]getConcatenation(int[]nums) {
18+
int[] ans=newint[2* nums.length];
19+
int idx=0;
20+
for (int i=0; i<2; i++) {
21+
for (int num: nums) {
22+
ans[idx++]= num;
23+
}
24+
}
25+
return ans;
26+
}
27+
}
28+
```
29+
30+
```cpp
31+
classSolution {
32+
public:
33+
vector<int> getConcatenation(vector<int>& nums) {
34+
vector<int> ans;
35+
for (int i = 0; i < 2; ++i) {
36+
for (int num : nums) {
37+
ans.push_back(num);
38+
}
39+
}
40+
return ans;
41+
}
42+
};
43+
```
44+
45+
```javascript
46+
class Solution {
47+
/**
48+
* @param {number[]} nums
49+
* @return {number[]}
50+
*/
51+
getConcatenation(nums) {
52+
let ans = [];
53+
for (let i = 0; i < 2; i++) {
54+
for (let num of nums) {
55+
ans.push(num);
56+
}
57+
}
58+
return ans;
59+
}
60+
}
61+
```
62+
63+
::tabs-end
64+
65+
###Time & Space Complexity
66+
67+
* Time complexity: $O(n)$
68+
* Space complexity: $O(1)$
69+
70+
---
71+
72+
##2. Iteration (One Pass)
73+
74+
::tabs-start
75+
76+
```python
77+
classSolution:
78+
defgetConcatenation(self,nums: List[int]) -> List[int]:
79+
n=len(nums)
80+
ans= [0]* (2* n)
81+
for i, numinenumerate(nums):
82+
ans[i]= ans[i+ n]= num
83+
return ans
84+
```
85+
86+
```java
87+
publicclassSolution {
88+
publicint[]getConcatenation(int[]nums) {
89+
int n= nums.length;
90+
int[] ans=newint[2* n];
91+
for (int i=0; i< n; i++) {
92+
ans[i]= ans[i+ n]= nums[i];
93+
}
94+
return ans;
95+
}
96+
}
97+
```
98+
99+
```cpp
100+
classSolution {
101+
public:
102+
vector<int> getConcatenation(vector<int>& nums) {
103+
int n = nums.size();
104+
vector<int> ans(2 * n);
105+
for (int i = 0; i < n; ++i) {
106+
ans[i] = ans[i + n] = nums[i];
107+
}
108+
return ans;
109+
}
110+
};
111+
```
112+
113+
```javascript
114+
class Solution {
115+
/**
116+
* @param {number[]} nums
117+
* @return {number[]}
118+
*/
119+
getConcatenation(nums) {
120+
let n = nums.length;
121+
let ans = new Array(2 * n);
122+
for (let i = 0; i < n; i++) {
123+
ans[i] = ans[i + n] = nums[i];
124+
}
125+
return ans;
126+
}
127+
}
128+
```
129+
130+
::tabs-end
131+
132+
###Time & Space Complexity
133+
134+
* Time complexity: $O(n)$
135+
* Space complexity: $O(1)$

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp