Skip to content

Instantly share code, notes, and snippets.

@abantej
Created December 13, 2018 08:44
Show Gist options
  • Save abantej/5dbc07769e9aad28aeee804443f96c66 to your computer and use it in GitHub Desktop.
Save abantej/5dbc07769e9aad28aeee804443f96c66 to your computer and use it in GitHub Desktop.

Sock Merchant

  • When you need to count number of pairs in a given list, you can use a map (e.g. HashMap<Integer, Integer>) to put the unique items then increment the counter when the same item appears again. Use modulo after incrementing the count to determine if count is a pair (e.g. n % 2 == 0 then it is a pair). (the ‘Unique Tracker Technique’)

Counting Valleys

  • When you need to count something that requires a certain condition involving equilibrium (or a valley) then use an index. In this example, a valley usually will have an exit point of ‘up’ which will hit the index. With that condition, you can count the number of valleys based on that. (the ‘Breaking Condition’ technique)

Repeated String

  • Rather than scanning every character in a big string, you can divide the small string first then multiply the output to how many instances it should have appeared in the big string. (the ‘Divide and Conquer’ technique)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment