โ
โ
โ
#HackerRank Amazon Fresh Deliveries - Java Solutions - HackerRank Solutions
import java.util.*;
public class Main
{
public static int[][] closestKLocations(int[][] allLocations, int k) {
PriorityQueue<int[]> pq = new PriorityQueue<>(k+1, new Comparator<int[]>() {
public int compare(int[] a1, int[] a2) {
int x1 = a1[0];
int y1 = a1[1];
int x2 = a2[0];
int y2 = a2[1];
int distance1 = x1*x1 + y1*y1;
int distance2 = x2*x2 + y2*y2;
if (distance1 == distance2) return x2 - x1;
return distance2 - distance1;
}
});
for (int[] location : allLocations) {
pq.add(location);
if (pq.size() > k) pq.poll();
}
int[][] result = new int[k][2];
pq.toArray(result);
return result;
}
public static void main(String[] args) {
int[][] test1 = new int[][] {
{1, 2},
{1, -1},
{3, 4}
};
int[][] result = closestKLocations(test1, 2);
for (int[] each_elt : result) {
System.out.println(String.format("[%d,%d]", each_elt[0], each_elt[1]));
}
}
}
๐ช๐ช๐ช๐ช๐ช๐ช๐ช๐ช๐ช๐ช๐ช๐ช๐ช๐ช๐ช๐ช๐ช๐ช๐ช๐ชimport java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
public class Main {
List<List<Integer>> ClosestXdestinations(int numDestinations, List<List<Integer>> allLocations,
int numDeliveries) {
// CORNER CASE
if (allLocations == null || allLocations.size() == 0 || allLocations.size() < numDeliveries) {
return new ArrayList<>();
}
List<List<Integer>> result = new ArrayList<>();
PriorityQueue<Position> minHeap = new PriorityQueue<>((o1, o2) -> o1.distance - o2.distance);
/*
PART1: add each position into minHeap
*/
for (int i = 0; i < allLocations.size(); i++) {
List<Integer> list = allLocations.get(i);
// Pythagorean Theorem
int distance = list.get(0) * list.get(0) + list.get(1) * list.get(1);
Position p = new Position(list, distance);
minHeap.add(p);
}
/*
PART2: grab the number of numDeliveries from minHeap
*/
for (int i = 0; i < numDestinations && i < numDeliveries; i++) {
result.add(minHeap.poll().list);
}
return result;
}
class Position {
List<Integer> list;
int distance;
public Position(List<Integer> list, int distance) {
this.list = list;
this.distance = distance;
}
}
/*
TEST CASE
*/
public static void main(String[] args) {
Main test = new Main();
int numDestinations = 3;
int numDeliveries = 2;
List<Integer> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();
List<Integer> list3 = new ArrayList<>();
List<List<Integer>> allLocations = new ArrayList<>();
list1.add(1);
list1.add(2);
list2.add(3);
list2.add(4);
list3.add(1);
list3.add(-1);
allLocations.add(list1);
allLocations.add(list2);
allLocations.add(list3);
List<List<Integer>> result = test.ClosestXdestinations(numDestinations, allLocations, numDeliveries);
for (int i = 0; i < result.size() ; i++) {
System.out.println("[" + result.get(i).get(0) + "," + result.get(i).get(1) + "]");
}
}
}
โ
โ
โ
#HackerRank Largest Number of Orders - SQL Solutions
select z
from (select x.customer_ID as z,
count(x.customer_ID) as l
from Orders x, Orders y
where x.ID = y.ID
group by x.customer_ID
order by l desc)
where rownum = 1;
โ
โ
โ
#HackerRank Metro Land Festival - C++ HackerRank Solution
int cost(x, y, a, b) {
return (abs(x-a)+abs(y-b));
}
int minimizeCost(vector<int> numpeople, vector<int> x, vector<int> y){
vector<int> xx, yy;
int ans = 0;
for(int i = 0 ; i < numpeople.size();i++){
int count = numpeople[i];
while(count--){
xx.push_back(x[i]);
yy.push_back(y[i]);
}
}
sort(xx.begin(), xx.end());
sort(yy.begin(), yy.end());
int mx, my;
mx = xx[xx.size() / 2];
my = yy[yy.size() / 2];
for(int i = 0; i < numpeople.size(); i++){
ans += numpeople[i] * cost(mx, my, x[i], y[i]);
}
return ans;
}
๐2
โ
โ
โ
#HackerRank Highly Profitable Months - HackerRank C++ Solutions
int countHighlyProfitableMonths(vector<int> stockPrices,int k){
int n=stockPrices.size(),count=1;
vector<int> a;
for(int i=0;i+1<n;i++){
if(stockPrices[i+1]>stockPrices[i])
count+=1;
else{
a.push_back(count);
count=1;
}
}
a.push_back(count);
int ans=0;
for(auto x:a){
if(x>=k)
ans+=(x-k+1);
}
return ans;
}
โ
โ
โ
#HackerRank Last and Second-Last - Python HackerRank Solutions
def lastLetters(word):
return word[-1]+" "+word[-2]
word="APPLE"
print(lastLetters(word))
โ
โ
โ
#HackerRank Product of the Maximum and Minimum in a Dataset - Java HackerRank Solution
public static List<Long> maxMin(List<String> operations, List<Integer> x) {
List<Long> productArray = new ArrayList<Long>();
List<Long> a=new ArrayList<Long>();
int opsize=operations.size();
for(int y=0;y<opsize;y++)
{
if(operations.get(y).equals("push"))
{
a.add((long)x.get(y));
Collections.sort(a);
productArray.add(a.get(0)*a.get(a.size()-1));
}
else
{
a.remove((long)x.get(y));
Collections.sort(a);
productArray.add(a.get(0)*a.get(a.size()-1));
}
}
return productArray;
}
โ
โ
โ
#HackerRank Reverse Array Queries - Python HackerRank Solutions
def performOperations(arr, operations):
for i in operations:
arr[i[0]:i[1]+1]=reversed(arr[i[0]:i[1]+1])
return arr
โ
โ
โ
#HackerRank Department Summary - SQL HackerRank Solution
select d.name, count(unique(e.id)) as c
from department d, employee e
where d.id = e.dept_id
group by d.name
order by c desc, d.name;
select d.name, 0
from department d
left outer join employee e on d.id = e.dept_id
where e.id is null
group by d.name
order by d.name;
โ
Share in ur College WhatsApp Groupsโ ๏ธโ ๏ธโ ๏ธ#HackerRank Rest API: Country Code - HackerRank Python Solutions
import urllib.request
import json
import re
def getPhoneNumbers(country,phoneNumber):
api="https://jsonmock.hackerrank.com/api/countries?name="
url = api+country
with urllib.request.urlopen(url) as response:
html = response.read()
html = json.loads(html)
code = html["data"][0]["callingCodes"][0]
phoneNumber="+"+str(code)+" "+str(phoneNumber)
return phoneNumber
๐1
โ
โ
โ
#HackerRank EV Positive Prefixes - HackerRank Python Solutions
https://csalgo.tech/threads/ev-positive-prefixes-hackerrank-python-solutions.261/
https://csalgo.tech/threads/ev-positive-prefixes-hackerrank-python-solutions.261/
CS-Algo
HackerRank - EV Positive Prefixes - HackerRank Python Solutions
EV Positive Prefixes - HackerRank Python Solutions
Python 3 - Code
(Hidden text: Visit the forum thread!)
EV Positive Prefixes - HackerRank Python Solutions
Python 3 - Code
(Hidden text: Visit the forum thread!)
EV Positive Prefixes - HackerRank Python Solutions
๐1
โ
โ
โ
#HackerRank EV_First Unique Character - HackerRank C++ Solution
https://csalgo.tech/threads/ev_first-unique-character-hackerrank-c-solution.271/
https://csalgo.tech/threads/ev_first-unique-character-hackerrank-c-solution.271/
CS-Algo
HackerRank - EV_First Unique Character - HackerRank C++ Solution
First Unique Character - HackerRank C++ Solution
(Hidden text: Visit the forum thread!)
First Unique Character - HackerRank C++ Solution
(Hidden text: Visit the forum thread!)
First Unique Character - HackerRank C++ Solution
โ
โ
โ
#HackerRank Expand the Mathematical Expression - HackerRank Python Solution - IBM Code
https://csalgo.tech/threads/expand-the-mathematical-expression-hackerrank-python-solution.276/
https://csalgo.tech/threads/expand-the-mathematical-expression-hackerrank-python-solution.276/
CS-Algo
HackerRank - Expand the Mathematical Expression - HackerRank...
Expand the Mathematical Expression - HackerRank Python Solution
(Hidden text: Visit the forum thread!)
Expand the Mathematical Expression - HackerRank Python Solution
(Hidden text: Visit the forum thread!)
Expand the Mathematical Expression - HackerRank Python Solution
๐1