image_2021-11-19_17-58-20.png
48.5 KB
#N1047. Remove All Adjacent Duplicates In String
problem link
#solution
problem link
#solution
class Solution {
public String removeDuplicates(String s) {
StringBuilder res=new StringBuilder();
Stack<Character> stack=new Stack<>();
char[] letters=s.toCharArray();
for(int i=letters.length-1; i>=0; i--){
if(!stack.isEmpty()&&letters[i]==stack.peek())
stack.pop();
else
stack.push(letters[i]);
}
while(!stack.isEmpty()){
res.append(stack.pop());
}
return res.toString();
}
}image_2021-11-22_11-40-28.png
37 KB
#N1700. Number of Students Unable to Eat Lunch
problem link
#solution
problem link
#solution
class Solution {
public int countStudents(int[] students, int[] sandwiches) {
int[] count=new int[2];
int i=0;
for(int student:students)
count[student]++;
for(; i<sandwiches.length && count[sandwiches[i]]!=0; i++)
count[sandwiches[i]]--;
return sandwiches.length-i;
}
}image_2021-11-22_18-01-53.png
45.3 KB
#N933. Number of Recent Calls
problem link
#solution
problem link
#solution
class RecentCounter {
Queue<Integer> queue = new LinkedList<>();
public RecentCounter() {
}
public int ping(int t) {
while(!queue.isEmpty() && queue.peek()<t-3000) queue.remove();
queue.add(t);
return queue.size();
}
}image_2021-11-24_13-46-29.png
43.9 KB
#N1656. Design an Ordered Stream
problem link
#solution
problem link
#solution
class OrderedStream {
int index=1;
Map<Integer, String> map=new HashMap<>();
public OrderedStream(int n) {
}
public List<String> insert(int key, String value) {
map.put(key, value);
List<String> list=new ArrayList<>();
while(map.containsKey(index)){
list.add(map.get(index));
index++;
}
return list;
}
}image_2021-11-24_17-35-55.png
43.3 KB
#N938. Range Sum of BST
problem link
#solution
problem link
#solution
class Solution {
int sum=0;
public int rangeSumBST(TreeNode root, int low, int high) {
if(root == null) {
return 0;
}
if(low<=root.val&&high>=root.val) sum+=root.val;
if(root.val>low){
rangeSumBST(root.left, low, high);
}
if(root.val<=high){
rangeSumBST(root.right, low, high);
}
return sum;
}
}image_2021-11-25_13-24-04.png
40.7 KB
#N897. Increasing Order Search Tree
problem link
#solution
problem link
#solution
class Solution {
TreeNode res = new TreeNode();
TreeNode curr=res;
public TreeNode increasingBST(TreeNode root) {
inOrder(root);
return res.right;
}
public void inOrder(TreeNode root){
if(root == null) return;
inOrder(root.left);
curr.right = new TreeNode(root.val);
curr = curr.right;
inOrder(root.right);
}
}image_2021-11-25_13-25-08.png
35.9 KB
#N700. Search in a Binary Search Tree
problem link
#solution
problem link
#solution
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if(root == null) return null;
if(root.val>val){
root = searchBST(root.left, val);
}
else if(root.val<val){
root = searchBST(root.right, val);
}
return root;
}
}image_2021-11-27_01-37-20.png
32.3 KB
#N35. Search Insert Position
problem link
#solution
problem link
#solution
class Solution {
public int searchInsert(int[] nums, int target) {
int start=0, end=nums.length-1, mid;
while(start<=end){
mid=(start+end)/2;
if(nums[mid]==target) return mid;
else if(nums[mid]<target) start=mid+1;
else if(nums[mid]>target) end=mid-1;
}
return start;
}
}image_2021-11-27_02-12-57.png
16.5 KB
#N2057. Smallest Index With Equal Value
problem link
#solution
problem link
#solution
class Solution {
public int smallestEqual(int[] nums) {
for(int i=0; i<nums.length; i++)
if((i-nums[i])%10==0) return i;
return -1;
}
}image_2021-11-27_02-27-01.png
39.4 KB
#N2078. Two Furthest Houses With Different Colors
problem link
#solution
problem link
#solution
class Solution {
public int maxDistance(int[] colors) {
int x1=0, x2=0;
for(int i=colors.length-1; i>0; i--)
if(colors[i]!=colors[0]){
x1=i;
break;
}
for(int i=0; i<colors.length-1; i++)
if(colors[i]!=colors[colors.length-1]) {
x2=colors.length-1-i;
break;
}
return Math.max(x1, x2);
}
}#statistics
Till this time, 144 in total,
133 easy;
11 medium;
type of problem solutions have been posted
Till this time, 144 in total,
133 easy;
11 medium;
type of problem solutions have been posted
image_2021-11-29_17-37-18.png
29.5 KB
#N2089. Find Target Indices After Sorting Array
problem link
#solution
problem link
#solution
class Solution {
public List<Integer> targetIndices(int[] nums, int target) {
Arrays.sort(nums);
List<Integer> list = new ArrayList<>();
for(int i=0; i<nums.length; i++)
if(nums[i]==target) list.add(i);
return list;
}
}image_2021-11-29_18-29-48.png
40.7 KB
#N1991. Find the Middle Index in Array
problem link
#solution
problem link
#solution
class Solution {
public int findMiddleIndex(int[] nums) {
int sum = 0, preSum=0, index=-1;
for(int n:nums)
sum+=n;
for(int i=0; i<nums.length; i++){
if(sum-nums[i]==preSum){
return i;
}
else{
preSum+=nums[i];
sum-=nums[i];
}
}
return index;
}
}image_2021-11-29_18-31-35.png
36.8 KB
#N724. Find Pivot Index
problem link
#solution
problem link
#solution
class Solution {
public int pivotIndex(int[] nums) {
int sum = 0, preSum=0, index=-1;
for(int n:nums)
sum+=n;
for(int i=0; i<nums.length; i++){
if(sum-nums[i]==preSum){
return i;
}
else{
preSum+=nums[i];
sum-=nums[i];
}
}
return index;
}
}image_2021-11-30_11-05-51.png
39.7 KB
#N1822. Sign of the Product of an Array
problem link
#solution
problem link
#solution
class Solution {
public int arraySign(int[] nums) {
long product=1;
for(int n:nums){
if(n<0) product = -product;
if(n==0) return 0;
}
return signFunc(product);
}
int signFunc(long product){
if(product > 0) return 1;
else if(product < 0) return -1;
else return 0;
}
}image_2021-11-30_11-23-41.png
55.2 KB
#medium
#N807. Max Increase to Keep City Skyline
problem link
#solution
#N807. Max Increase to Keep City Skyline
problem link
#solution
class Solution {
public int maxIncreaseKeepingSkyline(int[][] grid) {
int len=grid.length, sum=0;
int[] rowMax = new int[len];
int[] colMax = new int[len];
for(int i=0; i<len; i++)
for(int j=0; j<len; j++)
rowMax[i] = Math.max(rowMax[i], grid[i][j]);
for(int i=0; i<len; i++)
for(int j=0; j<len; j++)
colMax[i] = Math.max(colMax[i], grid[j][i]);
for(int i=0; i<len; i++)
for(int j=0; j<len; j++)
sum += Math.min(rowMax[i], colMax[j])-grid[i][j];
return sum;
}
}
Leetcode in Java && Oracle
image_2021-11-30_11-05-51.png
image_2021-11-30_11-30-57.png
25.8 KB
#python
#N1822. Sign of the Product of an Array
#anotherSolution
#N1822. Sign of the Product of an Array
#anotherSolution
class Solution:
def arraySign(self, nums: List[int]) -> int:
counter = 0
for i in nums:
if i == 0:
return 0
if i < 0:
counter +=1
return 1 if counter % 2 == 0 else -1
image_2021-11-30_16-14-54.png
36.1 KB
#N617. Merge Two Binary Trees
problem link
#solution
problem link
#solution
class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if(root1 == null && root2 == null) return null;
else if(root1 == null) return root2;
else if(root2 == null) return root1;
TreeNode res = new TreeNode(root1.val + root2.val);
res.left = mergeTrees(root1.left, root2.left);
res.right = mergeTrees(root1.right, root2.right);
return res;
}
}image_2021-12-03_18-30-06.png
41.6 KB
#N2053. Kth Distinct String in an Array
problem link
#solution
problem link
#solution
class Solution {
public String kthDistinct(String[] arr, int k) {
Map<String, Integer> map = new HashMap<>();
for(String s:arr){
if(map.containsKey(s)) map.put(s, map.get(s)+1);
else map.put(s, 1);
}
for(String s:arr){
if(map.get(s)==1)
if(k-- == 1)
return s;
}
return "";
}
}#recommendation
Advent of Code is a annually virtual event taking place in every December 1th-25th. Every day features new programming exercises, which can be solved in your programming language of choice.
It is full of interesting algorithmic problems, i recommend it)👍🏻
https://adventofcode.com/2021
Advent of Code is a annually virtual event taking place in every December 1th-25th. Every day features new programming exercises, which can be solved in your programming language of choice.
It is full of interesting algorithmic problems, i recommend it)👍🏻
https://adventofcode.com/2021
image_2021-12-04_14-43-10.png
73.3 KB
#N1160. Find Words That Can Be Formed by Characters
problem link
#solution
problem link
#solution
class Solution {
public int countCharacters(String[] words, String chars) {
int sum=0;
Map<Character, Integer> map = new HashMap<>();
for(char ch:chars.toCharArray()){
if(map.containsKey(ch))
map.put(ch, map.get(ch)+1);
else
map.put(ch, 1);
}
for(String word:words){
Map<Character, Integer> temp = new HashMap<>(map);
boolean isGood = true;
for(char ch:word.toCharArray()){
temp.put(ch, temp.getOrDefault(ch, 0)-1);
if(temp.get(ch)<0) {
isGood=false;
break;
}
}
if(isGood) {
sum+=word.length();
}
}
return sum;
}
}