Skip to content

Instantly share code, notes, and snippets.

@yanhsiah
Last active August 25, 2019 21:10
Show Gist options
  • Save yanhsiah/6c2ff2732ca896a04fd770a54022ffad to your computer and use it in GitHub Desktop.
Save yanhsiah/6c2ff2732ca896a04fd770a54022ffad to your computer and use it in GitHub Desktop.
779. K-th Symbol in Grammar
class Solution {
public:
int kthGrammar(int N, int K) {
if (N == 1 && K == 1) return 0;
if (K % 2) {
return kthGrammar(N - 1, (K + 1) / 2) ? 1 : 0;
} else {
return kthGrammar(N - 1, (K + 1) / 2) ? 0 : 1;
}
/*
value: 0
index: 1
value: 01
index: 1
value: 01 10
index: 2
value: 01 10 10 01
index: 4
value: 01 10 10 01 10 01 01 10
index: 78
*/
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment