Skip to content

Commit 75602fb

Browse files
Changing Structure
1 parent 626c227 commit 75602fb

88 files changed

Lines changed: 810 additions & 678 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/in/knowledgegate/dsa/array/MaximumSubArray.java renamed to 1 Arrays/MaximumSubArray.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
package in.knowledgegate.dsa.array;
21

32
/**
43
* Given an integer array nums, find the contiguous subarray (containing at
@@ -15,7 +14,7 @@
1514
* <p>
1615
* Constraints: 1 <= nums.length <= 105 -104 <= nums[i] <= 104
1716
*/
18-
public class MaximumSubArray {
17+
class MaximumSubArray {
1918
public int maxSubArray(int[] nums) {
2019
int maxEndingHere = nums[0], maxSoFar = nums[0];
2120
for (int i = 1; i < nums.length; i++) {

src/in/knowledgegate/dsa/array/RemoveDuplicatesSortedArray.java renamed to 1 Arrays/RemoveDuplicatesSortedArray.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
package in.knowledgegate.dsa.array;
21

32
/**
43
* Given an integer array nums sorted in non-decreasing order, remove the
@@ -29,9 +28,10 @@
2928
* Constraints: 1 <= nums.length <= 3 * 104 -100 <= nums[i] <= 100 nums is
3029
* sorted in non-decreasing order.
3130
*/
32-
public class RemoveDuplicatesSortedArray {
31+
class RemoveDuplicatesSortedArray {
3332
public int removeDuplicates(int[] nums) {
34-
if (nums.length == 0) return 0;
33+
if (nums.length == 0)
34+
return 0;
3535
int prev = 0;
3636

3737
for (int i = 1; i < nums.length; i++) {

src/in/knowledgegate/dsa/array/SurfaceAreaOf3DShapes.java renamed to 1 Arrays/SurfaceAreaOf3DShapes.java

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
package in.knowledgegate.dsa.array;
21

32
/**
43
* You are given an n x n grid where you have
@@ -18,13 +17,13 @@
1817
*
1918
* Example 1:
2019
* Input: grid = [[1,2],
21-
* [3,4]]
20+
* [3,4]]
2221
* Output: 34 = 4 + 7 + 10 + 13 = 34
2322
*
2423
* Example 2:
2524
* Input: grid = [[1,1,1],
26-
* [1,0,1],
27-
* [1,1,1]]
25+
* [1,0,1],
26+
* [1,1,1]]
2827
* Output: 32
2928
*
3029
* Example 3:
@@ -36,34 +35,33 @@
3635
* 1 <= n <= 50
3736
* 0 <= grid[i][j] <= 50
3837
*/
39-
public class SurfaceAreaOf3DShapes {
38+
class SurfaceAreaOf3DShapes {
4039
public static void main(String[] args) {
41-
SurfaceAreaOf3DShapes shapes =
42-
new SurfaceAreaOf3DShapes();
43-
int[][] grid = new int[][]{{1,2},{3,4}};
40+
SurfaceAreaOf3DShapes shapes = new SurfaceAreaOf3DShapes();
41+
int[][] grid = new int[][] { { 1, 2 }, { 3, 4 } };
4442
System.out.println("Area is:" + shapes.surfaceArea(grid));
4543
}
4644

4745
public int surfaceArea(int[][] grid) {
4846
int total = 0;
4947
int size = grid.length;
50-
for (int i = 0 ; i< size; i++) {
48+
for (int i = 0; i < size; i++) {
5149
for (int j = 0; j < size; j++) {
52-
if (grid[i][j] == 0) continue;
53-
int areaGained =
54-
4 * grid[i][j] + 2;
50+
if (grid[i][j] == 0)
51+
continue;
52+
int areaGained = 4 * grid[i][j] + 2;
5553
int areaLost = 0;
5654
if (i - 1 >= 0) {
57-
areaLost += Math.min(grid[i][j], grid[i-1][j]);
55+
areaLost += Math.min(grid[i][j], grid[i - 1][j]);
5856
}
5957
if (i + 1 < size) {
60-
areaLost += Math.min(grid[i][j], grid[i+1][j]);
58+
areaLost += Math.min(grid[i][j], grid[i + 1][j]);
6159
}
6260
if (j - 1 >= 0) {
63-
areaLost += Math.min(grid[i][j], grid[i][j-1]);
61+
areaLost += Math.min(grid[i][j], grid[i][j - 1]);
6462
}
6563
if (j + 1 < size) {
66-
areaLost += Math.min(grid[i][j], grid[i][j+1]);
64+
areaLost += Math.min(grid[i][j], grid[i][j + 1]);
6765
}
6866
total += areaGained - areaLost;
6967
}
@@ -72,12 +70,3 @@ public int surfaceArea(int[][] grid) {
7270
return total;
7371
}
7472
}
75-
76-
77-
78-
79-
80-
81-
82-
83-

src/in/knowledgegate/dsa/array/TwoSum.java renamed to 1 Arrays/TwoSum.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
package in.knowledgegate.dsa.array;
21

32
/**
43
* Given an array of integers nums and an integer
@@ -23,12 +22,12 @@
2322
* nums[i] <= 109 -109 <= target <= 109 Only one
2423
* valid answer exists.
2524
*/
26-
public class TwoSum {
25+
class TwoSum {
2726
public int[] twoSum(int[] nums, int target) {
2827
for (int i = 0; i < nums.length; i++) {
2928
for (int j = i + 1; j < nums.length; j++) {
3029
if (nums[i] + nums[j] == target) {
31-
return new int[] {i , j};
30+
return new int[] { i, j };
3231
}
3332
}
3433
}

src/in/knowledgegate/dsa/array/ValidSudoku.java renamed to 1 Arrays/ValidSudoku.java

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
package in.knowledgegate.dsa.array;
21

32
import java.util.HashSet;
43
import java.util.Set;
@@ -23,15 +22,15 @@
2322
*
2423
* Example 1:
2524
* Input: board =
26-
* { {'5','3','.','.','7','.','.','.','.'}
27-
* , {'6','.','.','1','9','5','.','.','.'}
28-
* , {'.','9','8','.','.','.','.','6','.'}
29-
* , {'8','.','.','.','6','.','.','.','3'}
30-
* , {'4','.','.','8','.','3','.','.','1'}
31-
* , {'7','.','.','.','2','.','.','.','6'}
32-
* , {'.','6','.','.','.','.','2','8','.'}
33-
* , {'.','.','.','4','1','9','.','.','5'}
34-
* , {'.','.','.','.','8','.','.','7','9'}}
25+
* { {'5','3','.','.','7','.','.','.','.'}
26+
* , {'6','.','.','1','9','5','.','.','.'}
27+
* , {'.','9','8','.','.','.','.','6','.'}
28+
* , {'8','.','.','.','6','.','.','.','3'}
29+
* , {'4','.','.','8','.','3','.','.','1'}
30+
* , {'7','.','.','.','2','.','.','.','6'}
31+
* , {'.','6','.','.','.','.','2','8','.'}
32+
* , {'.','.','.','4','1','9','.','.','5'}
33+
* , {'.','.','.','.','8','.','.','7','9'}}
3534
* Output: true
3635
*
3736
* Example 2:
@@ -58,19 +57,14 @@
5857
* board[i].length == 9
5958
* board[i][j] is a digit 1-9 or '.'.
6059
*/
61-
public class ValidSudoku {
60+
class ValidSudoku {
6261
public static void main(String[] args) {
6362
ValidSudoku checker = new ValidSudoku();
64-
char[][] puzzle = new char[][]
65-
{{'5','3','.','.','7','.','.','.','.'}
66-
,{'6','.','.','1','9','5','.','.','.'}
67-
,{'.','9','8','.','.','.','.','6','.'}
68-
,{'8','.','.','.','6','.','.','.','3'}
69-
,{'4','.','.','8','.','3','.','.','1'}
70-
,{'7','.','.','.','2','.','.','.', '6'}
71-
,{'.','6','.','.','.','.','2','8','.'}
72-
,{'.','.','.','4','1','9','.','.','5'}
73-
,{'.','.','.','.','8','.','.', '7','9'}};
63+
char[][] puzzle = new char[][] { { '5', '3', '.', '.', '7', '.', '.', '.', '.' },
64+
{ '6', '.', '.', '1', '9', '5', '.', '.', '.' }, { '.', '9', '8', '.', '.', '.', '.', '6', '.' },
65+
{ '8', '.', '.', '.', '6', '.', '.', '.', '3' }, { '4', '.', '.', '8', '.', '3', '.', '.', '1' },
66+
{ '7', '.', '.', '.', '2', '.', '.', '.', '6' }, { '.', '6', '.', '.', '.', '.', '2', '8', '.' },
67+
{ '.', '.', '.', '4', '1', '9', '.', '.', '5' }, { '.', '.', '.', '.', '8', '.', '.', '7', '9' } };
7468
System.out.println("IsValid:" + checker.isValidSudoku(puzzle));
7569
}
7670

@@ -79,17 +73,15 @@ public boolean isValidSudoku(char[][] board) {
7973
for (int i = 0; i < 9; i++) {
8074
for (int j = 0; j < 9; j++) {
8175
char c = board[i][j];
82-
if (c == '.') continue;
83-
if (!visited.add(c +"row" + i) ||
84-
!visited.add(c + "col" + j) ||
85-
!visited.add(c + "box" + i/3 + "-" + j/3)) {
76+
if (c == '.')
77+
continue;
78+
if (!visited.add(c + "row" + i) ||
79+
!visited.add(c + "col" + j) ||
80+
!visited.add(c + "box" + i / 3 + "-" + j / 3)) {
8681
return false;
8782
}
8883
}
8984
}
9085
return true;
9186
}
9287
}
93-
94-
95-

src/in/knowledgegate/dsa/recursion/Factorial.java renamed to 10 Recursion/Factorial.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
package in.knowledgegate.dsa.recursion;
21

32
/**
43
* Factorial
@@ -9,9 +8,10 @@
98
* F(3) = 6 = 3 * 2
109
* F(n) = n * F(n-1), n >= 1
1110
*/
12-
public class Factorial {
11+
class Factorial {
1312
public int fact(int n) {
14-
if (n == 0 || n == 1) return 1;
15-
return n * fact(n-1);
13+
if (n == 0 || n == 1)
14+
return 1;
15+
return n * fact(n - 1);
1616
}
1717
}

src/in/knowledgegate/dsa/recursion/Fibonacci.java renamed to 10 Recursion/Fibonacci.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
package in.knowledgegate.dsa.recursion;
21

32
/**
43
* The Fibonacci numbers, commonly denoted F(n)
@@ -30,15 +29,17 @@
3029
* Constraints:
3130
* 0 <= n <= 30
3231
*/
33-
public class Fibonacci {
32+
class Fibonacci {
3433
public int fibRecursive(int n) {
35-
if (n == 0 || n == 1) return n;
34+
if (n == 0 || n == 1)
35+
return n;
3636
return fibRecursive(n - 1) +
37-
fibIterative( n - 2);
37+
fibIterative(n - 2);
3838
}
3939

4040
public int fibIterative(int n) {
41-
if (n == 0 || n == 1) return n;
41+
if (n == 0 || n == 1)
42+
return n;
4243
int first = 0, second = 1;
4344
for (int i = 1; i < n; i++) {
4445
int next = first + second;

src/in/knowledgegate/dsa/binarytree/InorderTraversalIterative.java renamed to 11 Tree and Binary Tree/InorderTraversalIterative.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
package in.knowledgegate.dsa.binarytree;
21

32
import in.knowledgegate.dsa.binarytree.model.TreeNode;
43

@@ -10,7 +9,7 @@
109
* Write Inorder traversal of a binary tree using
1110
* iterative approach
1211
*/
13-
public class InorderTraversalIterative {
12+
class InorderTraversalIterative {
1413
public List<Integer> inorderTraversal(TreeNode root) {
1514
Stack<TreeNode> stack = new Stack<>();
1615
List<Integer> result = new ArrayList<>();

src/in/knowledgegate/dsa/binarytree/InorderTraversalRecursive.java renamed to 11 Tree and Binary Tree/InorderTraversalRecursive.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
package in.knowledgegate.dsa.binarytree;
21

32
import in.knowledgegate.dsa.binarytree.model.TreeNode;
43

@@ -9,15 +8,17 @@
98
* Write Inorder traversal of a binary tree using
109
* recursive approach
1110
*/
12-
public class InorderTraversalRecursive {
11+
class InorderTraversalRecursive {
1312
public List<Integer> inorderTraversal(TreeNode root) {
1413
List<Integer> result = new ArrayList<>();
1514
inorderTraversal(root, result);
1615
return result;
1716
}
17+
1818
private void inorderTraversal(TreeNode root,
1919
List<Integer> result) {
20-
if (root == null) return;
20+
if (root == null)
21+
return;
2122
inorderTraversal(root.left, result);
2223
result.add(root.val);
2324
inorderTraversal(root.right, result);

src/in/knowledgegate/dsa/binarytree/PostorderTraversalIterative.java renamed to 11 Tree and Binary Tree/PostorderTraversalIterative.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
package in.knowledgegate.dsa.binarytree;
21

32
import in.knowledgegate.dsa.binarytree.model.TreeNode;
43

@@ -10,7 +9,7 @@
109
* Write Postorder traversal of a binary tree
1110
* using iterative approach
1211
*/
13-
public class PostorderTraversalIterative {
12+
class PostorderTraversalIterative {
1413
public List<Integer> iterativePostorder(TreeNode root) {
1514
List<Integer> result = new ArrayList<>();
1615
if (root == null) {

0 commit comments

Comments
 (0)