Skip to content

Commit ddbe7d6

Browse files
committed
added
1 parent fc1f6f6 commit ddbe7d6

Some content is hidden

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

48 files changed

+219
-44
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package Arrays;
2+
3+
import java.util.Arrays;
4+
5+
public class FindTheScoreOfAllPrefixesOfAnArray {
6+
public static void main(String[] args) {
7+
int[] nums = {2, 3, 7, 5, 10};
8+
System.out.println(Arrays.toString(findPrefixScore(nums)));
9+
}
10+
11+
public static long[] findPrefixScore(int[] nums) {
12+
long[] ans = new long[nums.length];
13+
long max = nums[0];
14+
long sum = 0;
15+
for (int i = 0; i < nums.length; i++) {
16+
max = max < nums[i] ? nums[i] : max;
17+
sum += nums[i] + max;
18+
ans[i] = sum;
19+
}
20+
return ans;
21+
}
22+
}

src/Backtracking/sudokuSolver.java

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,109 @@
22

33
public class sudokuSolver {
44
public static void main(String[] args) {
5+
int[][] board = new int[][]{
6+
{3, 0, 6, 5, 0, 8, 4, 0, 0},
7+
{5, 2, 0, 0, 0, 0, 0, 0, 0},
8+
{0, 8, 7, 0, 0, 0, 0, 3, 1},
9+
{0, 0, 3, 0, 1, 0, 0, 8, 0},
10+
{9, 0, 0, 8, 6, 3, 0, 0, 5},
11+
{0, 5, 0, 0, 9, 0, 6, 0, 0},
12+
{1, 3, 0, 0, 0, 0, 2, 5, 0},
13+
{0, 0, 0, 0, 0, 0, 0, 7, 4},
14+
{0, 0, 5, 2, 0, 6, 3, 0, 0}
15+
};
516

17+
if (solve(board)) {
18+
display(board);
19+
} else {
20+
System.out.println("Cannot solve");
21+
}
22+
}
23+
24+
private static void display(int[][] board) {
25+
for (int[] row : board) {
26+
for (int num : row) {
27+
System.out.print(num + " ");
28+
}
29+
System.out.println();
30+
}
31+
}
32+
33+
34+
public static boolean solve(int[][] board) {
35+
int n = board.length;
36+
int row = -1;
37+
int col = -1;
38+
39+
boolean emptyLeft = true;
40+
41+
// this is how we are replacing the row and col from argument
42+
for (int i = 0; i < n; i++) {
43+
for (int j = 0; j < n; j++) {
44+
if (board[i][j] == 0) {
45+
row = i;
46+
col = j;
47+
emptyLeft = false;
48+
break;
49+
}
50+
}
51+
// if you found some elements in row ,then break
52+
if (!emptyLeft) {
53+
break;
54+
}
55+
}
56+
57+
// sudoku is solved
58+
if (emptyLeft) {
59+
return true;
60+
}
61+
62+
// backtrack
63+
for (int number = 0; number <= 9; number++) {
64+
if (isSafe(board, row, col, number)) {
65+
board[row][col] = number;
66+
67+
if (solve(board)) {
68+
// found the ans
69+
return true;
70+
} else {
71+
// backtrack
72+
board[row][col] = 0;
73+
}
74+
}
75+
}
76+
return false;
77+
}
78+
79+
private static boolean isSafe(int[][] board, int row, int col, int num) {
80+
// check the row
81+
for (int i = 0; i < board.length; i++) {
82+
// check if the number is in the row
83+
if (board[row][i] == num) {
84+
return false;
85+
}
86+
}
87+
88+
// check the col
89+
for (int[] nums : board) {
90+
// check if the number is in the col
91+
if (nums[col] == num) {
92+
return false;
93+
}
94+
}
95+
96+
int sqrt = (int) (Math.sqrt(board.length));
97+
int rowStart = row - row % sqrt;
98+
int colStart = col - col % sqrt;
99+
100+
for (int r = rowStart; r < rowStart + sqrt; r++) {
101+
for (int c = colStart; c < colStart + sqrt; c++) {
102+
if (board[r][c] == num) {
103+
return false;
104+
}
105+
}
106+
}
107+
return true;
6108
}
7109

8110
}

src/DynamicProgramming/ArithmeticSlices.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package DynamicProgramming;
1+
package DynamicProgramming;
22

33
public class ArithmeticSlices {
44
public static void main(String[] args) {

src/DynamicProgramming/ArithmeticSlicesIISubsequence.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package DynamicProgramming;
1+
package DynamicProgramming;
22

33
public class ArithmeticSlicesIISubsequence {
44
public static void main(String[] args) {

src/DynamicProgramming/BestTeamWithNoConflicts.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package DynamicProgramming;
1+
package DynamicProgramming;
22

33
import java.util.Arrays;
44

src/DynamicProgramming/ChampagneTower.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package DynamicProgramming;
1+
package DynamicProgramming;
22

33
public class ChampagneTower {
44
public static void main(String[] args) {

src/DynamicProgramming/CombinationSumIV.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package DynamicProgramming;
1+
package DynamicProgramming;
22

33
// Question link -> https://leetcode.com/problems/combination-sum/
44

src/DynamicProgramming/CountNumberOfWaysToPlaceHouses.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package DynamicProgramming;
1+
package DynamicProgramming;
22

33
// Question link -> https://leetcode.com/problems/count-number-of-ways-to-place-houses/
44

src/DynamicProgramming/CountTotalNumberOfColoredCells.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package DynamicProgramming;
1+
package DynamicProgramming;
22

33
public class CountTotalNumberOfColoredCells {
44
public static void main(String[] args) {

src/DynamicProgramming/CountWaysToBuildGoodStrings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package DynamicProgramming;
1+
package DynamicProgramming;
22

33
// Question link -> https://leetcode.com/problems/count-ways-to-build-good-strings/
44

0 commit comments

Comments
 (0)