kvdeshmukh1989
BAN USERI think we can convert time to 2400 format and round it to floor hundred value.e.g. 01:15-> 1315->1300.
and then store them in following HashMap
HashMap<Integer, ArrayList<TimeOrderNode>> timeOrderMap;
public class TimeOrderNode {
public int time;
public int orderCount;
}
TimeOrderNode root = new TimeOrderNode();
root.time = 1205;
root.orderCount = 5;
ArrayList<TimeOrderNode> list = new ArrayLIst<>();
list.put(root);
timeOrderMap.put(1200, list);
The ArrayList will not grow beyond 60 mins, so traversing will be constant time.
Do you think its a good approach??
public static int sumTheNumbers(String s) {
int sum = 0;
if (s == null || s.isEmpty()) {
return sum;
}
int currentValue = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (Character.isDigit(c)) {
if (currentValue > 0) {
currentValue = currentValue * 10 + Character.getNumericValue(c);
} else {
currentValue = Character.getNumericValue(c);
}
} else {
sum = sum + currentValue;
currentValue = 0;
}
}
return sum + currentValue;
}
public static String removeDuplicates(String input) {
//1) Doing it in O(n)
//2) Maintaining the order
//3) Takes O(n) space but then if it needs to be done in place then we need to take chararray which also takes O(n);
LinkedHashSet<Character> visited = new LinkedHashSet<Character>();
StringBuilder sb = new StringBuilder();
for(int i = 0; i<input.length(); i++){
if(!visited.contains(input.charAt(i))){
visited.add(input.charAt(i));
sb.append(input.charAt(i));
}
}
return sb.toString();
}
Updated the approach and removed the iterator
- kvdeshmukh1989 December 22, 2016public static String removeDuplicates(String input) {
//1) Doing it in O(n)
//2) Maintaining the order
//3) Takes O(n) space but then if it needs to be done in place then we need to take chararray which also takes O(n);
LinkedHashSet<Character> visited = new LinkedHashSet<Character>();
for(int i = 0; i<input.length(); i++){
if(!visited.contains(input.charAt(i))){
visited.add(input.charAt(i));
}
}
StringBuilder sb = new StringBuilder();
Iterator<Character> it = visited.iterator();
while(it.hasNext()){
sb.append(it.next());
}
return sb.toString();
}
public static int getPMean(int nums[]){
int maxPMeanValue = Integer.MIN_VALUE;
for(int i =0; i<nums.length;i++){
int pmean = calcuateMean(nums, i);
if(pmean > maxPMeanValue){
maxPMeanValue = pmean;
}
}
return maxPMeanValue;
}
private static int calcuateMean(int nums[], int startIndex){
int sum = 0;
int n = nums.length;
for(int i =0; i<nums.length;i++){
sum = sum + (nums[startIndex] *(i+1));
startIndex = (startIndex+1)%n;
}
return sum;
}
}
- kvdeshmukh1989 December 21, 2016I solved this using DFS.
As far as I have understood this problem, it says that if that for each city, calculate the traffic coming from its neighboring city.
Following is my solution which works in O(n) time and space.
public static void findTrafficDetails(Graph g, int cityNum){
Vertex city = g.findVertex(cityNum);
HashSet<Integer> visited = new HashSet<>();
for(Vertex child: city.adjacencyList){
System.out.print(child + " ");
int val = calculateTraffic(child, visited);
System.out.println(val);
}
}
private static int calculateTraffic(Vertex v, HashSet<Integer> visited) {
visited.add(v.data);
int trafficCount = v.data;
for (Vertex adjacentVertex : v.adjacencyList) {
if (!visited.contains(adjacentVertex.data)) {
trafficCount = trafficCount+ calculateTraffic(adjacentVertex, visited);
}
}
return trafficCount;
}
public static void main(String[] args) {
Graph graph = new Graph();
graph.addEdge(5, 1);
graph.addEdge(5, 2);
graph.addEdge(5, 3);
graph.addEdge(5, 4);
findTrafficDetails(graph, 1);
}
Can be solved using level order traversal in O(n) time.
private static int getCountBetweenRange(Node root, int min, int max){
if (root == null) {
return 0;
}
Queue<Node> helperQueue = new LinkedList<Node>();
helperQueue.add(root);
int count =0;
while(!helperQueue.isEmpty()){
Node temp = helperQueue.remove();
if(temp.data>= min && temp.data<=max){
count++;
}
if (temp.left != null) {
helperQueue.add(temp.left);
}
if (temp.right != null) {
helperQueue.add(temp.right);
}
}
return count;
}
Sorry about the formatting in last one.
Here's my approach based on the pre-order traversal and stack.
public static void printStack(Stack<Integer> nodeData){
Iterator<Integer> it = nodeData.iterator();
System.out.println("Start Stack");
while(it.hasNext()){
int data = it.next();
System.out.println(data);
}
System.out.println("End Stack");
}
public static void printAllPath(TreeNode root, Stack<Integer> nodeData){
if(root!=null){
nodeData.push(root.data);
printAllPath(root.left, nodeData);
printAllPath(root.right, nodeData);
nodeData.pop();
}
if(root != null && root.left == null && root.right == null){
nodeData.push(root.data);
printStack(nodeData);
nodeData.pop();
}
}
public static void printStack(Stack<Integer> nodeData){
Iterator<Integer> it = nodeData.iterator();
System.out.println("Start Stack");
while(it.hasNext()){
int data = it.next();
System.out.println(data);
}
System.out.println("End Stack");
}
public static void printAllPath(TreeNode root, Stack<Integer> nodeData){
if(root!=null){
nodeData.push(root.data);
printAllPath(root.left, nodeData);
printAllPath(root.right, nodeData);
nodeData.pop();
}
if(root != null && root.left == null && root.right == null){
nodeData.push(root.data);
printStack(nodeData);
nodeData.pop();
}
}
Repeleanormzavala63, Android Engineer at 247quickbookshelp
Hello I am a publicity agent, I have completed all my studies from Brighton. And now-a-days I am working as ...
Repjanetjgonzales87, Android Engineer at 247quickbookshelp
Hello I am an internet developer. And I am very much sorry to travel and read some interesting books. Right ...
Repjoeevansjoe6, Repairer at Monlinks
I have been functioning as a repairman at monlinks organization for a long time . Here I learn numerous things . My ...
Assumptions:
1) Number on input streams in known.
2) No random addition of a stream on the fly.
3) The size of input stream is infinite so assuming its a linked list.
- kvdeshmukh1989 October 19, 2018