Leetcode in Java && Oracle
422 subscribers
8 photos
397 files
400 links
Second channel: @codeforces_java

Let's Develop Together!
Download Telegram
image_2022-04-01_16-31-47.png
55.6 KB
#N953. Verifying an Alien Dictionary
problem link

#solution
class Solution {
public boolean isAlienSorted(String[] words, String order) {

for(int i=0; i<words.length-1; i++){
if(!check(words[i], words[i+1], order))
return false;
}

return true;
}

public boolean check(String s1, String s2, String order){
for(int i=0; i<Math.min(s1.length(), s2.length()); i++){
if(order.indexOf(s1.charAt(i))<order.indexOf(s2.charAt(i)))
return true;
if(order.indexOf(s1.charAt(i))>order.indexOf(s2.charAt(i)))
return false;
}

return s1.length()<=s2.length();
}
}
part_1.jpg
226.3 KB
#N705. Design HashSet
problem link

#solution
class MyHashSet {
List<Integer>[] container = null;
int cap = 1000;
double loadFactor = 0.75;
int count = 0;
public MyHashSet() {
container = new LinkedList[cap];
}
public void add(int key) {
if(contains(key)) return;
if(loadFactor*cap == count){
count = 0;
//rehash
cap *= 2;
List<Integer>[] oldC = container;
container = new LinkedList[cap];
for(int i=0; i < oldC.length; i++){
List<Integer> list = oldC[i];
if(list != null){
for(int entry : list)
this.add(entry);
}
}
}
int hash = key % cap;
if(container[hash] == null)
container[hash] = new LinkedList<>();
container[hash].add(key);
++count;
}
part_2.jpg
182 KB
    public void remove(int key) {
int hash = key % cap;
List<Integer> list = container[hash];
if(list != null){
Iterator<Integer> itr = list.iterator();
while(itr.hasNext())
if(itr.next() == key){
itr.remove();
--count;
break;
}
}
}
public boolean contains(int key) {
int hash = key % cap;
List<Integer> list = container[hash];
if(list != null){
Iterator<Integer> itr = list.iterator();
while(itr.hasNext())
if(itr.next() == key)
return true;
}
return false;
}
}
👍2
image_2022-04-03_13-28-26.png
70.5 KB
#medium

#N1396. Design Underground System
problem link

#solution
class UndergroundSystem {
Map<Integer, Pair<String, Integer>> checkInMap = new HashMap<>();
Map<String, Pair<Integer, Integer>> timeMap = new HashMap<>();
public UndergroundSystem() {}
public void checkIn(int id, String stationName, int t) {
checkInMap.put(id, new Pair<>(stationName, t));
}
public void checkOut(int id, String stationName, int t) {
Pair<String, Integer> checkIn = checkInMap.get(id);
String stations = checkIn.getKey()+"-"+stationName;
Pair<Integer, Integer> times = timeMap.getOrDefault(stations, new Pair<>(0, 0));
int sum=(t-checkIn.getValue())+times.getKey();
timeMap.put(stations, new Pair<>(sum, times.getValue()+1));
}
public double getAverageTime(String startStation, String endStation) {
Pair<Integer, Integer> times = timeMap.get(startStation+"-"+endStation);
return (double)times.getKey()/times.getValue();
}
}
image_2022-04-03_13-33-53.png
26.2 KB
#N104. Maximum Depth of Binary Tree
problem link

#solution
class Solution {
public int maxDepth(TreeNode root) {
if(root==null){
return 0;
}
return 1+Math.max(maxDepth(root.left),maxDepth(root.right));
}
}
image_2022-04-03_13-50-35.png
59.6 KB
#N1356. Sort Integers by The Number of 1 Bits
problem link

#solution
class Solution {
public int[] sortByBits(int[] arr) {
int n=arr.length;
for (int i = 0; i < n-1; i++){
for (int j = 0; j < n-i-1; j++){
if (func(arr[j]) > func(arr[j+1]) || (arr[j] > arr[j+1] && func(arr[j]) == func(arr[j+1])))
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
return arr;
}
public int func(int n) {
int count=0;
while(n!=0){
if((n&1)==1)
count++;
n=n>>>1;
}
return count;
}
}
🔥2
image_2022-04-03_14-25-13.png
57 KB
#medium

#N1357. Apply Discount Every n Orders
problem link

#solution
class Cashier {
int count=0, order;
double discountProduct;
Map<Integer, Integer> map = new HashMap<>();

public Cashier(int n, int discount, int[] products, int[] prices) {
this.order=n;
this.discountProduct=1-(double)discount/100;
for(int i=0; i<prices.length; i++){
map.put(products[i], prices[i]);
}
}

public double getBill(int[] product, int[] amount) {
count++;
int total=0;
for(int i=0; i<amount.length; i++){
total+=map.get(product[i])*amount[i];
}

return count%order==0? total*discountProduct : total;
}
}
image_2022-04-03_14-42-37.png
67.9 KB
#medium

#N2043. Simple Bank System
problem link
#solution
class Bank {
long [] balance;
public Bank(long[] balance) {this.balance=balance;}

public boolean transfer(int account1, int account2, long money) {
if(account1>balance.length || account2>balance.length || balance[--account1]<money)
return false;

balance[account1]-=money;
balance[account2-1]+=money;
return true;
}

public boolean deposit(int account, long money) {
if(account>balance.length)
return false;
balance[account-1]+=money;
return true;
}

public boolean withdraw(int account, long money) {
if(account>balance.length || balance[--account]<money)
return false;
balance[account]-=money;
return true;
}
}
🔥31
#N1795. Rearrange Products Table
problem link

#solution
select product_id, 'store1' as store, store1 as price from products where store1 is not null
union
select product_id, 'store2' as store, store2 as price from products where store2 is not null
union
select product_id, 'store3' as store, store3 as price from products where store3 is not null
image_2022-04-06_11-13-29.png
30.5 KB
#N2073. Time Needed to Buy Tickets
problem link
#solution
class Solution {
public int timeRequiredToBuy(int[] tickets, int k) {
int time=0;
while(tickets[k]!=0){
for(int i=0; i<tickets.length; i++){
if(tickets[i]-- > 0) time++;
if(tickets[k]==0) break;
}
}

return time;
}
}
image_2022-04-06_11-26-11.png
36.2 KB
#N387. First Unique Character in a String
problem link
#solution
class Solution {
public int firstUniqChar(String s) {
int arr[] = new int[26];
for(Character ch: s.toCharArray()){
arr[ch-'a']++;
}
for(Character ch: s.toCharArray()){
if(arr[ch-'a']==1)
return s.indexOf(ch);
}

return -1;
}
}
image_2022-04-06_11-50-48.png
47.7 KB
#N225. Implement Stack using Queues
problem link
#solution
class MyStack {
Queue<Integer> q = new LinkedList<>();
Queue<Integer> temp = new LinkedList<>();
public MyStack() {}
public void push(int x) {
q.add(x);
for(int i=0;i<q.size()-1;i++)
{
q.add(q.poll());
}
}
public int pop() {
return q.remove();
}
public int top() {
return q.peek();
}
public boolean empty() {
return q.isEmpty();
}
}
image_2022-04-11_23-33-26.png
43.6 KB
#medium
#N11. Container With Most Water
problem link
#solution
class Solution {
public int maxArea(int[] height) {
int area=0, temp;
int left=0, right=height.length-1;
while(left<right){
temp=(right-left)*Math.min(height[left], height[right]);
area=Math.max(area, temp);
if(height[left]>height[right]) {
right--;
} else if(height[left]<height[right]) {
left++;
} else {
left++;
right--;
}
}
return area;
}
}
image_2022-04-12_00-23-56.png
52.4 KB
#N1260. Shift 2D Grid
problem link
#solution
class Solution {
public List<List<Integer>> shiftGrid(int[][] grid, int k) {
int m=grid.length, n=grid[0].length, counter=m*n-k%(m*n);
List<Integer> list = new ArrayList<>();
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
list.add(grid[i][j]);
}
}
List<Integer> piece;
List<List<Integer>> ans = new ArrayList<>();
for(int i=0; i<m; i++){
piece = new ArrayList<>();
for(int j=0; j<n; j++){
piece.add(list.get(counter++ % (m*n)));
}
ans.add(piece);
}
return ans;
}
}
👍2
image_2022-04-12_12-41-42.png
26.2 KB
#N1581. Customer Who Visited but Did Not Make Any Transactions
problem link
#solution
select v.customer_id, count(*) as count_no_trans
from visits v
left join transactions t
on v.visit_id=t.visit_id
where t.transaction_id is null
group by customer_id
image_2022-04-12_13-03-59.png
40.4 KB
#medium
#N2181. Merge Nodes in Between Zeros
problem link
#solution
class Solution {
public ListNode mergeNodes(ListNode head) {
ListNode slow = head.next;
ListNode fast = head.next;
int sum=0;
while(fast!=null){
sum+=fast.val;
if(fast.val==0){
slow.val=sum;
slow.next=fast.next;
slow=slow.next;
sum=0;
}
fast=fast.next;
}
return head.next;
}
}
image_2022-04-12_13-28-44.png
49.7 KB
#medium
#N1315. Sum of Nodes with Even-Valued Grandparent
problem link
#solution
class Solution {
int sum=0;
public int sumEvenGrandparent(TreeNode root) {
if(root==null) return 0;
helper(root, null, null);
return sum;
}

public void helper(TreeNode node, TreeNode p, TreeNode gp){
if(node == null) return;

if(gp!=null && gp.val%2==0){
sum+=node.val;
}

helper(node.left, node, p);
helper(node.right, node, p);
}
}
image_2022-04-12_13-40-26.png
23.9 KB
#N1484. Group Sold Products By The Date
problem link
#solution
select sell_date,
count(distinct product) as num_sold,
group_concat(distinct product order by product) as products
from activities
group by sell_date
image_2022-04-12_23-28-23.png
29.9 KB
#medium
#N1038. Binary Search Tree to Greater Sum Tree
problem link
#solution
class Solution {
int sum=0;
public TreeNode bstToGst(TreeNode root) {
if(root==null) return null;
bstToGst(root.right);
sum+=root.val;
root.val=sum;
bstToGst(root.left);

return root;
}
}
image_2022-04-12_23-30-41.png
30.1 KB
#medium
#N538. Convert BST to Greater Tree
problem link
#solution
class Solution {
int sum=0;
public TreeNode convertBST(TreeNode root) {
if(root==null) return null;
convertBST(root.right);
sum+=root.val;
root.val=sum;
convertBST(root.left);

return root;
}
}