mayankjaiho8
BAN USER// package whatever; // don't place package name!
import java.io.*;
import java.util.*;
class MyCode
{
private static int getMinCost(int[] arr, int length)
{
return getMinCost(arr, 0, length, 0, arr.length-1);
}
private static int getMinCost(int[] arr, int startLength, int endLength, int startIdx, int endIdx)
{
if(startIdx > endIdx)
return 0;
System.out.println("startIdx -> " + startIdx + " endIdx -> " + endIdx);
int currentCost = endLength - startLength;
int minCost = Integer.MAX_VALUE;
for(int i=startIdx; i <= endIdx; i++)
{
int currentMinCost = getMinCost(arr, startLength, arr[i], startIdx, i-1) +
getMinCost(arr, arr[i], endLength, i+1, endIdx);
if(currentMinCost < minCost)
minCost = currentMinCost;
}
return currentCost + minCost;
}
public static void main (String[] args)
{
int arr[] = new int[3];
arr[0] = 2;
arr[1] = 4;
arr[2] = 7;
int length = 10;
int minCost = getMinCost(arr, length);
System.out.println("Min Cost -> " + minCost);
}
}
private static boolean isStringsSimilar(String longStr, String shortStr)
{
int longStrIndex = longStr.length()-1;
int shortStrIndex = shortStr.length()-1;
while(longStrIndex >=0 && shortStrIndex >=0)
{
if(longStrIndex > 0 && longStr.charAt(longStrIndex) == 'b'
&& longStr.charAt(longStrIndex - 1) == new Character('\\'))
{
longStrIndex = longStrIndex -3;
continue;
}
else if(longStrIndex > 1 && longStr.charAt(longStrIndex -1) == 'c'
&& longStr.charAt(longStrIndex -2) == new Character('\\'))
{
if(Character.toUpperCase(longStr.charAt(longStrIndex)) != shortStr.charAt(shortStrIndex))
{
return false;
}
longStrIndex = longStrIndex -3;
}
else
{
if(longStr.charAt(longStrIndex) != shortStr.charAt(shortStrIndex))
{
return false;
}
longStrIndex--;
}
shortStrIndex--;
}
return (longStrIndex < 0 && shortStrIndex < 0);
}
// package whatever; // don't place package name!
import java.io.*;
import java.util.*;
class MyCode
{
private static class LinkedListNode
{
int data;
LinkedListNode next;
}
private static class LinkedList
{
LinkedListNode head;
void add(int data)
{
LinkedListNode node = new LinkedListNode();
node.data = data;
if(head == null)
{
head = node;
}
else
{
LinkedListNode currentNode = new LinkedListNode();
currentNode = head;
while(currentNode.next != null)
{
currentNode = currentNode.next;
}
currentNode.next = node;
}
}
void traverse()
{
LinkedListNode currentNode = head;
if(currentNode == null)
{
System.out.println("List is empty");
return;
}
while(currentNode.next != null)
{
System.out.print(currentNode.data + " ->");
currentNode = currentNode.next;
}
System.out.print(currentNode.data);
}
}
static int getMajor(LinkedList list)
{
HashSet<Integer> majorElemList = new HashSet<Integer>();
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
LinkedListNode currentNode = list.head;
if(currentNode == null)
return -1;
while(currentNode != null)
{
int currElem = currentNode.data;
if(!map.containsKey(currElem))
{
map.put(currElem, 1);
}
else
{
map.put(currElem, map.get(currElem) + 1);
}
if(majorElemList.size() == 0)
{
majorElemList.add(currElem);
}
else
{
int currElemFreq = map.get(currElem);
Iterator iter = majorElemList.iterator();
int majorElemFreq = map.get((int)iter.next());
if(currElemFreq == majorElemFreq)
{
majorElemList.add(currElem);
}
else if(currElemFreq > majorElemFreq)
{
majorElemList.clear();
majorElemList.add(currElem);
}
}
currentNode = currentNode.next;
}
int majorElem = -1;
if(majorElemList.size() == 1)
{
Iterator iter = majorElemList.iterator();
majorElem = (int)iter.next();
}
else
{
currentNode = list.head;
while(currentNode != null)
{
if(majorElemList.contains(currentNode.data))
{
majorElem = currentNode.data;
break;
}
currentNode = currentNode.next;
}
}
return majorElem;
}
public static void main (String[] args)
{
LinkedList list = new LinkedList();
list.add(4);
list.add(3);
list.add(5);
list.add(3);
list.add(4);
list.add(5);
//list.add(2);
int majorElem = getMajor(list);
//list.traverse();
System.out.println("Major element is " + majorElem);
}
}
Reploreleijhansen, Aghori Mahakal Tantrik at ABC TECH SUPPORT
I am Lorelei.I am working in a store as a Bonus clerk promoting the development, and implementation and solutions ...
Repharryhamesh, Android Engineer at ADP
I placement officers usually work in colleges and universities. One of My friends taught me about prayers that break curses ...
Repjoankelly306, Site Manager at EFI
Hi, I am Joan from Fairbanks, in USA. I have been a Food Product Manager in a Food Barn Company ...
RepEvieBBlack, AT&T Customer service email at ASAPInfosystemsPvtLtd
I am Evie from Santa Fe Springs USA, I am working as a manager in a worldwide company. I also ...
- mayankjaiho8 May 30, 2018