It can be complicated. Don't know what codes your area has but this is what I go by:
2015 IBC:
1107.6.2.2 Apartment houses, monasteries and convents.
Type A units and Type B units shall be provided
in apartment houses, monasteries and convents in
accordance with Sections 1107.6.2.2.1 and
1107.6.2.2.2.
1107.6.2.2.1 Type A units. In Group R-2 occupancies
containing more than 20 dwelling units or sleeping
units, at least 2 percent but not less than one of
the units shall be a Type A unit. All Group R-2 units
on a site shall be considered to determine the total
number of units and the required number of Type A
units. Type A units shall be dispersed among the various
classes of units. Bedrooms in monasteries and
convents shall be counted as sleeping units for the
purpose of determining the number of units. Where
the sleeping units are grouped into suites, only one
sleeping unit in each suite shall count towards the
number of required Type A units.
Exceptions:
1. The number of Type A units is permitted to
be reduced in accordance with Section
1107.7.
2. Existing structures on a site shall not contribute
to the total number of units on a site.
1107.6.2.2.2 Type B units. Where there are four or
more dwelling units or sleeping units intended to be
occupied as a residence in a single structure, every
dwelling unit and sleeping unit intended to be occupied
as a residence shall be a Type B unit.
Exception: The number of Type B units is permitted
to be reduced in accordance with Section
1107.7.
1107.7 General exceptions. Where specifically permitted by
Section 1107.5 or 1107.6, the required number of Type A
units and Type B units is permitted to be reduced in accordance
with Sections 1107.7.1 through 1107.7.5.
1107.7.1 Structures without elevator service. Where no
elevator service is provided in a structure, only the dwelling
units and sleeping units that are located on stories indicated
in Sections 1107.7.1.1 and 1107.7.1.2 are required to
be Type A units and Type B units, respectively. The number
of Type A units shall be determined in accordance with
Section 1107.6.2.2.1.
1107.7.1.1 One story with Type B units required. At
least one story containing dwelling units or sleeping
units intended to be occupied as a residence shall be
provided with an accessible entrance from the exterior
of the structure and all units intended to be occupied as
a residence on that story shall be Type B units.
1107.7.1.2 Additional stories with Type B units. On
all other stories that have a building entrance in proximity
to arrival points intended to serve units on that
story, as indicated in Items 1 and 2, all dwelling units
and sleeping units intended to be occupied as a residence
served by that entrance on that story shall be
Type B units.
1. Where the slopes of the undisturbed site measured
between the planned entrance and all vehicular
or pedestrian arrival points within 50 feet (15
240 mm) of the planned entrance are 10 percent
or less, and
2. Where the slopes of the planned finished grade
measured between the entrance and all vehicular
or pedestrian arrival points within 50 feet (15 240
mm) of the planned entrance are 10 percent or
less.
Where no such arrival points are within 50 feet (15
240 mm) of the entrance, the closest arrival point shall
be used unless that arrival point serves the story
required by Section 1107.7.1.1.
1107.7.2 Multistory units. A multistory dwelling unit or
sleeping unit that is not provided with elevator service is
not required to be a Type B unit. Where a multistory unit is
provided with external elevator service to only one floor,
the floor provided with elevator service shall be the primary
entry to the unit, shall comply with the requirements
for a Type B unit and, where provided within the unit, a
living area, a kitchen and a toilet facility shall be provided
on that floor.
1107.7.3 Elevator service to the lowest story with units.
Where elevator service in the building provides an accessible
route only to the lowest story containing dwelling
units or sleeping units intended to be occupied as a residence,
only the units on that story that are intended to be
occupied as a residence are required to be Type B units.
1107.7.4 Site impracticality. On a site with multiple nonelevator
buildings, the number of units required by Section
1107.7.1 to be Type B units is permitted to be reduced to a
percentage that is equal to the percentage of the entire site
having grades, prior to development, that are less than 10
percent, provided that all of the following conditions are
met:
1. Not less than 20 percent of the units required by
Section 1107.7.1 on the site are Type B units;
2. Units required by Section 1107.7.1, where the slope
between the building entrance serving the units on
that story and a pedestrian or vehicular arrival point
is no greater than 8.33 percent, are Type B units;
3. Units required by Section 1107.7.1, where an elevated
walkway is planned between a building
entrance serving the units on that story and a pedestrian
or vehicular arrival point and the slope between
them is 10 percent or less, are Type B units; and
4. Units served by an elevator in accordance with Section
1107.7.3 are Type B units.
1107.7.5 Design flood elevation. The required number of
Type A units and Type B units shall not apply to a site
where the required elevation of the lowest floor or the lowest
horizontal structural building members of nonelevator
buildings are at or above the design flood elevation resulting
in:
1. A difference in elevation between the minimum
required floor elevation at the primary entrances and
vehicular and pedestrian arrival points within 50 feet
(15 240 mm) exceeding 30 inches (762 mm), and
2. A slope exceeding 10 percent between the minimum
required floor elevation at the primary entrances and
vehicular and pedestrian arrival points within 50 feet
(15 240 mm).
Where no such arrival points are within 50 feet (15 240
mm) of the primary entrances, the closest arrival points
shall be used.