Skip to content

Instantly share code, notes, and snippets.

@sajith-rahim
Last active March 10, 2022 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sajith-rahim/e6febccddaf58a7fae88d07533ef840f to your computer and use it in GitHub Desktop.
Save sajith-rahim/e6febccddaf58a7fae88d07533ef840f to your computer and use it in GitHub Desktop.
// Day of week that is K days later
// Given current day as day of the week and an integer K,
// the task is to find the day of the week after K days.
import java.util.*;
class Solution {
public static String KDaysLater(String day, int k){
List<String> days = List.of("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
int index = days.indexOf(day);
index = (index + k) % 7;
return days.get(index);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("KDaysLater");
System.out.println();
String day = scanner.nextLine();
int k = scanner.nextInt();
scanner.close();
System.out.println(KDaysLater(day,k));
}
}
/*
Lexicographically Smallest String
Given a string str, the task is to find the lexicographically
smallest string that can be formed by removing at most one character from the given string.
*/
private static String LexicographicallySmallestString(String str) {
int len = str.length();
for (int i = 0; i < len - 1; i++) {
if (str.charAt(i) > str.charAt(i + 1)) {
return str.substring(0, i) + str.substring(i + 1);
}
}
return str.substring(0, len - 1);
}
/*
Max Network Rank
An infrastructure consisting of n cities from l to n, and m bidirectional roads between them are given. Roads do not intersect apart from at their start and endpoints (they can pass through underground tunnels to avoid collisions).
For each pair of cities directly connected by a road, let’s define their network rank as the total number of roads that are connected to either of the two cities.
Write a function, given two arrays starts, ends consisting of m integers each and an integer n, where starts[i] and ends[i] are cities at the two ends of the i-th road, returns the maximal network rank in the whole infrastructure.
Example:
Input:
starts = [1, 2, 3, 3]
ends = [2, 3, 1, 4]
n = 4
*/
public static int maxNetworkRank(List<Integer> starts, List<Integer> ends, int n) {
int[] edgeCount = new int[n];
int m = starts.size();
int maxRank = Integer.MIN_VALUE;
for (int i = 0; i < m; i++) {
edgeCount[starts.get(i) - 1]++;
edgeCount[ends.get(i) - 1]++;
}
for (int i = 0; i < m; i++) {
int rank = edgeCount[starts.get(i) - 1] + edgeCount[ends.get(i) - 1] - 1;
if (rank > maxRank) {
maxRank = rank;
}
}
return maxRank;
}
//Min Deletions To Obtain String in Right Format
//Given a string with only characters X and Y.
//Find the minimum number of characters to remove from the string
//such that there is no interleaving of character X and Y
//and all the Xs appear before any Y.
import java.util.*;
class Solution {
public static int minDel(String s){
int numX = 0;
int numY = 0;
int minDel;
// count numX
for(char c: s.toCharArray()){
if(c == 'X'){
numX++;
}
}
//initialize minDel : min in deleting all X or Y
minDel = Math.min(numX, s.length()-numX);
for(char c: s.toCharArray()){
if(c == 'X'){
numX--;
}
else{
// to add Y
int del = numX + numY;
minDel = Math.min(minDel, del);
numY++;
}
}
return minDel;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("minDel");
String str = scanner.nextLine();
scanner.close();
System.out.println(minDel(str));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment