codelover
BAN USERimport java.util.ArrayList;
import java.util.Arrays;
public class GoogleSubsetLeastCommon {
public static void compare(ArrayList<ArrayList<String>> data) {
ArrayList<String> temp1;
ArrayList<String> temp2;
int countMaxUncommon = 0;
ArrayList<String> ret = null;
for (int i = 0; i < data.size(); i++) {
if(i != 0) {
for (int j = i-1; j >= 0; j--) {
temp1 = new ArrayList<String>(data.get(i));
temp2 = new ArrayList<String>(data.get(j));
temp1.removeAll(temp2);
if(temp1.size() > countMaxUncommon) {
countMaxUncommon = temp1.size();
ret = data.get(i);
}
}
}
}
System.out.println(ret);
}
public static void main(String[] args) {
ArrayList<ArrayList<String>> data = new ArrayList<>();
data.add(new ArrayList<>(Arrays.asList("a", "b", "c", "d")));
data.add(new ArrayList<>(Arrays.asList("a", "b", "f", "g")));
data.add(new ArrayList<>(Arrays.asList("a", "q", "h", "i")));
data.add(new ArrayList<>(Arrays.asList("j", "k", "l", "m")));
compare(data);
}
}
If I am right then the cost will be same for fixed 3 row of houses, 3 combinations of paint and variable number of columns(houses) N. The program below shows the answer.
Order of complexity O(n*m).
I'm open to suggestions, improvement and learning. :)
public static List<List<string>> InitRows(int row, int columns)
{
List<List<string>> rowOfHouses = new List<List<string>>();
for (int i = 0; i < row; i++)
rowOfHouses.Insert(i, InitHouses(i, columns));
return rowOfHouses;
}
// Create Columns of Houses
public static List<string> InitHouses(int row, int n)
{
List<string> houses = new List<string>();
for (int j = 0; j < n; j++)
houses.Add(("H="+ row + "-" + j).ToString());
return houses;
}
public static List<List<string>> PaintHouses(int rows, int columns, List<List<string>> rowOfHouses)
{
string[] color = { "R", "G", "B" };
int colorSelector = 0;
int[] colorCount = new int[color.Length];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
rowOfHouses[i][j] = (rowOfHouses[i][j] + ":" + color[colorSelector]);
colorCount[colorSelector] = colorCount[colorSelector]+1;
if (colorSelector < 2)
colorSelector++;
else
colorSelector = 0;
}
colorSelector++;
if (colorSelector >= color.Length)
colorSelector = 0;
}
Console.WriteLine("-------------------------------------------------------------");
Console.WriteLine("-------------------------------------------------------------");
Console.WriteLine("Red: " + colorCount[0]);
Console.WriteLine("Green: " + colorCount[1]);
Console.WriteLine("Blue: " + colorCount[2]);
return rowOfHouses;
}
public static void Print(List<List<string>> list)
{
Console.WriteLine("H=[Row]-[Column]:[Color]");
for(int i = 0; i < list.Count; i++)
{
for(int j = 0; j < list[i].Count; j++)
Console.Write(list[i][j] + " ");
Console.Write("\n");
}
}
static void Main(string[] args)
{
// Number of Row of Houses
int rows = 3;
// Number of Columns = N
int columns = 6;
List<List<string>> rowOfHouses = InitRows(rows, columns);
// Print Initial Houses
Print(rowOfHouses);
// Paint Houses
rowOfHouses = PaintHouses(rows, columns, rowOfHouses);
Console.WriteLine("-------------------------------------------------------------");
Print(rowOfHouses);
}
test.txt
02/11/2017 0002
11/11/2017 0004
09/11/2017 0004
04/11/2017 0005
04/11/2017 0007
05/11/2017 0002
10/11/2017 0004
01/11/2017 0002
02/11/2017 0003
03/11/2017 0003
04/11/2017 0003
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Globalization;
namespace PracticeQuestions
{
public class CheckConsecutiveDays
{
int numberOfDays = 0;
List<DateTime> date = new List<DateTime>();
Dictionary<string, List<DateTime>> records = new Dictionary<string, List<DateTime>>();
public CheckConsecutiveDays(string fileName, int noOfDays)
{
numberOfDays = noOfDays;
try
{
string line;
using (StreamReader sr = new StreamReader(fileName))
{
while ((line = sr.ReadLine()) != null)
{
string[] str = line.Split('\t');
if (records.ContainsKey(str[1]))
{
records[str[1]].Add(Convert.ToDateTime(DateTime.ParseExact(str[0], ("dd/mm/yyyy"), new CultureInfo("en-CA"))));
}
else
{
List<DateTime> temp = new List<DateTime>();
temp.Add(Convert.ToDateTime(DateTime.ParseExact(str[0], ("dd/mm/yyyy"), new CultureInfo("en-CA"))));
records.Add(str[1].ToString(), temp);
}
}
}
}
catch (Exception e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
public void PrintCustomerIDList()
{
foreach (string key in records.Keys)
{
int count = 0;
string startDate = "", endDate = "";
records[key].Sort();
for (int i = 0; i < records[key].Count; i++)
{
if (i == 0)
{
count++;
startDate = records[key][i].ToString("dd/mm/yyyy");
}
else
{
int val = (Convert.ToDateTime(records[key][i]).CompareTo(Convert.ToDateTime(records[key][i - 1])));
val = Convert.ToInt32((records[key][i].Date - (records[key][i - 1].Date)).TotalDays);
if (((records[key][i].Date - (records[key][i - 1].Date)).TotalDays == 1) || ((records[key][i].Date - (records[key][i - 1].Date)).TotalDays == 1))
{
count++;
endDate = records[key][i].ToString("dd/mm/yyyy");
}
else
{
count = 1;
startDate = records[key][i].ToString("dd/mm/yyyy");
}
}
if (count > numberOfDays)
{
Console.WriteLine("Key: " + key + " Start Date: " + startDate + " End Date: " + endDate);
}
}
}
}
}
class Program
{
static void Main(string[] args)
{
CheckConsecutiveDays ccd = new CheckConsecutiveDays("test.txt", 2);
ccd.PrintCustomerIDList();
}
}
}
import java.util.ArrayList;
- codelover June 26, 2020import java.util.HashMap;
import java.util.Map;
public class GoogleFunctionSolving {
public static Map<String, Integer> variables = new HashMap<String, Integer>();
public static int fountValues = 0;
public static void initVariables (ArrayList<String> eq) {
for (String line: eq) {
String[] tempSplit = line.split("=");
String LHS = tempSplit[0].trim();
String RHS = tempSplit[1].trim();
String[] subStringLHS = LHS.split("\\+");
String[] subStringRHS = RHS.split("\\+");
for (String c : subStringLHS) {
c = c.trim();
if ((Character.isLetter(c.charAt(0)) == true)) {
variables.put(c, Integer.MIN_VALUE);
}
}
for (String c : subStringRHS) {
c = c.trim();
if ((Character.isLetter(c.charAt(0)) == true)) {
variables.put(c, Integer.MIN_VALUE);
}
}
}
}
public static int countChars(String line) {
String[] elements = line.split(" ");
int count = 0;
for (String element : elements) {
element = element.trim();
if (Character.isLetter(element.charAt(0))) {
if(variables.get(element) == Integer.MIN_VALUE) {
count++;
}
}
}
return count;
}
public static void loopPart(String line) {
int charCount = countChars(line);
if(charCount == 1) {
solve(line);
}
}
public static void solve (String line) {
String[] elements = line.replace("+", "").split(" ");
int LHValue = 0;
int RHValue = 0;
String variable = null;
boolean isVariableLeft = true;
boolean isRHS = false;
for(String element : elements) {
element = element.trim();
if (!element.isEmpty()) {
if (!isRHS) {
if (!element.equals("=")) {
if (!Character.isLetter(element.charAt(0))) {
LHValue += Integer.parseInt(element);
} else {
if (variables.get(element) != Integer.MIN_VALUE) {
LHValue += variables.get(element);
} else {
isVariableLeft = true;
variable = element;
}
}
} else {
isRHS = true;
}
} else {
if (!Character.isLetter(element.charAt(0))) {
RHValue += Integer.parseInt(element);
} else {
if (variables.get(element) != Integer.MIN_VALUE) {
RHValue += variables.get(element);
} else {
isVariableLeft = false;
variable = element;
}
}
}
}
}
if (isVariableLeft) {
variables.put(variable, (RHValue - LHValue));
} else {
variables.put(variable, (LHValue - RHValue));
}
}
public static void main(String[] args) {
ArrayList<String> eq = new ArrayList<>();
eq.add("y = x + 1");
eq.add("5 = x + 3");
eq.add("10 = z + y + 2");
eq.add("a = b + 1");
eq.add("5 = b + 3");
eq.add("10 = d + z + y + a + b");
initVariables(eq);
int end = variables.size();
// O(numberOfLines*numberOfVariables)
for(int i = 0; i < end; i++) {
for (String arr : eq) {
loopPart(arr);
}
}
boolean isSolved = true;
for (String key : variables.keySet()) {
if(variables.get(key) == Integer.MIN_VALUE) {
System.out.println("Cannot be solved");
isSolved = false;
break;
}
}
if(isSolved) {
for (String key : variables.keySet()) {
System.out.println(key + ": " + variables.get(key));
}
}
}
}