File tree 4 files changed +163
-0
lines changed
4 files changed +163
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < vector>
2
+ using namespace std ;
3
+
4
+ class Solution {
5
+ public:
6
+ /* *
7
+ * @brief LC2275: Largest Combinations w/ Bitwise AND GT 0
8
+ * Time: O(n logU), n: len(candidate), U = max(candidate)
9
+ * Space: O(1)
10
+ *
11
+ * @param candidates
12
+ * @return int
13
+ */
14
+ int largestCombination (vector<int >& candidates) {
15
+ int m = 0 ;
16
+ int max_val = *max_element (candidates.begin (), candidates.end ());
17
+ while (max_val > 0 ) {
18
+ max_val >>= 1 ;
19
+ m++;
20
+ }
21
+ int ans = 0 ;
22
+ for (int i = 0 ; i < m; i++) {
23
+ int cnt = 0 ;
24
+ for (int x : candidates) {
25
+ cnt += x >> i & 1 ;
26
+ }
27
+ ans = max (ans, cnt);
28
+ }
29
+ return ans;
30
+ }
31
+ };
Original file line number Diff line number Diff line change
1
+ #include < vector>
2
+ using namespace std ;
3
+
4
+ class Solution {
5
+ public:
6
+ /* *
7
+ * @brief LC 3095: Shortest subarray w/ OR at least K
8
+ * Time: O(N), Space: O(1)
9
+ *
10
+ * @param nums
11
+ * @param k
12
+ * @return int
13
+ */
14
+ int minimumSubarrayLength (vector<int >& nums, int k) {
15
+ int ans = INT_MAX, left = 0 , bottom = 0 , right_or = 0 ;
16
+ for (int right = 0 ; right < nums.size (); right++) {
17
+ right_or |= nums[right];
18
+ while (left <= right && (nums[left] | right_or) >= k) {
19
+ ans = min (ans, right - left + 1 );
20
+ left++;
21
+ if (bottom < left) {
22
+ for (int i = right - 1 ; i >= left; i--) {
23
+ nums[i] |= nums[i + 1 ];
24
+ }
25
+ bottom = right;
26
+ right_or = 0 ;
27
+ }
28
+ }
29
+ }
30
+ return ans == INT_MAX ? -1 : ans;
31
+ }
32
+
33
+ /* *
34
+ * @brief LogTrick
35
+ * Time: O(n * logU), Space: O(1)
36
+ *
37
+ * @param nums
38
+ * @param k
39
+ * @return int
40
+ */
41
+ int minimumSubarrayLength1 (vector<int >& nums, int k) {
42
+ int ans = INT_MAX;
43
+ for (int i = 0 ; i < nums.size (); i++) {
44
+ int x = nums[i];
45
+ if (x >= k) return 1 ;
46
+ for (int j = i - 1 ; j >= 0 && (nums[j] | x) != nums[j]; j--) {
47
+ nums[j] |= x;
48
+ if (nums[j] >= k) {
49
+ ans = min (ans, i - j + 1 );
50
+ }
51
+ }
52
+ }
53
+ return ans == INT_MAX ? -1 : ans;
54
+ }
55
+ };
Original file line number Diff line number Diff line change
1
+ #include < vector>
2
+ using namespace std ;
3
+
4
+ class Solution {
5
+ public:
6
+ /* *
7
+ * @brief LC2239: find closest num to zero [E]
8
+ * Time: O(N), Space: O(1)
9
+ *
10
+ * @param nums
11
+ * @return int
12
+ */
13
+ int findClosestNumber (vector<int >& nums) {
14
+ int ans = INT_MAX;
15
+ for (int num : nums) {
16
+ if (abs (num) < abs (ans)) {
17
+ ans = num;
18
+ }
19
+ if (abs (num) == abs (ans)) {
20
+ ans = max (ans, num);
21
+ }
22
+ }
23
+ return ans;
24
+ }
25
+ };
Original file line number Diff line number Diff line change
1
+ #include < string>
2
+ using namespace std ;
3
+
4
+ class Solution {
5
+ public:
6
+ /* *
7
+ * @brief LC3297: count substrings that can be rearranged to contain a string
8
+ * Time: O(N + |\Sigma|)
9
+ *
10
+ * @param word1
11
+ * @param word2
12
+ * @return long long
13
+ */
14
+ long long validSubstringCount (string word1, string word2) {
15
+ if (word1.length () < word2.length ()) return 0 ;
16
+
17
+ int diff[26 ]{}; // word1 and word2 char diffs
18
+ for (char c : word2) {
19
+ diff[c - ' a' ]++;
20
+ }
21
+
22
+ // count how many alphabet chars that less than word2 in window
23
+ int less = 0 ;
24
+ for (int d : diff) {
25
+ if (d > 0 ) {
26
+ less++;
27
+ }
28
+ }
29
+
30
+ long long ans = 0 ;
31
+ int left = 0 ;
32
+ for (char c : word1) {
33
+ diff[c - ' a' ]--;
34
+ if (diff[c - ' a' ] == 0 ) {
35
+ // move c to window, occur of c same as word2
36
+ less--;
37
+ }
38
+ while (less == 0 ) { // window is right
39
+ char out_char = word1[left++] - ' a' ; // to move out of window
40
+ if (diff[out_char] == 0 ) {
41
+ // before out_char move out of window, check occur
42
+ // if out_char in window is same as word2 occur
43
+ // when out_char move out of window, out_char in window lt word2
44
+ less++;
45
+ }
46
+ diff[out_char]++;
47
+ }
48
+ ans += left;
49
+ }
50
+ return ans;
51
+ }
52
+ };
You can’t perform that action at this time.
0 commit comments