xyz
BAN USER- 0of 0 votes
AnswersN cows are standing at the origin on x-axis, each cow has some appetite, in other word hunger index. A cow can sleep of 1 unit of time or eat for one unit of time or move left or move right. There are some vessels placed on the x-axis, they are having infinite supply of fiod. Find minimum time in which all cows appetite would be filled.
- xyz in United States
Input:
cow Apetitte = {1,1}
Vessle locations = {-1,1}
Answer would be 2 since both cow can go in different direction they would eat for one seconds. One second for moving and one second for eating.
This problem looks to be similar to rotten eggs/tomatoes.| Report Duplicate | Flag | PURGE
Allegient SDE-2 Algorithm
If using recursion is allowed then we can generate as
package com.practice.algo;
public class PrinNNString {
private static void printNNString(String s, int n, int k) {
if (k == 1) {
for (int i = 0; i < n; i++) {
System.out.print(s);
}
} else {
for (int i = 0; i < n; i++) {
printNNString(s, n, k - 1);
}
}
}
private static void printNNString(String s, int n) {
printNNString(s, n, n);
System.out.println("");
}
public static void main(String[] args) {
printNNString("Hello", 3);
printNNString("Hello", 2);
printNNString("Hello", 1);
}
}
Algo:
step 1: Convert this string to list of the substrings of size x
step 2: sort individual substring characters make things better use only unique characters from each substring so that duplicate number generation can be avoided
step 3: generate n th string from all these substrings, i.e. generate N the set in the cartesian product of these substring characters.
package com.practice.algo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
public class KthNumberInSorted {
private static List<Integer> getNthNumber(List<List<Integer>> lists, int n) {
int lSize = lists.size();
int[] mul = new int[lSize];
mul[lSize - 1] = 1;
for (int i = lSize - 2; i >= 0; i--) {
mul[i] = mul[i + 1] * lists.get(i + 1).size();
}
List<Integer> l = new ArrayList<>();
n -= 1;
for (int i = 0; i < lSize; i++) {
int r = n / mul[i];
n = n - r * mul[i];
l.add(lists.get(i).get(r));
}
return l;
}
private static String getNthNumber(String s, int x, int k) {
List<Integer> l = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
l.add(s.charAt(i) - '0');
}
List<List<Integer>> listOfLists = new ArrayList<>();
int start = 0;
while (start < s.length()) {
int end = Math.min(s.length(), start + x);
// make it unique
List<Integer> list = new ArrayList<>(new HashSet<>(l.subList(start, end)));
Collections.sort(list);
listOfLists.add(list);
start = end;
}
StringBuilder stringBuilder = new StringBuilder();
for (Integer i : getNthNumber(listOfLists, k)) {
stringBuilder.append(i);
}
return stringBuilder.toString();
}
public static void main(String[] args) {
System.out.println(getNthNumber("1234567891", 5, 3));
System.out.println(getNthNumber("123456789", 3, 3));
}
}
public class MaxWeapons {
private static int getMaxWeapons(int x, int y, int z) {
if (x < y) {
return Math.max(x - z, 0);
}
int count = x;
for (int i = 1; i <= z; i++) {
count = count - (int) Math.ceil(((double) count) / y);
if (count <= 0) {
break;
}
}
return Math.max(0, count);
}
public static void main(String[] args) {
System.out.println(getMaxWeapons(3000, 1000, 1000));
System.out.println(getMaxWeapons(1000, 1200, 1));
System.out.println(getMaxWeapons(1000, 1200, 100));
}
}
To get maximum weapons to the destination we can move all the weapons to one KM and then again full load the truck and carry. So we need to keep doing unloading/loading at 1 KM of distance.
- xyz September 16, 2019#include <iostream>
#include <deque>
using namespace std;
struct stream{
int key;
int index;
};
int main(){
int k,key,n,i = 0;
stream s;
deque< stream > Q;
cin >> k >> key;
while ( i < k-1 && key != -1 ){
while ( !Q.empty() && key >= Q.back().key )
Q.pop_back();
s.key = key;
s.index = i++;
Q.push_back( s );
cin >> key ;
}
while ( key != -1 ) {
cout<< Q.front().key<<"\n";
cin >> key;
while ( !Q.empty() && key >= Q.back().key )
Q.pop_back();
while ( !Q.empty() && ( Q.front().index <= i- k ) )
Q.pop_front();
s.key = key;
s.index = i++;
Q.push_back( s );
}
return 0;
}
This will work in O(N) time and O(k) space and it's taking input one by one .
- xyz July 07, 2015void countBitset(std::string s){
auto a[256], p = 2, z;
a[0] = 0;
a[1] = 1;
while (p < 256){
z = p << 1;
a[p]=1;
for (auto i=p+1;i < z;i++){
a[i] = a[p]+a[i-p];
}
p = z;
}
auto total = 0;
for (auto i=0;i < s.length();i++){
total = total + a[s[i]];
}
std::cout << total;
}
int getHeight(Node *p) {
int height = 0;
while (p) {
height++;
p = p->parent;
}
return height;
}
// As root->parent is NULL, we don't need to pass root in.
Node *LCA(Node *p, Node *q) {
int h1 = getHeight(p);
int h2 = getHeight(q);
// swap both nodes in case p is deeper than q.
if (h1 > h2) {
swap(h1, h2);
swap(p, q);
}
// invariant: h1 <= h2.
int dh = h2 - h1;
for (int h = 0; h < dh; h++)
q = q->parent;
while (p && q) {
if (p == q) return p;
p = p->parent;
q = q->parent;
}
return NULL; // p and q are not in the same tree
}
Algo: Time O(n) and space O(1)
1 . Reverse each word in sentence
2 . Reverse sentence it self
3 . Return sentence
e.g.
I wish you a merry Christmas
step 1 . I hisw uoy a yrrem samtsirhC
step 2 . Christmas merry a you wish I
Method 2:
1. Token the string on space (' ') using strtok() in c++
2. Reverse the all tokens get the answer
O(n) in time and O(n) in space if we store token separately .
else O(n) time and O(1) in space
In python just one line
print ' '.join(s.split()[::-1])
Repmakaylamelua, Blockchain Developer at AMD
I am a sound editing and music composer with experience of handling a wide variety of programs.During my free ...
RepAlmaRJude, Quality Assurance Engineer at Brocade
I am Alma from Livermore USA, I also enjoy reading – my favourite topics being social history, the development and use ...
Repnancyhfloress, Computer Scientist at AMD
Hey there, I’m Nancy. I’m a small business owner living in Sunrise, FL 33323. I am a fan ...
Repashleymbosse, Associate at Accenture
I am an enthusiastic, hard-working and disciplined Catering Assistant with excellent track-record in working in the food industry. I am ...
Repannehbell8, Quality Assurance Engineer at Globaltech Research
Build and maintain relationships with convention vendors. Resolve issues throughout meeting event timelines.Plan room layouts and event programs, schedule ...
Generate n the number using 4,5 now concatenate the number of it's reverse.
- xyz September 17, 2019