You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You are given a sequence of black and white horses, and a set of K stables numbered 1 to K. You have to accommodate the horses into the stables in such a way that the following conditions are satisfied:
3
+
4
+
You fill the horses into the stables preserving the relative order of horses. For instance, you cannot put horse 1 into stable 2 and horse 2 into stable 1. You have to preserve the ordering of the horses.
5
+
No stable should be empty and no horse should be left unaccommodated.
6
+
Take the product (number of white horses * number of black horses) for each stable and take the sum of all these products. This value should be the minimum among all possible accommodation arrangements
7
+
Example:
8
+
9
+
10
+
Input: {WWWB} , K = 2
11
+
Output: 0
12
+
13
+
Explanation:
14
+
We have 3 choices {W, WWB}, {WW, WB}, {WWW, B}
15
+
for first choice we will get 1*0 + 2*1 = 2.
16
+
for second choice we will get 2*0 + 1*1 = 1.
17
+
for third choice we will get 3*0 + 0*1 = 0.
18
+
19
+
Of the 3 choices, the third choice is the best option.
Say you have an array for which the ith element is the price of a given stock on day i.
3
+
4
+
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Say you have an array for which the ith element is the price of a given stock on day i.
3
+
4
+
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
There are N coins (Assume N is even) in a line. Two players take turns to take a coin from one of the ends of the line until there are no more coins left. The player with the larger amount of money wins. Assume that you go first.
3
+
4
+
Write a program which computes the maximum amount of money you can win.
Given two sequences S, T, count number of unique ways in sequence S, to form a subsequence that is identical to the sequence T.
3
+
4
+
Subsequence : A subsequence of a string is a new string which is formed from the original string by deleting some (can be none ) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess.
3
+
4
+
The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately.
5
+
6
+
Some of the rooms are guarded by demons, so the knight loses health (negative integers) upon entering these rooms; other rooms are either empty (0’s) or contain magic orbs that increase the knight’s health (positive integers).
7
+
8
+
In order to reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.
9
+
10
+
Write a function to determine the knight’s minimum initial health so that he is able to rescue the princess.
11
+
12
+
For example, given the dungeon below, the initial health of the knight must be at least 7 if he follows the optimal path
13
+
14
+
RIGHT-> RIGHT -> DOWN -> DOWN.
15
+
16
+
Dungeon Princess: Example 1
17
+
18
+
19
+
20
+
Input arguments to function:
21
+
Your function will get an M*N matrix (2-D array) as input which represents the 2D grid as described in the question. Your function should return an integer corresponding to the knight’s minimum initial health required.
22
+
23
+
24
+
25
+
Note:
26
+
The knight’s health has no upper bound.
27
+
Any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned.
0 commit comments