├── HDU └── README.md ├── LeetCode ├── leetcode_nimgame.cpp ├── leetcode_bulb-switcher.cpp ├── leetcode_adddigits.cpp ├── leetcode_power-of-two.cpp ├── leetcode_power-of-four.cpp ├── leetcode_power-of-three.cpp ├── leetcode_longest-uncommon-subsequence-i.cpp ├── leetcode_sort-an-array.cpp ├── leetcode_to-lower-case.cpp ├── leetcode_minimum-operations-to-make-array-equal.cpp ├── leetcode_valid-boomerang.cpp ├── leetcode_singlenumber.cpp ├── leetcode_angle-between-hands-of-a-clock.cpp ├── leetcode_number-of-1-bits.cpp ├── leetcode_squares-of-a-sorted-array.cpp ├── leetcode_remove-9.cpp ├── leetcode_jumpgame.cpp ├── leetcode_hamming-distance.cpp ├── leetcode_poor-pigs.cpp ├── leetcode_range-addition-ii.cpp ├── leetcode_running-sum-of-1d-array.cpp ├── leetcode_water-and-jug-problem.cpp ├── leetcode_check-if-a-number-is-majority-element-in-a-sorted-array.cpp ├── leetcode_magical-string.cpp ├── leetcode_jump-game.cpp ├── leetcode_game-of-nim.cpp ├── leetcode_climbing-stairs.cpp ├── leetcode_is-subsequence.cpp ├── leetcode_number-of-good-pairs.cpp ├── leetcode_print-immutable-linked-list-in-reverse.cpp ├── leetcode_sum-of-digits-in-base-k.cpp ├── leetcode_next-greater-element-iii.cpp ├── leetcode_reverse-string.cpp ├── leetcode_find-numbers-with-even-number-of-digits.cpp ├── leetcode_rectangle-overlap.cpp ├── leetcode_array-partition-i.cpp ├── leetcode_complement-of-base-10-integer.cpp ├── leetcode_counting-elements.cpp ├── leetcode_fibonacci-number.cpp ├── leetcode_xor-operation-in-an-array.cpp ├── leetcode_defanging-an-ip-address.cpp ├── leetcode_fixed-point.cpp ├── leetcode_number-of-steps-to-reduce-a-number-to-zero.cpp ├── leetcode_remove-vowels-from-a-string.cpp ├── leetcode_subtract-the-product-and-sum-of-digits-of-an-integer.cpp ├── leetcode_maximum-product-difference-between-two-pairs.cpp ├── leetcode_besttimetobuyandsellstockII.cpp ├── leetcode_smallest-range-i.cpp ├── leetcode_contains-duplicate.cpp ├── leetcode_perfect-number.cpp ├── leetcode_build-array-from-permutation.cpp ├── leetcode_calculate-money-in-leetcode-bank.cpp ├── leetcode_factorial-trailing-zeroes.cpp ├── leetcode_construct-the-rectangle.cpp ├── leetcode_convert-a-number-to-hexadecimal.cpp ├── leetcode_find-the-highest-altitude.cpp ├── leetcode_number-of-days-in-a-month.cpp ├── leetcode_shuffle-string.cpp ├── leetcode_coin-change-2.cpp ├── leetcode_richest-customer-wealth.cpp ├── leetcode_bulb-switcher-iv.cpp ├── leetcode_max-consecutive-ones.cpp ├── leetcode_peak-index-in-a-mountain-array.cpp ├── leetcode_determine-color-of-a-chessboard-square.cpp ├── leetcode_elimination-game.cpp ├── leetcode_sort-colors.cpp ├── leetcode_uniquepaths.cpp ├── leetcode_count-odd-numbers-in-an-interval-range.cpp ├── leetcode_uniquebinarysearchtrees.cpp ├── leetcode_replace-all-digits-with-characters.cpp ├── leetcode_count-all-valid-pickup-and-delivery-options.cpp ├── leetcode_greatest-common-divisor-of-strings.cpp ├── leetcode_removeelement.cpp ├── leetcode_binary-number-with-alternating-bits.cpp ├── leetcode_longest-harmonious-subsequence.cpp ├── leetcode_minimum-value-to-get-positive-step-by-step-sum.cpp ├── leetcode_partitioning-into-minimum-number-of-deci-binary-numbers.cpp ├── leetcode_patching-array.cpp ├── leetcode_decode-xored-array.cpp ├── leetcode_pascaltriangleII.cpp ├── leetcode_minimum-moves-to-equal-array-elements.cpp ├── leetcode_generate-a-string-with-characters-that-have-odd-counts.cpp ├── leetcode_k-th-symbol-in-grammar.cpp ├── leetcode_shuffle-the-array.cpp ├── leetcode_n-th-tribonacci-number.cpp ├── leetcode_split-a-string-in-balanced-strings.cpp ├── leetcode_broken-calculator.cpp ├── leetcode_consecutive-characters.cpp ├── leetcode_largest-odd-number-in-string.cpp ├── leetcode_majority-element.cpp ├── leetcode_maximum-number-of-consecutive-values-you-can-make.cpp ├── leetcode_the-kth-factor-of-n.cpp ├── leetcode_triangle.cpp ├── leetcode_wigglesort.cpp ├── leetcode_count-of-matches-in-tournament.cpp ├── leetcode_isomorphic-strings.cpp ├── leetcode_largest-perimeter-triangle.cpp ├── leetcode_last-moment-before-all-ants-fall-out-of-a-plank.cpp ├── leetcode_graycode.cpp ├── leetcode_find-permutation.cpp ├── leetcode_maximum-subarray.cpp ├── leetcode_mean-of-array-after-removing-some-elements.cpp ├── leetcode_minimize-maximum-pair-sum-in-array.cpp ├── leetcode_container-with-most-water.cpp ├── leetcode_domino-and-tromino-tiling.cpp ├── leetcode_remove-palindromic-subsequences.cpp ├── leetcode_closest-divisors.cpp ├── leetcode_replace-elements-with-greatest-element-on-right-side.cpp ├── leetcode_transpose-matrix.cpp ├── leetcode_uglynumber.cpp ├── leetcode_check-if-all-1s-are-at-least-length-k-places-away.cpp ├── leetcode_gas-station.cpp ├── leetcode_maximum-number-of-coins-you-can-get.cpp ├── leetcode_optimal-division.cpp ├── leetcode_search-in-a-binary-search-tree.cpp ├── leetcode_best-sightseeing-pair.cpp ├── leetcode_sign-of-the-product-of-an-array.cpp ├── leetcode_assign-cookies.cpp ├── leetcode_maximum-69-number.cpp ├── leetcode_maximum-nesting-depth-of-the-parentheses.cpp ├── leetcode_number-of-burgers-with-no-waste-of-ingredients.cpp ├── leetcode_binary-subarrays-with-sum.cpp ├── leetcode_decompress-run-length-encoded-list.cpp ├── leetcode_find-n-unique-integers-sum-up-to-zero.cpp ├── leetcode_minimize-product-sum-of-two-arrays.cpp ├── leetcode_maximum-average-subarray-i.cpp ├── leetcode_shortest-unsorted-continuous-subarray.cpp ├── leetcode_binary-prefix-divisible-by-5.cpp ├── leetcode_maximum-ice-cream-bars.cpp ├── leetcode_min-cost-climbing-stairs.cpp ├── leetcode_minimum-add-to-make-parentheses-valid.cpp ├── leetcode_output-contest-matches.cpp ├── leetcode_remove-duplicates-from-sorted-array.cpp ├── leetcode_132-pattern.cpp ├── leetcode_get-equal-substrings-within-budget.cpp ├── leetcode_how-many-apples-can-you-put-into-the-basket.cpp ├── leetcode_longest-happy-prefix.cpp ├── leetcode_maximum-product-of-two-elements-in-an-array.cpp ├── leetcode_shifting-letters.cpp ├── leetcode_check-if-n-and-its-double-exist.cpp ├── leetcode_ongest-continuous-increasing-subsequence.cpp ├── leetcode_check-if-binary-string-has-at-most-one-segment-of-ones.cpp ├── leetcode_flipgame.cpp ├── leetcode_matrix-diagonal-sum.cpp ├── leetcode_maximum-absolute-sum-of-any-subarray.cpp ├── leetcode_moving-stones-until-consecutive.cpp ├── leetcode_number-complement.cpp ├── leetcode_detect-capital.cpp ├── leetcode_find-the-derangement-of-an-array.cpp ├── leetcode_height-checker.cpp ├── leetcode_minimum-operations-to-make-the-array-increasing.cpp ├── leetcode_number-of-segments-in-a-string.cpp ├── leetcode_three-consecutive-odds.cpp ├── leetcode_unique-number-of-occurrences.cpp ├── leetcode_average-salary-excluding-the-minimum-and-maximum-salary.cpp ├── leetcode_check-if-the-sentence-is-pangram.cpp ├── leetcode_find-bottom-left-tree-value.cpp ├── leetcode_sum-of-digits-in-the-minimum-number.cpp ├── leetcode_truncate-sentence.cpp ├── leetcode_sum-of-unique-elements.cpp ├── leetcode_construct-k-palindrome-strings.cpp ├── leetcode_divisor-game.cpp ├── leetcode_kth-largest-element-in-a-stream.cpp ├── leetcode_binary-gap.cpp ├── leetcode_count-numbers-with-unique-digits.cpp ├── leetcode_find-lucky-integer-in-an-array.cpp ├── leetcode_find-pivot-index.cpp ├── leetcode_largest-unique-number.cpp ├── leetcode_number-of-recent-calls.cpp ├── leetcode_crawler-log-folder.cpp ├── leetcode_min-stack.cpp ├── leetcode_minimum-difference-between-largest-and-smallest-value-in-three-moves.cpp ├── leetcode_check-if-it-is-a-good-array.cpp ├── leetcode_clumsy-factorial.cpp ├── leetcode_convex-polygon.cpp ├── leetcode_day-of-the-year.cpp ├── leetcode_intersection-of-two-linked-lists.cpp ├── leetcode_single-number.cpp ├── leetcode_di-string-match.cpp ├── leetcode_first-bad-version.cpp ├── leetcode_incremental-memory-leak.cpp ├── leetcode_check-if-number-is-a-sum-of-powers-of-three.cpp ├── leetcode_escape-the-ghosts.cpp ├── leetcode_final-prices-with-a-special-discount-in-a-shop.cpp ├── leetcode_guess-number-higher-or-lower.cpp ├── leetcode_jewels-and-stones.cpp ├── leetcode_my-calendar-iii.cpp ├── leetcode_student-attendance-record-i.cpp ├── leetcode_sum-of-square-numbers.cpp ├── leetcode_build-an-array-with-stack-operations.cpp ├── leetcode_convert-binary-number-in-a-linked-list-to-integer.cpp ├── leetcode_count-number-of-teams.cpp ├── leetcode_increasing-triplet-subsequence.cpp ├── leetcode_break-a-palindrome.cpp ├── leetcode_confusing-number.cpp ├── leetcode_delete-columns-to-make-sorted.cpp ├── leetcode_largest-substring-between-two-equal-characters.cpp ├── leetcode_max-consecutive-ones-iii.cpp ├── leetcode_stone-game-iv.cpp ├── leetcode_can-make-arithmetic-progression-from-sequence.cpp ├── leetcode_check-if-a-string-contains-all-binary-codes-of-size-k.cpp ├── leetcode_gasstation.cpp ├── leetcode_lengthoflastword.cpp ├── leetcode_shift-2d-grid.cpp ├── leetcode_single-row-keyboard.cpp ├── leetcode_toeplitz-matrix.cpp ├── leetcode_finding-the-users-active-minutes.cpp ├── leetcode_number-of-students-doing-homework-at-a-given-time.cpp ├── leetcode_subarray-sum-equals-k.cpp ├── leetcode_count-and-say.cpp ├── leetcode_fizzbuzz.cpp ├── leetcode_integertoroman.cpp ├── leetcode_online-stock-span.cpp ├── leetcode_simplified-fractions.cpp ├── leetcode_single-number-ii.cpp ├── leetcode_buildings-with-an-ocean-view.cpp ├── leetcode_combination-sum-iv.cpp ├── leetcode_maximum-xor-for-each-query.cpp ├── leetcode_unique-substrings-in-wraparound-string.cpp ├── leetcode_2-keys-keyboard.cpp ├── leetcode_find-anagram-mappings.cpp ├── leetcode_find-smallest-letter-greater-than-target.cpp ├── leetcode_longest-arithmetic-subsequence-of-given-difference.cpp ├── leetcode_my-calendar-i.cpp ├── leetcode_base-7.cpp ├── leetcode_candy.cpp ├── leetcode_eliminate-maximum-number-of-monsters.cpp ├── leetcode_maximum-ascending-subarray-sum.cpp ├── leetcode_maximum-nesting-depth-of-two-valid-parentheses-strings.cpp ├── leetcode_minimum-cost-to-connect-sticks.cpp ├── leetcode_set-mismatch.cpp ├── leetcode_super-washing-machines.cpp ├── leetcode_coin-change.cpp ├── leetcode_contains-duplicate-ii.cpp ├── leetcode_create-target-array-in-the-given-order.cpp ├── leetcode_number-of-rectangles-that-can-form-the-largest-square.cpp └── leetcode_special-array-with-x-elements-greater-than-or-equal-x.cpp ├── LintCode ├── lintcode_reverse-array.cpp ├── lintcode_sum-of-all-subsets.cpp ├── lintcode_sort-integers-ii.cpp ├── lintcode_lowercase-to-uppercase.cpp ├── lintcode_leap-year.cpp ├── lintcode_coins-in-a-line.cpp ├── lintcode_greatest-common-divisor.cpp ├── lintcode_a+b.cpp ├── lintcode_singleton.java ├── lintcode_palindrome-number.cpp ├── lintcode_trailing-zeros.cpp ├── lintcode_reverse-3-digit-integer.cpp ├── lintcode_next-permutation.cpp ├── lintcode_single-number.cpp ├── lintcode_last-digit-by-factorial-divide.cpp ├── lintcode_previous-permutation.cpp ├── lintcode_update-bits.cpp ├── lintcode_valid-triangle.cpp ├── lintcode_flip-bits.cpp ├── lintcode_lintcode_o1-check-power-of-2.cpp ├── lintcode_next-permutation-ii.cpp ├── lintcode_climbing-stairs.cpp ├── lintcode_add-digits.cpp ├── lintcode_Ugly-number.cpp ├── lintcode_singleton.cpp ├── lintcode_fibonacci.cpp ├── lintcode_check-full-binary-tree.cpp ├── lintcode_unique-characters.cpp ├── lintcode_find-the-missing-number.cpp ├── lintcode_count-1-in-binary.cpp ├── lintcode_permutations.cpp ├── lintcode_maximum-subarray.cpp ├── lintcode_unique-binary-search-trees.cpp ├── lintcode_gray-code.cpp ├── lintcode_best-time-to-buy-and-sell-stock-ii.cpp ├── lintcode_wiggle-sort.cpp ├── lintcode_sort-colors-ii.cpp ├── lintcode_paint-fence.cpp ├── lintcode_top-k-largest-numbers.cpp ├── lintcode_length-of-last-word.cpp ├── lintcode_maximum-gap.cpp ├── lintcode_backpack-ii.cpp ├── lintcode_remove-element.cpp ├── lintcode_move-zeroes.cpp └── lintcode_remove-duplicates-from-sorted-array.cpp ├── Codility └── prefix_set.cpp └── Hackerrank_InterviewStreet ├── solve-me-first.cpp ├── tree-inorder-traversal.cpp ├── tree-preorder-traversal.cpp ├── tree-postorder-traversal.cpp ├── abstract-classes.cpp ├── game-with-cells.cpp ├── sherlock-and-squares.cpp ├── pythagorean-triple.cpp ├── minimum-absolute-difference-in-an-array.cpp ├── marcs-cakewalk.cpp ├── p1-paper-cutting.cpp ├── simple-array-sum.cpp ├── lowest-triangle.cpp ├── is-binary-search-tree.cpp ├── tower-breakers-1.cpp ├── a-very-big-sum.cpp ├── strange-advertising.cpp ├── arrays_ds.cpp ├── save-the-prisoner.cpp ├── big-sorting.cpp ├── hackerrank-in-a-string.cpp ├── migratory-birds.cpp ├── countingsort1.cpp ├── beautiful-3-set.cpp ├── equality-in-a-array.cpp ├── print-the-elements-of-a-linked-list.cpp ├── tree-height-of-a-binary-tree.cpp ├── strings-xor.cpp ├── make-it-anagram-mglines.cpp ├── staircase.cpp ├── tree-level-order-traversal.cpp ├── array-rotation-2.cpp ├── lonely-integer-fill-the-key-line.cpp ├── library-fine.cpp ├── bob-and-ben.cpp ├── bon-appetit.cpp └── countingsort2.cpp /HDU/README.md: -------------------------------------------------------------------------------- 1 | # HDU OJ Problems Solution List 2 | 3 | -------------------------------------------------------------------------------- /LeetCode/leetcode_nimgame.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Game Theory 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | bool canWinNim(int n) { 10 | return n%4; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LeetCode/leetcode_bulb-switcher.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int bulbSwitch(int n) { 10 | return (int)sqrt(n); 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LeetCode/leetcode_adddigits.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Tag: Math 3 | * Time: O(1) 4 | * Space: O(1) 5 | */ 6 | class Solution { 7 | public: 8 | int addDigits(int num) { 9 | return num%9 == 0?(num == 0?0:9):num%9; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /LeetCode/leetcode_power-of-two.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Bit Manipulation 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | bool isPowerOfTwo(int n) { 10 | return n>0 && (n&(n-1))==0; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LeetCode/leetcode_power-of-four.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Bit Manipulation 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | bool isPowerOfFour(int num) { 10 | return num > 0 && !(num&(num-1)) && ((num&1431655765)==num); 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LeetCode/leetcode_power-of-three.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(lgn) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | bool isPowerOfThree(int n) { 10 | return n == 0? false : log10(n)/log10(3) == floor(log10(n)/log10(3)); 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LeetCode/leetcode_longest-uncommon-subsequence-i.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int findLUSlength(string a, string b) { 10 | return a == b?-1:max(a.size(), b.size()); 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LeetCode/leetcode_sort-an-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Sort 4 | * Time: O(nlgn) 5 | * Space: O(lgn) 6 | */ 7 | class Solution { 8 | public: 9 | vector sortArray(vector& nums) { 10 | sort(nums.begin(), nums.end()); 11 | return nums; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LeetCode/leetcode_to-lower-case.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | string toLowerCase(string str) { 10 | transform(str.begin(),str.end(),str.begin(),::tolower); 11 | return str; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LeetCode/leetcode_minimum-operations-to-make-array-equal.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int minOperations(int n) { 11 | return (n&1) > 0? (n>>1)*((n>>1) + 1) : (n>>1) + (n>>1)*((n>>1) - 1); 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /LintCode/lintcode_reverse-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implement 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param nums: a integer array 11 | * @return: nothing 12 | */ 13 | void reverseArray(vector &nums) { 14 | reverse(nums.begin(), nums.end()); 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LintCode/lintcode_sum-of-all-subsets.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /* 10 | * @param : the given number 11 | * @return: Sum of elements in subsets 12 | */ 13 | int subSum(int n) { 14 | return n <= 1? n : n*(n + 1)*(1<<(n - 2)); 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode/leetcode_valid-boomerang.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | bool isBoomerang(vector>& points) { 10 | return (points[0][1] - points[1][1])*(points[2][0] - points[1][0]) != (points[2][1] - points[1][1])*(points[0][0] - points[1][0]); 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LeetCode/leetcode_singlenumber.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Bit Manipulation 4 | * Time : O(n) 5 | * Memory : O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int singleNumber(vector& nums) { 10 | int ans = 0; 11 | for(int num : nums){ 12 | ans ^= num; 13 | } 14 | 15 | return ans; 16 | } 17 | }; 18 | 19 | -------------------------------------------------------------------------------- /LintCode/lintcode_sort-integers-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | /** 4 | * @param A an integer array 5 | * @return void 6 | */ 7 | void sortIntegers2(vector& A) { 8 | // Write your code here 9 | if(!A.size()) 10 | return ; 11 | sort(A.begin(),A.end()); 12 | return ; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LintCode/lintcode_lowercase-to-uppercase.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param character: a character 11 | * @return: a character 12 | */ 13 | char lowercaseToUppercase(char character) { 14 | return toupper(character); 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode/leetcode_angle-between-hands-of-a-clock.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | double angleClock(int hour, int minutes) { 10 | hour = hour == 12 ? 0 : hour; 11 | double ang = fabs(minutes*6.0 - hour*30 - 0.5*minutes); 12 | return min(ang, 360 - ang); 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /LintCode/lintcode_leap-year.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param n: a number represent year 11 | * @return: whether year n is a leap year. 12 | */ 13 | bool isLeapYear(int n) { 14 | return n%400 == 0 || (n%4 == 0 && n%100 != 0); 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode/leetcode_number-of-1-bits.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Bit Manipulation 4 | * Time: O(lgn) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int hammingWeight(uint32_t n) { 11 | int cnt = 0; 12 | while(n > 0){ 13 | if(n&1) ++ cnt; 14 | n >>= 1; 15 | } 16 | 17 | return cnt; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /LeetCode/leetcode_squares-of-a-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Sort 4 | * Time: O(nlgn) 5 | * Space: O(lgn) 6 | */ 7 | class Solution { 8 | public: 9 | vector sortedSquares(vector& A) { 10 | for(int i = 0; i < A.size(); ++ i){ 11 | A[i] = A[i]*A[i]; 12 | } 13 | sort(A.begin(), A.end()); 14 | return A; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode/leetcode_remove-9.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(lgn) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int newInteger(int n) { 10 | int ans = 0, fac = 1; 11 | while(n > 0){ 12 | ans += (n%9)*fac; 13 | n /= 9; 14 | fac *= 10; 15 | } 16 | 17 | return ans; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /LeetCode/leetcode_jumpgame.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Algorithm: DP 3 | * Time complexity: O(n) 4 | * Memory complexity: O(1) 5 | */ 6 | class Solution { 7 | public: 8 | bool canJump(int A[], int n) { 9 | int reach = 1; 10 | for(int i = 0; reach < n && i < reach; i ++){ 11 | reach = max(reach, A[i] + i + 1); 12 | } 13 | return reach >= n; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LeetCode/leetcode_hamming-distance.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Bit Manipulation 4 | * Time: O(lgn) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int hammingDistance(int x, int y) { 10 | int v = x^y, res = 0; 11 | while(v > 0){ 12 | if(v&1) 13 | ++ res; 14 | v >>= 1; 15 | } 16 | return res; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LeetCode/leetcode_poor-pigs.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int poorPigs(int buckets, int minutesToDie, int minutesToTest) { 10 | if(buckets <= 1) 11 | return 0; 12 | long long t = (minutesToTest - 1)/minutesToDie + 1; 13 | return log(buckets - 1)/log(t + 1) + 1; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /LeetCode/leetcode_range-addition-ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Brute Force 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int maxCount(int m, int n, vector>& ops) { 10 | for(int i = 0; i < ops.size(); ++ i){ 11 | m = min(m, ops[i][0]); 12 | n = min(n, ops[i][1]); 13 | } 14 | return m*n; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /LeetCode/leetcode_running-sum-of-1d-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | vector runningSum(vector& nums) { 11 | vector ans = nums; 12 | for(int i = 1; i < nums.size(); ++ i) { 13 | ans[i] = ans[i - 1] + nums[i]; 14 | } 15 | return ans; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LeetCode/leetcode_water-and-jug-problem.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(lgn) 5 | * Space: O(lgn) 6 | */ 7 | class Solution { 8 | public: 9 | bool canMeasureWater(int x, int y, int z) { 10 | if(z > x && z > y) return false; 11 | return z%gcd(x, y) == 0; 12 | } 13 | private: 14 | int gcd(int a, int b){ 15 | return b == 0?a:gcd(b, a%b); 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LeetCode/leetcode_check-if-a-number-is-majority-element-in-a-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | bool isMajorityElement(vector& nums, int target) { 10 | int mid = nums.size()/2; 11 | return nums.size()%2 == 1 ? nums[mid] == target : nums[mid - 1] == target && nums[mid] == target; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /Codility/prefix_set.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Hash 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | int solution(vector &A) { 8 | // write your code in C++11 (g++ 4.8.2) 9 | int n = A.size(); 10 | vector vis(n + 1); 11 | int ans = 0; 12 | for(int i = 0; i < n; ++ i){ 13 | if(!vis[A[i]]) 14 | ans = i; 15 | vis[A[i]] = 1; 16 | } 17 | return ans; 18 | } 19 | -------------------------------------------------------------------------------- /LeetCode/leetcode_magical-string.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: String 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | class Solution { 8 | public: 9 | int magicalString(int n) { 10 | string S = "122"; 11 | int i = 2; 12 | while (S.size() < n){ 13 | S += string(S[i++] - '0', S.back() ^ 3); 14 | } 15 | return count(S.begin(), S.begin() + n, '1'); 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LintCode/lintcode_coins-in-a-line.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param n: an integer 11 | * @return: a boolean which equals to true if the first player will win 12 | */ 13 | bool firstWillWin(int n) { 14 | // write your code here 15 | return !((n%3==0 && n) || !n); 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LintCode/lintcode_greatest-common-divisor.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(lgn) 5 | * Space: O(lgn) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param a: the given number 11 | * @param b: another number 12 | * @return: the greatest common divisor of two numbers 13 | */ 14 | int gcd(int a, int b) { 15 | return b == 0? a : gcd(b, a%b); 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LeetCode/leetcode_jump-game.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | bool canJump(vector& nums) { 11 | int reach = 1, n = nums.size(); 12 | for(int i = 0; i < reach && reach < n; i ++){ 13 | reach = max(reach, nums[i] + i + 1); 14 | } 15 | 16 | return reach >= n; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LintCode/lintcode_a+b.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /* 10 | * @param a: The first integer 11 | * @param b: The second integer 12 | * @return: The sum of a and b 13 | */ 14 | int aplusb(int a, int b) { 15 | // Just submit this code, then you will get accepted! 16 | return a + b; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LeetCode/leetcode_game-of-nim.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Game Theory 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | bool nimGame(vector& piles) { 10 | if(piles.size() == 0) { 11 | return false; 12 | } 13 | 14 | int v = 0; 15 | for(int p : piles) { 16 | v ^= p; 17 | } 18 | return v > 0; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /LintCode/lintcode_singleton.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Design 4 | */ 5 | class Solution { 6 | private static volatile Solution instance = new Solution(); 7 | /** 8 | * @return: The same instance of this class every time 9 | */ 10 | private Solution() { 11 | } 12 | 13 | public static Solution getInstance() { 14 | // write your code here 15 | return instance; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LeetCode/leetcode_climbing-stairs.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DP 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int climbStairs(int n) { 10 | int prev = 0; 11 | int cur = 1; 12 | for(int i = 1; i <= n; i ++){ 13 | int tmp = cur; 14 | cur += prev; 15 | prev = tmp; 16 | } 17 | 18 | return cur; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /LeetCode/leetcode_is-subsequence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | bool isSubsequence(string s, string t) { 10 | int n = s.size(), m = t.size(), i = 0, j = 0; 11 | while(i < n && j < m){ 12 | if(s[i] == t[j]) 13 | ++ i; 14 | ++ j; 15 | } 16 | return i == n; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LeetCode/leetcode_number-of-good-pairs.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Hash 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int numIdenticalPairs(vector& nums) { 11 | int ans = 0; 12 | vector numCnt(1000, 0); 13 | for(int num : nums) { 14 | ans += numCnt[num]; 15 | ++ numCnt[num]; 16 | } 17 | return ans; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /LeetCode/leetcode_print-immutable-linked-list-in-reverse.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Recusrion 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | void printLinkedListInReverse(ImmutableListNode* head) { 11 | if(head == NULL) { 12 | return ; 13 | } 14 | 15 | printLinkedListInReverse(head->getNext()); 16 | head->printValue(); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LeetCode/leetcode_sum-of-digits-in-base-k.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(lgn) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int sumBase(int n, int k) { 11 | if(n == 0 || k < 2) { 12 | return 0; 13 | } 14 | 15 | int sum = 0; 16 | for(; n > 0; n /= k) { 17 | sum += n%k; 18 | } 19 | return sum; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /LeetCode/leetcode_next-greater-element-iii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DFS 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | class Solution { 8 | public: 9 | int nextGreaterElement(int n) { 10 | if(n <= 10) 11 | return -1; 12 | string v = to_string(n); 13 | next_permutation(begin(v), end(v)); 14 | long long ans = stoll(v); 15 | return (ans > INT_MAX || ans <= n) ? -1 : (int)ans; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LeetCode/leetcode_reverse-string.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Brute Force 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | string reverseString(string s) { 10 | if(!s.size()) 11 | return s; 12 | int i = 0, j = s.size() - 1; 13 | while(i < j){ 14 | swap(s[i], s[j]); 15 | ++ i; 16 | -- j; 17 | } 18 | return s; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /LintCode/lintcode_palindrome-number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: String 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | class Solution { 8 | public: 9 | /* 10 | * @param num: a positive number 11 | * @return: true if it's a palindrome or false 12 | */ 13 | bool isPalindrome(int num) { 14 | string str_num = to_string(num); 15 | return str_num == string(str_num.rbegin(), str_num.rend()); 16 | } 17 | 18 | }; 19 | -------------------------------------------------------------------------------- /LeetCode/leetcode_find-numbers-with-even-number-of-digits.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int findNumbers(vector& nums) { 10 | int cnt = 0; 11 | for(int num : nums){ 12 | if((to_string(num).size()&1) == 0) { 13 | ++ cnt; 14 | } 15 | } 16 | 17 | return cnt; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /LeetCode/leetcode_rectangle-overlap.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | bool isRectangleOverlap(vector& rec1, vector& rec2) { 11 | if(rec1.size() == 0 || rec2.size() == 0){ 12 | return true; 13 | } 14 | 15 | return rec1[0] < rec2[2] && rec2[0] < rec1[2] && rec1[1] < rec2[3] && rec2[1] < rec1[3]; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LeetCode/leetcode_array-partition-i.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(ngln) 5 | * Space: O(lgn) 6 | */ 7 | class Solution { 8 | public: 9 | int arrayPairSum(vector& nums) { 10 | int sum = 0; 11 | if(nums.size() == 0) 12 | return sum; 13 | sort(nums.begin(), nums.end()); 14 | for(int i = 0; i < nums.size(); i += 2) 15 | sum += nums[i]; 16 | return sum; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LeetCode/leetcode_complement-of-base-10-integer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Bit Manipulation 4 | * Time: O(lgn) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int bitwiseComplement(int N) { 10 | if(N == 0){ 11 | return 1; 12 | } 13 | 14 | long long mask = 1; 15 | while(mask <= N){ 16 | N ^= mask; 17 | mask <<= 1; 18 | } 19 | return N; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /LeetCode/leetcode_counting-elements.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Hash 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int countElements(vector& arr) { 11 | unordered_set dict(arr.begin(), arr.end()); 12 | int ans = 0; 13 | for(int v : arr) { 14 | if(dict.count(v + 1) > 0) { 15 | ++ ans; 16 | } 17 | } 18 | return ans; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /LeetCode/leetcode_fibonacci-number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int fib(int N) { 10 | if(N < 2){ 11 | return N; 12 | } 13 | int pre = 0, cur = 1; 14 | for(int i = 2; i <= N; ++ i){ 15 | int nxt = pre + cur; 16 | pre = cur; 17 | cur = nxt; 18 | } 19 | return cur; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /LeetCode/leetcode_xor-operation-in-an-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int xorOperation(int n, int start) { 11 | if(n < 2) { 12 | return start; 13 | } 14 | 15 | int ans = start; 16 | for(int i = 1; i < n; ++ i) { 17 | ans ^= (start + 2*i); 18 | } 19 | return ans; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /LeetCode/leetcode_defanging-an-ip-address.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | class Solution { 8 | public: 9 | string defangIPaddr(string address) { 10 | string ans = ""; 11 | for(char ch : address) { 12 | if(ch == '.'){ 13 | ans += "[.]"; 14 | continue; 15 | } 16 | ans += ch; 17 | } 18 | return ans; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /LeetCode/leetcode_fixed-point.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int fixedPoint(vector& A) { 11 | if(A.size() == 0) { 12 | return -1; 13 | } 14 | 15 | for(int i = 0; i < A.size(); ++ i) { 16 | if(A[i] == i) { 17 | return i; 18 | } 19 | } 20 | return -1; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /LeetCode/leetcode_number-of-steps-to-reduce-a-number-to-zero.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Bit Manipulation 4 | * Time: O(lgn) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int numberOfSteps (int num) { 10 | int ans = 0; 11 | while(num > 0) { 12 | if((num&1) > 0) { 13 | ++ ans; 14 | } 15 | ++ ans; 16 | num >>= 1; 17 | } 18 | return ans - 1; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /LeetCode/leetcode_remove-vowels-from-a-string.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | class Solution { 8 | public: 9 | string removeVowels(string S) { 10 | string ans = ""; 11 | for(char s : S){ 12 | if(s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u') { 13 | continue; 14 | } 15 | ans += s; 16 | } 17 | return ans; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /LeetCode/leetcode_subtract-the-product-and-sum-of-digits-of-an-integer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(lgn) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int subtractProductAndSum(int n) { 11 | int sum = 0, product = 1; 12 | while(n > 0) { 13 | int v = n%10; 14 | n /= 10; 15 | sum += v; 16 | product *= v; 17 | } 18 | return product - sum; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /LintCode/lintcode_trailing-zeros.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(lgn) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | // param n : description of n 10 | // return: description of return 11 | long long trailingZeros(long long n) { 12 | long long ans = 0; 13 | long long div = 5, mul = 5; 14 | while(n/div){ 15 | ans += n/div; 16 | div *= mul; 17 | } 18 | return ans; 19 | } 20 | }; 21 | 22 | -------------------------------------------------------------------------------- /LeetCode/leetcode_maximum-product-difference-between-two-pairs.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(nlgn) 5 | * Space: O(lgn) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int maxProductDifference(vector& nums) { 11 | if(nums.size() < 4) { 12 | return 0; 13 | } 14 | 15 | sort(nums.begin(), nums.end()); 16 | int n = nums.size(); 17 | return nums[n-1]*nums[n-2] - nums[0]*nums[1]; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /LeetCode/leetcode_besttimetobuyandsellstockII.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Algorithm: Greedy 3 | * Time complexity: O(n) 4 | * Memory complexity: O(n) 5 | */ 6 | class Solution { 7 | public: 8 | int maxProfit(vector &prices) { 9 | int ans = 0; 10 | for(int i = 1; i < prices.size(); i ++){ 11 | if(prices[i] > prices[i - 1]){ 12 | ans += (prices[i] - prices[i - 1]); 13 | } 14 | } 15 | return ans; 16 | } 17 | }; 18 | 19 | -------------------------------------------------------------------------------- /LeetCode/leetcode_smallest-range-i.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int smallestRangeI(vector& A, int K) { 10 | int maxVal = INT_MIN, minVal = INT_MAX; 11 | for(int i = 0; i < A.size(); ++ i){ 12 | maxVal = max(maxVal, A[i]); 13 | minVal = min(minVal, A[i]); 14 | } 15 | return maxVal - minVal < 2*K ? 0 : maxVal - minVal - 2*K; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /LeetCode/leetcode_contains-duplicate.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Sort 4 | * Time: O(nlgn) 5 | * Space: O(lgn) 6 | */ 7 | class Solution { 8 | public: 9 | bool containsDuplicate(vector& nums) { 10 | if(!nums.size()) 11 | return false; 12 | sort(nums.begin(), nums.end()); 13 | for(int i = 1; i < nums.size(); ++ i){ 14 | if(nums[i] == nums[i - 1]) 15 | return true; 16 | } 17 | return false; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /LeetCode/leetcode_perfect-number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(n^0.5) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | bool checkPerfectNumber(int num) { 10 | if(num <= 1) 11 | return false; 12 | int sum = 0, n = sqrt(num); 13 | for(int i = 2; i <= n; ++ i){ 14 | if(num%i == 0) 15 | sum += (i + (i*i == num?0:num/i)); 16 | } 17 | ++ sum; 18 | return sum == num; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /LeetCode/leetcode_build-array-from-permutation.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | vector buildArray(vector& nums) { 11 | if(nums.size() == 0) { 12 | return nums; 13 | } 14 | 15 | vector ans = nums; 16 | for(int i = 0; i < nums.size(); ++ i) { 17 | ans[i] = nums[nums[i]]; 18 | } 19 | return ans; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /LeetCode/leetcode_calculate-money-in-leetcode-bank.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int totalMoney(int n) { 11 | if(n == 0) { 12 | return 0; 13 | } 14 | 15 | int sum = 0, start = 1; 16 | for(; n >= 7; n -= 7, start ++) { 17 | sum += (2*start + 6)*7/2; 18 | } 19 | sum += (2*start + n - 1)*n/2; 20 | return sum; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/solve-me-first.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(1) 5 | * Space: O(1) 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | int solveMeFirst(int a, int b) { 15 | return a+b; 16 | } 17 | 18 | int main() { 19 | int num1, num2; 20 | int sum; 21 | scanf("%d%d",&num1,&num2); 22 | sum = solveMeFirst(num1,num2); 23 | printf("%d",sum); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_factorial-trailing-zeroes.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(lgn) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int trailingZeroes(int n) { 11 | int ans = 0; 12 | if(n < 5) { 13 | return ans; 14 | } 15 | 16 | int p = 5; 17 | long long k = 5; 18 | while(n/k){ 19 | ans += n/k; 20 | k *= p; 21 | } 22 | 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_construct-the-rectangle.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(sqrt(n)) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | vector constructRectangle(int area) { 10 | vector ans(2); 11 | int w = (int)sqrt(area); 12 | while(w >= 1){ 13 | if(area%w == 0){ 14 | ans[0] = area/w, ans[1] = w; 15 | break; 16 | } 17 | -- w; 18 | } 19 | return ans; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /LeetCode/leetcode_convert-a-number-to-hexadecimal.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Bit Manipulation 4 | * Time: O(lgn) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | string toHex(int num) { 10 | unsigned int val = (int)num; 11 | string res = ""; 12 | do{ 13 | res = dict[(val&mask)] + res; 14 | val >>= 4; 15 | }while(val); 16 | return res; 17 | } 18 | private: 19 | string dict="0123456789abcdef"; 20 | int mask=15; 21 | }; 22 | -------------------------------------------------------------------------------- /LeetCode/leetcode_find-the-highest-altitude.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int largestAltitude(vector& gain) { 11 | if(gain.size() == 0) { 12 | return 0; 13 | } 14 | 15 | int ans = 0, sum = 0; 16 | for(int i = 0; i < gain.size(); ++ i) { 17 | sum += gain[i]; 18 | ans = max(ans, sum); 19 | } 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /LeetCode/leetcode_number-of-days-in-a-month.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | private: 9 | int numOfDaysOfMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; 10 | public: 11 | int numberOfDays(int Y, int M) { 12 | return isLunarYear(Y) && M == 2? numOfDaysOfMonth[M] + 1 : numOfDaysOfMonth[M]; 13 | } 14 | private: 15 | bool isLunarYear(int y) { 16 | return y%400 == 0 || (y%4 == 0 && y%100 != 0); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LeetCode/leetcode_shuffle-string.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | string restoreString(string s, vector& indices) { 10 | if(s.size() != indices.size() || s.size() == 0) { 11 | return ""; 12 | } 13 | 14 | string ans = s; 15 | for(int i = 0; i < indices.size(); ++ i) { 16 | ans[indices[i]] = s[i]; 17 | } 18 | return ans; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/tree-inorder-traversal.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DFS 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | /* you only have to complete the function given below. 8 | Node is defined as 9 | 10 | struct node 11 | { 12 | int data; 13 | node* left; 14 | node* right; 15 | }; 16 | 17 | */ 18 | 19 | void Inorder(node *root) { 20 | if(root->left) 21 | Inorder(root->left); 22 | printf("%d ",root->data); 23 | if(root->right) 24 | Inorder(root->right); 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_coin-change-2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DP 4 | * Time: O(nm) 5 | * Space: O(m) 6 | */ 7 | class Solution { 8 | public: 9 | int change(int amount, vector& coins) { 10 | vector dp(amount + 1, 0); 11 | dp[0] = 1; 12 | for(int i = 0; i < coins.size(); ++ i){ 13 | for(int j = coins[i]; j <= amount; ++ j){ 14 | dp[j] += dp[j - coins[i]]; 15 | } 16 | } 17 | return amount == 0 ? 1 : dp[amount]; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /LeetCode/leetcode_richest-customer-wealth.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(nm) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int maximumWealth(vector>& accounts) { 11 | int ans = 0; 12 | for(vector account : accounts) { 13 | int sum = 0; 14 | for(int v : account) { 15 | sum += v; 16 | } 17 | ans = max(ans, sum); 18 | } 19 | return ans; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /LintCode/lintcode_reverse-3-digit-integer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param number: A 3-digit number. 11 | * @return: Reversed number. 12 | */ 13 | int reverseInteger(int number) { 14 | int ans = 0; 15 | 16 | while(number > 0){ 17 | ans = (ans*10) + (number%10); 18 | number /= 10; 19 | } 20 | 21 | return ans; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/tree-preorder-traversal.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DFS 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | /* you only have to complete the function given below. 8 | Node is defined as 9 | 10 | struct node 11 | { 12 | int data; 13 | node* left; 14 | node* right; 15 | }; 16 | 17 | */ 18 | 19 | void Preorder(node *root) { 20 | printf("%d ",root->data); 21 | if(root->left) 22 | Preorder(root->left); 23 | if(root->right) 24 | Preorder(root->right); 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_bulb-switcher-iv.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int minFlips(string target) { 11 | if(target.size() == 0) { 12 | return 0; 13 | } 14 | 15 | int ans = 0, cnt = 0; 16 | for(char c : target) { 17 | int v = c - '0'; 18 | if((ans&1) != v){ 19 | ++ ans; 20 | } 21 | } 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_max-consecutive-ones.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int findMaxConsecutiveOnes(vector& nums) { 10 | int ans = 0; 11 | if(nums.size() == 0) 12 | return ans; 13 | int sum = 0; 14 | for(int i = 0; i < nums.size(); ++ i){ 15 | sum = (sum + nums[i])*nums[i]; 16 | ans = max(ans, sum); 17 | } 18 | return ans; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /LeetCode/leetcode_peak-index-in-a-mountain-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int peakIndexInMountainArray(vector& A) { 10 | int ans = 1; 11 | 12 | for(int i = 1; i < A.size(); ++ i){ 13 | if(A[i] > A[i - 1]){ 14 | ans = i; 15 | } else { 16 | break; 17 | } 18 | } 19 | 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/tree-postorder-traversal.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DFS 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | /* you only have to complete the function given below. 8 | Node is defined as 9 | 10 | struct node 11 | { 12 | int data; 13 | node* left; 14 | node* right; 15 | }; 16 | 17 | */ 18 | 19 | 20 | void Postorder(node *root) { 21 | if(root->left) 22 | Postorder(root->left); 23 | if(root->right) 24 | Postorder(root->right); 25 | printf("%d ",root->data); 26 | } 27 | -------------------------------------------------------------------------------- /LeetCode/leetcode_determine-color-of-a-chessboard-square.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | bool squareIsWhite(string coordinates) { 11 | if(coordinates.size() != 2) { 12 | return false; 13 | } 14 | 15 | int r = coordinates[1] - '1', c = coordinates[0] - 'a'; 16 | if((c&1) == 0) { 17 | return (r&1) == 1; 18 | } 19 | return (r&1) == 0; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /LeetCode/leetcode_elimination-game.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(lgn) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int lastRemaining(int n) { 10 | int res = 1, cnt = 0, p = 1; 11 | while(n > 1){ 12 | n >>= 1; 13 | p <<= 1; 14 | ++ cnt; 15 | if(cnt%2) 16 | res += p/2 + p*(n - 1); 17 | else 18 | res -= p/2 + p*(n - 1); 19 | } 20 | return res; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /LeetCode/leetcode_sort-colors.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Hash 4 | * Tinme: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | void sortColors(vector& nums) { 11 | int colCnt[3] = {0}; 12 | for(int num : nums) 13 | colCnt[num] ++; 14 | int idx = 0; 15 | for(int i = 0; i < 3; i ++){ 16 | for(int j = 0; j < colCnt[i]; j ++){ 17 | nums[idx] = i; 18 | ++ idx; 19 | } 20 | } 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /LeetCode/leetcode_uniquepaths.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Algorithm: DP 3 | * Time complexity: O(n^2) 4 | * Memory complexity: O(n^2) 5 | */ 6 | class Solution { 7 | public: 8 | int uniquePaths(int m, int n) { 9 | vector > dp(m + 1, vector(n + 1, 0)); 10 | dp[1][1] = 1; 11 | for(int i = 1; i <= m; i ++){ 12 | for(int j = 1; j <= n; j ++){ 13 | dp[i][j] += (dp[i][j - 1] + dp[i - 1][j]); 14 | } 15 | } 16 | return dp[m][n]; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LintCode/lintcode_next-permutation.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: ? 4 | * Time: ? 5 | * Space: ? 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param nums: An array of integers 11 | * @return: An array of integers that's next permuation 12 | */ 13 | vector nextPermuation(vector &nums) { 14 | // write your code here 15 | if(nums.size() == 0) 16 | return nums; 17 | next_permutation(nums.begin(), nums.end()); 18 | return nums; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /LintCode/lintcode_single-number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Bit Manipulation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param A: Array of integers. 11 | * return: The single number. 12 | */ 13 | int singleNumber(vector &A) { 14 | // write your code here 15 | if(A.size() == 0) 16 | return 0; 17 | int ans = A[0], n = A.size(); 18 | for(int i = 1; i < n; ++ i) 19 | ans ^= A[i]; 20 | return ans; 21 | } 22 | 23 | }; 24 | -------------------------------------------------------------------------------- /LeetCode/leetcode_count-odd-numbers-in-an-interval-range.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int countOdds(int low, int high) { 11 | int len = high - low, ans; 12 | ans = len/2; 13 | if(len&1 == 1) { 14 | if((low&1) == 0) { 15 | ++ ans; 16 | } 17 | } 18 | if((low&1) == 1) { 19 | ++ ans; 20 | } 21 | 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_uniquebinarysearchtrees.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Algorithm: O(n) 3 | * Time complexity: O(n^2) 4 | * Memory complexity: O(n^2) 5 | */ 6 | class Solution { 7 | public: 8 | int numTrees(int n) { 9 | vector ans(n + 1, 0); 10 | 11 | ans[0] = 1; 12 | ans[1] = 1; 13 | for(int i = 2; i <= n; i ++){ 14 | for(int k = 1; k <= i; k ++){ 15 | ans[i] += ans[k - 1]*ans[i - k]; 16 | } 17 | } 18 | return ans[n]; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /LintCode/lintcode_last-digit-by-factorial-divide.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int computeLastDigit(long long A, long long B) { 10 | if(A == B) 11 | return 1; 12 | if(B - A >= 10) 13 | return 0; 14 | int ans = 1; 15 | for(long long i = A + 1; i <= B; ++ i){ 16 | int factor = i%10; 17 | ans = (ans * factor)%10; 18 | } 19 | return ans; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/abstract-classes.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Design 4 | */ 5 | class MyBook : public Book { 6 | private: 7 | int price; 8 | public: 9 | MyBook(string title, string author, int price) 10 | : Book (title, author){ 11 | this->price = price; 12 | } 13 | 14 | void display(){ 15 | cout<<"Title: "< previousPermuation(vector &nums) { 14 | // write your code here 15 | if(nums.size() == 0) 16 | return nums; 17 | prev_permutation(nums.begin(), nums.end()); 18 | return nums; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/game-with-cells.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | int n, m; 14 | 15 | int main() { 16 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 17 | scanf("%d%d",&n,&m); 18 | int tr = n/2, onr = n%2, tc = m/2, onc = m%2; 19 | printf("%d\n",tr*tc+tr*onc+onr*tc+onr*onc); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /LeetCode/leetcode_count-all-valid-pickup-and-delivery-options.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | private: 10 | const int MOD = 1e9 + 7; 11 | public: 12 | int countOrders(int n) { 13 | long long ans = 1; 14 | for(int i = 2; i <= n; ++ i) { 15 | long long m = 2*i - 1; 16 | long long sum = m*(m + 1)/2; 17 | ans *= (sum%MOD); 18 | ans %= MOD; 19 | } 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /LeetCode/leetcode_greatest-common-divisor-of-strings.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(lgn) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | string gcdOfStrings(string str1, string str2) { 11 | if(str1 + str2 != str2 + str1) { 12 | return ""; 13 | } 14 | 15 | int len = gcd(str1.size(), str2.size()); 16 | return str1.substr(0, len); 17 | } 18 | private: 19 | int gcd(int a, int b) { 20 | return b == 0? a : gcd(b, a%b); 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /LeetCode/leetcode_removeelement.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Algorithm: Brute force 3 | * Time complexity: O(n) 4 | * Memory complexity: O(n) 5 | */ 6 | class Solution { 7 | public: 8 | int removeElement(int A[], int n, int elem) { 9 | int loc_idx = 0; 10 | int len = n; 11 | for(int i = 0; i < n; i ++){ 12 | if(elem != A[i]){ 13 | A[loc_idx] = A[i]; 14 | ++ loc_idx; 15 | }else 16 | len --; 17 | } 18 | return len; 19 | } 20 | }; 21 | 22 | -------------------------------------------------------------------------------- /LintCode/lintcode_update-bits.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Bit Manipulation 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | *@param n, m: Two integer 11 | *@param i, j: Two bit positions 12 | *return: An integer 13 | */ 14 | int updateBits(int n, int m, int i, int j) { 15 | // write your code here 16 | int l = j-i+1; 17 | unsigned int mask = l==32 ? 0xffffffff : ((1< 8 | #include 9 | #include 10 | using namespace std; 11 | 12 | int main(){ 13 | int T; 14 | scanf("%d",&T); 15 | while(T --){ 16 | int a,b; 17 | scanf("%d%d",&a,&b); 18 | int sa = sqrt(a), sb=sqrt(b); 19 | int ans = sb - sa; 20 | if(sa*sa == a) 21 | ans ++; 22 | printf("%d\n",ans); 23 | } 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_binary-number-with-alternating-bits.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Bit Manipulation 4 | * Time: O(lgn) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | bool hasAlternatingBits(int n) { 10 | if(n <= 1){ 11 | return true; 12 | } 13 | 14 | while(n > 0){ 15 | if((n&1) == ((n>>1)&1)){ 16 | return false; 17 | } 18 | 19 | n >>= 1; 20 | } 21 | 22 | return true; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_longest-harmonious-subsequence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Hash 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | class Solution { 8 | public: 9 | int findLHS(vector& nums) { 10 | unordered_map numCnt; 11 | for(int num : nums){ 12 | ++ numCnt[num]; 13 | } 14 | 15 | int ans = 0; 16 | for(int num : nums){ 17 | ans = max(ans, numCnt.count(num + 1) ? (numCnt[num] + numCnt[num + 1]) : 0); 18 | } 19 | return ans; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /LeetCode/leetcode_minimum-value-to-get-positive-step-by-step-sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int minStartValue(vector& nums) { 11 | if(nums.size() == 0) { 12 | return 1; 13 | } 14 | 15 | int sum = 0, minval = INT_MAX; 16 | for(int num : nums) { 17 | sum += num; 18 | minval = min(minval, sum); 19 | } 20 | return minval < 0? -minval + 1 : 1; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /LeetCode/leetcode_partitioning-into-minimum-number-of-deci-binary-numbers.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int minPartitions(string n) { 11 | if(n.size() == 0) { 12 | return 0; 13 | } 14 | 15 | int ans = 0; 16 | for(char c : n) { 17 | ans = max(ans, c - '0'); 18 | if(ans == 9) { 19 | break; 20 | } 21 | } 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_patching-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int minPatches(vector& nums, int n) { 10 | long miss = 1; 11 | int cnt = 0, i = 0; 12 | while(miss <= n){ 13 | if(i < nums.size() && nums[i] <= miss){ 14 | miss += nums[i ++]; 15 | }else{ 16 | miss += miss; 17 | ++ cnt; 18 | } 19 | } 20 | return cnt; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/pythagorean-triple.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | #include 8 | using namespace std; 9 | int a; 10 | 11 | int main() { 12 | scanf("%d",&a); 13 | long long m, n, v; 14 | printf("%d ",a); 15 | if(a%2 == 0){ 16 | m = a/2, n = 1; 17 | printf("%lld %lld\n", m*m - n*n, m*m + n*n); 18 | }else{ 19 | n = (a - 1)/2; 20 | m = n + 1; 21 | printf("%lld %lld\n", 2*m*n, m*m + n*n); 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_decode-xored-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | vector decode(vector& encoded, int first) { 11 | if(encoded.size() == 0) { 12 | return {first}; 13 | } 14 | 15 | int n = encoded.size(); 16 | vector ans(n + 1, first); 17 | for(int i = 0; i < n; ++ i) { 18 | ans[i + 1] = ans[i]^encoded[i]; 19 | } 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /LintCode/lintcode_valid-triangle.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param a: a integer represent the length of one edge 11 | * @param b: a integer represent the length of one edge 12 | * @param c: a integer represent the length of one edge 13 | * @return: whether three edges can form a triangle 14 | */ 15 | bool isValidTriangle(int a, int b, int c) { 16 | return (a + b > c) && (a + c > b) && (b + c > a); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /LeetCode/leetcode_pascaltriangleII.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Algorithm: Brute force 3 | * Time complexity: O(n^2) 4 | * Memory complexity: O(n) 5 | */ 6 | class Solution { 7 | public: 8 | vector getRow(int rowIndex) { 9 | vector ans = vector(rowIndex + 1,0); 10 | ans[0] = 1; 11 | for(int i = 1; i <= rowIndex; i ++){ 12 | ans[i] = 1; 13 | for(int j = i - 1; j > 0; j --){ 14 | ans[j] = ans[j] + ans[j - 1]; 15 | } 16 | } 17 | return ans; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/minimum-absolute-difference-in-an-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(nlgn) 5 | * Space: O(lgn) 6 | */ 7 | #include 8 | using namespace std; 9 | const int N = 200100; 10 | int arr[N], n; 11 | 12 | int main(){ 13 | scanf("%d",&n); 14 | for(int i = 0; i < n; ++ i) 15 | scanf("%d",&arr[i]); 16 | sort(arr, arr + n); 17 | int ans = INT_MAX; 18 | for(int i = 1; i < n; ++ i) 19 | ans = min(ans, arr[i] - arr[i - 1]); 20 | printf("%d\n",ans); 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /LeetCode/leetcode_minimum-moves-to-equal-array-elements.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time:O(n) 5 | * Space:O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int minMoves(vector& nums) { 10 | int ans = 0; 11 | if(nums.size() == 0) 12 | return ans; 13 | int minval = INT_MAX; 14 | for(int i = 0; i < nums.size(); ++ i) 15 | minval = min(minval, nums[i]); 16 | for(int i = 0; i < nums.size(); ++ i) 17 | ans += (nums[i] - minval); 18 | return ans; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /LintCode/lintcode_flip-bits.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Bit Manipulation 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | *@param a, b: Two integer 11 | *return: An integer 12 | */ 13 | int bitSwapRequired(int a, int b) { 14 | // write your code here 15 | int c = a^b; 16 | int iter = 32; 17 | int cnt = 0; 18 | while(iter --){ 19 | if(c&1) 20 | ++ cnt; 21 | c>>=1; 22 | } 23 | return cnt; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_generate-a-string-with-characters-that-have-odd-counts.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: String 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | string generateTheString(int n) { 11 | if(n == 1) { 12 | return "a"; 13 | } 14 | 15 | string ans = ""; 16 | if(n%2 == 0){ 17 | ans += "a"; 18 | -- n; 19 | } 20 | for(int i = 0; i < n; ++ i) { 21 | ans += "b"; 22 | } 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_k-th-symbol-in-grammar.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Bit Manipulation + DFS 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | class Solution { 8 | private: 9 | const string SYMBOLS = "0110100110010110"; 10 | public: 11 | int kthGrammar(int N, int K) { 12 | if(N <= 5){ 13 | return (SYMBOLS[(K - 1)%SYMBOLS.size()] - '0'); 14 | } 15 | 16 | int halfLenOfSymbols = 1<<(N - 2); 17 | return K <= halfLenOfSymbols ? kthGrammar(N - 1, K) : (kthGrammar(N - 1, K - halfLenOfSymbols)^1); 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /LeetCode/leetcode_shuffle-the-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | vector shuffle(vector& nums, int n) { 11 | if(nums.size() != 2*n || n < 2) { 12 | return nums; 13 | } 14 | 15 | vector ans = nums; 16 | for(int idx = 0, i = 0, j = n; i < n; ++ i, ++ j, idx += 2) { 17 | ans[idx] = nums[i]; 18 | ans[idx + 1] = nums[j]; 19 | } 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /LintCode/lintcode_lintcode_o1-check-power-of-2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(lgn) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /* 10 | * @param n: An integer 11 | * @return: True or false 12 | */ 13 | bool checkPowerOf2(int n) { 14 | // write your code here 15 | int cnt = 0; 16 | if(n < 0) 17 | return false; 18 | while(n){ 19 | if(n&1) 20 | ++ cnt; 21 | n >>= 1; 22 | } 23 | return cnt == 1; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/marcs-cakewalk.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(nlgn) 5 | * Space: O(lgn) 6 | */ 7 | #include 8 | using namespace std; 9 | const int N = 100; 10 | int cake[N], n; 11 | 12 | int main(){ 13 | scanf("%d",&n); 14 | for(int i = 0; i < n; ++ i) 15 | scanf("%d",&cake[i]); 16 | sort(cake, cake + n, greater()); 17 | long long ans = 0, mask = 1; 18 | for(int i = 0; i < n; ++ i, mask <<= 1){ 19 | ans += mask*cake[i]; 20 | } 21 | printf("%lld\n",ans); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/p1-paper-cutting.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | #include 8 | 9 | using namespace std; 10 | 11 | long long solve(long long n, long long m){ 12 | long long res = 0; 13 | if(n <= m) 14 | res = (n - 1) + n*(m - 1); 15 | else 16 | res = (m - 1) + m*(n - 1); 17 | return res; 18 | } 19 | 20 | int main() { 21 | long long n, m; 22 | cin >> n >> m; 23 | long long result = solve(n, m); 24 | cout << result << endl; 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /LeetCode/leetcode_n-th-tribonacci-number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int tribonacci(int n) { 11 | if(n == 0) { 12 | return 0; 13 | } 14 | if(n <= 2) { 15 | return 1; 16 | } 17 | 18 | int t[3] = {0, 1, 1}; 19 | for(int i = 3; i <= n; ++ i) { 20 | int sum = t[0] + t[1] + t[2]; 21 | t[0] = t[1], t[1] = t[2], t[2] = sum; 22 | } 23 | return t[2]; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_split-a-string-in-balanced-strings.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int balancedStringSplit(string s) { 11 | int ans = 0, lcnt = 0; 12 | for(int i = 0; i < s.size(); ++ i) { 13 | if(s[i] == 'L') { 14 | ++ lcnt; 15 | } else { 16 | -- lcnt; 17 | } 18 | if(lcnt == 0) { 19 | ++ ans; 20 | } 21 | } 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LintCode/lintcode_next-permutation-ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DFS 4 | * Time: ? 5 | * Space: ? 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param nums: a vector of integers 11 | * @return: return nothing (void), do not return anything, modify nums in-place instead 12 | */ 13 | void nextPermutation(vector &num) { 14 | // write your code here 15 | if(num.size() == 0) 16 | return ; 17 | if(!next_permutation(num.begin(), num.end())) 18 | sort(num.begin(), num.end()); 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /LeetCode/leetcode_broken-calculator.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(lgn) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int brokenCalc(int x, int y) { 11 | if(x >= y) { 12 | return x - y; 13 | } 14 | 15 | int cnt = 0; 16 | while(y > x) { 17 | if((y&1) == 1) { 18 | ++ y; 19 | } else { 20 | y >>= 1; 21 | } 22 | ++ cnt; 23 | } 24 | cnt += (x - y); 25 | return cnt; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_consecutive-characters.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: brute force 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int maxPower(string s) { 11 | if(s.size() == 0) { 12 | return 0; 13 | } 14 | 15 | int ans = 1, cnt = 1; 16 | for(int i = 1; i < s.size(); ++ i) { 17 | if(s[i] != s[i - 1]) { 18 | cnt = 0; 19 | } 20 | ++ cnt; 21 | ans = max(ans, cnt); 22 | } 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_largest-odd-number-in-string.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | string largestOddNumber(string num) { 11 | if(num.size() == 0) { 12 | return ""; 13 | } 14 | 15 | int n = num.size(); 16 | for(int i = n - 1; i >= 0; -- i) { 17 | int v = num[i] - '0'; 18 | if(v%2 == 1) { 19 | return num.substr(0, i + 1); 20 | } 21 | } 22 | return ""; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_majority-element.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int majorityElement(vector &nums) { 10 | int ans = 0, count = 0; 11 | for(int num : nums){ 12 | if(count == 0){ 13 | ++ count; 14 | ans = num; 15 | } else if(ans != num) { 16 | -- count; 17 | } else { 18 | ++ count; 19 | } 20 | } 21 | 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_maximum-number-of-consecutive-values-you-can-make.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy + Math 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int getMaximumConsecutive(vector& coins) { 11 | if(coins.size() == 0) { 12 | return 0; 13 | } 14 | 15 | sort(coins.begin(), coins.end()); 16 | 17 | int ans = 0; 18 | for (int i = 0; i < coins.size() && coins[i] <= ans+1; ++i) 19 | ans += coins[i]; 20 | return ans+1; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /LeetCode/leetcode_the-kth-factor-of-n.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int kthFactor(int n, int k) { 11 | if(n < 1 || k > n) { 12 | return -1; 13 | } 14 | 15 | for(int i = 1, cnt = 0; i <= n; ++ i) { 16 | if(n%i != 0) { 17 | continue; 18 | } 19 | ++ cnt; 20 | if(cnt >= k) { 21 | return i; 22 | } 23 | } 24 | return -1; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /LeetCode/leetcode_triangle.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Algorithm: DP 3 | * Time complexity: O(n^2) 4 | * Memory complexity: O(1) 5 | */ 6 | class Solution { 7 | public: 8 | int minimumTotal(vector > &triangle) { 9 | int len = triangle.size(); 10 | for(int i = len - 2; i >= 0; i --){ 11 | for(int j = 0; j < triangle[i].size(); j ++){ 12 | triangle[i][j] = min(triangle[i][j] + triangle[i + 1][j], triangle[i][j] + triangle[i + 1][j + 1]); 13 | } 14 | } 15 | return triangle[0][0]; 16 | } 17 | }; 18 | 19 | -------------------------------------------------------------------------------- /LeetCode/leetcode_wigglesort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Brute Force 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | void wiggleSort(vector& nums) { 10 | if(nums.size() <= 1) 11 | return ; 12 | int n = nums.size(); 13 | for(int i = 1; i < n; i += 2){ 14 | if(nums[i - 1] > nums[i]) 15 | swap(nums[i - 1], nums[i]); 16 | if(i + 1 < n && nums[i + 1] > nums[i]) 17 | swap(nums[i + 1], nums[i]); 18 | } 19 | return ; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /LintCode/lintcode_climbing-stairs.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DP 4 | * Time: O(n) 5 | * Space: O(n) (Best: O(1)) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param n: An integer 11 | * @return: An integer 12 | */ 13 | int climbStairs(int n) { 14 | // write your code here 15 | if(!n) 16 | return 0; 17 | vector dp(n + 1); 18 | dp[0] = 1; 19 | dp[1] = 1; 20 | for(int i = 2; i <= n; ++ i) 21 | dp[i] = dp[i - 1] + dp[i - 2]; 22 | return dp[n]; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_count-of-matches-in-tournament.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Bit Manipulation 4 | * Time: O(lgn) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int numberOfMatches(int n) { 11 | if(n < 2) { 12 | return 0; 13 | } 14 | 15 | int ans = 0; 16 | for(; n > 1;) { 17 | ans += (n>>1); 18 | if((n&1) == 1) { 19 | n = (n>>1) + 1; 20 | } else { 21 | n >>= 1; 22 | } 23 | } 24 | return ans; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /LeetCode/leetcode_isomorphic-strings.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Hash 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | bool isIsomorphic(string s, string t) { 10 | char s_mp[300] = {0}, t_mp[300] = {0}; 11 | for(int i = 0; i < s.size(); ++ i){ 12 | s_mp[s[i]] = t[i]; 13 | t_mp[t[i]] = s[i]; 14 | } 15 | for(int i = 0; i < s.size(); ++ i){ 16 | if(s_mp[s[i]] != t[i] || t_mp[t[i]] != s[i]) 17 | return false; 18 | } 19 | return true; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /LeetCode/leetcode_largest-perimeter-triangle.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Sort + Math 4 | * Time: O(nlgn) 5 | * Space: O(lgn) 6 | */ 7 | class Solution { 8 | public: 9 | int largestPerimeter(vector& A) { 10 | if(A.size() < 3){ 11 | return 0; 12 | } 13 | 14 | sort(A.begin(), A.end(), greater()); 15 | for(int i = 0; i < A.size() - 2; ++ i){ 16 | if(A[i + 1] + A[i + 2] > A[i]){ 17 | return A[i] + A[i + 1] + A[i + 2]; 18 | } 19 | } 20 | return 0; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /LeetCode/leetcode_last-moment-before-all-ants-fall-out-of-a-plank.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Brainteaser 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int getLastMoment(int n, vector& left, vector& right) { 11 | if(n == 0) { 12 | return 0; 13 | } 14 | 15 | int ans = 0; 16 | for(int l : left) { 17 | ans = max(ans, l); 18 | } 19 | for(int r : right) { 20 | ans = max(ans, n - r); 21 | } 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_graycode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Algorithm: Brute force, Implementation 3 | * Time complexity: O(2^n) 4 | * Memory complexity: O(2^n) 5 | */ 6 | class Solution { 7 | public: 8 | vector grayCode(int n) { 9 | vector res; 10 | res.reserve(1<= 0; j --){ 15 | res.push_back(high_bit | res[j]); 16 | } 17 | } 18 | return res; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/simple-array-sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | int n, sum; 14 | 15 | int main() { 16 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 17 | sum = 0; 18 | scanf("%d",&n); 19 | while(n --){ 20 | int a; 21 | scanf("%d",&a); 22 | sum += a; 23 | } 24 | printf("%d\n",sum); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /LeetCode/leetcode_find-permutation.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | vector findPermutation(string s) { 10 | vector ans; 11 | if(s.size() == 0) 12 | return ans; 13 | for(int i = 0; i <= s.size(); ++ i){ 14 | if(i == s.size() || s[i] == 'I'){ 15 | for(int j = i + 1, k = ans.size(); j > k; -- j){ 16 | ans.push_back(j); 17 | } 18 | } 19 | } 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /LeetCode/leetcode_maximum-subarray.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int maxSubArray(vector& nums) { 10 | int ans = nums[0]; 11 | int sum = nums[0]; 12 | for(int i = 1; i < nums.size(); i ++){ 13 | if(sum + nums[i] > nums[i]){ 14 | sum += nums[i]; 15 | }else{ 16 | sum = nums[i]; 17 | } 18 | 19 | ans = max(ans, sum); 20 | } 21 | 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_mean-of-array-after-removing-some-elements.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(nlgn) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | double trimMean(vector& arr) { 11 | if(arr.size() == 0) { 12 | return 0; 13 | } 14 | 15 | sort(arr.begin(), arr.end()); 16 | 17 | int n = arr.size(), m = n/20, sum = 0, tot = 0; 18 | for(int i = m; i < n - m; ++ i, ++ tot) { 19 | sum += arr[i]; 20 | } 21 | return (double)sum/(double)tot; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /LeetCode/leetcode_minimize-maximum-pair-sum-in-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(nlgn) 5 | * Space: O(lgn) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int minPairSum(vector& nums) { 11 | if(nums.size()%2 == 1) { 12 | return 0; 13 | } 14 | 15 | sort(nums.begin(), nums.end()); 16 | 17 | int ans = 0, n = nums.size(), m = nums.size()/2; 18 | for(int i = 0; i < m; ++ i) { 19 | ans = max(ans, nums[i] + nums[n - 1 - i]); 20 | } 21 | return ans; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /LintCode/lintcode_add-digits.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(m^2) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param num a non-negative integer 11 | * @return one digit 12 | */ 13 | int addDigits(int num) { 14 | // Write your code here 15 | int tmp = 0; 16 | while(num >= 10){ 17 | tmp = 0; 18 | while(num){ 19 | tmp += num%10; 20 | num /= 10; 21 | } 22 | num = tmp; 23 | } 24 | return num; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/lowest-triangle.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | #include 8 | 9 | using namespace std; 10 | 11 | int lowestTriangle(int base, int area){ 12 | if(area%base == 0 || (2*area)%base == 0) 13 | return 2*area/base; 14 | int mod_v = (2*area)%base; 15 | return (2*area + (base - mod_v))/base; 16 | } 17 | 18 | int main() { 19 | int base; 20 | int area; 21 | cin >> base >> area; 22 | int height = lowestTriangle(base, area); 23 | cout << height << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_container-with-most-water.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * TODO 4 | * Tag: Two Pointers 5 | * Time: O(n) 6 | * Space: O(1) 7 | */ 8 | class Solution { 9 | public: 10 | int maxArea(vector &height) { 11 | int l = 0, r = height.size() - 1; 12 | int ans = INT_MIN; 13 | while(l < r){ 14 | ans = max(ans, min(height[l],height[r])*(r - l)); 15 | if(height[l] <= height[r]){ 16 | l ++; 17 | }else{ 18 | r --; 19 | } 20 | } 21 | 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_domino-and-tromino-tiling.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Better Solution 4 | * Tag: DP 5 | * Time: O(n^2) 6 | * Space: O(n) 7 | */ 8 | class Solution { 9 | private: 10 | const int MOD = 1000000007; 11 | public: 12 | int numTilings(int N) { 13 | vector dp(N + 1, 0); 14 | dp[0] = 1; 15 | for(int i = 1; i <= N; ++ i){ 16 | for(int j = 1; j <= i; ++ j){ 17 | dp[i] += (dp[i - j]*(j <= 2 ? 1 : 2))%MOD; 18 | dp[i] %= MOD; 19 | } 20 | } 21 | 22 | return dp[N]; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_remove-palindromic-subsequences.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Two Pointer 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int removePalindromeSub(string s) { 11 | if(s.size() == 0) { 12 | return 0; 13 | } 14 | 15 | bool isPalind = true; 16 | for(int l = 0, r = s.size() - 1; l <= r; ++ l, -- r) { 17 | if(s[l] != s[r]) { 18 | isPalind = false; 19 | break; 20 | } 21 | } 22 | return isPalind ? 1 : 2; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LintCode/lintcode_Ugly-number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(lgn) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param num an integer 11 | * @return true if num is an ugly number or false 12 | */ 13 | bool isUgly(int num) { 14 | // Write your code here 15 | if(!num) 16 | return false; 17 | for(int i = 0; i < 3; ++ i){ 18 | while(num%fact[i] == 0) 19 | num /= fact[i]; 20 | } 21 | return num == 1; 22 | } 23 | private: 24 | int fact[3]={2,3,5}; 25 | }; 26 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/is-binary-search-tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DFS 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | bool checkBST(Node* root) { 8 | bool ans = true; 9 | int maxval = -(1<<30); 10 | solve(root, maxval, ans); 11 | return ans; 12 | } 13 | 14 | void solve(Node* root, int &maxval, bool &ans){ 15 | if(root->left) 16 | solve(root->left, maxval, ans); 17 | if(maxval >= root->data){ 18 | ans = false; 19 | return ; 20 | } 21 | maxval = max(maxval, root->data); 22 | if(root->right) 23 | solve(root->right, maxval, ans); 24 | } 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_closest-divisors.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(sqrt(n)) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | vector closestDivisors(int num) { 11 | if(num < 0) { 12 | return {}; 13 | } 14 | 15 | for(int i = sqrt(num + 2); i > 0; -- i) { 16 | if((num + 1)%i == 0) { 17 | return {i, (num + 1)/i}; 18 | } 19 | if((num + 2)%i == 0) { 20 | return {i, (num + 2)/i}; 21 | } 22 | } 23 | return {}; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_replace-elements-with-greatest-element-on-right-side.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | vector replaceElements(vector& arr) { 10 | vector ans(arr.size()); 11 | if(arr.size() == 0){ 12 | return ans; 13 | } 14 | 15 | int maxVal = -1; 16 | for(int i = arr.size() - 1; i >= 0; -- i){ 17 | ans[i] = maxVal; 18 | maxVal = max(maxVal, arr[i]); 19 | } 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /LeetCode/leetcode_transpose-matrix.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(nm) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | vector> transpose(vector>& A) { 10 | if(A.size() == 0){ 11 | return A; 12 | } 13 | 14 | vector> ans(A[0].size(), vector(A.size())); 15 | for(int i = 0; i < A.size(); ++ i){ 16 | for(int j = 0; j < A[i].size(); ++ j){ 17 | ans[j][i] = A[i][j]; 18 | } 19 | } 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /LeetCode/leetcode_uglynumber.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Tag: Math 3 | * Time: O(lgn) where n is the given num 4 | * Space: O(1) 5 | * 6 | */ 7 | class Solution { 8 | public 9 | bool isUgly(int num) { 10 | if(num <= 0) 11 | return false; 12 | if(num <= 6) 13 | return true; 14 | for(int i = 0; i < 3; ++ i){ 15 | while(num%div_val[i] == 0) 16 | num/=div_val[i]; 17 | if(num == 1) 18 | break; 19 | } 20 | return num == 1; 21 | } 22 | private: 23 | vector div_val={2,3,5}; 24 | }; 25 | -------------------------------------------------------------------------------- /LintCode/lintcode_singleton.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Design 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @return: The same instance of this class every time 11 | */ 12 | static Solution* getInstance() { 13 | // write your code here 14 | static Solution* pa = new Solution(); 15 | return pa; 16 | } 17 | 18 | // Noncopyable. 19 | Solution(const Solution&) = delete; 20 | Solution& operator=(const Solution&) = delete; 21 | 22 | private: 23 | Solution() {} 24 | ~Solution() {} 25 | }; 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_check-if-all-1s-are-at-least-length-k-places-away.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Sliding Window 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | bool kLengthApart(vector& nums, int k) { 11 | int prev = -k-1; 12 | for(int i = 0; i < nums.size(); ++ i) { 13 | if(nums[i] == 0) { 14 | continue; 15 | } 16 | 17 | if(i - prev <= k) { 18 | return false; 19 | } 20 | prev = i; 21 | } 22 | return true; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_gas-station.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int canCompleteCircuit(vector& gas, vector& cost) { 11 | int start = 0, total = 0, tank = 0; 12 | for(int i = 0; i < gas.size(); ++ i){ 13 | tank += gas[i] - cost[i]; 14 | if(tank < 0){ 15 | start = i + 1; 16 | total += tank; 17 | tank = 0; 18 | } 19 | } 20 | 21 | return (total + tank < 0) ? -1 : start; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /LeetCode/leetcode_maximum-number-of-coins-you-can-get.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(nlgn) 5 | * Space: O(lgn) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int maxCoins(vector& piles) { 11 | if(piles.size() < 3 || piles.size()%3 != 0 ) { 12 | return 0; 13 | } 14 | 15 | sort(piles.begin(), piles.end()); 16 | 17 | int ans = 0, n = piles.size()/3; 18 | for(int i = 0, j = piles.size() - 1; i < n; ++ i, j -= 2){ 19 | ans += piles[j - 1]; 20 | } 21 | return ans; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /LeetCode/leetcode_optimal-division.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | string optimalDivision(vector& nums) { 10 | string ans = ""; 11 | int n = nums.size(); 12 | for(int i = 0; i < n; ++ i){ 13 | if(i >= 1) 14 | ans += "/"; 15 | if(i == 1 && n >= 3) 16 | ans += "("; 17 | ans += to_string(nums[i]); 18 | if(i == n - 1 && n >= 3) 19 | ans += ")"; 20 | } 21 | return ans; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /LeetCode/leetcode_search-in-a-binary-search-tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DFS 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | /** 8 | * Definition for a binary tree node. 9 | * struct TreeNode { 10 | * int val; 11 | * TreeNode *left; 12 | * TreeNode *right; 13 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 14 | * }; 15 | */ 16 | class Solution { 17 | public: 18 | TreeNode* searchBST(TreeNode* root, int val) { 19 | return root == NULL ? NULL : root->val == val? root : root->val > val ? searchBST(root->left, val) : searchBST(root->right, val); 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /LeetCode/leetcode_best-sightseeing-pair.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int maxScoreSightseeingPair(vector& A) { 11 | if(A.size() == 0){ 12 | return 0; 13 | } 14 | 15 | int ans = INT_MIN, maxSightseeing = A[A.size() - 1] - A.size() + 1; 16 | for(int i = A.size() - 2; i >= 0; -- i){ 17 | ans = max(ans, A[i] + i + maxSightseeing); 18 | maxSightseeing = max(maxSightseeing, A[i] - i); 19 | } 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /LeetCode/leetcode_sign-of-the-product-of-an-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int arraySign(vector& nums) { 11 | if(nums.size() == 0) { 12 | return 0; 13 | } 14 | 15 | int negCnt = 0; 16 | for(int num : nums) { 17 | if(num == 0) { 18 | return 0; 19 | } 20 | if(num < 0) { 21 | ++ negCnt; 22 | } 23 | } 24 | return (negCnt&1) == 0? 1 : -1; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /LeetCode/leetcode_assign-cookies.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(nlgn) 5 | * Space: O(lgn) 6 | */ 7 | class Solution { 8 | public: 9 | int findContentChildren(vector& g, vector& s) { 10 | if(g.size() == 0 || s.size() == 0) 11 | return 0; 12 | int ans = 0, gid = 0; 13 | sort(g.begin(), g.end()); 14 | sort(s.begin(), s.end()); 15 | for(int i = 0; i < s.size() && gid < g.size(); ++ i){ 16 | if(s[i] >= g[gid]){ 17 | ++ ans, ++ gid; 18 | } 19 | } 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /LeetCode/leetcode_maximum-69-number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int maximum69Number (int num) { 10 | string s_num = to_string(num); 11 | for(int i = 0; i < s_num[i]; ++ i){ 12 | if(s_num[i] == '6'){ 13 | s_num[i] = '9'; 14 | break; 15 | } 16 | } 17 | 18 | int ans = 0; 19 | for(int i = 0; i < s_num[i]; ++ i){ 20 | ans = (ans*10) + (s_num[i] - '0'); 21 | } 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_maximum-nesting-depth-of-the-parentheses.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int maxDepth(string s) { 11 | if(s.size() < 2) { 12 | return 0; 13 | } 14 | 15 | int ans = 0, cnt = 0; 16 | for(char c : s) { 17 | if(c == '(') { 18 | ++ cnt; 19 | } else if(c == ')') { 20 | -- cnt; 21 | } 22 | 23 | ans = max(ans, cnt); 24 | } 25 | return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_number-of-burgers-with-no-waste-of-ingredients.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(1) 5 | * Space:O(1) 6 | */ 7 | class Solution { 8 | public: 9 | vector numOfBurgers(int tomatoSlices, int cheeseSlices) { 10 | vector ans; 11 | if((tomatoSlices&1) == 1 || tomatoSlices < 2*cheeseSlices || 4*cheeseSlices < tomatoSlices){ 12 | return ans; 13 | } 14 | 15 | ans.resize(2); 16 | ans[0] = (tomatoSlices>>1) - cheeseSlices; 17 | ans[1] = 2*cheeseSlices - (tomatoSlices>>1); 18 | return ans; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/tower-breakers-1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Game 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | int n,m; 14 | 15 | int main() { 16 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 17 | int T; 18 | scanf("%d",&T); 19 | while(T --){ 20 | scanf("%d%d",&m,&n); 21 | if(n == 1 || m%2 == 0) 22 | puts("2"); 23 | else 24 | puts("1"); 25 | } 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_binary-subarrays-with-sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Hash Map 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | class Solution { 8 | public: 9 | int numSubarraysWithSum(vector& A, int S) { 10 | unordered_map sumCntMap; 11 | sumCntMap[0] = 1; 12 | 13 | int ans = 0, sum = 0; 14 | for(int i = 0; i < A.size(); ++ i){ 15 | sum += A[i]; 16 | if(sum >= S){ 17 | ans += sumCntMap[sum - S]; 18 | } 19 | ++ sumCntMap[sum]; 20 | } 21 | 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_decompress-run-length-encoded-list.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n*Sum(arr[i]) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | vector decompressRLElist(vector& nums) { 11 | if(nums.size() == 0 || nums.size()%2 == 1) { 12 | return {}; 13 | } 14 | 15 | vector ans; 16 | for(int i = 0; i < nums.size(); i += 2) { 17 | for(int j = 0; j < nums[i]; ++ j) { 18 | ans.push_back(nums[i + 1]); 19 | } 20 | } 21 | return ans; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /LeetCode/leetcode_find-n-unique-integers-sum-up-to-zero.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | class Solution { 8 | public: 9 | vector sumZero(int n) { 10 | vector ans(n); 11 | if(n == 0){ 12 | return ans; 13 | } 14 | 15 | int idx = 0; 16 | for(int i = 1; i <= (n>>1); ++ i, idx += 2){ 17 | ans[idx] = i; 18 | ans[idx + 1] = -i; 19 | } 20 | if((n&1) > 0){ 21 | ans[n - 1] = 0; 22 | } 23 | 24 | return ans; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /LeetCode/leetcode_minimize-product-sum-of-two-arrays.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(nlgn) 5 | * Space: O(lgn) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int minProductSum(vector& nums1, vector& nums2) { 11 | if(nums1.size() == 0) { 12 | return 0; 13 | } 14 | 15 | sort(nums1.begin(), nums1.end()); 16 | sort(nums2.begin(), nums2.end(), greater()); 17 | int sum = 0; 18 | for(int i = 0; i < nums1.size(); ++ i) { 19 | sum += nums1[i]*nums2[i]; 20 | } 21 | return sum; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/a-very-big-sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | long long sum; 14 | int n; 15 | 16 | int main() { 17 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 18 | sum = 0; 19 | scanf("%d",&n); 20 | long long v; 21 | for(int i = 0; i < n; ++ i){ 22 | scanf("%lld",&v); 23 | sum += v; 24 | } 25 | printf("%lld\n",sum); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/strange-advertising.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | int n; 14 | long long ans; 15 | 16 | int main() { 17 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 18 | scanf("%d",&n); 19 | long long v = 5; 20 | ans = 0; 21 | for(int i = 1; i <= n; ++ i){ 22 | ans += v/2; 23 | v = (v/2)*3; 24 | } 25 | printf("%lld\n",ans); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_maximum-average-subarray-i.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | double findMaxAverage(vector& nums, int k) { 10 | int maxSum = INT_MIN, sum = 0; 11 | for(int i = 0; i < nums.size(); ++ i){ 12 | if(i >= k){ 13 | sum -= nums[i - k]; 14 | } 15 | 16 | sum += nums[i]; 17 | if(i >= k - 1){ 18 | maxSum = max(maxSum, sum); 19 | } 20 | } 21 | 22 | return (double)maxSum/k; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_shortest-unsorted-continuous-subarray.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Sort 4 | * Time: O(nlgn) 5 | * Space: O(n) 6 | */ 7 | class Solution { 8 | public: 9 | int findUnsortedSubarray(vector& nums) { 10 | vector tmparr(nums); 11 | sort(tmparr.begin(), tmparr.end()); 12 | int lbnd = 0, rbnd = nums.size() - 1; 13 | for(; lbnd < nums.size() && tmparr[lbnd] == nums[lbnd]; ++ lbnd) ; 14 | if(lbnd >= nums.size()) 15 | return 0; 16 | for(; rbnd >= 0 && tmparr[rbnd] == nums[rbnd]; -- rbnd) ; 17 | return rbnd - lbnd + 1; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /LintCode/lintcode_fibonacci.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DP 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution{ 8 | public: 9 | /** 10 | * @param n: an integer 11 | * @return an integer f(n) 12 | */ 13 | int fibonacci(int n) { 14 | // write your code here 15 | if(n == 1) 16 | return 0; 17 | if(n == 2) 18 | return 1; 19 | int pre = 0, cur = 1; 20 | for(int i = 3; i <= n; ++ i){ 21 | int tmp = cur; 22 | cur += pre; 23 | pre = tmp; 24 | } 25 | return cur; 26 | } 27 | }; 28 | 29 | -------------------------------------------------------------------------------- /LeetCode/leetcode_binary-prefix-divisible-by-5.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Bit Manipulation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | vector prefixesDivBy5(vector& A) { 10 | vector ans(A.size(), false); 11 | if(A.size() == 0){ 12 | return ans; 13 | } 14 | 15 | int v = 0; 16 | for(int i = 0; i < A.size(); ++ i, v <<= 1){ 17 | v |= A[i]; 18 | v = v%5; 19 | if(v == 0) { 20 | ans[i] = true; 21 | } 22 | } 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_maximum-ice-cream-bars.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(nlgn) 5 | * Space: O(lgn) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int maxIceCream(vector& costs, int coins) { 11 | if(costs.size() == 0) { 12 | return 0; 13 | } 14 | 15 | sort(costs.begin(), costs.end()); 16 | int ans = 0, n = costs.size(); 17 | for(int sum = 0, i = 0; i < n; ++ i, ++ ans) { 18 | sum += costs[i]; 19 | if(sum > coins) { 20 | break; 21 | } 22 | } 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_min-cost-climbing-stairs.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DP 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int minCostClimbingStairs(vector& cost) { 10 | if(cost.size() == 2){ 11 | return min(cost[0], cost[1]); 12 | } 13 | 14 | int numOfStairs = cost.size(); 15 | vector dp(3, 0); 16 | for(int i = 2; i <= numOfStairs; ++ i){ 17 | dp[2] = min(dp[0] + cost[i - 2], dp[1] + cost[i - 1]); 18 | dp[0] = dp[1], dp[1] = dp[2]; 19 | } 20 | return min(dp[1], dp[2]); 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /LeetCode/leetcode_minimum-add-to-make-parentheses-valid.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Stack 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | class Solution { 8 | public: 9 | int minAddToMakeValid(string S) { 10 | if(S.size() == 0){ 11 | return 0; 12 | } 13 | 14 | stack stk; 15 | for(int i = 0; i < S.size(); ++ i){ 16 | if(!stk.empty() && stk.top() == '(' && S[i] == ')') { 17 | stk.pop(); 18 | } else { 19 | stk.push(S[i]); 20 | } 21 | } 22 | 23 | return stk.size(); 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_output-contest-matches.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(nlgn) 5 | * Space: O(n) 6 | */ 7 | class Solution { 8 | public: 9 | string findContestMatch(int n) { 10 | vector game_match( n + 1 ); 11 | int tot = 0; 12 | for(int i = 1; i <= n; ++ i ) 13 | game_match[tot ++] = to_string(i); 14 | while(tot > 1){ 15 | for(int i = 0; i < tot/2; ++ i) 16 | game_match[i] = "("+game_match[i]+","+game_match[tot - i - 1]+")"; 17 | tot >>= 1; 18 | } 19 | return game_match[0]; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /LeetCode/leetcode_remove-duplicates-from-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Two Pointers 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int removeDuplicates(vector& nums) { 11 | int n = nums.size(); 12 | if(n == 0) { 13 | return 0; 14 | } 15 | 16 | int len = n, i = 0, j = 1; 17 | for(; j < n; j ++){ 18 | if(nums[i] != nums[j]){ 19 | nums[++i] = nums[j]; 20 | len --; 21 | } 22 | } 23 | i ++; 24 | 25 | return i; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LintCode/lintcode_check-full-binary-tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DFS 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | class Solution { 8 | public: 9 | /* 10 | * @param : the given tree 11 | * @return: Whether it is a full tree 12 | */ 13 | bool isFullTree(TreeNode * root) { 14 | return root == NULL? true: hasOnlyOneChild(root)? false: isFullTree(root->left) && isFullTree(root->right); 15 | } 16 | private: 17 | bool hasOnlyOneChild(TreeNode *root){ 18 | return (root->left != NULL && root->right == NULL) || 19 | (root->left == NULL && root->right != NULL); 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /LintCode/lintcode_unique-characters.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Sort or Hash 4 | * Time: O(nlgn) or O(n) 5 | * Space: O(lgn) or O(n) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param str: a string 11 | * @return: a boolean 12 | */ 13 | bool isUnique(string &str) { 14 | // write your code here 15 | string tmp_str = str; 16 | sort(tmp_str.begin(), tmp_str.end()); 17 | int len = tmp_str.size() - 1; 18 | for(int i = 0; i < len; ++ i) 19 | if(tmp_str[i] == tmp_str[i + 1]) 20 | return false; 21 | return true; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/arrays_ds.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Brute Force 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | const int N = 1100; 14 | int n; 15 | int arr[N]; 16 | 17 | int main() { 18 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 19 | scanf("%d",&n); 20 | for(int i = 0; i < n ; ++ i) 21 | scanf("%d",&arr[i]); 22 | for(int i = n - 1; i >= 0; -- i) 23 | printf("%d ",arr[i]); 24 | puts(""); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /LeetCode/leetcode_132-pattern.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Data Structure (stack) 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | bool find132pattern(vector& nums) { 10 | int s2 = INT_MIN; 11 | stack stk; 12 | for(int i = nums.size() - 1; i >= 0; -- i){ 13 | if(nums[i] < s2) 14 | return true; 15 | else while(!stk.empty() && stk.top() < nums[i]){ 16 | s2 = stk.top(); 17 | stk.pop(); 18 | } 19 | stk.push(nums[i]); 20 | } 21 | return false; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /LeetCode/leetcode_get-equal-substrings-within-budget.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Brute Force 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int equalSubstring(string s, string t, int maxCost) { 11 | int ans = 0, cost = 0, n = s.size(); 12 | for(int l = 0, r = 0; r < n; ++ r){ 13 | cost += abs(s[r] - t[r]); 14 | while(cost > maxCost && l <= r) { 15 | cost -= abs(s[l] - t[l]); 16 | ++ l; 17 | } 18 | 19 | ans = max(ans, r - l + 1); 20 | } 21 | return ans; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /LeetCode/leetcode_how-many-apples-can-you-put-into-the-basket.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(nlgn) 5 | * Space: O(lgn) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int maxNumberOfApples(vector& arr) { 11 | if(arr.size() == 0){ 12 | return 0; 13 | } 14 | 15 | sort(arr.begin(), arr.end()); 16 | int ans = 0, sum = 0; 17 | for(int v : arr){ 18 | sum += v; 19 | if(sum > 5000) { 20 | break; 21 | } 22 | 23 | ++ ans; 24 | } 25 | return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_longest-happy-prefix.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Rolling Hash 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | private: 10 | const int MOD = 1e9 + 7; 11 | public: 12 | string longestPrefix(string s) { 13 | long long l = 0, r = 0, p = 1; 14 | int len = 0, n = s.size(); 15 | for (int i = 0; i < n - 1; i++) { 16 | l = (l * 26 + (s[i] -'a')) % MOD; 17 | r = (r + p * (s[n - i - 1] - 'a')) % MOD; 18 | if (l == r) len = i + 1; 19 | p = p * 26 % MOD; 20 | } 21 | return s.substr(0, len); 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /LeetCode/leetcode_maximum-product-of-two-elements-in-an-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int maxProduct(vector& nums) { 11 | int max1 = max(nums[0], nums[1]), max2 = min(nums[0], nums[1]); 12 | for(int i = 2; i = max1) { 14 | max2 = max1; 15 | max1 = nums[i]; 16 | } else if(nums[i] >= max2) { 17 | max2 = nums[i]; 18 | } 19 | } 20 | return (max1 - 1)*(max2 - 1); 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /LeetCode/leetcode_shifting-letters.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | string shiftingLetters(string S, vector& shifts) { 10 | if(S.size() == 0){ 11 | return S; 12 | } 13 | 14 | int totalShifts = 0; 15 | for(int i = shifts.size() - 1; i >= 0; -- i){ 16 | int val = (S[i] - 'a'); 17 | totalShifts = (totalShifts + shifts[i])%26; 18 | val = (val + totalShifts)%26; 19 | S[i] = (val + 'a'); 20 | } 21 | return S; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/save-the-prisoner.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | int n, m, s; 14 | 15 | int main() { 16 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 17 | int T; 18 | scanf("%d",&T); 19 | while(T --){ 20 | scanf("%d%d%d",&n,&m,&s); 21 | -- s; 22 | -- m; 23 | m %= n; 24 | s = (s + m)%n; 25 | printf("%d\n",s+1); 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /LeetCode/leetcode_check-if-n-and-its-double-exist.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Hash 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | bool checkIfExist(vector& arr) { 11 | if(arr.size() < 2) { 12 | return false; 13 | } 14 | 15 | unordered_set vis = {arr[0]}; 16 | for(int i = 1; i < arr.size(); ++ i){ 17 | if(vis.count(arr[i]*2) > 0 || (arr[i]%2 == 0 && vis.count(arr[i]/2) > 0)) { 18 | return true; 19 | } 20 | vis.insert(arr[i]); 21 | } 22 | return false; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_ongest-continuous-increasing-subsequence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DP 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int findLengthOfLCIS(vector& nums) { 10 | if(nums.size() <= 0){ 11 | return nums.size(); 12 | } 13 | 14 | int ans = 1, cnt = 1; 15 | for(int i = 1; i < nums.size(); ++ i){ 16 | if(nums[i] > nums[i - 1]){ 17 | ++ cnt; 18 | } else { 19 | cnt = 1; 20 | } 21 | ans = max(ans, cnt); 22 | } 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_check-if-binary-string-has-at-most-one-segment-of-ones.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | bool checkOnesSegment(string s) { 11 | if(s.size() == 0) { 12 | return false; 13 | } 14 | 15 | int cnt = 1; 16 | for(int i = 1; i < s.size(); ++ i){ 17 | if(s[i]=='1' && s[i - 1] =='1'){ 18 | cnt = 1; 19 | } else if(s[i]=='1' && cnt >= 1) { 20 | return false; 21 | } 22 | } 23 | return true; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_flipgame.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementaion 4 | * Time: O(n) 5 | * Space: O(1) (excluding the vector space for saving results) 6 | */ 7 | class Solution { 8 | public: 9 | vector generatePossibleNextMoves(string s) { 10 | vector ans; 11 | if(s.size() < 2) 12 | return ans; 13 | for(int i = 0; i < s.size() - 1; ++ i){ 14 | if(s[i] == '+' && s[i + 1] == '+'){ 15 | s[i] = s[i+ 1] = '-'; 16 | ans.push_back(s); 17 | s[i] = s[i+ 1] = '+'; 18 | } 19 | } 20 | return ans; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /LeetCode/leetcode_matrix-diagonal-sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int diagonalSum(vector>& mat) { 11 | if(mat.size() == 0 || mat[0].size() == 0) { 12 | return 0; 13 | } 14 | 15 | int n = mat.size(), m = mat[0].size(), ans = 0; 16 | for(int i = 0, j = m - 1; i < n && j >= 0; ++ i, -- j) { 17 | ans += mat[i][i] + mat[i][j]; 18 | if(i == j) { 19 | ans -= mat[i][i]; 20 | } 21 | } 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_maximum-absolute-sum-of-any-subarray.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int maxAbsoluteSum(vector& nums) { 11 | if(nums.size() == 0) { 12 | return 0; 13 | } 14 | 15 | int ans = 0, minSum = 0, maxSum = 0; 16 | for(int i = 0; i < nums.size(); ++ i) { 17 | minSum = min(0, minSum + nums[i]); 18 | maxSum = max(0, maxSum + nums[i]); 19 | 20 | ans = max(ans, max(-minSum, maxSum)); 21 | } 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_moving-stones-until-consecutive.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | vector numMovesStones(int a, int b, int c) { 11 | vector arr = {a, b, c}; 12 | sort(arr.begin(), arr.end()); 13 | 14 | int d1 = arr[1] - arr[0] - 1, d2 = arr[2] - arr[1] - 1; 15 | vector ans = {2, d1 + d2}; 16 | if(d1 == 0 && d2 == 0) { 17 | ans[0] = 0; 18 | } else if(d1 <= 1 || d2 <= 1) { 19 | ans[0] = 1; 20 | } 21 | 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_number-complement.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Bit Manipulation 4 | * Time: O(lgn) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int findComplement(int num) { 10 | int ans = 0, mask = 1; 11 | while(num){ 12 | if((num&1) == 0) 13 | ans |= mask; 14 | num >>= 1; 15 | mask <<= 1; 16 | } 17 | return ans; 18 | } 19 | }; 20 | 21 | class Solution { 22 | public: 23 | int findComplement(int num) { 24 | unsigned int mask = ~0; 25 | while(num & mask) mask <<= 1; 26 | return ~num & ~mask; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/big-sorting.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Sort 4 | * Time: O(nlgn) 5 | * Space: O(lgn) 6 | */ 7 | #include 8 | using namespace std; 9 | 10 | bool cmp(string &a, string &b){ 11 | return a.size() == b.size()? a < b : a.size() < b.size(); 12 | } 13 | 14 | int main(){ 15 | int n; 16 | cin >> n; 17 | vector unsorted(n); 18 | for(int unsorted_i = 0; unsorted_i < n; unsorted_i++){ 19 | cin >> unsorted[unsorted_i]; 20 | } 21 | sort(unsorted.begin(), unsorted.end(), cmp); 22 | for(int i = 0; i < n; ++ i) 23 | cout< 8 | using namespace std; 9 | string s, t = "hackerrank"; 10 | int i, j; 11 | 12 | int main(){ 13 | int q; 14 | cin >> q; 15 | for(int a0 = 0; a0 < q; a0++){ 16 | cin>>s; 17 | i = j = 0; 18 | for(i = 0; i < s.size() && j < t.size(); ++ i){ 19 | if(s[i] == t[j]) 20 | ++ j; 21 | } 22 | if(j >= t.size()) 23 | puts("YES"); 24 | else 25 | puts("NO"); 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/migratory-birds.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | #include 8 | using namespace std; 9 | int n, v, dict[6]; 10 | 11 | int main(){ 12 | scanf("%d",&n); 13 | memset(dict, 0, sizeof(dict)); 14 | for(int i = 0; i < n; ++ i){ 15 | scanf("%d",&v); 16 | ++ dict[v]; 17 | } 18 | int ans_id = 0, ans_cnt = 0; 19 | for(int i = 1; i <= 5; ++ i){ 20 | if(dict[i] > ans_cnt){ 21 | ans_cnt = dict[i]; 22 | ans_id = i; 23 | } 24 | } 25 | printf("%d\n",ans_id); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_detect-capital.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | bool detectCapitalUse(string word) { 10 | int capital_cnt = 0; 11 | bool isFirstCapital = false; 12 | for(int i = 0; i < word.size(); ++ i){ 13 | if(word[i] >= 'A' && word[i] <= 'Z'){ 14 | ++ capital_cnt; 15 | if(i == 0) 16 | isFirstCapital = true; 17 | } 18 | } 19 | return capital_cnt == 0 || capital_cnt == word.size() || (capital_cnt == 1 && isFirstCapital); 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /LeetCode/leetcode_find-the-derangement-of-an-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DP 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | private: 10 | const int MOD = 1e9 + 7; 11 | public: 12 | int findDerangement(int n) { 13 | if(n < 2) { 14 | return 0; 15 | } 16 | if(n == 2) { 17 | return 1; 18 | } 19 | 20 | long long ans = 0, prev = 0, cur = 1, idx = 2; 21 | for(; idx < n; ++ idx) { 22 | ans = ((prev + cur)*idx)%MOD; 23 | prev = cur; 24 | cur = ans; 25 | } 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /LeetCode/leetcode_height-checker.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Sort 4 | * Time: O(nlgn) 5 | * Space: O(n) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int heightChecker(vector& heights) { 11 | if(heights.size() < 2) { 12 | return 0; 13 | } 14 | 15 | vector sortHeights = heights; 16 | sort(sortHeights.begin(), sortHeights.end()); 17 | 18 | int ans = 0; 19 | for(int i = 0; i < heights.size(); ++ i) { 20 | if(heights[i] != sortHeights[i]) { 21 | ++ ans; 22 | } 23 | } 24 | return ans; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /LeetCode/leetcode_minimum-operations-to-make-the-array-increasing.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int minOperations(vector& nums) { 11 | if(nums.size() < 2) { 12 | return 0; 13 | } 14 | 15 | int n = nums.size(), ans = 0; 16 | for(int i = 1; i < n; ++ i) { 17 | if(nums[i - 1] < nums[i]) { 18 | continue; 19 | } 20 | ans += (nums[i - 1] - nums[i] + 1); 21 | nums[i] = nums[i - 1] + 1; 22 | } 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_number-of-segments-in-a-string.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int countSegments(string s) { 10 | int res = 0; 11 | bool meetSpace = true; 12 | if(s.size() == 0) 13 | return res; 14 | for(int i = 0; i < s.size(); ++ i){ 15 | if(s[i] == ' ') 16 | meetSpace = true; 17 | else{ 18 | if(meetSpace) 19 | ++ res; 20 | meetSpace = false; 21 | } 22 | } 23 | return res; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_three-consecutive-odds.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | bool threeConsecutiveOdds(vector& arr) { 11 | if(arr.size() < 3) { 12 | return false; 13 | } 14 | 15 | for(int i = 0; i < arr.size() - 2; ++ i) { 16 | if(arr[i]%2 == 0) { 17 | continue; 18 | } 19 | 20 | if(arr[i + 1]%2 == 1 && arr[i + 2]%2 == 1) { 21 | return true; 22 | } 23 | } 24 | return false; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /LeetCode/leetcode_unique-number-of-occurrences.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Hash 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | class Solution { 8 | public: 9 | bool uniqueOccurrences(vector& arr) { 10 | unordered_map numCnt; 11 | unordered_set cntDict; 12 | 13 | for(int num : arr){ 14 | ++ numCnt[num]; 15 | } 16 | for(pair cnt : numCnt){ 17 | if(cntDict.count(cnt.second) > 0){ 18 | return false; 19 | } 20 | cntDict.insert(cnt.second); 21 | } 22 | 23 | return true; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LintCode/lintcode_find-the-missing-number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Bit Manipulation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param nums: a vector of integers 11 | * @return: an integer 12 | */ 13 | int findMissing(vector &nums) { 14 | // write your code here 15 | if(!nums.size()) 16 | return 0; 17 | int n = nums.size(); 18 | int ans = nums[0]; 19 | for(int i = 1; i < n; ++ i) 20 | ans^=nums[i]; 21 | for(int i = 0; i <= n; ++ i) 22 | ans^=i; 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_average-salary-excluding-the-minimum-and-maximum-salary.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | double average(vector& salary) { 11 | if(salary.size() < 3) { 12 | return 0; 13 | } 14 | 15 | int minVal = salary[0], maxVal = salary[0], sum = 0, n = salary.size(); 16 | for(int v : salary) { 17 | minVal = min(minVal, v); 18 | maxVal = max(maxVal, v); 19 | sum += v; 20 | } 21 | return (sum - minVal - maxVal)/(double)(n - 2); 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /LeetCode/leetcode_check-if-the-sentence-is-pangram.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Hash 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | bool checkIfPangram(string sentence) { 11 | if(sentence.size() < 26) { 12 | return false; 13 | } 14 | 15 | vector dic(26, false); 16 | int cnt = 0; 17 | for(char c : sentence) { 18 | int idx = c - 'a'; 19 | if(dic[idx]) { 20 | continue; 21 | } 22 | ++ cnt; 23 | dic[idx] = true; 24 | } 25 | return cnt >= 26; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_find-bottom-left-tree-value.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DFS 4 | * Time: O(n) 5 | * Space: O(h) 6 | */ 7 | class Solution { 8 | public: 9 | int findBottomLeftValue(TreeNode* root) { 10 | int ans = 0, h = -1; 11 | dfs(root, ans, h, 0); 12 | return ans; 13 | } 14 | private: 15 | void dfs(TreeNode* root, int &ans, int &h, int dep){ 16 | if(dep > h){ 17 | ans = root->val; 18 | h = dep; 19 | } 20 | if(root->left) 21 | dfs(root->left, ans, h, dep + 1); 22 | if(root->right) 23 | dfs(root->right, ans, h, dep + 1); 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_sum-of-digits-in-the-minimum-number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int sumOfDigits(vector& A) { 11 | if(A.size() == 0){ 12 | return -1; 13 | } 14 | 15 | int minVal = A[0]; 16 | for(int i = 1; i < A.size(); ++ i){ 17 | minVal = min(minVal, A[i]); 18 | } 19 | 20 | int sum = 0; 21 | while(minVal > 0){ 22 | sum += minVal%10; 23 | minVal /= 10; 24 | } 25 | return (sum&1) == 0? 1 : 0; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_truncate-sentence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | string truncateSentence(string s, int k) { 11 | if(s.size() < k) { 12 | return s; 13 | } 14 | 15 | string res = ""; 16 | for(int i = 0, cnt = 0; i < s.size() && cnt < k; ++ i) { 17 | if(s[i] == ' ') { 18 | ++ cnt; 19 | if(cnt >= k) { 20 | break; 21 | } 22 | } 23 | res += s[i]; 24 | } 25 | return res; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_sum-of-unique-elements.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Hash 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int sumOfUnique(vector& nums) { 11 | if(nums.size() == 0) { 12 | return 0; 13 | } 14 | 15 | vector numCnt(110, 0); 16 | for(int num : nums) { 17 | ++ numCnt[num]; 18 | } 19 | 20 | int ans = 0; 21 | for(int i = 0; i < nums.size(); ++ i) { 22 | if(numCnt[nums[i]] == 1) { 23 | ans += nums[i]; 24 | } 25 | } 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /LeetCode/leetcode_construct-k-palindrome-strings.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | bool canConstruct(string s, int k) { 11 | if(k > s.size()) { 12 | return false; 13 | } 14 | 15 | unordered_map charCnt; 16 | int oddCnt = 0; 17 | for(char c : s) { 18 | ++ charCnt[c]; 19 | if(charCnt[c]%2 == 0) { 20 | -- oddCnt; 21 | } else { 22 | ++ oddCnt; 23 | } 24 | } 25 | 26 | return oddCnt <= k; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /LeetCode/leetcode_divisor-game.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Game, DP 4 | * Time: O(n^1.5) 5 | * Space: O(n) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | bool divisorGame(int N) { 11 | if(N == 0){ 12 | return false; 13 | } 14 | 15 | vector sg(N + 1, false); 16 | for(int i = 2; i <= N; ++ i){ 17 | bool res = true; 18 | for(int j = 1; j*j <= i && j < i; ++ j){ 19 | if(i%j == 0){ 20 | res &= sg[i - j]; 21 | } 22 | } 23 | sg[i] = res ? false : true; 24 | } 25 | return sg[N]; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_kth-largest-element-in-a-stream.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Heap (Data Structure) 4 | * Time: O(nlgn) 5 | * Space: O(k) 6 | */ 7 | 8 | class KthLargest { 9 | private: 10 | priority_queue, greater> pq; 11 | int sizeOfQueue; 12 | public: 13 | KthLargest(int k, vector nums) { 14 | sizeOfQueue = k; 15 | for(int num : nums){ 16 | add(num); 17 | } 18 | } 19 | 20 | int add(int val) { 21 | pq.push(val); 22 | 23 | while(pq.size() > sizeOfQueue){ 24 | pq.pop(); 25 | } 26 | 27 | return pq.top(); 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /LintCode/lintcode_count-1-in-binary.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Bit Manipulation 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param num: an integer 11 | * @return: an integer, the number of ones in num 12 | */ 13 | int countOnes(int num) { 14 | // write your code here 15 | int cnt = 0; 16 | if(!num) 17 | return cnt; 18 | int bitnum = 32; 19 | while(num && bitnum){ 20 | if(num&1){ 21 | ++ cnt; 22 | } 23 | num>>=1; 24 | --bitnum; 25 | } 26 | return cnt; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /LintCode/lintcode_permutations.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Backtracking 4 | * Time: O(n!) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param nums: A list of integers. 11 | * @return: A list of permutations. 12 | */ 13 | vector > permute(vector nums) { 14 | // write your code here 15 | vector > ans; 16 | if(nums.size() == 0) 17 | return ans; 18 | sort(nums.begin(), nums.end()); 19 | do{ 20 | ans.push_back(nums); 21 | }while(next_permutation(nums.begin(), nums.end())); 22 | return ans; 23 | } 24 | }; 25 | 26 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/countingsort1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Sort 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | 15 | int main() { 16 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 17 | int n; 18 | vector rec(101,0); 19 | cin>>n; 20 | for(int i = 0; i < n; i ++){ 21 | int a; 22 | cin>>a; 23 | rec[a] ++; 24 | } 25 | for(int i = 0; i < 100; i ++){ 26 | cout< 0){ 13 | if(N&1 == 1){ 14 | lastPos = curPos; 15 | curPos = pos; 16 | } 17 | 18 | if(lastPos >= 0){ 19 | ans = max(ans, curPos - lastPos); 20 | } 21 | 22 | N >>= 1; 23 | ++ pos; 24 | } 25 | 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /LeetCode/leetcode_count-numbers-with-unique-digits.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DP 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | private: 9 | vector dp; 10 | public: 11 | int countNumbersWithUniqueDigits(int n) { 12 | init(); 13 | return n <= 10? dp[n]:dp[10]; 14 | } 15 | 16 | private: 17 | void init(){ 18 | dp.resize(11); 19 | 20 | dp[0] = 1; 21 | dp[1] = 10; 22 | dp[10] = 0; 23 | int fact = 9, res = 1; 24 | for(int i = 9, j = 2; i >= 1; -- i, ++ j){ 25 | res *= i; 26 | dp[j] = dp[j - 1] + fact*res; 27 | } 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /LeetCode/leetcode_find-lucky-integer-in-an-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Hash 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int findLucky(vector& arr) { 11 | if(arr.size() == 0) { 12 | return -1; 13 | } 14 | 15 | unordered_map numCnt; 16 | for(int v : arr) { 17 | ++ numCnt[v]; 18 | } 19 | 20 | int ans = -1; 21 | for(pair num : numCnt) { 22 | if(num.first == num.second) { 23 | ans = max(ans, num.first); 24 | } 25 | } 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /LeetCode/leetcode_find-pivot-index.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int pivotIndex(vector& nums) { 10 | if(nums.size() < 2){ 11 | return -1; 12 | } 13 | 14 | int sum = 0; 15 | for(int num : nums){ 16 | sum += num; 17 | } 18 | 19 | int leftsum = 0; 20 | for(int i = 0; i < nums.size(); ++ i){ 21 | if(leftsum == sum - leftsum - nums[i]) { 22 | return i; 23 | } 24 | leftsum += nums[i]; 25 | } 26 | return -1; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /LeetCode/leetcode_largest-unique-number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: HashMap 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | class Solution { 8 | public: 9 | int largestUniqueNumber(vector& A) { 10 | int ans = -1; 11 | if(A.size() == 0) { 12 | return ans; 13 | } 14 | 15 | unordered_map numCnt; 16 | for(int a : A){ 17 | ++ numCnt[a]; 18 | } 19 | 20 | for(auto i = numCnt.begin(); i != numCnt.end(); i ++){ 21 | if(i->second == 1) { 22 | ans = max(ans, i->first); 23 | } 24 | } 25 | return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_number-of-recent-calls.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Data Structure (Queue) 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | 8 | class RecentCounter { 9 | private: 10 | queue requestQueue; 11 | public: 12 | RecentCounter() { 13 | 14 | } 15 | 16 | int ping(int t) { 17 | while(!requestQueue.empty()){ 18 | int oldestTime = requestQueue.front(); 19 | if(oldestTime + 3000 < t) { 20 | requestQueue.pop(); 21 | } else { 22 | break; 23 | } 24 | } 25 | requestQueue.push(t); 26 | return requestQueue.size(); 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /LintCode/lintcode_maximum-subarray.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DP 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param nums: A list of integers 11 | * @return: A integer indicate the sum of max subarray 12 | */ 13 | int maxSubArray(vector nums) { 14 | // write your code here 15 | int sum = nums[0], n = nums.size(); 16 | int ans = nums[0]; 17 | for(int i = 1; i < n; ++ i){ 18 | if(sum < 0) 19 | sum = nums[i]; 20 | else 21 | sum += nums[i]; 22 | ans = max(ans, sum); 23 | } 24 | return ans; 25 | } 26 | }; 27 | 28 | -------------------------------------------------------------------------------- /LintCode/lintcode_unique-binary-search-trees.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DP 4 | * Time: O(n^2) 5 | * Space: O(n) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @paramn n: An integer 11 | * @return: An integer 12 | */ 13 | int numTrees(int n) { 14 | // write your code here 15 | if(!n) 16 | return 1; 17 | int dp[n + 1]; 18 | dp[0] = 1; 19 | dp[1] = 1; 20 | for(int i = 2; i <= n; ++ i){ 21 | dp[i] = 0; 22 | for(int j = 1; j<= i; ++ j){ 23 | dp[i] += dp[j - 1]*dp[i - j]; 24 | } 25 | } 26 | return dp[n]; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/beautiful-3-set.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * From Codeforces 4 | * Tag: Construction 5 | * Time: O(n) 6 | * Space: O(1) 7 | */ 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | using namespace std; 16 | int n, k; 17 | 18 | int main(){ 19 | cin>>n; 20 | k = (2*n)/3; 21 | cout< 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | const int N = 200; 14 | int v, cnt[N], maxcnt; 15 | int n; 16 | 17 | int main() { 18 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 19 | scanf("%d",&n); 20 | maxcnt = 0; 21 | for(int i = 0; i < n; ++ i){ 22 | scanf("%d",&v); 23 | ++ cnt[v]; 24 | maxcnt = max(maxcnt, cnt[v]); 25 | } 26 | printf("%d\n",n - maxcnt); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /LeetCode/leetcode_crawler-log-folder.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int minOperations(vector& logs) { 11 | if(logs.size() == 0) { 12 | return 0; 13 | } 14 | 15 | int ans = 0; 16 | for(string log : logs){ 17 | if(log[0] == '.') { 18 | if(log.size() == 3 && ans > 0) { 19 | -- ans; 20 | } 21 | } else { 22 | ++ ans; 23 | } 24 | //cout< all_val, min_val; 31 | }; 32 | -------------------------------------------------------------------------------- /LeetCode/leetcode_minimum-difference-between-largest-and-smallest-value-in-three-moves.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Sort 4 | * Time: O(nlgn) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int minDifference(vector& nums) { 11 | if(nums.size() < 5) { 12 | return 0; 13 | } 14 | 15 | sort(nums.begin(), nums.end()); 16 | 17 | int ans = INT_MAX, n = nums.size() - 1; 18 | for(int i = 0; i <= 3; ++ i) { 19 | for(int j = 0; i + j <= 3; ++ j) { 20 | ans = min(ans, nums[n - j] - nums[i]); 21 | } 22 | } 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LintCode/lintcode_gray-code.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Bit Manipulation 4 | * Time: O(2^n) 5 | * Space: O(2^n) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param n a number 11 | * @return Gray code 12 | */ 13 | vector grayCode(int n) { 14 | // Write your code here 15 | vector res; 16 | res.reserve(1<= 0; -- j){ 21 | res.push_back(high_bit | res[j]); 22 | } 23 | } 24 | return res; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/print-the-elements-of-a-linked-list.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Brute Force 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | /* 8 | Print elements of a linked list on console 9 | head pointer input could be NULL as well for empty list 10 | Node is defined as 11 | struct Node 12 | { 13 | int data; 14 | struct Node *next; 15 | } 16 | */ 17 | void Print(Node *head) 18 | { 19 | // This is a "method-only" submission. 20 | // You only need to complete this method. 21 | if(head == NULL) 22 | return ; 23 | Node *p = head; 24 | while(p){ 25 | printf("%d\n",p->data); 26 | p = p->next; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/tree-height-of-a-binary-tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DFS 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | /*The tree node has data, left child and right child 8 | struct node 9 | { 10 | int data; 11 | node* left; 12 | node* right; 13 | }; 14 | 15 | */ 16 | void dfs(node * root, int &ans, int dep){ 17 | if(!root){ 18 | return ; 19 | } 20 | ++ dep; 21 | ans = max(ans, dep); 22 | if(root->left) 23 | dfs(root->left, ans, dep); 24 | if(root->right) 25 | dfs(root->right, ans, dep); 26 | } 27 | 28 | int height(node * root){ 29 | int ans = 0; 30 | dfs(root, ans, 0); 31 | return ans; 32 | } 33 | -------------------------------------------------------------------------------- /LeetCode/leetcode_check-if-it-is-a-good-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(nlgm) 5 | * Space: O(lgm) 6 | */ 7 | class Solution { 8 | public: 9 | bool isGoodArray(vector& nums) { 10 | if(nums.size() == 0){ 11 | return false; 12 | } 13 | 14 | int allGcd = nums[0]; 15 | for(int i = 1; i < nums.size(); ++ i){ 16 | allGcd = gcd(allGcd, nums[i]); 17 | if(allGcd == 1){ 18 | break; 19 | } 20 | } 21 | return allGcd == 1; 22 | } 23 | private: 24 | int gcd(int a, int b) { 25 | return b == 0? a : gcd(b, a % b); 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_clumsy-factorial.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int clumsy(int N) { 10 | int ans = 0; 11 | bool isFirst = true; 12 | for(; N > 0; N -= 4){ 13 | int v = N; 14 | if(N >= 3){ 15 | v = N*(N - 1)/(N - 2); 16 | } 17 | 18 | v *= -1; 19 | if(isFirst) { 20 | v *= -1; 21 | isFirst = false; 22 | } 23 | 24 | ans = ans + v + (N >= 4 ? N - 3 : 0); 25 | } 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /LeetCode/leetcode_convex-polygon.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Geometry 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | bool isConvex(vector>& p) { 10 | long n = p.size(), prev = 0, cur; 11 | for (int i = 0; i < n; ++i) { 12 | vector> A; // = {p[(i+1)%n]-p[i], p[(i+2)%n]-p[i]} 13 | for (int j = 1; j < 3; ++j) A.push_back({p[(i+j)%n][0]-p[i][0], p[(i+j)%n][1]-p[i][1]}); 14 | if (cur = det2(A)) if (cur*prev < 0) return false; else prev = cur; 15 | } 16 | return true; 17 | } 18 | 19 | long det2(vector>& A) { return A[0][0]*A[1][1] - A[0][1]*A[1][0]; } 20 | }; 21 | -------------------------------------------------------------------------------- /LeetCode/leetcode_day-of-the-year.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | private: 10 | const int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 11 | public: 12 | int dayOfYear(string date) { 13 | if(date.size() == 0) { 14 | return 0; 15 | } 16 | int y = stoi(date.substr(0, 4)), m = stoi(date.substr(5, 2)), d = stoi(date.substr(8)); 17 | int ans = d; 18 | if (m > 2 && y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)){ 19 | ++ans; 20 | } 21 | while (--m > 0) ans += days[m - 1]; 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_intersection-of-two-linked-lists.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * TODO 4 | * Tag: Two Pointers 5 | * Time: O(n) 6 | * Space: O(1) 7 | */ 8 | /** 9 | * Definition for singly-linked list. 10 | * struct ListNode { 11 | * int val; 12 | * ListNode *next; 13 | * ListNode(int x) : val(x), next(NULL) {} 14 | * }; 15 | */ 16 | class Solution { 17 | public: 18 | ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { 19 | ListNode *pA = headA, *pB = headB; 20 | while(pA != pB){ 21 | pA = pA ? pA->next : headB; 22 | pB = pB ? pB->next : headA; 23 | } 24 | 25 | return pA; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_single-number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Bit Manipulation 4 | * Time: O(1) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | uint32_t reverseBits(uint32_t n) { 10 | long long hbit = 31, lbit = 0; 11 | long long high = 1LL<= lbit){ 13 | long long hres = n&high, lres = low&n; 14 | if((hres && !lres) || (!hres && lres)){ 15 | n = n^high; 16 | n = n^low; 17 | } 18 | -- hbit; 19 | ++ lbit; 20 | high >>= 1; 21 | low <<= 1; 22 | } 23 | return n; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LintCode/lintcode_best-time-to-buy-and-sell-stock-ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param prices: Given an integer array 11 | * @return: Maximum profit 12 | */ 13 | int maxProfit(vector &prices) { 14 | // write your code here 15 | int ans = 0; 16 | if(prices.size() < 2) 17 | return ans; 18 | int n = prices.size(); 19 | for(int i = 0; i < n - 1; ++ i){ 20 | if(prices[i + 1] > prices[i]) 21 | ans += (prices[i + 1] - prices[i]); 22 | } 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LintCode/lintcode_wiggle-sort.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Brute Force 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param nums a list of integer 11 | * @return void 12 | */ 13 | void wiggleSort(vector& nums) { 14 | // Write your code here 15 | if(nums.size() <= 1) 16 | return ; 17 | int n = nums.size(); 18 | for(int i = 1; i < n; i += 2){ 19 | if(nums[i] < nums[i - 1]) 20 | swap(nums[i], nums[i - 1]); 21 | if(i + 1 < n && nums[i] < nums[i + 1]) 22 | swap(nums[i], nums[i + 1]); 23 | } 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_di-string-match.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | vector diStringMatch(string S) { 11 | vector ans; 12 | if(S.size() == 0){ 13 | return ans; 14 | } 15 | 16 | int n = S.size(); 17 | int low = 0, high = n; 18 | ans.resize(n + 1); 19 | for(int i = 0; i < n; ++ i){ 20 | if(S[i] == 'I') { 21 | ans[i] = low ++ ; 22 | } else { 23 | ans[i] = high --; 24 | } 25 | } 26 | ans[n] = low; 27 | return ans; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /LeetCode/leetcode_first-bad-version.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Binary Search 4 | * Time: O(logn) 5 | * Space: O(1) 6 | * 7 | // Forward declaration of isBadVersion API. 8 | bool isBadVersion(int version); 9 | 10 | class Solution { 11 | public: 12 | int firstBadVersion(int n) { 13 | int ans = n; 14 | if(n < 2) 15 | return n; 16 | long long l = 1, r = n; 17 | while(l <= r){ 18 | long long mid = (l + r)>>1; 19 | if(isBadVersion(mid)){ 20 | ans = (int)mid; 21 | r = mid - 1; 22 | }else 23 | l = mid + 1; 24 | } 25 | return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_incremental-memory-leak.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: implementation 4 | * Time: O(n^0.5) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | vector memLeak(int memory1, int memory2) { 11 | if(memory1 <= 0 && memory2 <= 0) { 12 | return {1, 0, 0}; 13 | } 14 | 15 | int t = 1; 16 | for(int i = 1; i <= memory1 || i <= memory2; ++ i) { 17 | if(memory1 >= memory2) { 18 | memory1 -= i; 19 | } else { 20 | memory2 -= i; 21 | } 22 | ++ t; 23 | } 24 | 25 | return {t, memory1, memory2}; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LintCode/lintcode_sort-colors-ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Brute Force 4 | * Time: O(n) 5 | * Space: O(k) 6 | */ 7 | class Solution{ 8 | public: 9 | /** 10 | * @param colors: A list of integer 11 | * @param k: An integer 12 | * @return: nothing 13 | */ 14 | void sortColors2(vector &colors, int k) { 15 | // write your code here 16 | vector cnt(k); 17 | for(int i = 0; i < colors.size(); ++ i){ 18 | ++ cnt[colors[i] - 1]; 19 | } 20 | int idx = 0; 21 | for(int i = 0; i < k; ++ i){ 22 | while(cnt[i] --) 23 | colors[idx ++] = i + 1; 24 | } 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/strings-xor.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Bit Manipulation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | string strings_xor(string s, string t) { 15 | 16 | string res = ""; 17 | for(int i = 0; i < s.size(); i++) { 18 | if(s[i] == t[i]) 19 | res += '0'; 20 | else 21 | res += '1'; 22 | } 23 | 24 | return res; 25 | } 26 | 27 | int main() { 28 | string s, t; 29 | cin >> s >> t; 30 | cout << strings_xor(s, t) << endl; 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /LeetCode/leetcode_check-if-number-is-a-sum-of-powers-of-three.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(lgn) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | bool checkPowersOfThree(int n) { 10 | if(n < 1) { 11 | return false; 12 | } 13 | if(n == 1) { 14 | return true; 15 | } 16 | while(n >= 3) { 17 | int v = n%3; 18 | if(v == 0) { 19 | n /= 3; 20 | } else if(v == 1) { 21 | n -= 1; 22 | n /= 3; 23 | } else { 24 | n = 2; 25 | } 26 | } 27 | return n == 1; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /LeetCode/leetcode_escape-the-ghosts.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | bool escapeGhosts(vector>& ghosts, vector& target) { 10 | vector source = {0, 0}; 11 | for(vector ghost : ghosts){ 12 | if(manhattanDistance(ghost, target) <= manhattanDistance(source, target)){ 13 | return false; 14 | } 15 | } 16 | 17 | return true; 18 | } 19 | private: 20 | int manhattanDistance(vector source, vector target){ 21 | return abs(source[0] - target[0]) + abs(source[1] - target[1]); 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /LeetCode/leetcode_final-prices-with-a-special-discount-in-a-shop.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n^2) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | vector finalPrices(vector& prices) { 11 | vector ans = prices; 12 | for(int i = 0; i < ans.size() - 1; ++ i) { 13 | int discount = 0; 14 | for(int j = i + 1; j < ans.size(); ++ j) { 15 | if(prices[j] <= prices[i]) { 16 | discount = prices[j]; 17 | break; 18 | } 19 | } 20 | ans[i] -= discount; 21 | } 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_guess-number-higher-or-lower.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Binary Search 4 | * Time: O(lgn) 5 | * Space: O(1) 6 | */ 7 | int guess(int num); 8 | 9 | class Solution { 10 | public: 11 | int guessNumber(int n) { 12 | long long l = 1, r = n; 13 | int ans = 1; 14 | while(l <= r){ 15 | long long mid = (l + r)>>1; 16 | int v = guess((int)mid); 17 | if(v == 0){ 18 | ans = mid; 19 | break; 20 | }else if(v == 1){ 21 | l = mid + 1; 22 | }else{ 23 | r = mid - 1; 24 | } 25 | } 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /LeetCode/leetcode_jewels-and-stones.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Hash 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | class Solution { 8 | public: 9 | int numJewelsInStones(string J, string S) { 10 | int ans = 0; 11 | if(J.size() == 0 || S.size() == 0){ 12 | return ans; 13 | } 14 | 15 | unordered_set jewels; 16 | for(int i = 0; i < J.size(); ++ i){ 17 | jewels.insert(J[i]); 18 | } 19 | 20 | for(int i = 0; i < S.size(); ++ i){ 21 | if(jewels.count(S[i]) != 0){ 22 | ++ ans; 23 | } 24 | } 25 | 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /LeetCode/leetcode_my-calendar-iii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Binary Search Tree 4 | * Time: O(n^2) 5 | * Space: O(n) 6 | */ 7 | class MyCalendarThree { 8 | private: 9 | map timeScheduler; 10 | public: 11 | MyCalendarThree() { 12 | timeScheduler.clear(); 13 | } 14 | 15 | int book(int start, int end) { 16 | ++ timeScheduler[start]; 17 | -- timeScheduler[end]; 18 | 19 | int ans = 0, delta = 0; 20 | for(auto it = timeScheduler.begin(); it != timeScheduler.end(); ++ it){ 21 | delta += it->second; 22 | ans = max(ans, delta); 23 | } 24 | 25 | return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_student-attendance-record-i.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | bool checkRecord(string s) { 10 | if(s.size() == 0) 11 | return true; 12 | int cntA = 0, cntL = 0, n = s.size(); 13 | for(int i = 0; i < n; ++ i){ 14 | if(s[i] == 'L'){ 15 | ++ cntL; 16 | }else{ 17 | cntL = 0; 18 | if(s[i] == 'A') 19 | ++ cntA; 20 | } 21 | if(cntA > 1 || cntL > 2) 22 | return false; 23 | } 24 | return true; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /LeetCode/leetcode_sum-of-square-numbers.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(n^1.5 * lgn) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | bool judgeSquareSum(int c) { 10 | if(c < 0){ 11 | return false; 12 | } 13 | if(c == 0){ 14 | return true; 15 | } 16 | int v = sqrt(c); 17 | if(v*v == c){ 18 | return true; 19 | } 20 | for(long long i = 1; i*i <= c; ++ i){ 21 | int res = c - i*i; 22 | v = sqrt(res); 23 | if(v*v == res) { 24 | return true; 25 | } 26 | } 27 | return false; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /LintCode/lintcode_paint-fence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DP 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param n non-negative integer, n posts 11 | * @param k non-negative integer, k colors 12 | * @return an integer, the total number of ways 13 | */ 14 | int numWays(int n, int k) { 15 | // Write your code here 16 | if(n <= 1 || !k) 17 | return n*k; 18 | int a = k, b = k*(k - 1), c = 0; 19 | for(int i = 2; i < n; ++ i){ 20 | c = (k - 1)*(a + b); 21 | a = b; 22 | b = c; 23 | } 24 | 25 | return a+b; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_build-an-array-with-stack-operations.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | vector buildArray(vector& target, int n) { 11 | if(target.size() == 0 || n == 0) { 12 | return {}; 13 | } 14 | 15 | vector ans; 16 | for(int i = 1, j = 0; i <= n && j < target.size(); ++ i) { 17 | ans.push_back("Push"); 18 | if(i == target[j]) { 19 | ++ j; 20 | continue; 21 | } 22 | ans.push_back("Pop"); 23 | } 24 | return ans; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /LeetCode/leetcode_convert-binary-number-in-a-linked-list-to-integer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Bit Manipulation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | /** 8 | * Definition for singly-linked list. 9 | * struct ListNode { 10 | * int val; 11 | * ListNode *next; 12 | * ListNode(int x) : val(x), next(NULL) {} 13 | * }; 14 | */ 15 | class Solution { 16 | public: 17 | int getDecimalValue(ListNode* head) { 18 | int mask = (1<<30), len = 0, ans = 0; 19 | for(; head != NULL; head = head->next, ++ len, mask >>= 1) { 20 | ans |= (head->val == 1? mask : 0); 21 | } 22 | ans >>= (30 - len + 1); 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_count-number-of-teams.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Brute Force 4 | * Time: O(n^3) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int numTeams(vector& rating) { 11 | int ans = 0, n = rating.size(); 12 | for(int i = 0; i < n - 2; ++ i){ 13 | for(int j = i + 1; j < n - 1; j ++) { 14 | for(int k = j + 1; k < n; ++ k) { 15 | if((rating[i] > rating[j] && rating[j] > rating[k]) || (rating[i] < rating[j] && rating[j] < rating[k])) { 16 | ++ ans; 17 | } 18 | } 19 | } 20 | } 21 | return ans; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /LeetCode/leetcode_increasing-triplet-subsequence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Brute Force (Two Pointers) 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | bool increasingTriplet(vector& nums) { 10 | if(nums.size() < 3) 11 | return false; 12 | int p1 = nums[0], p2 = INT_MAX; 13 | for(int i = 1; i < nums.size(); ++ i){ 14 | if(nums[i] < p1){ 15 | p1 = nums[i]; 16 | }else if(nums[i] > p2){ 17 | return true; 18 | }else if(nums[i] > p1 && nums[i] < p2){ 19 | p2 = nums[i]; 20 | } 21 | } 22 | return false; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LintCode/lintcode_top-k-largest-numbers.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Data Structure 4 | * Time: O(nlgn) 5 | * Space: O(n) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param nums: an integer array 11 | * @param k: An integer 12 | * @return: the top k largest numbers in array 13 | */ 14 | vector topk(vector &nums, int k) { 15 | vector ans(k); 16 | priority_queue > maxHeap(nums.begin(), nums.end()); 17 | 18 | for(int i = 0; i < k && i < nums.size(); ++ i) { 19 | ans[i] = maxHeap.top(); 20 | maxHeap.pop(); 21 | } 22 | 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_break-a-palindrome.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | string breakPalindrome(string palindrome) { 10 | if(palindrome.size() < 2) { 11 | return ""; 12 | } 13 | 14 | string ans = palindrome; 15 | int len = palindrome.size()/2; 16 | for(int i = 0; i < len; ++ i){ 17 | if(palindrome[i] != 'a') { 18 | ans[i] = 'a'; 19 | break; 20 | } 21 | if(i == len - 1){ 22 | ans[ans.size() - 1] = 'b'; 23 | } 24 | } 25 | return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_confusing-number.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(lgn) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | bool confusingNumber(int N) { 11 | long long rotatedN = 0, n = N; 12 | while(N > 0) { 13 | int v = N%10; 14 | if(v == 2 || v == 3 || v == 4 || v == 5 || v == 7) { 15 | return false; 16 | } 17 | if(v == 6) { 18 | v = 9; 19 | } else if(v == 9) { 20 | v = 6; 21 | } 22 | rotatedN = (rotatedN*10) + v; 23 | N /= 10; 24 | } 25 | 26 | return rotatedN != n; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /LeetCode/leetcode_delete-columns-to-make-sorted.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(nm) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int minDeletionSize(vector& A) { 10 | int ans = 0; 11 | if(A.size() == 0 || A[0].size() == 0){ 12 | return ans; 13 | } 14 | 15 | int numOfCols = A[0].size(); 16 | for(int i = 0; i < numOfCols; ++ i){ 17 | for(int j = 1; j < A.size(); ++ j){ 18 | if(A[j][i] < A[j - 1][i]){ 19 | ++ ans; 20 | break; 21 | } 22 | } 23 | } 24 | return ans; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /LeetCode/leetcode_largest-substring-between-two-equal-characters.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Hash 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int maxLengthBetweenEqualCharacters(string s) { 11 | if(s.size() < 2) { 12 | return 0; 13 | } 14 | 15 | vector pos(26, -1); 16 | int ans = -1; 17 | for(int i = 0; i < s.size(); ++ i) { 18 | int idx = (s[i] - 'a'); 19 | if(pos[idx] == -1) { 20 | pos[idx] = i; 21 | } else { 22 | ans = max(ans, i - pos[idx] - 1); 23 | } 24 | } 25 | return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_max-consecutive-ones-iii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Sliding Windows 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int longestOnes(vector& A, int K) { 11 | int l = -1, r = 0, zeroCnt = 0, ans = 0; 12 | for(; r < A.size(); ++ r) { 13 | if(A[r] == 0) { 14 | ++ zeroCnt; 15 | } 16 | 17 | while(zeroCnt > K) { 18 | ++ l; 19 | if(A[l] == 0) { 20 | -- zeroCnt; 21 | } 22 | } 23 | 24 | ans = max(ans, r - l); 25 | } 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /LeetCode/leetcode_stone-game-iv.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Game Theory, DP 4 | * Time: O(n^1.5) 5 | * Space: O(n) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | bool winnerSquareGame(int n) { 11 | if(n <= 0) { 12 | return false; 13 | } 14 | 15 | vector sg(n + 1, false); 16 | sg[0] = false; 17 | for(int i = 1; i <= n; ++ i) { 18 | for(int j = 1; j*j <= i; ++ j) { 19 | int v = j*j; 20 | if(!sg[i - v]){ 21 | sg[i] = true; 22 | break; 23 | } 24 | } 25 | } 26 | 27 | return sg[n]; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /LintCode/lintcode_length-of-last-word.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Brute Force 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param s A string 11 | * @return the length of last word 12 | */ 13 | int lengthOfLastWord(string& s) { 14 | // Write your code here 15 | int ans = 0; 16 | if(!s.size()) 17 | return ans; 18 | int i = s.size() - 1; 19 | while(i >= 0 && s[i] == ' ') 20 | -- i; 21 | if(!i) 22 | return ans; 23 | while(i >= 0 && s[i] != ' '){ 24 | -- i; 25 | ++ ans; 26 | } 27 | return ans; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /LintCode/lintcode_maximum-gap.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Sort 4 | * Time: O(nlgn) 5 | * Space: O(lgn) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param nums: a vector of integers 11 | * @return: the maximum difference 12 | */ 13 | int maximumGap(vector nums) { 14 | // write your code here 15 | int ans = 0; 16 | if(nums.size() < 2) 17 | return ans; 18 | sort(nums.begin(), nums.end()); 19 | int n = nums.size(); 20 | int gap; 21 | for(int i = 1; i < n; ++ i){ 22 | gap = nums[i] - nums[i - 1]; 23 | ans = max(ans, gap); 24 | } 25 | return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/make-it-anagram-mglines.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Hash 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | int main() { 15 | char s1[10010], s2[10010]; 16 | cin >> s1 >> s2; 17 | int a[26] = {0}; 18 | for(int i = 0; i < strlen(s1); i++) { 19 | a[s1[i] - 'a']++; 20 | } 21 | for(int i = 0; i < strlen(s2); i++) { 22 | a[s2[i] - 'a']--; 23 | } 24 | long long int ans = 0; 25 | for(int i = 0; i < 26; i++) 26 | ans += abs(a[i]); 27 | cout << ans << endl; 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/staircase.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n^2) 5 | * Space: O(1) 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | int n; 14 | 15 | int main() { 16 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 17 | scanf("%d",&n); 18 | int num_space = n - 1; 19 | for(int i = 1; i <= n; ++ i){ 20 | for(int j = 0; j < num_space; ++ j) 21 | printf(" "); 22 | for(int j = 0; j < i; ++ j) 23 | printf("#"); 24 | puts(""); 25 | -- num_space; 26 | } 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/tree-level-order-traversal.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: BFS 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | /* 8 | struct node 9 | { 10 | int data; 11 | node* left; 12 | node* right; 13 | }*/ 14 | #include 15 | void LevelOrder(node * root){ 16 | queue q; 17 | bool isfirst = true; 18 | q.push(root); 19 | while(!q.empty()){ 20 | node *tmp = q.front(); 21 | q.pop(); 22 | if(!isfirst) 23 | printf(" "); 24 | printf("%d",tmp->data); 25 | if(tmp->left) 26 | q.push(tmp->left); 27 | if(tmp->right) 28 | q.push(tmp->right); 29 | isfirst = false; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /LeetCode/leetcode_can-make-arithmetic-progression-from-sequence.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Sort 4 | * Time: O(nlgn) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | bool canMakeArithmeticProgression(vector& arr) { 11 | if(arr.size() == 0) { 12 | return false; 13 | } 14 | if(arr.size() == 1) { 15 | return true; 16 | } 17 | 18 | sort(arr.begin(), arr.end()); 19 | int diff = arr[1] - arr[0]; 20 | for(int i = 2; i < arr.size(); ++ i) { 21 | if(arr[i] - arr[i - 1] != diff) { 22 | return false; 23 | } 24 | } 25 | return true; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_check-if-a-string-contains-all-binary-codes-of-size-k.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Bit Manipulation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | bool hasAllCodes(string s, int k) { 10 | unordered_set dic; 11 | int tot = 1<= k - 1) { 18 | dic.insert(res); 19 | v = res; 20 | } 21 | 22 | } 23 | return dic.size() == tot; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_gasstation.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Algorithm: Greedy 4 | * Time Complexity: O(n) 5 | * Memory Complexity: O(n) 6 | * 7 | */ 8 | 9 | class Solution { 10 | public: 11 | int canCompleteCircuit(vector &gas, vector &cost) { 12 | double rate = -9999999.99; 13 | int ind = 0, totgas = 0, totcost = 0; 14 | for(int i = 0; i < gas.size(); i ++){ 15 | totgas += gas[i]; 16 | totcost += cost[i]; 17 | if(rate < (double)gas[i]/(double)cost[i]){ 18 | rate = (double)gas[i]/(double)cost[i]; 19 | ind = i; 20 | } 21 | } 22 | return totgas >= totcost? ind : -1; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_lengthoflastword.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Algorithm: Brute force 3 | * Time complexity: O(n) 4 | * Memory complexity: O(1) 5 | */ 6 | class Solution { 7 | public: 8 | int lengthOfLastWord(const char *s) { 9 | // Start typing your C/C++ solution below 10 | // DO NOT write int main() function 11 | if(s[0]=='\0') return 0; 12 | int ans = 0, cnt = 0; 13 | for(int i = 0; s[i]!='\0'; i ++){ 14 | if(s[i]==' '){ 15 | if(cnt != 0) ans = cnt; 16 | cnt = 0; 17 | }else{ 18 | cnt ++; 19 | } 20 | } 21 | if(cnt != 0) ans = cnt; 22 | return ans; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_shift-2d-grid.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(nm) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | vector> shiftGrid(vector>& grid, int k) { 11 | if(k == 0){ 12 | return grid; 13 | } 14 | 15 | int n = grid.size(), m = grid[0].size(); 16 | int size = n*m; 17 | vector> ans(n, vector(m, 0)); 18 | for(int i = 0; i < n; ++ i){ 19 | for(int j = 0; j < m; ++ j){ 20 | int nxt = (i*m + j + k)%size; 21 | ans[nxt/m][nxt%m] = grid[i][j]; 22 | } 23 | } 24 | return ans; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /LeetCode/leetcode_single-row-keyboard.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Hash 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int calculateTime(string keyboard, string word) { 10 | if(word.size() == 0){ 11 | return 0; 12 | } 13 | 14 | vector pos(26, 0); 15 | for(int i = 0; i < keyboard.size(); ++ i){ 16 | pos[keyboard[i] - 'a'] = i; 17 | } 18 | 19 | int ans = 0; 20 | int cur = 0; 21 | for(char w : word) { 22 | int nxt = pos[w - 'a']; 23 | ans += abs(nxt - cur); 24 | cur = nxt; 25 | } 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /LeetCode/leetcode_toeplitz-matrix.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: 4 | * Time: O(n*m) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | bool isToeplitzMatrix(vector>& matrix) { 10 | if(matrix.size() <= 1){ 11 | return true; 12 | } 13 | 14 | int numOfRows = matrix.size(), numOfColumns = matrix[0].size(); 15 | for(int r = 0; r < numOfRows; ++ r){ 16 | for(int c = 0; c < numOfColumns; ++ c ){ 17 | if(r > 0 && c > 0 && matrix[r][c] != matrix[r - 1][c - 1]){ 18 | return false; 19 | } 20 | } 21 | } 22 | 23 | return true; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LintCode/lintcode_backpack-ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DP 4 | * Time: O(nm) 5 | * Space: O(m) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param m: An integer m denotes the size of a backpack 11 | * @param A & V: Given n items with size A[i] and value V[i] 12 | * @return: The maximum value 13 | */ 14 | int backPackII(int m, vector A, vector V) { 15 | // write your code here 16 | vector dp(m + 1); 17 | for(int i = 0; i < A.size(); ++ i){ 18 | for(int j = m; j >= A[i]; -- j){ 19 | dp[j] = max(dp[j], dp[j - A[i]] + V[i]); 20 | } 21 | } 22 | return dp[m]; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/array-rotation-2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Brute Force 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | const int N = 100100; 14 | int s[N]; 15 | int n, m, v, id; 16 | 17 | int main() { 18 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 19 | scanf("%d%d",&n,&m); 20 | m %= n; 21 | for(int i = 0; i < n; ++ i){ 22 | scanf("%d",&v); 23 | id = (i - m + n)%n; 24 | s[id] = v; 25 | } 26 | for(int i = 0; i < n; ++ i) 27 | printf("%d ",s[i]); 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/lonely-integer-fill-the-key-line.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Bit Manipulation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | #include 8 | 9 | using namespace std; 10 | 11 | int lonely_integer(vector < int > a) { 12 | int answer = 0; 13 | for(int i = 0; i < a.size(); i++) { 14 | // FILL THE MISSING LINE HERE 15 | } 16 | return answer; 17 | } 18 | 19 | int main() { 20 | int res; 21 | int _a_size; 22 | cin >> _a_size; 23 | vector _a(_a_size); 24 | for(int _a_i = 0; _a_i < _a_size; _a_i++) { 25 | cin >> _a[_a_i]; 26 | } 27 | res = lonely_integer(_a); 28 | cout << res; 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /LeetCode/leetcode_finding-the-users-active-minutes.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Hash 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | vector findingUsersActiveMinutes(vector>& logs, int k) { 11 | if(logs.size() == 0) { 12 | return {}; 13 | } 14 | 15 | unordered_map> uam; 16 | for(vector log : logs) { 17 | uam[log[0]].insert(log[1]); 18 | } 19 | 20 | vector ans(k, 0); 21 | for(auto it : uam) { 22 | int cnt = it.second.size(); 23 | ++ ans[cnt - 1]; 24 | } 25 | return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_number-of-students-doing-homework-at-a-given-time.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int busyStudent(vector& startTime, vector& endTime, int queryTime) { 11 | int n = startTime.size(); 12 | if(n == 0) { 13 | return 0; 14 | } 15 | 16 | int ans = 0; 17 | for(int i = 0; i < n; ++ i) { 18 | if(startTime[i] <= queryTime) { 19 | ++ ans; 20 | } 21 | if(endTime[i] + 1 <= queryTime) { 22 | -- ans; 23 | } 24 | } 25 | 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /LeetCode/leetcode_subarray-sum-equals-k.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Data Structure (HashMap) 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | class Solution { 8 | public: 9 | int subarraySum(vector& nums, int k) { 10 | int ans = 0; 11 | if(nums.size() == 0) 12 | return ans; 13 | unordered_map dict; 14 | dict.clear(); 15 | int sum = 0; 16 | for(int i = 0; i < nums.size(); ++ i){ 17 | sum += nums[i]; 18 | if(sum == k) 19 | ++ ans; 20 | if(dict.count(sum - k)) 21 | ans += dict[sum - k]; 22 | ++ dict[sum]; 23 | } 24 | return ans; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /LeetCode/leetcode_count-and-say.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | string countAndSay(int n) { 10 | string s("1"); 11 | 12 | while(--n){ 13 | s = getNext(s); 14 | } 15 | 16 | return s; 17 | } 18 | 19 | string getNext(const string &s) { 20 | stringstream ss; 21 | 22 | for(auto i = s.begin(); i != s.end(); ){ 23 | auto j = find_if(i,s.end(),bind1st(not_equal_to(),*i)); 24 | ss << distance(i, j) << *i; 25 | i = j; 26 | } 27 | 28 | return ss.str(); 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /LeetCode/leetcode_fizzbuzz.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | vector fizzBuzz(int n) { 10 | vector ans; 11 | if(!n) 12 | return ans; 13 | ans.resize(n); 14 | string tmp = ""; 15 | for(int i = 0; i < n; ++ i){ 16 | tmp = ""; 17 | if((i + 1)%3 != 0 && (i + 1)%5 != 0) 18 | tmp = to_string((i+1)); 19 | if((i + 1)%3 == 0) 20 | tmp += "Fizz"; 21 | if((i + 1)%5 == 0) 22 | tmp += "Buzz"; 23 | ans[i] = tmp; 24 | } 25 | return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_integertoroman.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Algorithm: Brute force, Simulate 3 | * Time complexity: O(num) 4 | * Memory complexity: O(n) 5 | */ 6 | class Solution { 7 | public: 8 | string intToRoman(int num) { 9 | const int radix[] = {1000, 900, 500, 400, 100, 90,50, 40, 10, 9, 5, 4, 1}; 10 | const string symbol[] = {"M", "CM", "D", "CD", "C", "XC","L", "XL", "X","IX", "V", "IV", "I"}; 11 | 12 | string roman; 13 | for(size_t i = 0; num > 0; ++ i){ 14 | int count = num/radix[i]; 15 | num %= radix[i]; 16 | for(; count > 0; -- count) 17 | roman += symbol[i]; 18 | } 19 | return roman; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /LeetCode/leetcode_online-stock-span.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Stack (Data Structure) 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | 8 | class StockSpanner { 9 | private: 10 | stack> stockSpans; 11 | 12 | public: 13 | int next(int price) { 14 | int span = 1; 15 | while(!stockSpans.empty()){ 16 | pair lastStockSpan = stockSpans.top(); 17 | if(lastStockSpan.first > price){ 18 | break; 19 | } 20 | 21 | span += lastStockSpan.second; 22 | stockSpans.pop(); 23 | } 24 | stockSpans.push(make_pair(price, span)); 25 | 26 | return span; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /LeetCode/leetcode_simplified-fractions.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O((n^2)lgn) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | vector simplifiedFractions(int n) { 11 | if(n < 2) { 12 | return {}; 13 | } 14 | 15 | vector ans; 16 | for(int i = 2; i <= n; ++ i) { 17 | for(int j = 1; j < i; ++ j) { 18 | if(__gcd(j, i) > 1) { 19 | continue; 20 | } 21 | 22 | string res = to_string(j) + "/" + to_string(i); 23 | ans.push_back(res); 24 | } 25 | } 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /LeetCode/leetcode_single-number-ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Tag: Bit Manipulation 3 | * Time : O(n) 4 | * Memory : O(1) 5 | */ 6 | class Solution { 7 | public: 8 | int singleNumber(vector& nums) { 9 | vector bitCount(32, 0); 10 | if(nums.size() == 1){ 11 | return nums[0]; 12 | } 13 | 14 | for(int num : nums){ 15 | for(int i = 0; i < 32; ++ i){ 16 | bitCount[i] = (bitCount[i] + ((num>>i)&1))%3; 17 | } 18 | } 19 | 20 | int ans = 0; 21 | for(int i = 0; i < bitCount.size(); ++ i){ 22 | ans |= (bitCount[i]< 8 | #include 9 | using namespace std; 10 | int d1, m1, y1, d2, m2, y2; 11 | 12 | int main() { 13 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 14 | scanf("%d%d%d%d%d%d",&d1,&m1,&y1,&d2,&m2,&y2); 15 | int ans = 0; 16 | if(y1 > y2) 17 | ans = 10000; 18 | else{ 19 | if(y1 == y2 && m1 > m2) 20 | ans = 500*(m1 - m2); 21 | else{ 22 | if(y1 == y2 && m1 == m2 && d1 > d2) 23 | ans = 15*(d1 - d2); 24 | } 25 | } 26 | printf("%d\n",ans); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /LeetCode/leetcode_buildings-with-an-ocean-view.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | vector findBuildings(vector& heights) { 11 | if(heights.size() == 0) { 12 | return {}; 13 | } 14 | 15 | int n = heights.size(), maxH = heights[n - 1]; 16 | vector ans = {n - 1}; 17 | for(int i = n - 2; i >= 0; -- i) { 18 | if(heights[i] <= maxH) { 19 | continue; 20 | } 21 | ans.push_back(i); 22 | maxH = heights[i]; 23 | } 24 | reverse(ans.begin(), ans.end()); 25 | return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_combination-sum-iv.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DP 4 | * Time: O(max(nlgn, mn)) where m = target number 5 | * Space: O(m) 6 | */ 7 | class Solution { 8 | public: 9 | int combinationSum4(vector& nums, int target) { 10 | vector dp(target + 1, 0); 11 | if(!nums.size() || !target) { 12 | return 0; 13 | } 14 | 15 | sort(nums.begin(), nums.end()); 16 | 17 | dp[0] = 1; 18 | for(int i = 1; i <= target; ++ i){ 19 | for(int j = 0; j < nums.size() && nums[j] <= i; ++ j){ 20 | dp[i] += dp[i - nums[j]]; 21 | } 22 | } 23 | 24 | return dp[target]; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /LeetCode/leetcode_maximum-xor-for-each-query.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: bit manipulation 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | vector getMaximumXor(vector& nums, int maximumBit) { 11 | if(maximumBit < 0) { 12 | return {}; 13 | } 14 | 15 | int mask = (1< ans(n, 0); 21 | for(int i = n - 1,j = 0; i >= 0; -- i, ++ j) { 22 | ans[j] = cur^mask; 23 | cur ^= nums[i]; 24 | } 25 | return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_unique-substrings-in-wraparound-string.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DP 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int findSubstringInWraproundString(string p) { 10 | int ans = 0; 11 | if(p.size() == 0) 12 | return ans; 13 | vector cnt(26, 0); 14 | int cur = 0, len = 0; 15 | for(int i = 0; i < p.size(); ++ i){ 16 | cur = p[i] - 'a'; 17 | if(i > 0 && p[i - 1] != (cur + 25)%26 + 'a') len = 0; 18 | if(++ len > cnt[cur]){ 19 | ans += len - cnt[cur]; 20 | cnt[cur] = len; 21 | } 22 | } 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LintCode/lintcode_remove-element.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Brute Force 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | *@param A: A list of integers 11 | *@param elem: An integer 12 | *@return: The new length after remove 13 | */ 14 | int removeElement(vector &A, int elem) { 15 | // write your code here 16 | int ans = 0; 17 | int idx = 0; 18 | for(int i = 0; i < A.size(); ++ i){ 19 | if(A[i] == elem){ 20 | ++ ans; 21 | }else{ 22 | A[idx] = A[i]; 23 | ++ idx; 24 | } 25 | } 26 | return A.size() - ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/bob-and-ben.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Game Theory 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | #include 8 | #include 9 | using namespace std; 10 | int n, m, ans, u, v; 11 | 12 | int main(){ 13 | int T; 14 | scanf("%d",&T); 15 | while (T --){ 16 | scanf("%d",&n); 17 | ans = 0; 18 | for(int i = 0; i < n; ++ i){ 19 | scanf("%d%d",&u,&v); 20 | if ( u == 1) ans^=1; 21 | else if (u > 2){ 22 | if (u%2==1) ans^=1; 23 | else ans^=2; 24 | } 25 | } 26 | if (ans) 27 | puts("BOB"); 28 | else 29 | puts("BEN"); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/bon-appetit.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | int n, k, v, b; 14 | 15 | int main() { 16 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 17 | scanf("%d%d",&n,&k); 18 | int ans = 0; 19 | for(int i = 0; i < n; ++ i){ 20 | scanf("%d",&v); 21 | if(i != k) 22 | ans += v; 23 | } 24 | scanf("%d",&b); 25 | if(ans/2 == b) 26 | puts("Bon Appetit"); 27 | else 28 | printf("%d\n",b - ans/2); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Hackerrank_InterviewStreet/countingsort2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Sort 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | 15 | int main() { 16 | /* Enter your code here. Read input from STDIN. Print output to STDOUT */ 17 | int n; 18 | vector rec(100,0); 19 | int tmp; 20 | cin >> n; 21 | for(int i = 0; i < n; i ++) { 22 | cin >> tmp; 23 | rec[tmp] ++; 24 | } 25 | for(int i = 0; i < 100; i ++){ 26 | for(int j = 0; j < rec[i]; j ++) 27 | cout< dp(n + 1, n); 15 | dp[1] = 0; 16 | for(int i = 2; i <= n; ++ i){ 17 | dp[i] = min(dp[i], i); 18 | 19 | for(int j = 2; j*j <= i; ++ j){ 20 | if(i%j == 0){ 21 | dp[i] = min(dp[i], dp[j] + i/j); 22 | dp[i] = min(dp[i], dp[i/j] + j); 23 | } 24 | } 25 | } 26 | 27 | return dp[n]; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /LeetCode/leetcode_find-anagram-mappings.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Data Structure (Hash) 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | class Solution { 8 | public: 9 | vector anagramMappings(vector& A, vector& B) { 10 | vector ans(A.size()); 11 | if(A.size() == 0){ 12 | return ans; 13 | } 14 | 15 | unordered_map indexOfElementsInArrayB; 16 | for(int i = 0; i < B.size(); ++ i){ 17 | indexOfElementsInArrayB[B[i]] = i; 18 | } 19 | 20 | for(int i = 0; i < A.size(); ++ i){ 21 | ans[i] = indexOfElementsInArrayB[A[i]]; 22 | } 23 | 24 | return ans; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /LeetCode/leetcode_find-smallest-letter-greater-than-target.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Binary Search 4 | * Time: O(lgn) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | char nextGreatestLetter(vector& letters, char target) { 10 | if(target >= letters.back() || target < letters.front()){ 11 | return letters.front(); 12 | } 13 | 14 | int l = 0, r = letters.size() - 1; 15 | while(l < r){ 16 | int mid = (l + r)>>1; 17 | if(letters[mid] > target) { 18 | r = mid; 19 | } else { 20 | l = mid + 1; 21 | } 22 | } 23 | 24 | return letters[l]; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /LeetCode/leetcode_longest-arithmetic-subsequence-of-given-difference.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DP 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | class Solution { 8 | public: 9 | int longestSubsequence(vector& arr, int difference) { 10 | if(arr.size() == 0) { 11 | return 0; 12 | } 13 | 14 | unordered_map dp; 15 | int ans = 0, n = arr.size(); 16 | dp[arr[0]] = 1; 17 | for(int i = 1; i < n; ++ i) { 18 | int prev = arr[i] - difference, res = 1; 19 | res += dp[prev]; 20 | dp[arr[i]] = max(dp[arr[i]], res); 21 | ans = max(ans, res); 22 | } 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LeetCode/leetcode_my-calendar-i.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Data Structure (Binary Search Tree) 4 | * Time: O(lgn) 5 | * Space: O(n) 6 | */ 7 | class MyCalendar { 8 | private: 9 | map scheduledEvent; 10 | public: 11 | MyCalendar() { 12 | scheduledEvent.clear(); 13 | } 14 | 15 | bool book(int start, int end) { 16 | auto it = scheduledEvent.lower_bound(start); 17 | 18 | if(it->first == start){ 19 | ++ it; 20 | } 21 | 22 | if(it != scheduledEvent.end() && it->second < end){ 23 | return false; 24 | } 25 | 26 | scheduledEvent[end] = start; 27 | 28 | return true; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /LintCode/lintcode_move-zeroes.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param nums an integer array 11 | * @return nothing, do this in-place 12 | */ 13 | void moveZeroes(vector& nums) { 14 | // Write your code here 15 | int cntzeros = 0; 16 | if(!nums.size()) 17 | return ; 18 | int id = 0; 19 | for(int i = 0; i < nums.size(); ++ i){ 20 | if(nums[i]) 21 | nums[id ++] = nums[i]; 22 | else 23 | ++ cntzeros; 24 | } 25 | while(cntzeros --) 26 | nums[id ++] = 0; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /LeetCode/leetcode_base-7.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(lgn) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | string convertToBase7(int num) { 10 | string ans = ""; 11 | if(num == 0){ 12 | ans = "0"; 13 | return ans; 14 | } 15 | int v = 0; 16 | bool isneg = false; 17 | if(num < 0){ 18 | isneg = true; 19 | num = -num; 20 | } 21 | while(num > 0){ 22 | v = num%7; 23 | num /= 7; 24 | ans += to_string(v); 25 | } 26 | if(isneg) 27 | ans += "-"; 28 | reverse(ans.begin(), ans.end()); 29 | return ans; 30 | } 31 | }; 32 | -------------------------------------------------------------------------------- /LeetCode/leetcode_candy.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Algorithm: DP 3 | * Time complexity: O(n) 4 | * Memory complexity: O(n) 5 | */ 6 | class Solution { 7 | public: 8 | int candy(vector &ratings) { 9 | const int n = ratings.size(); 10 | vector res = vector(n,0); 11 | for(int i = 1; i < n; i ++){ 12 | if(ratings[i] > ratings[i - 1]){ 13 | res[i] = max(res[i], res[i - 1] + 1); 14 | } 15 | } 16 | for(int i = n - 2; i >= 0; i --){ 17 | if(ratings[i] > ratings[i + 1]){ 18 | res[i] = max(res[i], res[i + 1] + 1); 19 | } 20 | } 21 | return accumulate(&res[0],&res[0] + n, n); 22 | } 23 | }; 24 | 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_eliminate-maximum-number-of-monsters.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(nlgn) 5 | * Space: O(n) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int eliminateMaximum(vector& dist, vector& speed) { 11 | if(dist.size() != speed.size()) { 12 | return 0; 13 | } 14 | 15 | int n = dist.size(); 16 | vector t(n, 0); 17 | for(int i = 0; i < n; ++ i) { 18 | t[i] = (double)dist[i]/(double)speed[i]; 19 | } 20 | 21 | sort(t.begin(), t.end()); 22 | 23 | int ans = 1; 24 | for(int i = 1; i < n && (double)i < t[i]; ++ i, ++ ans) ; 25 | return ans; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_maximum-ascending-subarray-sum.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Brute Force 4 | * Time: O(n^2) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int maxAscendingSum(vector& nums) { 11 | if(nums.size() == 0) { 12 | return 0; 13 | } 14 | 15 | int ans = 0, n = nums.size(); 16 | for(int i = 0; i < n; ++ i) { 17 | int sum = nums[i]; 18 | for(int j = i + 1; j < n; ++ j) { 19 | if(nums[j] <= nums[j - 1]) { 20 | break; 21 | } 22 | sum += nums[j]; 23 | } 24 | ans = max(ans, sum); 25 | } 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /LeetCode/leetcode_maximum-nesting-depth-of-two-valid-parentheses-strings.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | vector maxDepthAfterSplit(string seq) { 11 | if(seq.size() == 0) { 12 | return {}; 13 | } 14 | 15 | int n = seq.size(), curSet = 1; 16 | vector ans(n, 0); 17 | for(int i = 0; i < n; ++ i) { 18 | if(seq[i] == '(') { 19 | curSet ^= 1; 20 | ans[i] = curSet; 21 | } else { 22 | ans[i] = curSet; 23 | curSet ^= 1; 24 | } 25 | } 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /LeetCode/leetcode_minimum-cost-to-connect-sticks.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Greedy 4 | * Time: O(nlgn) 5 | * Space: O(n) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int connectSticks(vector& sticks) { 11 | if(sticks.size() == 0) { 12 | return 0; 13 | } 14 | 15 | priority_queue, greater> pq(sticks.begin(), sticks.end()); 16 | int ans = 0; 17 | while(pq.size() > 1) { 18 | int u = pq.top(); 19 | pq.pop(); 20 | int v = pq.top(); 21 | pq.pop(); 22 | int sum = u + v; 23 | ans += sum; 24 | pq.push(sum); 25 | } 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /LeetCode/leetcode_set-mismatch.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Hash 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | vector findErrorNums(vector& nums) { 10 | vector ans(2); 11 | if(nums.size() < 1) { 12 | return ans; 13 | } 14 | 15 | for(int i = 0; i < nums.size(); ++ i) { 16 | while(nums[i] != nums[nums[i] - 1]) { 17 | swap(nums[i], nums[nums[i] - 1]); 18 | } 19 | } 20 | 21 | for(int i = 0; i < nums.size(); ++ i) { 22 | if(nums[i] != i + 1) 23 | return {nums[i], i + 1}; 24 | } 25 | 26 | return ans; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /LeetCode/leetcode_super-washing-machines.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Math 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | int findMinMoves(vector& machines) { 10 | int ans = 0; 11 | if(machines.size() == 0) 12 | return ans; 13 | int sum = 0, n = machines.size(); 14 | for(int i = 0; i < n; ++ i) 15 | sum += machines[i]; 16 | if(sum%n != 0) 17 | return -1; 18 | int avg = sum/n, res = 0, tmpsum = 0; 19 | for(int i = 0; i < n; ++ i){ 20 | tmpsum += machines[i] - avg; 21 | ans = max(ans, max(machines[i] - avg, abs(tmpsum))); 22 | } 23 | return ans; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /LintCode/lintcode_remove-duplicates-from-sorted-array.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Brute Force 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | class Solution { 8 | public: 9 | /** 10 | * @param A: a list of integers 11 | * @return : return an integer 12 | */ 13 | int removeDuplicates(vector &nums) { 14 | // write your code here 15 | int len = nums.size(); 16 | if(!nums.size()) 17 | return len; 18 | int id = 1, n = nums.size(); 19 | for(int i = 1; i < n; ++ i){ 20 | if(nums[i] != nums[i - 1]){ 21 | nums[id ++] = nums[i]; 22 | }else 23 | -- len; 24 | } 25 | return len; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /LeetCode/leetcode_coin-change.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: DP 4 | * Time: O(n*v) where v is the value of amount 5 | * Space: O(v) where v is the value of amount 6 | */ 7 | class Solution { 8 | public: 9 | int coinChange(vector& coins, int amount) { 10 | int ans; 11 | if(!coins.size() && amount) 12 | return -1; 13 | int INF = amount + 1; 14 | vector dp(amount + 1, INF); 15 | dp[0] = 0; 16 | for(int i = 0; i < coins.size(); ++ i){ 17 | for(int j = coins[i]; j <= amount; ++ j){ 18 | dp[j] = min(dp[j], dp[j - coins[i]] + 1); 19 | } 20 | } 21 | 22 | return dp[amount] == INF?-1:dp[amount]; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /LeetCode/leetcode_contains-duplicate-ii.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Hash (Data Structure) 4 | * Time: O(n) 5 | * Space: O(n) 6 | */ 7 | class Solution { 8 | public: 9 | bool containsNearbyDuplicate(vector& nums, int k) { 10 | if(nums.size() == 0) 11 | return false; 12 | 13 | unordered_map dict; 14 | for(int i = 0; i < nums.size(); ++ i){ 15 | if(dict.find(nums[i]) == dict.end()){ 16 | dict[nums[i]] = i; 17 | }else{ 18 | if(abs(i - dict[nums[i]]) <= k) 19 | return true; 20 | dict[nums[i]] = i; 21 | } 22 | } 23 | 24 | return false; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /LeetCode/leetcode_create-target-array-in-the-given-order.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Brute Force 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | vector createTargetArray(vector& nums, vector& index) { 11 | if(nums.size() != index.size()) { 12 | return nums; 13 | } 14 | 15 | vector ans(nums.begin(), nums.end()); 16 | for(int i = 0; i < index.size(); ++ i) { 17 | int idx = index[i]; 18 | for(int j = nums.size() - 1; j > idx; -- j) { 19 | ans[j] = ans[j - 1]; 20 | } 21 | 22 | ans[idx] = nums[i]; 23 | } 24 | return ans; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /LeetCode/leetcode_number-of-rectangles-that-can-form-the-largest-square.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int countGoodRectangles(vector>& rectangles) { 11 | if(rectangles.size() == 0) { 12 | return 0; 13 | } 14 | 15 | int maxLen = 0, ans = 0; 16 | for(vector r : rectangles) { 17 | if(maxLen < min(r[0], r[1])) { 18 | maxLen = max(maxLen, min(r[0], r[1])); 19 | ans = 1; 20 | } else if(maxLen == min(r[0], r[1])) { 21 | ++ ans; 22 | } 23 | } 24 | return ans; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /LeetCode/leetcode_special-array-with-x-elements-greater-than-or-equal-x.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | * Tag: Implementation 4 | * Time: O(n^2) 5 | * Space: O(1) 6 | */ 7 | 8 | class Solution { 9 | public: 10 | int specialArray(vector& nums) { 11 | if(nums.size() == 0) { 12 | return -1; 13 | } 14 | 15 | int n = nums.size(); 16 | for(int x = 1; x <= n; ++ x) { 17 | int cnt = 0; 18 | for(int j = 0; j < n; ++ j) { 19 | if(nums[j] >= x) { 20 | ++ cnt; 21 | } 22 | } 23 | if(cnt == x) { 24 | return x; 25 | } 26 | } 27 | return -1; 28 | } 29 | }; 30 | --------------------------------------------------------------------------------