This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LastStoneWeight { | |
public int lastStoneWeight(int[] stones) { | |
if (stones == null || stones.length == 0) { | |
return 0; | |
} | |
PriorityQueue<Integer> stonesPQ = new PriorityQueue<>(stones.length, Comparator.reverseOrder()); | |
for (int stone : stones) { | |
stonesPQ.offer(stone); | |
} | |
int lastStoneWeight; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int findMaxLength(int[] nums) { | |
int maxLen = 0; | |
int sum = nums.length; | |
int[] prevSums = new int[2 * nums.length + 1]; | |
for (int i = 0; i < prevSums.length; i++) { | |
prevSums[i] = -2; | |
} | |
prevSums[nums.length] = -1; | |
for (int i = 0; i < nums.length; i++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PerformStringShifts { | |
public String stringShift(String s, int[][] shift) { | |
int shiftNumber = 0; | |
for (int[] shiftOperation : shift) { | |
if (shiftOperation[0] == 1) { | |
shiftNumber += shiftOperation[1]; | |
} else { | |
shiftNumber -= shiftOperation[1]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) throws Exception { | |
Scanner scanner = new Scanner(System.in); | |
int[] a = new int[12]; | |
int[] b = new int[12]; | |
int[] c = new int[24]; | |
System.out.println("Enter elements for firs array"); | |
for (int i = 0; i < 12; i++) { | |
System.out.print("Enter element number " + (i + 1) + ":"); | |
a[i] = scanner.nextInt(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProductOfArrayExceptSelf { | |
public int[] productExceptSelf(int[] nums) { | |
if (nums.length < 2) { | |
return nums; | |
} | |
int[] products = new int[nums.length]; | |
int factor = 1; | |
for (int i = nums.length - 1; i >= 0; i--) { | |
products[i] = factor * nums[i]; | |
factor = products[i]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ValidParenthesisString { | |
public boolean checkValidString(String s) { | |
int openParenthesis = 0; | |
int closedParenthesis = 0; | |
for (int i = 0; i < s.length(); i++) { | |
char c = s.charAt(i); | |
if (c == '(' || c == '*') { | |
openParenthesis++; | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NumberOfIslands { | |
public int numIslands(char[][] grid) { | |
int numIslands = 0; | |
for (int r = 0; r < grid.length; r++) { | |
for (int c = 0; c < grid[r].length; c++) { | |
if (grid[r][c] == '1') { | |
dfs(grid, r, c); | |
numIslands++; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MinimumPathSum { | |
public int minPathSum(int[][] grid) { | |
if (grid == null || grid.length == 0 || grid[0].length == 0) { | |
return 0; | |
} | |
int m = grid.length; | |
int n = grid[0].length; | |
int dp[] = new int[n + 1]; | |
for (int j = 1; j <= n; j++) { | |
dp[j] = dp[j - 1] + grid[0][j - 1]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SearchInRotatedArray { | |
public int search(int[] nums, int target) { | |
if (nums == null || nums.length == 0) { | |
return -1; | |
} | |
int left = 0; | |
int right = nums.length - 1; | |
int mid; | |
while (left <= right) { | |
mid = left + (right - left) / 2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BSTFromPreOrder { | |
public TreeNode bstFromPreorder(int[] preorder) { | |
TreeNode root = null; | |
for (int val : preorder) { | |
root = insert(root, val); | |
} | |
return root; | |
} | |
private TreeNode insert(TreeNode node, int val) { | |
if (node == null) { |
OlderNewer