Skip to content

BinaryWatch #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions binaryWatch/binaryWatch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Source : https://leetcode.com/problems/binary-watch/

/***************************************************************************************
*
* A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6
* LEDs on the bottom represent the minutes (0-59).
* Each LED represents a zero or one, with the least significant bit on the right.
*
* For example, the above binary watch reads "3:25".
*
* Given a non-negative integer n which represents the number of LEDs that are
* currently on, return all possible times the watch could represent.
*
* Example:
* Input: n = 1Return: ["1:00", "2:00", "4:00", "8:00", "0:01", "0:02", "0:04", "0:08",
* "0:16", "0:32"]
*
* Note:
*
* The order of output does not matter.
* The hour must not contain a leading zero, for example "01:00" is not valid, it
* should be "1:00".
* The minute must be consist of two digits and may contain a leading zero, for example
* "10:2" is not valid, it should be "10:02".
***************************************************************************************/

class Solution {
private:
void combination(int nLED, int nLight, int max, bool zero,
int start, int k, int solution,
vector<vector<string>>& result) {
if (solution > max){
return;
}
if (k == 0) {
char tmp[5] = "";
if (zero) {
sprintf(tmp, "%02d", solution);
}else{
sprintf(tmp, "%d", solution);
}
result[nLight].push_back(tmp);
return;
}
for (int i=start; i<=nLED-k; i++) {
solution += pow(2, i);
combination(nLED, nLight, max, zero, i+1, k-1, solution, result);
solution -= pow(2, i);
}
}

void generate_combination(int nLED, int max, bool zero, vector<vector<string>>& result) {
for (int i=0; i<nLED; i++) {
combination(nLED, i, max, zero, 0, i, 0, result);
}
}

void print(vector<vector<string>>& vv) {
for(auto v : vv) {
cout << "[ ";
for (auto i : v) {
cout << i << " ";
}
cout << "]" << endl;
}
}

private:
vector<vector<string>> hour;
vector<vector<string>> mins;

public:

Solution():hour(4, vector<string>()), mins(6, vector<string>()){
generate_combination(4, 11, false, hour);
//print(hour);
//[ 0 ]
//[ 1 2 4 8 ]
//[ 3 5 9 6 10 ]
//[ 7 11 ]


generate_combination(6, 59, true, mins);
//print(mins);
//[ 00 ]
//[ 01 02 04 08 16 32 ]
//[ 03 05 09 17 33 06 10 18 34 12 20 36 24 40 48 ]
//[ 07 11 19 35 13 21 37 25 41 49 14 22 38 26 42 50 28 44 52 56 ]
//[ 15 23 39 27 43 51 29 45 53 57 30 46 54 58 ]
//[ 31 47 55 59 ]
}

vector<string> readBinaryWatch(int num) {

vector<string> result;
for (int i = 0; i <= 3 && i <= num; i++) {
if (num - i > 5) {
continue;
}
for (auto h : hour[i]) {
for (auto m : mins[num - i]) {
result.push_back( h + ":" + m );
}
}

}
return result;
}
};
52 changes: 52 additions & 0 deletions brokenCalculator/brokenCalculator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Source : https://leetcode.com/problems/broken-calculator/

/*****************************************************************************************************
*
* On a broken calculator that has a number showing on its display, we can perform two operations:
*
* Double: Multiply the number on the display by 2, or;
* Decrement: Subtract 1 from the number on the display.
*
* Initially, the calculator is displaying the number X.
*
* Return the minimum number of operations needed to display the number Y.
*
* Example 1:
*
* Input: X = 2, Y = 3
* Output: 2
* Explanation: Use double operation and then decrement operation {2 -> 4 -> 3}.
*
* Example 2:
*
* Input: X = 5, Y = 8
* Output: 2
* Explanation: Use decrement and then double {5 -> 4 -> 8}.
*
* Example 3:
*
* Input: X = 3, Y = 10
* Output: 3
* Explanation: Use double, decrement and double {3 -> 6 -> 5 -> 10}.
*
* Example 4:
*
* Input: X = 1024, Y = 1
* Output: 1023
* Explanation: Use decrement operations 1023 times.
*
* Note:
*
* 1 <= X <= 10^9
* 1 <= Y <= 10^9
******************************************************************************************************/


class Solution {
public:
int brokenCalc(int X, int Y) {
if (X >= Y) return X-Y ;
if ( Y%2 ==0 ) return brokenCalc(X, Y/2) + 1;
return brokenCalc(X, Y+1) + 1;
}
};
92 changes: 92 additions & 0 deletions buddyStrings/buddyStrings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Source : https://leetcode.com/problems/buddy-strings/description/

/***************************************************************************************
*
* Given two strings A and B of lowercase letters, return true if and only if we can
* swap two letters in A so that the result equals B.
*
*
*
* Example 1:
*
*
*
* Input: A = "ab", B = "ba"
* Output: true
*
*
*
* Example 2:
*
*
* Input: A = "ab", B = "ab"
* Output: false
*
*
*
* Example 3:
*
*
* Input: A = "aa", B = "aa"
* Output: true
*
*
*
* Example 4:
*
*
* Input: A = "aaaaaaabc", B = "aaaaaaacb"
* Output: true
*
*
*
* Example 5:
*
*
* Input: A = "", B = "aa"
* Output: false
*
*
*
*
* Note:
*
*
* 0 <= A.length <= 20000
* 0 <= B.length <= 20000
* A and B consist only of lowercase letters.
*
*
*
*
*
***************************************************************************************/



class Solution {
public:
bool buddyStrings(string A, string B) {
if (A.size() != B.size()) return false;
if (A.size()<2) return false;

bool bRepeat = false;
bool map[26] = {false};
int idx[2], diffCnt=0;

for (int i=0; i<A.size(); i++){
if (map[A[i]-'a']) { bRepeat = true;}
map[A[i]-'a']=true;
if ( A[i] != B[i] ) {
if (diffCnt>=2) return false;
idx[diffCnt++] = i;

}
}
//if A == B and there has repeated chars , then return true
if (diffCnt==0 && bRepeat) return true;

return (A[idx[0]] == B[idx[1]] && A[idx[1]] == B[idx[0]]);

}
};
Loading