Skip to content

Commit eb94b57

Browse files
authored
Merge pull request #3939 from ImmidiSivani/leetcode-648
added solution to leetcode 648
2 parents 82b9f90 + dede018 commit eb94b57

File tree

5 files changed

+378
-112
lines changed

5 files changed

+378
-112
lines changed

dsa-solutions/gfg-solutions/Easy problems/Square-Root.md

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ id: square-root
33
title: Square Root
44
sidebar_label: Square-Root
55
tags:
6-
- Math
7-
- Binary Search
6+
- Math
7+
- Binary Search
88
description: "This document provides solutions to the problem of finding the Square Root of an integer."
99
---
1010

@@ -38,6 +38,7 @@ You don't need to read input or print anything. The task is to complete the func
3838
**Expected Auxiliary Space:** $O(1)$
3939

4040
**Constraints**
41+
4142
- `1 ≤ x ≤ 10^7`
4243

4344
## Solution
@@ -128,25 +129,27 @@ public:
128129
129130
```javascript
130131
class Solution {
131-
floorSqrt(x) {
132-
if (x === 0 || x === 1) {
133-
return x;
134-
}
135-
let start = 1, end = x, ans = 0;
136-
while (start <= end) {
137-
let mid = Math.floor((start + end) / 2);
138-
if (mid * mid === x) {
139-
return mid;
140-
}
141-
if (mid * mid < x) {
142-
start = mid + 1;
143-
ans = mid;
144-
} else {
145-
end = mid - 1;
146-
}
147-
}
148-
return ans;
132+
floorSqrt(x) {
133+
if (x === 0 || x === 1) {
134+
return x;
135+
}
136+
let start = 1,
137+
end = x,
138+
ans = 0;
139+
while (start <= end) {
140+
let mid = Math.floor((start + end) / 2);
141+
if (mid * mid === x) {
142+
return mid;
143+
}
144+
if (mid * mid < x) {
145+
start = mid + 1;
146+
ans = mid;
147+
} else {
148+
end = mid - 1;
149+
}
149150
}
151+
return ans;
152+
}
150153
}
151154
```
152155

@@ -155,25 +158,27 @@ class Solution {
155158

156159
```typescript
157160
class Solution {
158-
floorSqrt(x: number): number {
159-
if (x === 0 || x === 1) {
160-
return x;
161-
}
162-
let start = 1, end = x, ans = 0;
163-
while (start <= end) {
164-
let mid = Math.floor((start + end) / 2);
165-
if (mid * mid === x) {
166-
return mid;
167-
}
168-
if (mid * mid < x) {
169-
start = mid + 1;
170-
ans = mid;
171-
} else {
172-
end = mid - 1;
173-
}
174-
}
175-
return ans;
161+
floorSqrt(x: number): number {
162+
if (x === 0 || x === 1) {
163+
return x;
164+
}
165+
let start = 1,
166+
end = x,
167+
ans = 0;
168+
while (start <= end) {
169+
let mid = Math.floor((start + end) / 2);
170+
if (mid * mid === x) {
171+
return mid;
172+
}
173+
if (mid * mid < x) {
174+
start = mid + 1;
175+
ans = mid;
176+
} else {
177+
end = mid - 1;
178+
}
176179
}
180+
return ans;
181+
}
177182
}
178183
```
179184

@@ -185,4 +190,4 @@ class Solution {
185190
The provided solutions efficiently find the floor value of the square root of a given integer `x` using binary search. This approach ensures a time complexity of $ O(log N) and an auxiliary space complexity of $O(1)$. The algorithms are designed to handle large values of `x` up to 10^7 efficiently without relying on built-in square root functions.
186191

187192
**Time Complexity:** $O(log N)$
188-
**Auxiliary Space:** $O(1)$
193+
**Auxiliary Space:** $O(1)$

dsa-solutions/lc-solutions/0200-0299/0240-search-a-2d-matrix-II.md

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,15 @@ Output: false
6969
### Intuition and Approach
7070

7171
To search for a value in this matrix efficiently, we can utilize the properties of the matrix. Since the matrix is sorted both row-wise and column-wise, we can start our search from the top-right corner of the matrix. From here, we have two options:
72+
7273
1. If the target is greater than the current value, move downwards.
7374
2. If the target is less than the current value, move leftwards.
7475

7576
This approach ensures that we eliminate one row or one column in each step, leading to an efficient search.
7677

7778
<Tabs>
7879
<tabItem value="Greedy Search" label="Greedy Search">
79-
80+
8081
### Approach: Greedy Search
8182

8283
By leveraging the sorted properties of the matrix, we can search for the target value efficiently using a greedy approach. This involves starting from the top-right corner and adjusting our search direction based on the current value.
@@ -108,21 +109,16 @@ const matrix = [
108109
[2, 5, 8, 12, 19],
109110
[3, 6, 9, 16, 22],
110111
[10, 13, 14, 17, 24],
111-
[18, 21, 23, 26, 30]
112+
[18, 21, 23, 26, 30],
112113
];
113114
const target = 5;
114115
const result = searchMatrix(matrix, target);
115116

116117
return (
117118
<div>
118119
<p>
119-
<b>Input:</b> matrix = [
120-
[1, 4, 7, 11, 15],
121-
[2, 5, 8, 12, 19],
122-
[3, 6, 9, 16, 22],
123-
[10, 13, 14, 17, 24],
124-
[18, 21, 23, 26, 30]
125-
], target = 5
120+
<b>Input:</b> matrix = [ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9,
121+
16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30] ], target = 5
126122
</p>
127123
<p>
128124
<b>Output:</b> {result ? "true" : "false"}
@@ -182,7 +178,7 @@ return (
182178
```
183179

184180
</TabItem>
185-
<TabItem value="Python" label="Python">
181+
<TabItem value="Python" label="Python">
186182
<SolutionAuthor name="@aryansh-patel"/>
187183
```python
188184
def searchMatrix(matrix: List[List[int]], target: int) -> bool:
@@ -259,7 +255,7 @@ return (
259255
};
260256
```
261257

262-
</TabItem>
258+
</TabItem>
263259
</Tabs>
264260

265261
#### Complexity Analysis
@@ -269,7 +265,8 @@ return (
269265

270266
- The time complexity is linear in terms of the dimensions of the matrix. Each step eliminates either a row or a
271267

272-
column, leading to a linear time complexity of $O(m + n)$.
268+
column, leading to a linear time complexity of $O(m + n)$.
269+
273270
- The space complexity is constant because we only use a few extra variables regardless of the matrix size.
274271

275272
</tabItem>
@@ -287,16 +284,16 @@ This solution leverages the matrix's properties to reduce the search space effic
287284
288285
<TabItem value="en" label="English">
289286
290-
---
291-
287+
---
288+
292289
<Tabs>
293290
<TabItem value="javascript" label="JavaScript">
294291
<LiteYouTubeEmbed
295292
id="mH0X4jxrQjY"
296293
params="autoplay=1&autohide=1&showinfo=0&rel=0"
297294
title="Search a 2D Matrix II Problem Explanation | Search a 2D Matrix II Solution"
298295
poster="maxresdefault"
299-
webp
296+
webp
300297
/>
301298
</TabItem>
302299
@@ -306,7 +303,7 @@ This solution leverages the matrix's properties to reduce the search space effic
306303
params="autoplay=1&autohide=1&showinfo=0&rel=0"
307304
title="Search a 2D Matrix II Problem Explanation | Search a 2D Matrix II Solution"
308305
poster="maxresdefault"
309-
webp
306+
webp
310307
/>
311308
</TabItem>
312309
<TabItem value="java" label="Java">
@@ -315,7 +312,7 @@ This solution leverages the matrix's properties to reduce the search space effic
315312
params="autoplay=1&autohide=1&showinfo=0&rel=0"
316313
title="Search a 2D Matrix II Problem Explanation | Search a 2D Matrix II Solution"
317314
poster="maxresdefault"
318-
webp
315+
webp
319316
/>
320317
</TabItem>
321318
</Tabs>
@@ -328,7 +325,7 @@ This solution leverages the matrix's properties to reduce the search space effic
328325
params="autoplay=1&autohide=1&showinfo=0&rel=0"
329326
title="Search a 2D Matrix II Problem Explanation | Search a 2D Matrix II Solution"
330327
poster="maxresdefault"
331-
webp
328+
webp
332329
/>
333330
</TabItem>
334331

0 commit comments

Comments
 (0)